From 71de67180a751e0124aba261977159b0b3910276 Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Fri, 10 May 2024 12:57:07 +0200 Subject: [PATCH] Add conversion for summon command --- .../server/Rewrite-dataconverter-system.patch | 60 ++++++++++++++++++- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/patches/server/Rewrite-dataconverter-system.patch b/patches/server/Rewrite-dataconverter-system.patch index 7e6e24121a..93625c4205 100644 --- a/patches/server/Rewrite-dataconverter-system.patch +++ b/patches/server/Rewrite-dataconverter-system.patch @@ -27717,6 +27717,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 +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..00000000000000000000000000000000 +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..00000000000000000000000000000000 + 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..00000000000000000000000000000000 + ); + } + }); ++ for (final Consumer> 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..00000000000000000000000000000000 + public static final class ReplacementsBuilder { + private final Map, BiFunction, CommandBuildContext, ArgumentType>> replacements = + new HashMap<>(); ++ private final List>> extra = new ArrayList<>(); + + private ReplacementsBuilder() { + } @@ -27859,6 +27868,14 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + ) { + this.replacements.put(type, (BiFunction) upgrader); + } ++ ++ public void registerExtraCommand(final Consumer> consumer) { ++ this.extra.add(consumer); ++ } ++ } ++ ++ public interface UpgradableArgument { ++ String upgrade(int index, List>> arguments); + } + + public record UpgradedArgument(String upgraded) {} @@ -27961,11 +27978,14 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + final Map replacements = new LinkedHashMap<>(); + final List>> mergedArguments = new ArrayList<>(); + addArguments(mergedArguments, parseResult.getContext()); -+ mergedArguments.forEach(pair -> { ++ for (int i = 0; i < mergedArguments.size(); i++) { ++ final Pair> 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 next = replacements.entrySet().iterator().next(); @@ -28093,6 +28113,42 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + return result; + } + ++ public static void registerSummon_1_20_4_to_1_20_5(final CommandDispatcher 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() { ++ @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> 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() {