0
0
mirror of https://hub.spigotmc.org/stash/scm/spigot/craftbukkit.git synced 2024-11-21 22:26:17 +00:00
craftbukkit/nms-patches/net/minecraft/commands/CommandDispatcher.patch
2024-10-23 02:15:00 +11:00

198 lines
9.2 KiB
Diff

--- a/net/minecraft/commands/CommandDispatcher.java
+++ b/net/minecraft/commands/CommandDispatcher.java
@@ -139,6 +139,14 @@
import net.minecraft.world.level.GameRules;
import org.slf4j.Logger;
+// CraftBukkit start
+import com.google.common.base.Joiner;
+import java.util.Collection;
+import java.util.LinkedHashSet;
+import org.bukkit.event.player.PlayerCommandSendEvent;
+import org.bukkit.event.server.ServerCommandEvent;
+// CraftBukkit end
+
public class CommandDispatcher {
private static final ThreadLocal<ExecutionContext<CommandListenerWrapper>> CURRENT_EXECUTION_CONTEXT = new ThreadLocal();
@@ -151,6 +159,7 @@
private final com.mojang.brigadier.CommandDispatcher<CommandListenerWrapper> dispatcher = new com.mojang.brigadier.CommandDispatcher();
public CommandDispatcher(CommandDispatcher.ServerType commanddispatcher_servertype, CommandBuildContext commandbuildcontext) {
+ this(); // CraftBukkit
CommandAdvancement.register(this.dispatcher);
CommandAttribute.register(this.dispatcher, commandbuildcontext);
CommandExecute.register(this.dispatcher, commandbuildcontext);
@@ -252,6 +261,11 @@
CommandPublish.register(this.dispatcher);
}
+ // CraftBukkit start
+ }
+
+ public CommandDispatcher() {
+ // CraftBukkkit end
this.dispatcher.setConsumer(ExecutionCommandSource.resultConsumer());
}
@@ -262,18 +276,65 @@
return new ParseResults(commandcontextbuilder1, parseresults.getReader(), parseresults.getExceptions());
}
+ // CraftBukkit start
+ public void 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;
+ }
+ 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;
+ }
+
+ // Handle vanilla commands;
+ if (sender.getLevel().getCraftServer().getCommandBlockOverride(args[0])) {
+ args[0] = "minecraft:" + args[0];
+ }
+
+ String newCommand = joiner.join(args);
+ this.performPrefixedCommand(sender, newCommand, newCommand);
+ }
+ // CraftBukkit end
+
public void performPrefixedCommand(CommandListenerWrapper commandlistenerwrapper, String s) {
+ // CraftBukkit start
+ this.performPrefixedCommand(commandlistenerwrapper, s, s);
+ }
+
+ public void performPrefixedCommand(CommandListenerWrapper commandlistenerwrapper, String s, String label) {
s = s.startsWith("/") ? s.substring(1) : s;
- this.performCommand(this.dispatcher.parse(s, commandlistenerwrapper), s);
+ this.performCommand(this.dispatcher.parse(s, commandlistenerwrapper), s, label);
+ // CraftBukkit end
}
public void performCommand(ParseResults<CommandListenerWrapper> parseresults, String s) {
+ this.performCommand(parseresults, s, s);
+ }
+
+ public void performCommand(ParseResults<CommandListenerWrapper> parseresults, String s, String label) { // CraftBukkit
CommandListenerWrapper commandlistenerwrapper = (CommandListenerWrapper) parseresults.getContext().getSource();
Profiler.get().push(() -> {
return "/" + s;
});
- ContextChain<CommandListenerWrapper> contextchain = finishParsing(parseresults, s, commandlistenerwrapper);
+ ContextChain<CommandListenerWrapper> contextchain = finishParsing(parseresults, s, commandlistenerwrapper, label); // CraftBukkit
try {
if (contextchain != null) {
@@ -307,7 +368,7 @@
}
@Nullable
- private static ContextChain<CommandListenerWrapper> finishParsing(ParseResults<CommandListenerWrapper> parseresults, String s, CommandListenerWrapper commandlistenerwrapper) {
+ private static ContextChain<CommandListenerWrapper> finishParsing(ParseResults<CommandListenerWrapper> parseresults, String s, CommandListenerWrapper commandlistenerwrapper, String label) { // CraftBukkit
try {
validateParseResults(parseresults);
return (ContextChain) ContextChain.tryFlatten(parseresults.getContext().build(s)).orElseThrow(() -> {
@@ -318,7 +379,7 @@
if (commandsyntaxexception.getInput() != null && commandsyntaxexception.getCursor() >= 0) {
int i = Math.min(commandsyntaxexception.getInput().length(), commandsyntaxexception.getCursor());
IChatMutableComponent ichatmutablecomponent = IChatBaseComponent.empty().withStyle(EnumChatFormat.GRAY).withStyle((chatmodifier) -> {
- return chatmodifier.withClickEvent(new ChatClickable(ChatClickable.EnumClickAction.SUGGEST_COMMAND, "/" + s));
+ return chatmodifier.withClickEvent(new ChatClickable(ChatClickable.EnumClickAction.SUGGEST_COMMAND, label)); // CraftBukkit
});
if (i > 10) {
@@ -368,7 +429,7 @@
executioncontext1.close();
} finally {
- CommandDispatcher.CURRENT_EXECUTION_CONTEXT.set((Object) null);
+ CommandDispatcher.CURRENT_EXECUTION_CONTEXT.set(null); // CraftBukkit - decompile error
}
} else {
consumer.accept(executioncontext);
@@ -377,11 +438,36 @@
}
public void sendCommands(EntityPlayer entityplayer) {
- Map<CommandNode<CommandListenerWrapper>, CommandNode<ICompletionProvider>> map = Maps.newHashMap();
+ // CraftBukkit start
+ // Register Vanilla commands into builtRoot as before
+ Map<CommandNode<CommandListenerWrapper>, CommandNode<ICompletionProvider>> map = Maps.newIdentityHashMap(); // Use identity to prevent aliasing issues
+ RootCommandNode vanillaRoot = new RootCommandNode();
+
+ RootCommandNode<CommandListenerWrapper> vanilla = entityplayer.server.vanillaCommandDispatcher.getDispatcher().getRoot();
+ map.put(vanilla, vanillaRoot);
+ this.fillUsableCommands(vanilla, vanillaRoot, entityplayer.createCommandSourceStack(), (Map) map);
+
+ // Now build the global commands in a second pass
RootCommandNode<ICompletionProvider> rootcommandnode = new RootCommandNode();
map.put(this.dispatcher.getRoot(), rootcommandnode);
this.fillUsableCommands(this.dispatcher.getRoot(), rootcommandnode, entityplayer.createCommandSourceStack(), 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.connection.send(new PacketPlayOutCommands(rootcommandnode));
}
@@ -392,7 +478,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;
@@ -415,7 +501,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);
@@ -481,7 +567,7 @@
}
private <T> HolderLookup.b.a<T> createLookup(final HolderLookup.b<T> holderlookup_b) {
- return new HolderLookup.b.a<T>(this) {
+ return new HolderLookup.b.a<T>() { // CraftBukkit - decompile error
@Override
public HolderLookup.b<T> parent() {
return holderlookup_b;