diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/trait/Trait.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/trait/Trait.java index ee39fa7e..9fde17ae 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/trait/Trait.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/trait/Trait.java @@ -7,6 +7,7 @@ import java.util.stream.Stream; import com.minelittlepony.unicopia.Unicopia; +import net.minecraft.command.argument.EnumArgumentType; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NbtElement; @@ -14,8 +15,9 @@ import net.minecraft.nbt.NbtList; import net.minecraft.text.*; import net.minecraft.util.Formatting; import net.minecraft.util.Identifier; +import net.minecraft.util.StringIdentifiable; -public enum Trait { +public enum Trait implements StringIdentifiable { /** * Imparts physical strength or enhances endurance. * Spells with more of the strength trait hit harder and last longer. @@ -97,6 +99,11 @@ public enum Trait { return id; } + @Override + public String asString() { + return name(); + } + public TraitGroup getGroup() { return group; } @@ -149,4 +156,17 @@ public enum Trait { public static Optional fromName(String name) { return Optional.ofNullable(REGISTRY.getOrDefault(name.toUpperCase(), null)); } + + public static EnumArgumentType argument() { + return new ArgumentType(); + } + + public static final class ArgumentType extends EnumArgumentType { + @SuppressWarnings("deprecation") + static final Codec CODEC = StringIdentifiable.createCodec(Trait::values); + + protected ArgumentType() { + super(CODEC, Trait::values); + } + } } diff --git a/src/main/java/com/minelittlepony/unicopia/client/render/PlayerPoser.java b/src/main/java/com/minelittlepony/unicopia/client/render/PlayerPoser.java index ace33461..fbd4cbc4 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/render/PlayerPoser.java +++ b/src/main/java/com/minelittlepony/unicopia/client/render/PlayerPoser.java @@ -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.PlayerEntityModel; import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.command.argument.EnumArgumentType; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; import net.minecraft.sound.SoundEvent; import net.minecraft.util.Arm; +import net.minecraft.util.StringIdentifiable; import net.minecraft.util.math.*; public class PlayerPoser { @@ -232,7 +234,7 @@ public class PlayerPoser { model.leftLeg.yaw = roll / 7F; } - public enum Animation { + public enum Animation implements StringIdentifiable { NONE(0), WOLOLO(USounds.ENTITY_PLAYER_WOLOLO, 40), ARMS_FORWARD(5), @@ -265,6 +267,25 @@ public class PlayerPoser { public Optional getSound() { return sound; } + + @Override + public String asString() { + return name(); + } + + public static EnumArgumentType argument() { + return new ArgumentType(); + } + + public static final class ArgumentType extends EnumArgumentType { + @SuppressWarnings("deprecation") + static final Codec CODEC = StringIdentifiable.createCodec(Animation::values); + + protected ArgumentType() { + super(CODEC, Animation::values); + } + } + } public enum Context { diff --git a/src/main/java/com/minelittlepony/unicopia/command/Commands.java b/src/main/java/com/minelittlepony/unicopia/command/Commands.java index 739ec65a..d2449146 100644 --- a/src/main/java/com/minelittlepony/unicopia/command/Commands.java +++ b/src/main/java/com/minelittlepony/unicopia/command/Commands.java @@ -3,6 +3,9 @@ package com.minelittlepony.unicopia.command; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; 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.loader.api.FabricLoader; @@ -10,18 +13,12 @@ import net.minecraft.command.argument.serialize.ConstantArgumentSerializer; import net.minecraft.server.MinecraftServer; public class Commands { - @SuppressWarnings({ "deprecation", "unchecked", "rawtypes" }) + @SuppressWarnings("deprecation") public static void bootstrap() { - ArgumentTypeRegistry.registerArgumentType( - Unicopia.id("enumeration"), - EnumArgumentType.class, - new EnumArgumentType.Serializer() - ); - ArgumentTypeRegistry.registerArgumentType( - Unicopia.id("spell_traits"), - TraitsArgumentType.class, - ConstantArgumentSerializer.of(TraitsArgumentType::traits) - ); + ArgumentTypeRegistry.registerArgumentType(Unicopia.id("animation"), Animation.ArgumentType.class, ConstantArgumentSerializer.of(Animation::argument)); + ArgumentTypeRegistry.registerArgumentType(Unicopia.id("mana_type"), ManaType.ArgumentType.class, ConstantArgumentSerializer.of(ManaType::argument)); + ArgumentTypeRegistry.registerArgumentType(Unicopia.id("trait_type"), Trait.ArgumentType.class, ConstantArgumentSerializer.of(Trait::argument)); + ArgumentTypeRegistry.registerArgumentType(Unicopia.id("spell_traits"), TraitsArgumentType.class, ConstantArgumentSerializer.of(TraitsArgumentType::traits)); CommandRegistrationCallback.EVENT.register((dispatcher, registries, environment) -> { RacelistCommand.register(dispatcher); EmoteCommand.register(dispatcher); diff --git a/src/main/java/com/minelittlepony/unicopia/command/EmoteCommand.java b/src/main/java/com/minelittlepony/unicopia/command/EmoteCommand.java index d6c7b8de..09a85da7 100644 --- a/src/main/java/com/minelittlepony/unicopia/command/EmoteCommand.java +++ b/src/main/java/com/minelittlepony/unicopia/command/EmoteCommand.java @@ -5,6 +5,7 @@ import com.minelittlepony.unicopia.entity.player.Pony; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.IntegerArgumentType; import com.mojang.brigadier.exceptions.CommandSyntaxException; + import net.minecraft.server.command.CommandManager; import net.minecraft.server.command.ServerCommandSource; @@ -12,7 +13,7 @@ public class EmoteCommand { static void register(CommandDispatcher dispatcher) { dispatcher.register(CommandManager .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.getArgument("animation", Animation.class) ) diff --git a/src/main/java/com/minelittlepony/unicopia/command/EnumArgumentType.java b/src/main/java/com/minelittlepony/unicopia/command/EnumArgumentType.java deleted file mode 100644 index eeee14ea..00000000 --- a/src/main/java/com/minelittlepony/unicopia/command/EnumArgumentType.java +++ /dev/null @@ -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> implements ArgumentType, Serializable { - private static final long serialVersionUID = 3731493854867412243L; - - public static > EnumArgumentType of(Class type, Predicate filter, T def) { - return new EnumArgumentType<>(type, filter, def); - } - - public static > EnumArgumentType of(Class type, T def) { - return new EnumArgumentType<>(type, s -> true, def); - } - - public static > EnumArgumentType of(Class type) { - return new EnumArgumentType<>(type, s -> true, null); - } - - private final T def; - private final T[] values; - private final List suggestions; - - private EnumArgumentType(Class type, Predicate filter, T def) { - this.def = def; - values = type.getEnumConstants(); - suggestions = Arrays.stream(values).filter(filter).map(T::name).toList(); - } - - private EnumArgumentType(List 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 CompletableFuture listSuggestions(final CommandContext 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 getExamples() { - return suggestions; - } - - public static class Serializer> implements ArgumentSerializer, Serializer.Properties> { - @SuppressWarnings("unchecked") - @Override - public Properties fromPacket(PacketByteBuf buf) { - try (ObjectInputStream stream = new ObjectInputStream(new ByteBufInputStream(buf))) { - return getArgumentTypeProperties((EnumArgumentType)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 type) { - return new Properties(type); - } - - public final class Properties implements ArgumentTypeProperties> { - - private final EnumArgumentType type; - - public Properties(EnumArgumentType type) { - this.type = type; - } - - @Override - public EnumArgumentType createType(CommandRegistryAccess var1) { - return type; - } - - @Override - public ArgumentSerializer, ?> getSerializer() { - return Serializer.this; - } - } - } -} \ No newline at end of file diff --git a/src/main/java/com/minelittlepony/unicopia/command/ManaCommand.java b/src/main/java/com/minelittlepony/unicopia/command/ManaCommand.java index 2948ea45..372724f5 100644 --- a/src/main/java/com/minelittlepony/unicopia/command/ManaCommand.java +++ b/src/main/java/com/minelittlepony/unicopia/command/ManaCommand.java @@ -6,15 +6,18 @@ import com.minelittlepony.unicopia.entity.player.MagicReserves; import com.minelittlepony.unicopia.entity.player.Pony; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.FloatArgumentType; + +import net.minecraft.command.argument.EnumArgumentType; import net.minecraft.server.command.CommandManager; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.text.Text; +import net.minecraft.util.StringIdentifiable; public class ManaCommand { static void register(CommandDispatcher dispatcher) { dispatcher.register(CommandManager .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 pony = Pony.of(source.getSource().getPlayer()); var bar = type.getBar(pony.getMagicalReserves()); @@ -41,7 +44,7 @@ public class ManaCommand { ); } - enum ManaType { + enum ManaType implements StringIdentifiable { EXERTION(MagicReserves::getExertion), EXHAUSTION(MagicReserves::getExhaustion), ENERGY(MagicReserves::getEnergy), @@ -57,5 +60,23 @@ public class ManaCommand { public MagicReserves.Bar getBar(MagicReserves reserves) { return getter.apply(reserves); } + + @Override + public String asString() { + return name(); + } + + public static EnumArgumentType argument() { + return new ArgumentType(); + } + + public static final class ArgumentType extends EnumArgumentType { + @SuppressWarnings("deprecation") + static final Codec CODEC = StringIdentifiable.createCodec(ManaType::values); + + protected ArgumentType() { + super(CODEC, ManaType::values); + } + } } } diff --git a/src/main/java/com/minelittlepony/unicopia/command/TraitCommand.java b/src/main/java/com/minelittlepony/unicopia/command/TraitCommand.java index dbda7a73..8f8af6d4 100644 --- a/src/main/java/com/minelittlepony/unicopia/command/TraitCommand.java +++ b/src/main/java/com/minelittlepony/unicopia/command/TraitCommand.java @@ -22,7 +22,7 @@ class TraitCommand { .requires(s -> s.hasPermissionLevel(2)); 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( source.getSource(), source.getSource().getPlayer(), @@ -31,7 +31,7 @@ class TraitCommand { ))) )); 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().getPlayer(), source.getArgument("trait", Trait.class)