mirror of
https://github.com/PaperMC/Paper.git
synced 2024-12-30 16:19:03 +01:00
Add conversion for summon command
This commit is contained in:
parent
fb102cafd1
commit
71de67180a
1 changed files with 58 additions and 2 deletions
|
@ -27717,6 +27717,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
||||||
+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..00000000000000000000000000000000
|
||||||
+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..00000000000000000000000000000000
|
||||||
+ 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..00000000000000000000000000000000
|
||||||
+ );
|
+ );
|
||||||
+ }
|
+ }
|
||||||
+ });
|
+ });
|
||||||
|
+ 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..00000000000000000000000000000000
|
||||||
+ 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..00000000000000000000000000000000
|
||||||
+ ) {
|
+ ) {
|
||||||
+ 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..00000000000000000000000000000000
|
||||||
+ 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..00000000000000000000000000000000
|
||||||
+ 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() {
|
||||||
|
|
Loading…
Reference in a new issue