Paper/patch-remap/mache-spigotflower-stripped/net/minecraft/commands/Commands.java.patch

172 lines
8.3 KiB
Diff

--- a/net/minecraft/commands/Commands.java
+++ b/net/minecraft/commands/Commands.java
@@ -135,6 +134,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 Commands {
private static final ThreadLocal<ExecutionContext<CommandSourceStack>> CURRENT_EXECUTION_CONTEXT = new ThreadLocal();
@@ -146,7 +153,8 @@
public static final int LEVEL_OWNERS = 4;
private final CommandDispatcher<CommandSourceStack> dispatcher = new CommandDispatcher();
- public Commands(Commands.CommandSelection commands_commandselection, CommandBuildContext commandbuildcontext) {
+ public Commands(Commands.CommandSelection selection, CommandBuildContext context) {
+ this(); // CraftBukkit
AdvancementCommands.register(this.dispatcher);
AttributeCommand.register(this.dispatcher, commandbuildcontext);
ExecuteCommand.register(this.dispatcher, commandbuildcontext);
@@ -247,6 +255,11 @@
PublishCommand.register(this.dispatcher);
}
+ // CraftBukkit start
+ }
+
+ public Commands() {
+ // CraftBukkkit end
this.dispatcher.setConsumer(ExecutionCommandSource.resultConsumer());
}
@@ -257,18 +270,64 @@
return new ParseResults(commandcontextbuilder1, parseresults.getReader(), parseresults.getExceptions());
}
- public void performPrefixedCommand(CommandSourceStack commandsourcestack, String s) {
+ // CraftBukkit start
+ public void dispatchServerCommand(CommandSourceStack 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(CommandSourceStack commandlistenerwrapper, String s) {
+ // CraftBukkit start
+ this.performPrefixedCommand(commandlistenerwrapper, s, s);
+ }
+
+ public void performPrefixedCommand(CommandSourceStack commandlistenerwrapper, String s, String label) {
s = s.startsWith("/") ? s.substring(1) : s;
- this.performCommand(this.dispatcher.parse(s, commandsourcestack), s);
+ this.performCommand(this.dispatcher.parse(s, commandlistenerwrapper), s, label);
+ // CraftBukkit end
}
public void performCommand(ParseResults<CommandSourceStack> parseresults, String s) {
CommandSourceStack commandsourcestack = (CommandSourceStack) parseresults.getContext().getSource();
- commandsourcestack.getServer().getProfiler().push(() -> {
+ public void performCommand(ParseResults<CommandSourceStack> parseresults, String s, String label) { // CraftBukkit
+ CommandSourceStack commandlistenerwrapper = (CommandSourceStack) parseresults.getContext().getSource();
+
+ commandlistenerwrapper.getServer().getProfiler().push(() -> {
return "/" + s;
});
- ContextChain contextchain = finishParsing(parseresults, s, commandsourcestack);
+ ContextChain contextchain = finishParsing(parseresults, s, commandlistenerwrapper, label); // CraftBukkit
try {
if (contextchain != null) {
@@ -302,7 +362,7 @@
}
@Nullable
- private static ContextChain<CommandSourceStack> finishParsing(ParseResults<CommandSourceStack> parseresults, String s, CommandSourceStack commandsourcestack) {
+ private static ContextChain<CommandSourceStack> finishParsing(ParseResults<CommandSourceStack> parseresults, String s, CommandSourceStack commandlistenerwrapper, String label) { // CraftBukkit
try {
validateParseResults(parseresults);
return (ContextChain) ContextChain.tryFlatten(parseresults.getContext().build(s)).orElseThrow(() -> {
@@ -312,8 +372,8 @@
commandsourcestack.sendFailure(ComponentUtils.fromMessage(commandsyntaxexception.getRawMessage()));
if (commandsyntaxexception.getInput() != null && commandsyntaxexception.getCursor() >= 0) {
int i = Math.min(commandsyntaxexception.getInput().length(), commandsyntaxexception.getCursor());
- MutableComponent mutablecomponent = Component.empty().withStyle(ChatFormatting.GRAY).withStyle((style) -> {
- return style.withClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/" + s));
+ MutableComponent ichatmutablecomponent = Component.empty().withStyle(ChatFormatting.GRAY).withStyle((chatmodifier) -> {
+ return chatmodifier.withClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, label)); // CraftBukkit
});
if (i > 10) {
@@ -371,13 +431,38 @@
}
- public void sendCommands(ServerPlayer serverplayer) {
- Map<CommandNode<CommandSourceStack>, CommandNode<SharedSuggestionProvider>> map = Maps.newHashMap();
+ public void sendCommands(ServerPlayer player) {
+ // CraftBukkit start
+ // Register Vanilla commands into builtRoot as before
+ Map<CommandNode<CommandSourceStack>, CommandNode<SharedSuggestionProvider>> map = Maps.newIdentityHashMap(); // Use identity to prevent aliasing issues
+ RootCommandNode vanillaRoot = new RootCommandNode();
+
+ RootCommandNode<CommandSourceStack> vanilla = player.server.vanillaCommandDispatcher.getDispatcher().getRoot();
+ map.put(vanilla, vanillaRoot);
+ this.fillUsableCommands(vanilla, vanillaRoot, player.createCommandSourceStack(), (Map) map);
+
+ // Now build the global commands in a second pass
RootCommandNode<SharedSuggestionProvider> rootcommandnode = new RootCommandNode();
map.put(this.dispatcher.getRoot(), rootcommandnode);
- this.fillUsableCommands(this.dispatcher.getRoot(), rootcommandnode, serverplayer.createCommandSourceStack(), map);
- serverplayer.connection.send(new ClientboundCommandsPacket(rootcommandnode));
+ this.fillUsableCommands(this.dispatcher.getRoot(), rootcommandnode, player.createCommandSourceStack(), map);
+
+ Collection<String> bukkit = new LinkedHashSet<>();
+ for (CommandNode node : rootcommandnode.getChildren()) {
+ bukkit.add(node.getName());
+ }
+
+ PlayerCommandSendEvent event = new PlayerCommandSendEvent(player.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
+ player.connection.send(new ClientboundCommandsPacket(rootcommandnode));
}
private void fillUsableCommands(CommandNode<CommandSourceStack> commandnode, CommandNode<SharedSuggestionProvider> commandnode1, CommandSourceStack commandsourcestack, Map<CommandNode<CommandSourceStack>, CommandNode<SharedSuggestionProvider>> map) {