From d34db052374366f59d59f93961277209f6de4520 Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Sun, 28 Jul 2024 11:23:18 -0700 Subject: [PATCH] Add argument for FinePosition to brig API (#11094) --- patches/api/Brigadier-based-command-API.patch | 51 ++++++++++++++++++- .../server/Brigadier-based-command-API.patch | 18 +++++-- .../testplugin/brigtests/Registration.java | 12 +++++ 3 files changed, 76 insertions(+), 5 deletions(-) diff --git a/patches/api/Brigadier-based-command-API.patch b/patches/api/Brigadier-based-command-API.patch index 675e21d704..a62ac355ea 100644 --- a/patches/api/Brigadier-based-command-API.patch +++ b/patches/api/Brigadier-based-command-API.patch @@ -975,6 +975,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 +import io.papermc.paper.command.brigadier.argument.range.DoubleRangeProvider; +import io.papermc.paper.command.brigadier.argument.range.IntegerRangeProvider; +import io.papermc.paper.command.brigadier.argument.resolvers.BlockPositionResolver; ++import io.papermc.paper.command.brigadier.argument.resolvers.FinePositionResolver; +import io.papermc.paper.command.brigadier.argument.resolvers.PlayerProfileListResolver; +import io.papermc.paper.command.brigadier.argument.resolvers.selector.EntitySelectorArgumentResolver; +import io.papermc.paper.command.brigadier.argument.resolvers.selector.PlayerSelectorArgumentResolver; @@ -1056,7 +1057,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + * A selector argument that provides a list + * of player profiles. + * -+ * @return player profile arguments ++ * @return player profile argument + */ + public static @NotNull ArgumentType playerProfiles() { + return provider().playerProfiles(); @@ -1065,13 +1066,33 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + /** + * A block position argument. + * -+ * @return argument ++ * @return block position argument + */ + public static @NotNull ArgumentType blockPosition() { + return provider().blockPosition(); + } + + /** ++ * A fine position argument. ++ * ++ * @return fine position argument ++ * @see #finePosition(boolean) to center whole numbers ++ */ ++ public static @NotNull ArgumentType finePosition() { ++ return finePosition(false); ++ } ++ ++ /** ++ * A fine position argument. ++ * ++ * @param centerIntegers if whole numbers should be centered (+0.5) ++ * @return fine position argument ++ */ ++ public static @NotNull ArgumentType finePosition(final boolean centerIntegers) { ++ return provider().finePosition(centerIntegers); ++ } ++ ++ /** + * A blockstate argument which will provide rich parsing for specifying + * the specific block variant and then the block entity NBT if applicable. + * @@ -1530,6 +1551,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 +import io.papermc.paper.command.brigadier.argument.range.DoubleRangeProvider; +import io.papermc.paper.command.brigadier.argument.range.IntegerRangeProvider; +import io.papermc.paper.command.brigadier.argument.resolvers.BlockPositionResolver; ++import io.papermc.paper.command.brigadier.argument.resolvers.FinePositionResolver; +import io.papermc.paper.command.brigadier.argument.resolvers.PlayerProfileListResolver; +import io.papermc.paper.command.brigadier.argument.resolvers.selector.EntitySelectorArgumentResolver; +import io.papermc.paper.command.brigadier.argument.resolvers.selector.PlayerSelectorArgumentResolver; @@ -1580,6 +1602,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + + ArgumentType blockPosition(); + ++ ArgumentType finePosition(boolean centerIntegers); ++ + ArgumentType blockState(); + + ArgumentType itemStack(); @@ -1770,6 +1794,29 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 +@ApiStatus.NonExtendable +public interface BlockPositionResolver extends ArgumentResolver { +} +diff --git a/src/main/java/io/papermc/paper/command/brigadier/argument/resolvers/FinePositionResolver.java b/src/main/java/io/papermc/paper/command/brigadier/argument/resolvers/FinePositionResolver.java +new file mode 100644 +index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 +--- /dev/null ++++ b/src/main/java/io/papermc/paper/command/brigadier/argument/resolvers/FinePositionResolver.java +@@ -0,0 +0,0 @@ ++package io.papermc.paper.command.brigadier.argument.resolvers; ++ ++import io.papermc.paper.command.brigadier.CommandSourceStack; ++import io.papermc.paper.math.FinePosition; ++import org.jetbrains.annotations.ApiStatus; ++ ++/** ++ * An {@link ArgumentResolver} that's capable of resolving ++ * a fine position argument value using a {@link CommandSourceStack}. ++ * ++ * @see io.papermc.paper.command.brigadier.argument.ArgumentTypes#finePosition() ++ * @see io.papermc.paper.command.brigadier.argument.ArgumentTypes#finePosition(boolean) ++ */ ++@ApiStatus.Experimental ++@ApiStatus.NonExtendable ++public interface FinePositionResolver extends ArgumentResolver { ++} diff --git a/src/main/java/io/papermc/paper/command/brigadier/argument/resolvers/PlayerProfileListResolver.java b/src/main/java/io/papermc/paper/command/brigadier/argument/resolvers/PlayerProfileListResolver.java new file mode 100644 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 diff --git a/patches/server/Brigadier-based-command-API.patch b/patches/server/Brigadier-based-command-API.patch index ba50f3c0d5..09a7583580 100644 --- a/patches/server/Brigadier-based-command-API.patch +++ b/patches/server/Brigadier-based-command-API.patch @@ -1094,15 +1094,16 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 +import io.papermc.paper.command.brigadier.argument.range.IntegerRangeProvider; +import io.papermc.paper.command.brigadier.argument.range.RangeProvider; +import io.papermc.paper.command.brigadier.argument.resolvers.BlockPositionResolver; ++import io.papermc.paper.command.brigadier.argument.resolvers.FinePositionResolver; +import io.papermc.paper.command.brigadier.argument.resolvers.PlayerProfileListResolver; +import io.papermc.paper.command.brigadier.argument.resolvers.selector.EntitySelectorArgumentResolver; +import io.papermc.paper.command.brigadier.argument.resolvers.selector.PlayerSelectorArgumentResolver; +import io.papermc.paper.entity.LookAnchor; -+import io.papermc.paper.math.Position; +import io.papermc.paper.registry.PaperRegistries; +import io.papermc.paper.registry.RegistryAccess; +import io.papermc.paper.registry.RegistryKey; +import io.papermc.paper.registry.TypedKey; ++import io.papermc.paper.util.MCUtil; +import java.util.Collection; +import java.util.Collections; +import java.util.List; @@ -1137,6 +1138,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 +import net.minecraft.commands.arguments.UuidArgument; +import net.minecraft.commands.arguments.blocks.BlockStateArgument; +import net.minecraft.commands.arguments.coordinates.BlockPosArgument; ++import net.minecraft.commands.arguments.coordinates.Vec3Argument; +import net.minecraft.commands.arguments.item.ItemArgument; +import net.minecraft.commands.arguments.item.ItemPredicateArgument; +import net.minecraft.core.BlockPos; @@ -1146,6 +1148,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 +import net.minecraft.server.level.ServerLevel; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.level.Level; ++import net.minecraft.world.phys.Vec3; +import org.bukkit.GameMode; +import org.bukkit.HeightMap; +import org.bukkit.Keyed; @@ -1217,7 +1220,16 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + return this.wrap(BlockPosArgument.blockPos(), (result) -> sourceStack -> { + final BlockPos pos = result.getBlockPos((CommandSourceStack) sourceStack); + -+ return Position.block(pos.getX(), pos.getY(), pos.getZ()); ++ return MCUtil.toPosition(pos); ++ }); ++ } ++ ++ @Override ++ public ArgumentType finePosition(final boolean centerIntegers) { ++ return this.wrap(Vec3Argument.vec3(centerIntegers), (result) -> sourceStack -> { ++ final Vec3 vec3 = result.getPosition((CommandSourceStack) sourceStack); ++ ++ return MCUtil.toPosition(vec3); + }); + } + @@ -1310,7 +1322,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + return this.wrap(DimensionArgument.dimension(), dimensionLocation -> { + // based on DimensionArgument#getDimension + final ResourceKey resourceKey = ResourceKey.create(Registries.DIMENSION, dimensionLocation); -+ @Nullable final ServerLevel serverLevel = MinecraftServer.getServer().getLevel(resourceKey); ++ final @Nullable ServerLevel serverLevel = MinecraftServer.getServer().getLevel(resourceKey); + if (serverLevel == null) { + throw DimensionArgument.ERROR_INVALID_VALUE.create(dimensionLocation); + } else { diff --git a/test-plugin/src/main/java/io/papermc/testplugin/brigtests/Registration.java b/test-plugin/src/main/java/io/papermc/testplugin/brigtests/Registration.java index 67b0ed983d..f67feda8fa 100644 --- a/test-plugin/src/main/java/io/papermc/testplugin/brigtests/Registration.java +++ b/test-plugin/src/main/java/io/papermc/testplugin/brigtests/Registration.java @@ -7,6 +7,8 @@ import io.papermc.paper.command.brigadier.Commands; import io.papermc.paper.command.brigadier.argument.ArgumentTypes; import io.papermc.paper.command.brigadier.argument.RegistryArgumentExtractor; import io.papermc.paper.command.brigadier.argument.range.DoubleRangeProvider; +import io.papermc.paper.command.brigadier.argument.resolvers.FinePositionResolver; +import io.papermc.paper.math.FinePosition; import io.papermc.paper.plugin.bootstrap.BootstrapContext; import io.papermc.paper.plugin.lifecycle.event.LifecycleEventManager; import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents; @@ -59,6 +61,16 @@ public final class Registration { }) ).build() ); + commands.register(Commands.literal("fine-pos") + .then( + Commands.argument("pos", ArgumentTypes.finePosition(false)) + .executes(ctx -> { + final FinePositionResolver position = ctx.getArgument("pos", FinePositionResolver.class); + ctx.getSource().getSender().sendPlainMessage("Position: " + position.resolve(ctx.getSource())); + return Command.SINGLE_SUCCESS; + }) + ).build() + ); // ensure plugin commands override commands.register(Commands.literal("tag") .executes(ctx -> {