Add conversion for summon command

This commit is contained in:
Jason Penilla 2024-05-10 12:57:07 +02:00
parent fb102cafd1
commit 71de67180a

View file

@ -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<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..00000000000000000000000000000000
+ 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..00000000000000000000000000000000
+ ) {
+ 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..00000000000000000000000000000000
+ 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..00000000000000000000000000000000
+ 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() {