11package dev .plex .handlers ;
22
3+ import com .mojang .brigadier .builder .LiteralArgumentBuilder ;
4+ import com .mojang .brigadier .tree .LiteralCommandNode ;
35import dev .plex .Plex ;
46import dev .plex .command .PlexCommand ;
57import dev .plex .command .impl .*;
68import dev .plex .util .PlexLog ;
9+ import io .papermc .paper .command .brigadier .CommandSourceStack ;
710import io .papermc .paper .command .brigadier .Commands ;
811import io .papermc .paper .plugin .lifecycle .event .types .LifecycleEvents ;
912import java .util .ArrayList ;
@@ -79,11 +82,17 @@ private void register(Commands registrar)
7982 int labels = 0 ;
8083 for (PlexCommand command : commands )
8184 {
82- var registeredLabels = registrar .register (command .buildCommand (), command .getDescription (), command .getAliases ());
85+ LiteralCommandNode <CommandSourceStack > commandNode = command .buildCommand ();
86+ var registeredLabels = registrar .register (commandNode , command .getDescription (), List .of ());
8387 labels += registeredLabels .size ();
88+
8489 for (String alias : command .getAliases ())
8590 {
86- if (!registeredLabels .contains (alias ) && !registeredLabels .contains ("plex:" + alias ))
91+ // Paper does not let aliases replace an existing command. Register each
92+ // Plex alias as a primary command node so Plex deliberately takes priority.
93+ var registeredAliasLabels = registrar .register (copyWithLabel (commandNode , alias ), command .getDescription (), List .of ());
94+ labels += registeredAliasLabels .size ();
95+ if (!registeredAliasLabels .contains (alias ) && !registeredAliasLabels .contains ("plex:" + alias ))
8796 {
8897 PlexLog .warn ("Command alias {0} for {1} was not registered, likely because another command already owns it." , alias , command .getName ());
8998 }
@@ -94,6 +103,25 @@ private void register(Commands registrar)
94103 PlexLog .log ("Registered {0} Brigadier commands with {1} root labels." , commands .size (), labels );
95104 }
96105
106+ private LiteralCommandNode <CommandSourceStack > copyWithLabel (LiteralCommandNode <CommandSourceStack > commandNode , String label )
107+ {
108+ LiteralArgumentBuilder <CommandSourceStack > builder = Commands .literal (label )
109+ .requires (commandNode .getRequirement ());
110+ if (commandNode .getCommand () != null )
111+ {
112+ builder .executes (commandNode .getCommand ());
113+ }
114+ if (commandNode .getRedirect () != null )
115+ {
116+ builder .forward (commandNode .getRedirect (), commandNode .getRedirectModifier (), commandNode .isFork ());
117+ }
118+ else
119+ {
120+ commandNode .getChildren ().forEach (builder ::then );
121+ }
122+ return builder .build ();
123+ }
124+
97125 private void registerBuiltInCommands (boolean debugEnabled )
98126 {
99127 commands .addAll (List .of (
0 commit comments