Fix Brigadier command stuff in pandalib-paper

This commit is contained in:
Marc Baloup 2024-06-09 22:48:54 +02:00
parent e7b528718c
commit 0ff2ae1296
6 changed files with 66 additions and 77 deletions

View File

@ -1,7 +1,7 @@
package fr.pandacube.lib.paper.commands; package fr.pandacube.lib.paper.commands;
import com.destroystokyo.paper.brigadier.BukkitBrigadierCommandSource;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.SuggestionProvider; 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.BadCommandUsage;
import fr.pandacube.lib.commands.BrigadierCommand; import fr.pandacube.lib.commands.BrigadierCommand;
import fr.pandacube.lib.commands.SuggestionsSupplier; import fr.pandacube.lib.commands.SuggestionsSupplier;
import fr.pandacube.lib.paper.reflect.PandalibPaperReflect; import fr.pandacube.lib.paper.reflect.wrapper.craftbukkit.CraftVector;
import fr.pandacube.lib.paper.reflect.wrapper.craftbukkit.CraftServer;
import fr.pandacube.lib.paper.reflect.wrapper.craftbukkit.VanillaCommandWrapper; 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.AbstractOffPlayer;
import fr.pandacube.lib.players.standalone.AbstractOnlinePlayer; import fr.pandacube.lib.players.standalone.AbstractOnlinePlayer;
import fr.pandacube.lib.players.standalone.AbstractPlayerManager; import fr.pandacube.lib.players.standalone.AbstractPlayerManager;
@ -32,6 +32,7 @@ import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.util.Vector;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -39,26 +40,24 @@ import java.util.Set;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.stream.Stream; 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. * Abstract class to hold a command to be integrated into a Paper server vanilla command dispatcher.
*/ */
@SuppressWarnings("UnstableApiUsage") @SuppressWarnings("UnstableApiUsage")
public abstract class PaperBrigadierCommand extends BrigadierCommand<CommandSourceStack> implements Listener { public abstract class PaperBrigadierCommand extends BrigadierCommand<CommandSourceStack> implements Listener {
private static CommandDispatcher<CommandSourceStack> vanillaPaperDispatcher = null;
private static final Commands vanillaCommandDispatcher; public static CommandDispatcher<CommandSourceStack> getVanillaPaperDispatcher() {
private static final CommandDispatcher<BukkitBrigadierCommandSource> nmsDispatcher; return vanillaPaperDispatcher;
static {
wrapEx(PandalibPaperReflect::init);
vanillaCommandDispatcher = ReflectWrapper.wrapTyped(Bukkit.getServer(), CraftServer.class)
.getServer()
.vanillaCommandDispatcher();
nmsDispatcher = vanillaCommandDispatcher.dispatcher();
} }
public static RootCommandNode<CommandSourceStack> getRootNode() {
return vanillaPaperDispatcher == null ? null : vanillaPaperDispatcher.getRoot();
}
/** /**
* Removes a plugin command that overrides a vanilla command, so the vanilla command functionalities are fully * 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). * 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<CommandSour
*/ */
public static void restoreVanillaCommand(String name) { public static void restoreVanillaCommand(String name) {
CommandMap bukkitCmdMap = Bukkit.getCommandMap(); CommandMap bukkitCmdMap = Bukkit.getCommandMap();
Command vanillaCommand = bukkitCmdMap.getCommand("minecraft:" + name);
if (vanillaCommand == null || !VanillaCommandWrapper.REFLECT.get().isInstance(vanillaCommand)) {
Log.warning("There is no vanilla command '" + name + "' to restore.");
return;
}
Command bukkitCommand = bukkitCmdMap.getCommand(name); Command bukkitCommand = bukkitCmdMap.getCommand(name);
if (bukkitCommand != null) { if (bukkitCommand != null) {
if (VanillaCommandWrapper.REFLECT.get().isInstance(bukkitCommand)) { if (VanillaCommandWrapper.REFLECT.get().isInstance(bukkitCommand)) {
@ -73,39 +79,14 @@ public abstract class PaperBrigadierCommand extends BrigadierCommand<CommandSour
return; return;
} }
Log.info("Removing Bukkit command /" + name + " (" + getCommandIdentity(bukkitCommand) + ")"); Log.info("Removing Bukkit command /" + name + " (" + getCommandIdentity(bukkitCommand) + ")");
Log.warning("[1.20.6 update] Please test that the bukkit command removal is actually working."); Log.warning("[1.20.6 update] Please test that the bukkit command removal is actually working, and being replaced back by the vanilla one.");
bukkitCmdMap.getKnownCommands().remove(name.toLowerCase(java.util.Locale.ENGLISH)); bukkitCmdMap.getKnownCommands().remove(name.toLowerCase(java.util.Locale.ENGLISH));
bukkitCommand.unregister(bukkitCmdMap); bukkitCommand.unregister(bukkitCmdMap);
bukkitCmdMap.getKnownCommands().put(name.toLowerCase(java.util.Locale.ENGLISH), vanillaCommand);
LiteralCommandNode<BukkitBrigadierCommandSource> node = (LiteralCommandNode<BukkitBrigadierCommandSource>) getRootNode().getChild(name);
Command newCommand = new VanillaCommandWrapper(vanillaCommandDispatcher, node).__getRuntimeInstance();
bukkitCmdMap.getKnownCommands().put(name.toLowerCase(), newCommand);
newCommand.register(bukkitCmdMap);
} }
} }
/**
* Returns the vanilla instance of the Brigadier dispatcher.
* @return the vanilla instance of the Brigadier dispatcher.
*/
public static CommandDispatcher<BukkitBrigadierCommandSource> getNMSDispatcher() {
return nmsDispatcher;
}
/**
* Returns the root command node of the Brigadier dispatcher.
* @return the root command node of the Brigadier dispatcher.
*/
protected static RootCommandNode<BukkitBrigadierCommandSource> getRootNode() {
return nmsDispatcher.getRoot();
}
@ -160,6 +141,10 @@ public abstract class PaperBrigadierCommand extends BrigadierCommand<CommandSour
private void register() { private void register() {
plugin.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS, event -> { plugin.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS, event -> {
if (vanillaPaperDispatcher == null) {
vanillaPaperDispatcher = event.registrar().getDispatcher();
}
registeredAliases = new HashSet<>(event.registrar().register(commandNode, description, List.of(aliases))); registeredAliases = new HashSet<>(event.registrar().register(commandNode, description, List.of(aliases)));
if (registrationPolicy == RegistrationPolicy.ALL) { if (registrationPolicy == RegistrationPolicy.ALL) {
@ -364,6 +349,30 @@ public abstract class PaperBrigadierCommand extends BrigadierCommand<CommandSour
* Minecraft's argument type * Minecraft's argument type
*/ */
/**
* Creates a new instance of the Brigadier argument type {@code minecraft:vec3}.
* @return the {@code minecraft:vec3} argument type.
*/
public static ArgumentType<Object> 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<CommandSourceStack> context, String argument,
Vector deflt) {
return tryGetArgument(context, argument, Coordinates.MAPPING.runtimeClass(),
nmsCoordinate -> CraftVector.toBukkit(
ReflectWrapper.wrap(nmsCoordinate, Coordinates.class).getPosition(context.getSource())
),
deflt);
}

View File

@ -1,16 +1,14 @@
package fr.pandacube.lib.paper.reflect.wrapper.craftbukkit; package fr.pandacube.lib.paper.reflect.wrapper.craftbukkit;
import com.destroystokyo.paper.brigadier.BukkitBrigadierCommandSource;
import com.mojang.brigadier.tree.CommandNode; import com.mojang.brigadier.tree.CommandNode;
import fr.pandacube.lib.paper.reflect.OBCReflect; 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.paper.reflect.wrapper.minecraft.commands.Commands;
import fr.pandacube.lib.reflect.ReflectClass; import fr.pandacube.lib.reflect.ReflectClass;
import fr.pandacube.lib.reflect.ReflectConstructor; import fr.pandacube.lib.reflect.ReflectConstructor;
import fr.pandacube.lib.reflect.ReflectField; import fr.pandacube.lib.reflect.ReflectField;
import fr.pandacube.lib.reflect.ReflectMethod; 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.CommandSender;
import org.bukkit.command.defaults.BukkitCommand; import org.bukkit.command.defaults.BukkitCommand;
@ -23,17 +21,17 @@ public class VanillaCommandWrapper extends ReflectWrapperTyped<BukkitCommand> {
public static final ReflectField<?> vanillaCommand = wrapEx(() -> REFLECT.field("vanillaCommand")); public static final ReflectField<?> vanillaCommand = wrapEx(() -> REFLECT.field("vanillaCommand"));
public static final ReflectMethod<?> getListener = wrapEx(() -> REFLECT.method("getListener", CommandSender.class)); public static final ReflectMethod<?> getListener = wrapEx(() -> REFLECT.method("getListener", CommandSender.class));
public VanillaCommandWrapper(Commands dispatcher, CommandNode<BukkitBrigadierCommandSource> vanillaCommand) { public VanillaCommandWrapper(Commands dispatcher, CommandNode<CommandSourceStack> vanillaCommand) {
this(wrapReflectEx(() -> CONSTRUCTOR.instantiate(unwrap(dispatcher), vanillaCommand))); this(wrapReflectEx(() -> CONSTRUCTOR.instantiate(unwrap(dispatcher), vanillaCommand)));
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public CommandNode<BukkitBrigadierCommandSource> vanillaCommand() { public CommandNode<CommandSourceStack> vanillaCommand() {
return (CommandNode<BukkitBrigadierCommandSource>) wrapReflectEx(() -> vanillaCommand.getValue(__getRuntimeInstance())); return (CommandNode<CommandSourceStack>) wrapReflectEx(() -> vanillaCommand.getValue(__getRuntimeInstance()));
} }
public static BukkitBrigadierCommandSource getListener(CommandSender sender) { public static CommandSourceStack getListener(CommandSender sender) {
return (BukkitBrigadierCommandSource) wrapReflectEx(() -> getListener.invokeStatic(sender)); return (CommandSourceStack) wrapReflectEx(() -> getListener.invokeStatic(sender));
} }
protected VanillaCommandWrapper(Object obj) { protected VanillaCommandWrapper(Object obj) {

View File

@ -1,12 +1,11 @@
package fr.pandacube.lib.paper.reflect.wrapper.minecraft.commands; 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.NMSReflect;
import fr.pandacube.lib.reflect.wrapper.ReflectWrapperTyped; import fr.pandacube.lib.reflect.wrapper.ReflectWrapperTyped;
import static fr.pandacube.lib.util.ThrowableUtil.wrapEx; import static fr.pandacube.lib.util.ThrowableUtil.wrapEx;
public class CommandSourceStack extends ReflectWrapperTyped<BukkitBrigadierCommandSource> { public class CommandSourceStack extends ReflectWrapperTyped<io.papermc.paper.command.brigadier.CommandSourceStack> {
public static final NMSReflect.ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.commands.CommandSourceStack")); public static final NMSReflect.ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.commands.CommandSourceStack"));
protected CommandSourceStack(Object obj) { protected CommandSourceStack(Object obj) {

View File

@ -1,11 +1,9 @@
package fr.pandacube.lib.paper.reflect.wrapper.minecraft.commands; package fr.pandacube.lib.paper.reflect.wrapper.minecraft.commands;
import com.destroystokyo.paper.brigadier.BukkitBrigadierCommandSource;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import fr.pandacube.lib.paper.reflect.NMSReflect; 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.ReflectField;
import fr.pandacube.lib.reflect.wrapper.ReflectWrapper;
import static fr.pandacube.lib.util.ThrowableUtil.wrapEx; import static fr.pandacube.lib.util.ThrowableUtil.wrapEx;
import static fr.pandacube.lib.util.ThrowableUtil.wrapReflectEx; 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")); public static final ReflectField<?> dispatcher = wrapEx(() -> MAPPING.mojField("dispatcher"));
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public CommandDispatcher<BukkitBrigadierCommandSource> dispatcher() { public CommandDispatcher<CommandSourceStack> dispatcher() {
return (CommandDispatcher<BukkitBrigadierCommandSource>) wrapReflectEx(() -> dispatcher.getValue(__getRuntimeInstance())); return (CommandDispatcher<CommandSourceStack>) wrapReflectEx(() -> dispatcher.getValue(__getRuntimeInstance()));
} }
protected Commands(Object obj) { protected Commands(Object obj) {

View File

@ -1,33 +1,25 @@
package fr.pandacube.lib.paper.reflect.wrapper.minecraft.commands; 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.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.ConcreteWrapper;
import fr.pandacube.lib.reflect.wrapper.ReflectWrapper; import fr.pandacube.lib.reflect.wrapper.ReflectWrapper;
import fr.pandacube.lib.reflect.wrapper.ReflectWrapperI; 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.wrapEx;
import static fr.pandacube.lib.util.ThrowableUtil.wrapReflectEx; import static fr.pandacube.lib.util.ThrowableUtil.wrapReflectEx;
import static fr.pandacube.lib.reflect.wrapper.ReflectWrapper.wrap;
@ConcreteWrapper(Coordinates.__concrete.class) @ConcreteWrapper(Coordinates.__concrete.class)
public interface Coordinates extends ReflectWrapperI { public interface Coordinates extends ReflectWrapperI {
NMSReflect.ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.commands.arguments.coordinates.Coordinates")); NMSReflect.ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.commands.arguments.coordinates.Coordinates"));
ReflectMethod<?> getPosition = wrapEx(() -> MAPPING.mojMethod("getPosition", CommandSourceStack.MAPPING)); 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); 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 { class __concrete extends ReflectWrapper implements Coordinates {
protected __concrete(Object obj) { protected __concrete(Object obj) {
super(obj); super(obj);

View File

@ -3,21 +3,14 @@ package fr.pandacube.lib.paper.reflect.wrapper.minecraft.server;
import fr.pandacube.lib.paper.reflect.NMSReflect; import fr.pandacube.lib.paper.reflect.NMSReflect;
import fr.pandacube.lib.reflect.ReflectMethod; import fr.pandacube.lib.reflect.ReflectMethod;
import fr.pandacube.lib.reflect.wrapper.ReflectWrapper; 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.wrapEx;
import static fr.pandacube.lib.util.ThrowableUtil.wrapReflectEx; import static fr.pandacube.lib.util.ThrowableUtil.wrapReflectEx;
public class MinecraftServer extends ReflectWrapper { public class MinecraftServer extends ReflectWrapper {
public static final NMSReflect.ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.server.MinecraftServer")); 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")); private static final ReflectMethod<?> getPlayerList = wrapEx(() -> MAPPING.mojMethod("getPlayerList"));
public Commands vanillaCommandDispatcher() {
return wrap(wrapReflectEx(() -> vanillaCommandDispatcher.getValue(__getRuntimeInstance())), Commands.class);
}
public PlayerList getPlayerList() { public PlayerList getPlayerList() {
return wrap(wrapReflectEx(() -> getPlayerList.invoke(__getRuntimeInstance())), PlayerList.class); return wrap(wrapReflectEx(() -> getPlayerList.invoke(__getRuntimeInstance())), PlayerList.class);
} }