Skip to content

Commit 08f3ff0

Browse files
committed
override commands
1 parent 91f98ed commit 08f3ff0

1 file changed

Lines changed: 30 additions & 2 deletions

File tree

server/src/main/java/dev/plex/handlers/CommandHandler.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package dev.plex.handlers;
22

3+
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
4+
import com.mojang.brigadier.tree.LiteralCommandNode;
35
import dev.plex.Plex;
46
import dev.plex.command.PlexCommand;
57
import dev.plex.command.impl.*;
68
import dev.plex.util.PlexLog;
9+
import io.papermc.paper.command.brigadier.CommandSourceStack;
710
import io.papermc.paper.command.brigadier.Commands;
811
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
912
import 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

Comments
 (0)