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 773720f..9b232d2 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 @@ -73,13 +73,13 @@ public abstract class PaperBrigadierCommand extends BrigadierCommand wrappedRoot = vanillaPaperDispatcher.getRoot(); ReflectClass apiMirrorRootNodeClass = Reflect.ofClassOfInstance(wrappedRoot); try { - RootCommandNode actualRoot = ((CommandDispatcher) apiMirrorRootNodeClass.method("getDispatcher").invoke(wrappedRoot)).getRoot(); + RootCommandNode unwrappedRoot = ((CommandDispatcher) apiMirrorRootNodeClass.method("getDispatcher").invoke(wrappedRoot)).getRoot(); - Reflect.ofClass(CommandNode.class).field("unwrappedCached").setValue(wrappedRoot, actualRoot); - Reflect.ofClass(CommandNode.class).field("wrappedCached").setValue(actualRoot, wrappedRoot); + Reflect.ofClass(CommandNode.class).field("unwrappedCached").setValue(wrappedRoot, unwrappedRoot); + Reflect.ofClass(CommandNode.class).field("wrappedCached").setValue(unwrappedRoot, wrappedRoot); } catch (InvocationTargetException|IllegalAccessException|NoSuchMethodException|NoSuchFieldException e) { - Log.severe("Unable to trick the Paper/Brigadier unwrapper to properly handle redirecting to root command node.", e); + Log.severe("Unable to trick the Paper/Brigadier unwrapper to properly handle commands redirecting to root command node.", e); } } } @@ -183,6 +183,7 @@ public abstract class PaperBrigadierCommand extends BrigadierCommand(event.registrar().register(commandNode, description, List.of(aliases))); + doPostRegistrationFixes(); if (registrationPolicy == RegistrationPolicy.ALL) { // enforce registration of aliases @@ -233,6 +234,43 @@ public abstract class PaperBrigadierCommand extends BrigadierCommand(), commandNode); + } + + private void postRegistrationFixNode(Set> fixedNodes, CommandNode originalNode) { + if (originalNode instanceof RootCommandNode) + return; + if (fixedNodes.contains(originalNode)) + return; + fixedNodes.add(originalNode); + if (originalNode.getRedirect() != null) { + try { + ReflectClass cmdNodeClass = Reflect.ofClass(CommandNode.class); + CommandNode unwrappedNode = (CommandNode) cmdNodeClass.field("unwrappedCached").getValue(originalNode); + if (unwrappedNode != null) { + cmdNodeClass.field("modifier").setValue(unwrappedNode, cmdNodeClass.field("modifier").getValue(originalNode)); + cmdNodeClass.field("forks").setValue(unwrappedNode, cmdNodeClass.field("forks").getValue(originalNode)); + } + } catch (IllegalAccessException | NoSuchFieldException e) { + throw new RuntimeException(e); + } + + postRegistrationFixNode(fixedNodes, originalNode.getRedirect()); + } + else { + try { + for (CommandNode child : originalNode.getChildren()) + postRegistrationFixNode(fixedNodes, child); + } catch (UnsupportedOperationException ignored) { + // in case getChildren is not possible (vanilla commands are wrapped by Paper API) + } + } + } + + private static LiteralCommandNode getAliasNode(CommandNode commandNode, String alias) { return LiteralArgumentBuilder.literal(alias) .requires(commandNode.getRequirement())