diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/commands/PaperBrigadierCommand.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/commands/PaperBrigadierCommand.java index a0d0fca..d425c8a 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/commands/PaperBrigadierCommand.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/commands/PaperBrigadierCommand.java @@ -1,7 +1,7 @@ package fr.pandacube.lib.paper.commands; -import com.destroystokyo.paper.brigadier.BukkitBrigadierCommandSource; import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.arguments.ArgumentType; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.suggestion.SuggestionProvider; @@ -11,10 +11,10 @@ import fr.pandacube.lib.chat.Chat; import fr.pandacube.lib.commands.BadCommandUsage; import fr.pandacube.lib.commands.BrigadierCommand; import fr.pandacube.lib.commands.SuggestionsSupplier; -import fr.pandacube.lib.paper.reflect.PandalibPaperReflect; -import fr.pandacube.lib.paper.reflect.wrapper.craftbukkit.CraftServer; +import fr.pandacube.lib.paper.reflect.wrapper.craftbukkit.CraftVector; import fr.pandacube.lib.paper.reflect.wrapper.craftbukkit.VanillaCommandWrapper; -import fr.pandacube.lib.paper.reflect.wrapper.minecraft.commands.Commands; +import fr.pandacube.lib.paper.reflect.wrapper.minecraft.commands.Coordinates; +import fr.pandacube.lib.paper.reflect.wrapper.minecraft.commands.Vec3Argument; import fr.pandacube.lib.players.standalone.AbstractOffPlayer; import fr.pandacube.lib.players.standalone.AbstractOnlinePlayer; import fr.pandacube.lib.players.standalone.AbstractPlayerManager; @@ -32,6 +32,7 @@ import org.bukkit.command.PluginCommand; import org.bukkit.entity.Player; import org.bukkit.event.Listener; import org.bukkit.plugin.Plugin; +import org.bukkit.util.Vector; import java.util.HashSet; import java.util.List; @@ -39,26 +40,24 @@ import java.util.Set; import java.util.function.Predicate; import java.util.stream.Stream; -import static fr.pandacube.lib.util.ThrowableUtil.wrapEx; - /** * Abstract class to hold a command to be integrated into a Paper server vanilla command dispatcher. */ @SuppressWarnings("UnstableApiUsage") public abstract class PaperBrigadierCommand extends BrigadierCommand implements Listener { + private static CommandDispatcher vanillaPaperDispatcher = null; - private static final Commands vanillaCommandDispatcher; - private static final CommandDispatcher nmsDispatcher; - - static { - wrapEx(PandalibPaperReflect::init); - vanillaCommandDispatcher = ReflectWrapper.wrapTyped(Bukkit.getServer(), CraftServer.class) - .getServer() - .vanillaCommandDispatcher(); - nmsDispatcher = vanillaCommandDispatcher.dispatcher(); + public static CommandDispatcher getVanillaPaperDispatcher() { + return vanillaPaperDispatcher; } + public static RootCommandNode getRootNode() { + return vanillaPaperDispatcher == null ? null : vanillaPaperDispatcher.getRoot(); + } + + + /** * Removes a plugin command that overrides a vanilla command, so the vanilla command functionalities are fully * restored (so, not only the usage, but also the suggestions and the command structure sent to the client). @@ -66,6 +65,13 @@ public abstract class PaperBrigadierCommand extends BrigadierCommand node = (LiteralCommandNode) getRootNode().getChild(name); - Command newCommand = new VanillaCommandWrapper(vanillaCommandDispatcher, node).__getRuntimeInstance(); - bukkitCmdMap.getKnownCommands().put(name.toLowerCase(), newCommand); - newCommand.register(bukkitCmdMap); + bukkitCmdMap.getKnownCommands().put(name.toLowerCase(java.util.Locale.ENGLISH), vanillaCommand); } } - /** - * Returns the vanilla instance of the Brigadier dispatcher. - * @return the vanilla instance of the Brigadier dispatcher. - */ - public static CommandDispatcher getNMSDispatcher() { - return nmsDispatcher; - } - - /** - * Returns the root command node of the Brigadier dispatcher. - * @return the root command node of the Brigadier dispatcher. - */ - protected static RootCommandNode getRootNode() { - return nmsDispatcher.getRoot(); - } - - - - - - @@ -160,6 +141,10 @@ public abstract class PaperBrigadierCommand extends BrigadierCommand { + if (vanillaPaperDispatcher == null) { + vanillaPaperDispatcher = event.registrar().getDispatcher(); + } + registeredAliases = new HashSet<>(event.registrar().register(commandNode, description, List.of(aliases))); if (registrationPolicy == RegistrationPolicy.ALL) { @@ -364,6 +349,30 @@ public abstract class PaperBrigadierCommand extends BrigadierCommand argumentMinecraftVec3() { + return Vec3Argument.vec3(true); + } + + /** + * Gets the value of the provided argument of type {@code minecraft:vec3}, from the provided context. + * @param context the command execution context. + * @param argument the argument name. + * @param deflt a default value if the argument is not found. + * @return the value of the argument. + */ + public Vector tryGetMinecraftVec3Argument(CommandContext context, String argument, + Vector deflt) { + return tryGetArgument(context, argument, Coordinates.MAPPING.runtimeClass(), + nmsCoordinate -> CraftVector.toBukkit( + ReflectWrapper.wrap(nmsCoordinate, Coordinates.class).getPosition(context.getSource()) + ), + deflt); + } + diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/craftbukkit/VanillaCommandWrapper.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/craftbukkit/VanillaCommandWrapper.java index 85c5e6b..a813d90 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/craftbukkit/VanillaCommandWrapper.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/craftbukkit/VanillaCommandWrapper.java @@ -1,16 +1,14 @@ package fr.pandacube.lib.paper.reflect.wrapper.craftbukkit; -import com.destroystokyo.paper.brigadier.BukkitBrigadierCommandSource; import com.mojang.brigadier.tree.CommandNode; - import fr.pandacube.lib.paper.reflect.OBCReflect; -import fr.pandacube.lib.reflect.wrapper.ReflectWrapperTyped; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.commands.Commands; import fr.pandacube.lib.reflect.ReflectClass; import fr.pandacube.lib.reflect.ReflectConstructor; import fr.pandacube.lib.reflect.ReflectField; import fr.pandacube.lib.reflect.ReflectMethod; - +import fr.pandacube.lib.reflect.wrapper.ReflectWrapperTyped; +import io.papermc.paper.command.brigadier.CommandSourceStack; import org.bukkit.command.CommandSender; import org.bukkit.command.defaults.BukkitCommand; @@ -23,17 +21,17 @@ public class VanillaCommandWrapper extends ReflectWrapperTyped { public static final ReflectField vanillaCommand = wrapEx(() -> REFLECT.field("vanillaCommand")); public static final ReflectMethod getListener = wrapEx(() -> REFLECT.method("getListener", CommandSender.class)); - public VanillaCommandWrapper(Commands dispatcher, CommandNode vanillaCommand) { + public VanillaCommandWrapper(Commands dispatcher, CommandNode vanillaCommand) { this(wrapReflectEx(() -> CONSTRUCTOR.instantiate(unwrap(dispatcher), vanillaCommand))); } @SuppressWarnings("unchecked") - public CommandNode vanillaCommand() { - return (CommandNode) wrapReflectEx(() -> vanillaCommand.getValue(__getRuntimeInstance())); + public CommandNode vanillaCommand() { + return (CommandNode) wrapReflectEx(() -> vanillaCommand.getValue(__getRuntimeInstance())); } - public static BukkitBrigadierCommandSource getListener(CommandSender sender) { - return (BukkitBrigadierCommandSource) wrapReflectEx(() -> getListener.invokeStatic(sender)); + public static CommandSourceStack getListener(CommandSender sender) { + return (CommandSourceStack) wrapReflectEx(() -> getListener.invokeStatic(sender)); } protected VanillaCommandWrapper(Object obj) { diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/CommandSourceStack.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/CommandSourceStack.java index 5416cc0..900d41a 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/CommandSourceStack.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/CommandSourceStack.java @@ -1,12 +1,11 @@ package fr.pandacube.lib.paper.reflect.wrapper.minecraft.commands; -import com.destroystokyo.paper.brigadier.BukkitBrigadierCommandSource; import fr.pandacube.lib.paper.reflect.NMSReflect; import fr.pandacube.lib.reflect.wrapper.ReflectWrapperTyped; import static fr.pandacube.lib.util.ThrowableUtil.wrapEx; -public class CommandSourceStack extends ReflectWrapperTyped { +public class CommandSourceStack extends ReflectWrapperTyped { public static final NMSReflect.ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.commands.CommandSourceStack")); protected CommandSourceStack(Object obj) { diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/Commands.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/Commands.java index fe7db81..96fb64c 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/Commands.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/Commands.java @@ -1,11 +1,9 @@ package fr.pandacube.lib.paper.reflect.wrapper.minecraft.commands; -import com.destroystokyo.paper.brigadier.BukkitBrigadierCommandSource; import com.mojang.brigadier.CommandDispatcher; - import fr.pandacube.lib.paper.reflect.NMSReflect; -import fr.pandacube.lib.reflect.wrapper.ReflectWrapper; import fr.pandacube.lib.reflect.ReflectField; +import fr.pandacube.lib.reflect.wrapper.ReflectWrapper; import static fr.pandacube.lib.util.ThrowableUtil.wrapEx; import static fr.pandacube.lib.util.ThrowableUtil.wrapReflectEx; @@ -15,8 +13,8 @@ public class Commands extends ReflectWrapper { public static final ReflectField dispatcher = wrapEx(() -> MAPPING.mojField("dispatcher")); @SuppressWarnings("unchecked") - public CommandDispatcher dispatcher() { - return (CommandDispatcher) wrapReflectEx(() -> dispatcher.getValue(__getRuntimeInstance())); + public CommandDispatcher dispatcher() { + return (CommandDispatcher) wrapReflectEx(() -> dispatcher.getValue(__getRuntimeInstance())); } protected Commands(Object obj) { diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/Coordinates.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/Coordinates.java index 17a9b9c..2c0db63 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/Coordinates.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/Coordinates.java @@ -1,33 +1,25 @@ package fr.pandacube.lib.paper.reflect.wrapper.minecraft.commands; -import com.destroystokyo.paper.brigadier.BukkitBrigadierCommandSource; - import fr.pandacube.lib.paper.reflect.NMSReflect; +import fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.Vec3; +import fr.pandacube.lib.reflect.ReflectMethod; import fr.pandacube.lib.reflect.wrapper.ConcreteWrapper; import fr.pandacube.lib.reflect.wrapper.ReflectWrapper; import fr.pandacube.lib.reflect.wrapper.ReflectWrapperI; -import fr.pandacube.lib.paper.reflect.wrapper.minecraft.core.BlockPos; -import fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.Vec3; -import fr.pandacube.lib.reflect.ReflectMethod; +import static fr.pandacube.lib.reflect.wrapper.ReflectWrapper.wrap; import static fr.pandacube.lib.util.ThrowableUtil.wrapEx; import static fr.pandacube.lib.util.ThrowableUtil.wrapReflectEx; -import static fr.pandacube.lib.reflect.wrapper.ReflectWrapper.wrap; @ConcreteWrapper(Coordinates.__concrete.class) public interface Coordinates extends ReflectWrapperI { NMSReflect.ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.commands.arguments.coordinates.Coordinates")); ReflectMethod getPosition = wrapEx(() -> MAPPING.mojMethod("getPosition", CommandSourceStack.MAPPING)); - ReflectMethod getBlockPos = wrapEx(() -> MAPPING.mojMethod("getBlockPos", CommandSourceStack.MAPPING)); - default Vec3 getPosition(BukkitBrigadierCommandSource source) { + default Vec3 getPosition(io.papermc.paper.command.brigadier.CommandSourceStack source) { return wrap(wrapReflectEx(() -> getPosition.invoke(__getRuntimeInstance(), source)), Vec3.class); } - default BlockPos getBlockPos(BukkitBrigadierCommandSource source) { - return wrap(wrapReflectEx(() -> getBlockPos.invoke(__getRuntimeInstance(), source)), BlockPos.class); - } - class __concrete extends ReflectWrapper implements Coordinates { protected __concrete(Object obj) { super(obj); diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/server/MinecraftServer.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/server/MinecraftServer.java index 7882018..42a5caa 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/server/MinecraftServer.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/server/MinecraftServer.java @@ -3,21 +3,14 @@ package fr.pandacube.lib.paper.reflect.wrapper.minecraft.server; import fr.pandacube.lib.paper.reflect.NMSReflect; import fr.pandacube.lib.reflect.ReflectMethod; import fr.pandacube.lib.reflect.wrapper.ReflectWrapper; -import fr.pandacube.lib.paper.reflect.wrapper.minecraft.commands.Commands; -import fr.pandacube.lib.reflect.ReflectField; import static fr.pandacube.lib.util.ThrowableUtil.wrapEx; import static fr.pandacube.lib.util.ThrowableUtil.wrapReflectEx; public class MinecraftServer extends ReflectWrapper { public static final NMSReflect.ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.server.MinecraftServer")); - private static final ReflectField vanillaCommandDispatcher = wrapEx(() -> MAPPING.runtimeReflect().field("vanillaCommandDispatcher")); private static final ReflectMethod getPlayerList = wrapEx(() -> MAPPING.mojMethod("getPlayerList")); - public Commands vanillaCommandDispatcher() { - return wrap(wrapReflectEx(() -> vanillaCommandDispatcher.getValue(__getRuntimeInstance())), Commands.class); - } - public PlayerList getPlayerList() { return wrap(wrapReflectEx(() -> getPlayerList.invoke(__getRuntimeInstance())), PlayerList.class); }