Add conversion for summon command

This commit is contained in:
Jason Penilla 2024-05-10 12:57:07 +02:00 committed by Nassim Jahnke
parent 7f1a154d15
commit 4fd58a1b8f
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F

View File

@ -27703,10 +27703,10 @@ index 0000000000000000000000000000000000000000..62c0f4073aff301bf5b3187e0d4446fd
+}
diff --git a/src/main/java/ca/spottedleaf/dataconverter/util/CommandArgumentUpgrader.java b/src/main/java/ca/spottedleaf/dataconverter/util/CommandArgumentUpgrader.java
new file mode 100644
index 0000000000000000000000000000000000000000..24db54e29281076f87ef333e23da9365ad62e589
index 0000000000000000000000000000000000000000..e0f2772fa51e47b45eda5781ea8c34e59cc4b285
--- /dev/null
+++ b/src/main/java/ca/spottedleaf/dataconverter/util/CommandArgumentUpgrader.java
@@ -0,0 +1,435 @@
@@ -0,0 +1,491 @@
+package ca.spottedleaf.dataconverter.util;
+
+import ca.spottedleaf.dataconverter.minecraft.MCDataConverter;
@ -27717,6 +27717,7 @@ index 0000000000000000000000000000000000000000..24db54e29281076f87ef333e23da9365
+import com.google.gson.JsonParseException;
+import com.google.gson.internal.Streams;
+import com.google.gson.stream.JsonReader;
+import com.mojang.brigadier.Command;
+import com.mojang.brigadier.CommandDispatcher;
+import com.mojang.brigadier.LiteralMessage;
+import com.mojang.brigadier.ParseResults;
@ -27750,6 +27751,9 @@ index 0000000000000000000000000000000000000000..24db54e29281076f87ef333e23da9365
+import net.minecraft.commands.CommandSourceStack;
+import net.minecraft.commands.Commands;
+import net.minecraft.commands.arguments.ComponentArgument;
+import net.minecraft.commands.arguments.CompoundTagArgument;
+import net.minecraft.commands.arguments.ResourceLocationArgument;
+import net.minecraft.commands.arguments.coordinates.Vec3Argument;
+import net.minecraft.commands.arguments.item.ItemArgument;
+import net.minecraft.core.Holder;
+import net.minecraft.core.HolderLookup;
@ -27779,6 +27783,7 @@ index 0000000000000000000000000000000000000000..24db54e29281076f87ef333e23da9365
+ return new CommandArgumentUpgrader(functionPermissionLevel, builder -> {
+ builder.registerReplacement(ItemArgument.class, (argument, ctx) -> new ItemParser_1_20_4());
+ builder.registerReplacement(ComponentArgument.class, (argument, ctx) -> new ComponentParser_1_20_4());
+ builder.registerExtraCommand(CommandArgumentUpgrader::registerSummon_1_20_4_to_1_20_5);
+ });
+ }
+
@ -27829,6 +27834,9 @@ index 0000000000000000000000000000000000000000..24db54e29281076f87ef333e23da9365
+ );
+ }
+ });
+ for (final Consumer<CommandDispatcher<CommandSourceStack>> extra : builder.extra) {
+ extra.accept(this.dispatcher);
+ }
+ ExecuteCommand.register(this.dispatcher, context);
+ ReturnCommand.register(this.dispatcher);
+ // This looks weird, but it's what vanilla does when loading functions for datapacks
@ -27848,6 +27856,7 @@ index 0000000000000000000000000000000000000000..24db54e29281076f87ef333e23da9365
+ public static final class ReplacementsBuilder {
+ private final Map<Class<?>, BiFunction<ArgumentType<?>, CommandBuildContext, ArgumentType<?>>> replacements =
+ new HashMap<>();
+ private final List<Consumer<CommandDispatcher<CommandSourceStack>>> extra = new ArrayList<>();
+
+ private ReplacementsBuilder() {
+ }
@ -27859,6 +27868,14 @@ index 0000000000000000000000000000000000000000..24db54e29281076f87ef333e23da9365
+ ) {
+ this.replacements.put(type, (BiFunction) upgrader);
+ }
+
+ public void registerExtraCommand(final Consumer<CommandDispatcher<CommandSourceStack>> consumer) {
+ this.extra.add(consumer);
+ }
+ }
+
+ public interface UpgradableArgument {
+ String upgrade(int index, List<Pair<String, ParsedArgument<CommandSourceStack, ?>>> arguments);
+ }
+
+ public record UpgradedArgument(String upgraded) {}
@ -27961,11 +27978,14 @@ index 0000000000000000000000000000000000000000..24db54e29281076f87ef333e23da9365
+ final Map<StringRange, String> replacements = new LinkedHashMap<>();
+ final List<Pair<String, ParsedArgument<CommandSourceStack, ?>>> mergedArguments = new ArrayList<>();
+ addArguments(mergedArguments, parseResult.getContext());
+ mergedArguments.forEach(pair -> {
+ for (int i = 0; i < mergedArguments.size(); i++) {
+ final Pair<String, ParsedArgument<CommandSourceStack, ?>> pair = mergedArguments.get(i);
+ if (pair.value().getResult() instanceof UpgradedArgument upgraded) {
+ replacements.put(pair.value().getRange(), upgraded.upgraded());
+ } else if (pair.value().getResult() instanceof UpgradableArgument upgradable) {
+ replacements.put(pair.value().getRange(), upgradable.upgrade(i, mergedArguments));
+ }
+ });
+ }
+ String upgradedCommand = command;
+ while (!replacements.isEmpty()) {
+ final Map.Entry<StringRange, String> next = replacements.entrySet().iterator().next();
@ -28093,6 +28113,42 @@ index 0000000000000000000000000000000000000000..24db54e29281076f87ef333e23da9365
+ return result;
+ }
+
+ public static void registerSummon_1_20_4_to_1_20_5(final CommandDispatcher<CommandSourceStack> dispatcher) {
+ dispatcher.register(
+ Commands.literal("summon")
+ .then(Commands.argument("entity", ResourceLocationArgument.id())
+ .executes(commandContext -> Command.SINGLE_SUCCESS)
+ .then(Commands.argument("pos", Vec3Argument.vec3())
+ .executes(commandContext -> Command.SINGLE_SUCCESS)
+ .then(Commands.argument("nbt", new ArgumentType<UpgradableArgument>() {
+ @Override
+ public UpgradableArgument parse(final StringReader reader) throws CommandSyntaxException {
+ final CompoundTag tag = CompoundTagArgument.compoundTag().parse(reader);
+
+ return (index, args) -> {
+ final CompoundTag tagCopy = tag.copy();
+
+ final Pair<String, ParsedArgument<CommandSourceStack, ?>> entityTypePair =
+ args.get(index - 2);
+ final ResourceLocation entityType =
+ (ResourceLocation) entityTypePair.value().getResult();
+
+ tagCopy.putString("id", entityType.toString());
+ final CompoundTag convertedTag = MCDataConverter.convertTag(
+ MCTypeRegistry.ENTITY,
+ tagCopy,
+ MCVersions.V1_20_4, SharedConstants.getCurrentVersion().getDataVersion().getVersion()
+ );
+ convertedTag.remove("id");
+
+ return convertedTag.toString();
+ };
+ }
+ })
+ .executes(commandContext -> Command.SINGLE_SUCCESS))))
+ );
+ }
+
+ private static CommandBuildContext makeDummyCommandBuildContext() {
+ return Commands.createValidationContext(
+ new HolderLookup.Provider() {