Switch to using mojang's own EnumArgumentType

This commit is contained in:
Sollace 2023-07-29 19:54:49 +01:00
parent 86790a3150
commit 858720805e
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB
7 changed files with 78 additions and 164 deletions

View file

@ -7,6 +7,7 @@ import java.util.stream.Stream;
import com.minelittlepony.unicopia.Unicopia; import com.minelittlepony.unicopia.Unicopia;
import net.minecraft.command.argument.EnumArgumentType;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtElement; import net.minecraft.nbt.NbtElement;
@ -14,8 +15,9 @@ import net.minecraft.nbt.NbtList;
import net.minecraft.text.*; import net.minecraft.text.*;
import net.minecraft.util.Formatting; import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.StringIdentifiable;
public enum Trait { public enum Trait implements StringIdentifiable {
/** /**
* Imparts physical strength or enhances endurance. * Imparts physical strength or enhances endurance.
* Spells with more of the strength trait hit harder and last longer. * Spells with more of the strength trait hit harder and last longer.
@ -97,6 +99,11 @@ public enum Trait {
return id; return id;
} }
@Override
public String asString() {
return name();
}
public TraitGroup getGroup() { public TraitGroup getGroup() {
return group; return group;
} }
@ -149,4 +156,17 @@ public enum Trait {
public static Optional<Trait> fromName(String name) { public static Optional<Trait> fromName(String name) {
return Optional.ofNullable(REGISTRY.getOrDefault(name.toUpperCase(), null)); return Optional.ofNullable(REGISTRY.getOrDefault(name.toUpperCase(), null));
} }
public static EnumArgumentType<Trait> argument() {
return new ArgumentType();
}
public static final class ArgumentType extends EnumArgumentType<Trait> {
@SuppressWarnings("deprecation")
static final Codec<Trait> CODEC = StringIdentifiable.createCodec(Trait::values);
protected ArgumentType() {
super(CODEC, Trait::values);
}
}
} }

View file

@ -13,10 +13,12 @@ import net.minecraft.client.model.ModelPart;
import net.minecraft.client.render.entity.model.BipedEntityModel; import net.minecraft.client.render.entity.model.BipedEntityModel;
import net.minecraft.client.render.entity.model.PlayerEntityModel; import net.minecraft.client.render.entity.model.PlayerEntityModel;
import net.minecraft.client.util.math.MatrixStack; import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.command.argument.EnumArgumentType;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.sound.SoundEvent; import net.minecraft.sound.SoundEvent;
import net.minecraft.util.Arm; import net.minecraft.util.Arm;
import net.minecraft.util.StringIdentifiable;
import net.minecraft.util.math.*; import net.minecraft.util.math.*;
public class PlayerPoser { public class PlayerPoser {
@ -232,7 +234,7 @@ public class PlayerPoser {
model.leftLeg.yaw = roll / 7F; model.leftLeg.yaw = roll / 7F;
} }
public enum Animation { public enum Animation implements StringIdentifiable {
NONE(0), NONE(0),
WOLOLO(USounds.ENTITY_PLAYER_WOLOLO, 40), WOLOLO(USounds.ENTITY_PLAYER_WOLOLO, 40),
ARMS_FORWARD(5), ARMS_FORWARD(5),
@ -265,6 +267,25 @@ public class PlayerPoser {
public Optional<SoundEvent> getSound() { public Optional<SoundEvent> getSound() {
return sound; return sound;
} }
@Override
public String asString() {
return name();
}
public static EnumArgumentType<Animation> argument() {
return new ArgumentType();
}
public static final class ArgumentType extends EnumArgumentType<Animation> {
@SuppressWarnings("deprecation")
static final Codec<Animation> CODEC = StringIdentifiable.createCodec(Animation::values);
protected ArgumentType() {
super(CODEC, Animation::values);
}
}
} }
public enum Context { public enum Context {

View file

@ -3,6 +3,9 @@ package com.minelittlepony.unicopia.command;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import com.minelittlepony.unicopia.Unicopia; import com.minelittlepony.unicopia.Unicopia;
import com.minelittlepony.unicopia.ability.magic.spell.trait.Trait;
import com.minelittlepony.unicopia.client.render.PlayerPoser.Animation;
import com.minelittlepony.unicopia.command.ManaCommand.ManaType;
import net.fabricmc.fabric.api.command.v2.ArgumentTypeRegistry; import net.fabricmc.fabric.api.command.v2.ArgumentTypeRegistry;
import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.FabricLoader;
@ -10,18 +13,12 @@ import net.minecraft.command.argument.serialize.ConstantArgumentSerializer;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
public class Commands { public class Commands {
@SuppressWarnings({ "deprecation", "unchecked", "rawtypes" }) @SuppressWarnings("deprecation")
public static void bootstrap() { public static void bootstrap() {
ArgumentTypeRegistry.registerArgumentType( ArgumentTypeRegistry.registerArgumentType(Unicopia.id("animation"), Animation.ArgumentType.class, ConstantArgumentSerializer.of(Animation::argument));
Unicopia.id("enumeration"), ArgumentTypeRegistry.registerArgumentType(Unicopia.id("mana_type"), ManaType.ArgumentType.class, ConstantArgumentSerializer.of(ManaType::argument));
EnumArgumentType.class, ArgumentTypeRegistry.registerArgumentType(Unicopia.id("trait_type"), Trait.ArgumentType.class, ConstantArgumentSerializer.of(Trait::argument));
new EnumArgumentType.Serializer() ArgumentTypeRegistry.registerArgumentType(Unicopia.id("spell_traits"), TraitsArgumentType.class, ConstantArgumentSerializer.of(TraitsArgumentType::traits));
);
ArgumentTypeRegistry.registerArgumentType(
Unicopia.id("spell_traits"),
TraitsArgumentType.class,
ConstantArgumentSerializer.of(TraitsArgumentType::traits)
);
CommandRegistrationCallback.EVENT.register((dispatcher, registries, environment) -> { CommandRegistrationCallback.EVENT.register((dispatcher, registries, environment) -> {
RacelistCommand.register(dispatcher); RacelistCommand.register(dispatcher);
EmoteCommand.register(dispatcher); EmoteCommand.register(dispatcher);

View file

@ -5,6 +5,7 @@ import com.minelittlepony.unicopia.entity.player.Pony;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.IntegerArgumentType; import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.server.command.CommandManager; import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.command.ServerCommandSource;
@ -12,7 +13,7 @@ public class EmoteCommand {
static void register(CommandDispatcher<ServerCommandSource> dispatcher) { static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
dispatcher.register(CommandManager dispatcher.register(CommandManager
.literal("emote") .literal("emote")
.then(CommandManager.argument("animation", EnumArgumentType.of(Animation.class)).executes(source -> apply( .then(CommandManager.argument("animation", Animation.argument()).executes(source -> apply(
source.getSource(), source.getSource(),
source.getArgument("animation", Animation.class) source.getArgument("animation", Animation.class)
) )

View file

@ -1,146 +0,0 @@
package com.minelittlepony.unicopia.command;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Predicate;
import com.google.common.base.Strings;
import com.google.gson.JsonObject;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.serialize.ArgumentSerializer;
import net.minecraft.command.argument.serialize.ArgumentSerializer.ArgumentTypeProperties;
import net.minecraft.network.PacketByteBuf;
class EnumArgumentType<T extends Enum<T>> implements ArgumentType<T>, Serializable {
private static final long serialVersionUID = 3731493854867412243L;
public static <T extends Enum<T>> EnumArgumentType<T> of(Class<T> type, Predicate<T> filter, T def) {
return new EnumArgumentType<>(type, filter, def);
}
public static <T extends Enum<T>> EnumArgumentType<T> of(Class<T> type, T def) {
return new EnumArgumentType<>(type, s -> true, def);
}
public static <T extends Enum<T>> EnumArgumentType<T> of(Class<T> type) {
return new EnumArgumentType<>(type, s -> true, null);
}
private final T def;
private final T[] values;
private final List<String> suggestions;
private EnumArgumentType(Class<T> type, Predicate<T> filter, T def) {
this.def = def;
values = type.getEnumConstants();
suggestions = Arrays.stream(values).filter(filter).map(T::name).toList();
}
private EnumArgumentType(List<String> suggestions, T[] values, T def) {
this.suggestions = suggestions;
this.values = values;
this.def = def;
}
@Override
public T parse(StringReader reader) throws CommandSyntaxException {
return fromName(reader.readUnquotedString());
}
@Override
public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) {
suggestions.stream()
.filter(i -> i.toLowerCase().startsWith(builder.getRemaining().toLowerCase()))
.forEach(builder::suggest);
return builder.buildFuture();
}
@SuppressWarnings("unlikely-arg-type")
private T fromName(String s) {
if (!Strings.isNullOrEmpty(s)) {
for (T i : values) {
if (i.equals(s) || i.name().equalsIgnoreCase(s)) {
return i;
}
}
}
try {
int ordinal = Integer.parseInt(s);
if (ordinal >= 0 && ordinal < values.length) {
return values[ordinal];
}
} catch (NumberFormatException e) { }
return def;
}
@Override
public Collection<String> getExamples() {
return suggestions;
}
public static class Serializer<T extends Enum<T>> implements ArgumentSerializer<EnumArgumentType<T>, Serializer<T>.Properties> {
@SuppressWarnings("unchecked")
@Override
public Properties fromPacket(PacketByteBuf buf) {
try (ObjectInputStream stream = new ObjectInputStream(new ByteBufInputStream(buf))) {
return getArgumentTypeProperties((EnumArgumentType<T>)stream.readObject());
} catch (IOException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
@Override
public void writePacket(Properties properties, PacketByteBuf buf) {
try (ObjectOutputStream stream = new ObjectOutputStream(new ByteBufOutputStream(buf))) {
stream.writeObject(properties.type);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public void writeJson(Properties properties, JsonObject json) {
}
@Override
public Properties getArgumentTypeProperties(EnumArgumentType<T> type) {
return new Properties(type);
}
public final class Properties implements ArgumentTypeProperties<EnumArgumentType<T>> {
private final EnumArgumentType<T> type;
public Properties(EnumArgumentType<T> type) {
this.type = type;
}
@Override
public EnumArgumentType<T> createType(CommandRegistryAccess var1) {
return type;
}
@Override
public ArgumentSerializer<EnumArgumentType<T>, ?> getSerializer() {
return Serializer.this;
}
}
}
}

View file

@ -6,15 +6,18 @@ import com.minelittlepony.unicopia.entity.player.MagicReserves;
import com.minelittlepony.unicopia.entity.player.Pony; import com.minelittlepony.unicopia.entity.player.Pony;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.FloatArgumentType; import com.mojang.brigadier.arguments.FloatArgumentType;
import net.minecraft.command.argument.EnumArgumentType;
import net.minecraft.server.command.CommandManager; import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.util.StringIdentifiable;
public class ManaCommand { public class ManaCommand {
static void register(CommandDispatcher<ServerCommandSource> dispatcher) { static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
dispatcher.register(CommandManager dispatcher.register(CommandManager
.literal("mana").requires(s -> s.hasPermissionLevel(2)) .literal("mana").requires(s -> s.hasPermissionLevel(2))
.then(CommandManager.argument("type", EnumArgumentType.of(ManaType.class)).executes(source -> { .then(CommandManager.argument("type", ManaType.argument()).executes(source -> {
var type = source.getArgument("type", ManaType.class); var type = source.getArgument("type", ManaType.class);
var pony = Pony.of(source.getSource().getPlayer()); var pony = Pony.of(source.getSource().getPlayer());
var bar = type.getBar(pony.getMagicalReserves()); var bar = type.getBar(pony.getMagicalReserves());
@ -41,7 +44,7 @@ public class ManaCommand {
); );
} }
enum ManaType { enum ManaType implements StringIdentifiable {
EXERTION(MagicReserves::getExertion), EXERTION(MagicReserves::getExertion),
EXHAUSTION(MagicReserves::getExhaustion), EXHAUSTION(MagicReserves::getExhaustion),
ENERGY(MagicReserves::getEnergy), ENERGY(MagicReserves::getEnergy),
@ -57,5 +60,23 @@ public class ManaCommand {
public MagicReserves.Bar getBar(MagicReserves reserves) { public MagicReserves.Bar getBar(MagicReserves reserves) {
return getter.apply(reserves); return getter.apply(reserves);
} }
@Override
public String asString() {
return name();
}
public static EnumArgumentType<ManaType> argument() {
return new ArgumentType();
}
public static final class ArgumentType extends EnumArgumentType<ManaType> {
@SuppressWarnings("deprecation")
static final Codec<ManaType> CODEC = StringIdentifiable.createCodec(ManaType::values);
protected ArgumentType() {
super(CODEC, ManaType::values);
}
}
} }
} }

View file

@ -22,7 +22,7 @@ class TraitCommand {
.requires(s -> s.hasPermissionLevel(2)); .requires(s -> s.hasPermissionLevel(2));
builder.then(CommandManager.literal("add") builder.then(CommandManager.literal("add")
.then(CommandManager.argument("trait", EnumArgumentType.of(Trait.class)) .then(CommandManager.argument("trait", Trait.argument())
.then(CommandManager.argument("value", FloatArgumentType.floatArg()).executes(source -> add( .then(CommandManager.argument("value", FloatArgumentType.floatArg()).executes(source -> add(
source.getSource(), source.getSource(),
source.getSource().getPlayer(), source.getSource().getPlayer(),
@ -31,7 +31,7 @@ class TraitCommand {
))) )))
)); ));
builder.then(CommandManager.literal("remove") builder.then(CommandManager.literal("remove")
.then(CommandManager.argument("trait", EnumArgumentType.of(Trait.class)).executes(source -> remove( .then(CommandManager.argument("trait", Trait.argument()).executes(source -> remove(
source.getSource(), source.getSource(),
source.getSource().getPlayer(), source.getSource().getPlayer(),
source.getArgument("trait", Trait.class) source.getArgument("trait", Trait.class)