13
0
geforkt von Mirrors/Paper

Add conversion for summon command

Dieser Commit ist enthalten in:
Jason Penilla 2024-05-10 12:57:07 +02:00 committet von Nassim Jahnke
Ursprung 7f1a154d15
Commit 4fd58a1b8f
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: EF6771C01F6EF02F

Datei anzeigen

@ -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 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 new file mode 100644
index 0000000000000000000000000000000000000000..24db54e29281076f87ef333e23da9365ad62e589 index 0000000000000000000000000000000000000000..e0f2772fa51e47b45eda5781ea8c34e59cc4b285
--- /dev/null --- /dev/null
+++ b/src/main/java/ca/spottedleaf/dataconverter/util/CommandArgumentUpgrader.java +++ b/src/main/java/ca/spottedleaf/dataconverter/util/CommandArgumentUpgrader.java
@@ -0,0 +1,435 @@ @@ -0,0 +1,491 @@
+package ca.spottedleaf.dataconverter.util; +package ca.spottedleaf.dataconverter.util;
+ +
+import ca.spottedleaf.dataconverter.minecraft.MCDataConverter; +import ca.spottedleaf.dataconverter.minecraft.MCDataConverter;
@ -27717,6 +27717,7 @@ index 0000000000000000000000000000000000000000..24db54e29281076f87ef333e23da9365
+import com.google.gson.JsonParseException; +import com.google.gson.JsonParseException;
+import com.google.gson.internal.Streams; +import com.google.gson.internal.Streams;
+import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonReader;
+import com.mojang.brigadier.Command;
+import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.CommandDispatcher;
+import com.mojang.brigadier.LiteralMessage; +import com.mojang.brigadier.LiteralMessage;
+import com.mojang.brigadier.ParseResults; +import com.mojang.brigadier.ParseResults;
@ -27750,6 +27751,9 @@ index 0000000000000000000000000000000000000000..24db54e29281076f87ef333e23da9365
+import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.CommandSourceStack;
+import net.minecraft.commands.Commands; +import net.minecraft.commands.Commands;
+import net.minecraft.commands.arguments.ComponentArgument; +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.commands.arguments.item.ItemArgument;
+import net.minecraft.core.Holder; +import net.minecraft.core.Holder;
+import net.minecraft.core.HolderLookup; +import net.minecraft.core.HolderLookup;
@ -27779,6 +27783,7 @@ index 0000000000000000000000000000000000000000..24db54e29281076f87ef333e23da9365
+ return new CommandArgumentUpgrader(functionPermissionLevel, builder -> { + return new CommandArgumentUpgrader(functionPermissionLevel, builder -> {
+ builder.registerReplacement(ItemArgument.class, (argument, ctx) -> new ItemParser_1_20_4()); + builder.registerReplacement(ItemArgument.class, (argument, ctx) -> new ItemParser_1_20_4());
+ builder.registerReplacement(ComponentArgument.class, (argument, ctx) -> new ComponentParser_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); + ExecuteCommand.register(this.dispatcher, context);
+ ReturnCommand.register(this.dispatcher); + ReturnCommand.register(this.dispatcher);
+ // This looks weird, but it's what vanilla does when loading functions for datapacks + // 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 { + public static final class ReplacementsBuilder {
+ private final Map<Class<?>, BiFunction<ArgumentType<?>, CommandBuildContext, ArgumentType<?>>> replacements = + private final Map<Class<?>, BiFunction<ArgumentType<?>, CommandBuildContext, ArgumentType<?>>> replacements =
+ new HashMap<>(); + new HashMap<>();
+ private final List<Consumer<CommandDispatcher<CommandSourceStack>>> extra = new ArrayList<>();
+ +
+ private ReplacementsBuilder() { + private ReplacementsBuilder() {
+ } + }
@ -27859,6 +27868,14 @@ index 0000000000000000000000000000000000000000..24db54e29281076f87ef333e23da9365
+ ) { + ) {
+ this.replacements.put(type, (BiFunction) upgrader); + 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) {} + public record UpgradedArgument(String upgraded) {}
@ -27961,11 +27978,14 @@ index 0000000000000000000000000000000000000000..24db54e29281076f87ef333e23da9365
+ final Map<StringRange, String> replacements = new LinkedHashMap<>(); + final Map<StringRange, String> replacements = new LinkedHashMap<>();
+ final List<Pair<String, ParsedArgument<CommandSourceStack, ?>>> mergedArguments = new ArrayList<>(); + final List<Pair<String, ParsedArgument<CommandSourceStack, ?>>> mergedArguments = new ArrayList<>();
+ addArguments(mergedArguments, parseResult.getContext()); + 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) { + if (pair.value().getResult() instanceof UpgradedArgument upgraded) {
+ replacements.put(pair.value().getRange(), upgraded.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; + String upgradedCommand = command;
+ while (!replacements.isEmpty()) { + while (!replacements.isEmpty()) {
+ final Map.Entry<StringRange, String> next = replacements.entrySet().iterator().next(); + final Map.Entry<StringRange, String> next = replacements.entrySet().iterator().next();
@ -28093,6 +28113,42 @@ index 0000000000000000000000000000000000000000..24db54e29281076f87ef333e23da9365
+ return result; + 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() { + private static CommandBuildContext makeDummyCommandBuildContext() {
+ return Commands.createValidationContext( + return Commands.createValidationContext(
+ new HolderLookup.Provider() { + new HolderLookup.Provider() {