PaperMC/nms-patches/CommandDispatcher.patch

237 lines
12 KiB
Diff
Raw Normal View History

2018-07-15 02:00:00 +02:00
--- a/net/minecraft/server/CommandDispatcher.java
+++ b/net/minecraft/server/CommandDispatcher.java
2018-12-25 22:00:00 +01:00
@@ -22,12 +22,21 @@
2018-07-15 02:00:00 +02:00
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
+// CraftBukkit start
+import com.google.common.base.Joiner;
+import java.util.LinkedHashSet;
+import org.bukkit.craftbukkit.command.VanillaCommandWrapper;
+import org.bukkit.event.player.PlayerCommandSendEvent;
2018-07-15 02:00:00 +02:00
+import org.bukkit.event.server.ServerCommandEvent;
+// CraftBukkit end
+
public class CommandDispatcher {
private static final Logger a = LogManager.getLogger();
private final com.mojang.brigadier.CommandDispatcher<CommandListenerWrapper> b = new com.mojang.brigadier.CommandDispatcher();
- public CommandDispatcher(boolean flag) {
+ // CraftBukkit start
+ public final CommandDispatcher init(boolean flag) {
CommandAdvancement.a(this.b);
CommandExecute.a(this.b);
2018-08-26 04:00:00 +02:00
CommandBossBar.a(this.b);
2018-12-25 22:00:00 +01:00
@@ -95,8 +104,13 @@
}
2018-07-15 02:00:00 +02:00
this.b.findAmbiguities((commandnode, commandnode1, commandnode2, collection) -> {
- CommandDispatcher.a.warn("Ambiguity between arguments {} and {} with inputs: {}", this.b.getPath(commandnode1), this.b.getPath(commandnode2), collection);
+ // CommandDispatcher.a.warn("Ambiguity between arguments {} and {} with inputs: {}", this.b.getPath(commandnode1), this.b.getPath(commandnode2), collection); // CraftBukkit
2018-07-15 02:00:00 +02:00
});
+ return this;
+ }
+
+ public CommandDispatcher() {
+ // CraftBukkit end
2018-12-25 22:00:00 +01:00
this.b.setConsumer((commandcontext, flag1, i) -> {
((CommandListenerWrapper) commandcontext.getSource()).a(commandcontext, flag1, i);
2018-07-15 02:00:00 +02:00
});
2018-12-25 22:00:00 +01:00
@@ -111,7 +125,47 @@
2018-07-15 02:00:00 +02:00
}
+ // CraftBukkit start
+ public int dispatchServerCommand(CommandListenerWrapper sender, String command) {
+ Joiner joiner = Joiner.on(" ");
+ if (command.startsWith("/")) {
+ command = command.substring(1);
+ }
+
+ ServerCommandEvent event = new ServerCommandEvent(sender.getBukkitSender(), command);
+ org.bukkit.Bukkit.getPluginManager().callEvent(event);
+ if (event.isCancelled()) {
+ return 0;
+ }
+ command = event.getCommand();
+
+ String[] args = command.split(" ");
+
+ String cmd = args[0];
+ if (cmd.startsWith("minecraft:")) cmd = cmd.substring("minecraft:".length());
+ if (cmd.startsWith("bukkit:")) cmd = cmd.substring("bukkit:".length());
+
+ // Block disallowed commands
+ if (cmd.equalsIgnoreCase("stop") || cmd.equalsIgnoreCase("kick") || cmd.equalsIgnoreCase("op")
+ || cmd.equalsIgnoreCase("deop") || cmd.equalsIgnoreCase("ban") || cmd.equalsIgnoreCase("ban-ip")
+ || cmd.equalsIgnoreCase("pardon") || cmd.equalsIgnoreCase("pardon-ip") || cmd.equalsIgnoreCase("reload")) {
+ return 0;
+ }
+
+ // Handle vanilla commands;
+ if (sender.getWorld().getServer().getCommandBlockOverride(args[0])) {
+ args[0] = "minecraft:" + args[0];
+ }
+
+ return this.a(sender, joiner.join(args));
+ }
+
public int a(CommandListenerWrapper commandlistenerwrapper, String s) {
+ return this.a(commandlistenerwrapper, s, s);
+ }
+
+ public int a(CommandListenerWrapper commandlistenerwrapper, String s, String label) {
2018-07-15 02:00:00 +02:00
+ // CraftBukkit end
2018-08-26 04:00:00 +02:00
StringReader stringreader = new StringReader(s);
2018-07-15 02:00:00 +02:00
2018-12-25 22:00:00 +01:00
if (stringreader.canRead() && stringreader.peek() == '/') {
@@ -123,7 +177,6 @@
2018-07-15 02:00:00 +02:00
byte b0;
try {
- byte b1;
ChatComponentText chatcomponenttext;
try {
2018-12-25 22:00:00 +01:00
@@ -132,52 +185,56 @@
2018-07-15 02:00:00 +02:00
return i;
} catch (CommandException commandexception) {
commandlistenerwrapper.sendFailureMessage(commandexception.a());
- b1 = 0;
- return b1;
+ b0 = 0;
+ return b0;
} catch (CommandSyntaxException commandsyntaxexception) {
commandlistenerwrapper.sendFailureMessage(ChatComponentUtils.a(commandsyntaxexception.getRawMessage()));
2018-12-25 22:00:00 +01:00
if (commandsyntaxexception.getInput() != null && commandsyntaxexception.getCursor() >= 0) {
2018-07-15 02:00:00 +02:00
int j = Math.min(commandsyntaxexception.getInput().length(), commandsyntaxexception.getCursor());
- IChatBaseComponent ichatbasecomponent = (new ChatComponentText("")).a(EnumChatFormat.GRAY).a((chatmodifier) -> {
- chatmodifier.setChatClickable(new ChatClickable(ChatClickable.EnumClickAction.SUGGEST_COMMAND, s));
- });
+ chatcomponenttext = new ChatComponentText("");
2018-12-25 22:00:00 +01:00
if (j > 10) {
2018-07-15 02:00:00 +02:00
- ichatbasecomponent.a("...");
+ chatcomponenttext.a("...");
}
- ichatbasecomponent.a(commandsyntaxexception.getInput().substring(Math.max(0, j - 10), j));
+ chatcomponenttext.a(commandsyntaxexception.getInput().substring(Math.max(0, j - 10), j));
2018-12-25 22:00:00 +01:00
if (j < commandsyntaxexception.getInput().length()) {
2018-12-06 00:00:00 +01:00
- IChatBaseComponent ichatbasecomponent1 = (new ChatComponentText(commandsyntaxexception.getInput().substring(j))).a(new EnumChatFormat[]{EnumChatFormat.RED, EnumChatFormat.UNDERLINE});
2018-07-15 02:00:00 +02:00
+ ChatComponentText chatcomponenttext1 = new ChatComponentText(commandsyntaxexception.getInput().substring(j));
- ichatbasecomponent.addSibling(ichatbasecomponent1);
+ chatcomponenttext1.getChatModifier().setColor(EnumChatFormat.RED);
+ chatcomponenttext1.getChatModifier().setUnderline(Boolean.valueOf(true));
+ chatcomponenttext.addSibling(chatcomponenttext1);
}
2018-12-06 00:00:00 +01:00
- ichatbasecomponent.addSibling((new ChatMessage("command.context.here", new Object[0])).a(new EnumChatFormat[]{EnumChatFormat.RED, EnumChatFormat.ITALIC}));
2018-07-15 02:00:00 +02:00
- commandlistenerwrapper.sendFailureMessage(ichatbasecomponent);
+ ChatMessage chatmessage = new ChatMessage("command.context.here", new Object[0]);
+
+ chatmessage.getChatModifier().setItalic(Boolean.valueOf(true));
+ chatmessage.getChatModifier().setColor(EnumChatFormat.RED);
+ chatcomponenttext.addSibling(chatmessage);
+ chatcomponenttext.getChatModifier().setColor(EnumChatFormat.GRAY);
2018-08-26 04:00:00 +02:00
+ chatcomponenttext.getChatModifier().setChatClickable(new ChatClickable(ChatClickable.EnumClickAction.SUGGEST_COMMAND, label)); // CraftBukkit
2018-07-15 02:00:00 +02:00
+ commandlistenerwrapper.sendFailureMessage(chatcomponenttext);
}
- b1 = 0;
- return b1;
+ b0 = 0;
} catch (Exception exception) {
- chatcomponenttext = new ChatComponentText;
- }
-
2018-12-25 22:00:00 +01:00
- chatcomponenttext.<init>(exception.getMessage() == null ? exception.getClass().getName() : exception.getMessage());
- ChatComponentText chatcomponenttext1 = chatcomponenttext;
+ ChatMessage chatmessage1 = new ChatMessage("command.failed", new Object[0]);
2018-12-25 22:00:00 +01:00
- if (CommandDispatcher.a.isDebugEnabled()) {
- StackTraceElement[] astacktraceelement = exception.getStackTrace();
2018-07-15 02:00:00 +02:00
+ chatcomponenttext = new ChatComponentText(exception.getMessage() == null ? exception.getClass().getName() : exception.getMessage());
+ if (CommandDispatcher.a.isDebugEnabled()) {
+ StackTraceElement[] astacktraceelement = exception.getStackTrace();
2018-12-06 00:00:00 +01:00
- for(int k = 0; k < Math.min(astacktraceelement.length, 3); ++k) {
2018-07-15 02:00:00 +02:00
- chatcomponenttext1.a("\n\n").a(astacktraceelement[k].getMethodName()).a("\n ").a(astacktraceelement[k].getFileName()).a(":").a(String.valueOf(astacktraceelement[k].getLineNumber()));
+ for (int k = 0; k < Math.min(astacktraceelement.length, 3); ++k) {
+ chatcomponenttext.a("\n\n" + astacktraceelement[k].getMethodName() + "\n " + astacktraceelement[k].getFileName() + ":" + astacktraceelement[k].getLineNumber());
+ }
}
- }
- commandlistenerwrapper.sendFailureMessage((new ChatMessage("command.failed", new Object[0])).a((chatmodifier) -> {
2018-12-25 22:00:00 +01:00
- chatmodifier.setChatHoverable(new ChatHoverable(ChatHoverable.EnumHoverAction.SHOW_TEXT, chatcomponenttext1));
2018-07-15 02:00:00 +02:00
- }));
- b0 = 0;
+ chatmessage1.getChatModifier().setChatHoverable(new ChatHoverable(ChatHoverable.EnumHoverAction.SHOW_TEXT, chatcomponenttext));
+ commandlistenerwrapper.sendFailureMessage(chatmessage1);
+ byte b1 = 0;
+
+ return b1;
+ }
} finally {
2018-12-13 01:00:00 +01:00
commandlistenerwrapper.getServer().methodProfiler.exit();
2018-07-15 02:00:00 +02:00
}
2018-12-25 22:00:00 +01:00
@@ -186,11 +243,36 @@
}
public void a(EntityPlayer entityplayer) {
2018-12-25 22:00:00 +01:00
- Map<CommandNode<CommandListenerWrapper>, CommandNode<ICompletionProvider>> map = Maps.newHashMap();
+ // CraftBukkit start
+ // Register Vanilla commands into builtRoot as before
2018-12-25 22:00:00 +01:00
+ Map<CommandNode<CommandListenerWrapper>, CommandNode<ICompletionProvider>> map = Maps.newIdentityHashMap(); // Use identity to prevent aliasing issues
+ RootCommandNode vanillaRoot = new RootCommandNode();
+
+ RootCommandNode<CommandListenerWrapper> vanilla = entityplayer.server.vanillaCommandDispatcher.a().getRoot();
2018-12-25 22:00:00 +01:00
+ map.put(vanilla, vanillaRoot);
+ this.a(vanilla, vanillaRoot, entityplayer.getCommandListener(), (Map) map);
+
+ // Now build the global commands in a second pass
2018-12-25 22:00:00 +01:00
RootCommandNode<ICompletionProvider> rootcommandnode = new RootCommandNode();
2018-12-25 22:00:00 +01:00
map.put(this.b.getRoot(), rootcommandnode);
this.a(this.b.getRoot(), rootcommandnode, entityplayer.getCommandListener(), (Map) map);
+
+ Collection<String> bukkit = new LinkedHashSet<>();
+ for (CommandNode node : rootcommandnode.getChildren()) {
+ bukkit.add(node.getName());
+ }
+
+ PlayerCommandSendEvent event = new PlayerCommandSendEvent(entityplayer.getBukkitEntity(), new LinkedHashSet<>(bukkit));
+ event.getPlayer().getServer().getPluginManager().callEvent(event);
+
+ // Remove labels that were removed during the event
+ for (String orig : bukkit) {
+ if (!event.getCommands().contains(orig)) {
+ rootcommandnode.removeCommand(orig);
+ }
+ }
+ // CraftBukkit end
entityplayer.playerConnection.sendPacket(new PacketPlayOutCommands(rootcommandnode));
}
2018-12-25 22:00:00 +01:00
@@ -201,7 +283,7 @@
CommandNode<CommandListenerWrapper> commandnode2 = (CommandNode) iterator.next();
if (commandnode2.canUse(commandlistenerwrapper)) {
- ArgumentBuilder<ICompletionProvider, ?> argumentbuilder = commandnode2.createBuilder();
+ ArgumentBuilder argumentbuilder = commandnode2.createBuilder(); // CraftBukkit - decompile error
argumentbuilder.requires((icompletionprovider) -> {
return true;
@@ -224,7 +306,7 @@
argumentbuilder.redirect((CommandNode) map.get(argumentbuilder.getRedirect()));
}
- CommandNode<ICompletionProvider> commandnode3 = argumentbuilder.build();
+ CommandNode commandnode3 = argumentbuilder.build(); // CraftBukkit - decompile error
map.put(commandnode2, commandnode3);
commandnode1.addChild(commandnode3);