From 551d6ee71ec53e578d62a22f4ccc969d0937e9f8 Mon Sep 17 00:00:00 2001 From: Sweepyoface Date: Sat, 17 Jun 2017 18:48:21 -0400 Subject: [PATCH] Add UnknownCommandEvent Co-authored-by: Jake Potrebic --- .../commands/CommandSourceStack.java.patch | 25 +++++-- .../minecraft/commands/Commands.java.patch | 69 +++++++++++-------- .../org/bukkit/craftbukkit/CraftServer.java | 8 ++- 3 files changed, 67 insertions(+), 35 deletions(-) diff --git a/paper-server/patches/sources/net/minecraft/commands/CommandSourceStack.java.patch b/paper-server/patches/sources/net/minecraft/commands/CommandSourceStack.java.patch index 56d645a43a..99a2ebec6a 100644 --- a/paper-server/patches/sources/net/minecraft/commands/CommandSourceStack.java.patch +++ b/paper-server/patches/sources/net/minecraft/commands/CommandSourceStack.java.patch @@ -17,7 +17,7 @@ public CommandSourceStack(CommandSource output, Vec3 pos, Vec2 rot, ServerLevel world, int level, String name, Component displayName, MinecraftServer server, @Nullable Entity entity) { this(output, pos, rot, world, level, name, displayName, server, entity, false, CommandResultCallback.EMPTY, EntityAnchorArgument.Anchor.FEET, CommandSigningContext.ANONYMOUS, TaskChainer.immediate(server)); -@@ -171,9 +174,23 @@ +@@ -171,8 +174,22 @@ @Override public boolean hasPermission(int level) { @@ -30,18 +30,17 @@ + return this.permissionLevel >= level; } - ++ + // CraftBukkit start + public boolean hasPermission(int i, String bukkitPermission) { + // World is null when loading functions + return ((this.getLevel() == null || !this.getLevel().getCraftServer().ignoreVanillaPermissions) && this.permissionLevel >= i) || this.getBukkitSender().hasPermission(bukkitPermission); + } + // CraftBukkit end -+ + public Vec3 getPosition() { return this.worldPosition; - } -@@ -302,13 +319,13 @@ +@@ -302,21 +319,26 @@ while (iterator.hasNext()) { ServerPlayer entityplayer = (ServerPlayer) iterator.next(); @@ -57,7 +56,21 @@ this.server.sendSystemMessage(ichatmutablecomponent); } -@@ -400,4 +417,10 @@ + } + + public void sendFailure(Component message) { ++ // Paper start - Add UnknownCommandEvent ++ this.sendFailure(message, true); ++ } ++ public void sendFailure(Component message, boolean withStyle) { ++ // Paper end - Add UnknownCommandEvent + if (this.source.acceptsFailure() && !this.silent) { +- this.source.sendSystemMessage(Component.empty().append(message).withStyle(ChatFormatting.RED)); ++ this.source.sendSystemMessage(withStyle ? Component.empty().append(message).withStyle(ChatFormatting.RED) : message); // Paper - Add UnknownCommandEvent + } + + } +@@ -400,4 +422,10 @@ public boolean isSilent() { return this.silent; } diff --git a/paper-server/patches/sources/net/minecraft/commands/Commands.java.patch b/paper-server/patches/sources/net/minecraft/commands/Commands.java.patch index cddc3854da..cf19d88225 100644 --- a/paper-server/patches/sources/net/minecraft/commands/Commands.java.patch +++ b/paper-server/patches/sources/net/minecraft/commands/Commands.java.patch @@ -1,10 +1,9 @@ --- a/net/minecraft/commands/Commands.java +++ b/net/minecraft/commands/Commands.java -@@ -138,6 +138,14 @@ - import net.minecraft.world.flag.FeatureFlags; +@@ -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; @@ -12,9 +11,10 @@ +import org.bukkit.event.player.PlayerCommandSendEvent; +import org.bukkit.event.server.ServerCommandEvent; +// CraftBukkit end - ++ public class Commands { + private static final ThreadLocal> CURRENT_EXECUTION_CONTEXT = new ThreadLocal(); @@ -151,6 +159,7 @@ private final com.mojang.brigadier.CommandDispatcher dispatcher = new com.mojang.brigadier.CommandDispatcher(); @@ -35,12 +35,13 @@ this.dispatcher.setConsumer(ExecutionCommandSource.resultConsumer()); } -@@ -260,32 +274,79 @@ - CommandContextBuilder commandcontextbuilder1 = commandcontextbuilder.withSource(sourceMapper.apply(commandcontextbuilder.getSource())); - +@@ -262,30 +276,77 @@ return new ParseResults(commandcontextbuilder1, parseResults.getReader(), parseResults.getExceptions()); -+ } -+ + } + +- public void performPrefixedCommand(CommandSourceStack source, String command) { +- command = command.startsWith("/") ? command.substring(1) : command; +- this.performCommand(this.dispatcher.parse(command, source), command); + // CraftBukkit start + public void dispatchServerCommand(CommandSourceStack sender, String command) { + Joiner joiner = Joiner.on(" "); @@ -78,13 +79,11 @@ } + // CraftBukkit end - public void performPrefixedCommand(CommandSourceStack source, String command) { -- command = command.startsWith("/") ? command.substring(1) : command; -- this.performCommand(this.dispatcher.parse(command, source), command); ++ public void performPrefixedCommand(CommandSourceStack source, String command) { + // CraftBukkit start + this.performPrefixedCommand(source, command, command); - } - ++ } ++ + public void performPrefixedCommand(CommandSourceStack commandlistenerwrapper, String s, String label) { + s = s.startsWith("/") ? s.substring(1) : s; + this.performCommand(this.dispatcher.parse(s, commandlistenerwrapper), s, label); @@ -96,16 +95,15 @@ + this.performCommand(parseResults, command, command); + } -- Profiler.get().push(() -> { -- return "/" + command; + public void performCommand(ParseResults parseresults, String s, String label) { // CraftBukkit + CommandSourceStack commandlistenerwrapper = (CommandSourceStack) parseresults.getContext().getSource(); + -+ Profiler.get().push(() -> { + Profiler.get().push(() -> { +- return "/" + command; + return "/" + s; }); - ContextChain contextchain = Commands.finishParsing(parseResults, command, commandlistenerwrapper); -+ ContextChain contextchain = Commands.finishParsing(parseresults, s, commandlistenerwrapper, label); // CraftBukkit ++ ContextChain contextchain = this.finishParsing(parseresults, s, commandlistenerwrapper, label); // CraftBukkit // Paper - Add UnknownCommandEvent try { if (contextchain != null) { @@ -132,12 +130,12 @@ } } finally { Profiler.get().pop(); -@@ -307,18 +368,18 @@ +@@ -307,18 +368,22 @@ } @Nullable - private static ContextChain finishParsing(ParseResults parseResults, String command, CommandSourceStack source) { -+ private static ContextChain finishParsing(ParseResults parseresults, String s, CommandSourceStack commandlistenerwrapper, String label) { // CraftBukkit ++ private ContextChain finishParsing(ParseResults parseresults, String s, CommandSourceStack commandlistenerwrapper, String label) { // CraftBukkit // Paper - Add UnknownCommandEvent try { - Commands.validateParseResults(parseResults); - return (ContextChain) ContextChain.tryFlatten(parseResults.getContext().build(command)).orElseThrow(() -> { @@ -148,7 +146,11 @@ }); } catch (CommandSyntaxException commandsyntaxexception) { - source.sendFailure(ComponentUtils.fromMessage(commandsyntaxexception.getRawMessage())); -+ commandlistenerwrapper.sendFailure(ComponentUtils.fromMessage(commandsyntaxexception.getRawMessage())); ++ // Paper start - Add UnknownCommandEvent ++ final net.kyori.adventure.text.TextComponent.Builder builder = net.kyori.adventure.text.Component.text(); ++ // commandlistenerwrapper.sendFailure(ComponentUtils.fromMessage(commandsyntaxexception.getRawMessage())); ++ builder.color(net.kyori.adventure.text.format.NamedTextColor.RED).append(io.papermc.paper.brigadier.PaperBrigadier.componentFromMessage(commandsyntaxexception.getRawMessage())); ++ // Paper end - Add UnknownCommandEvent if (commandsyntaxexception.getInput() != null && commandsyntaxexception.getCursor() >= 0) { int i = Math.min(commandsyntaxexception.getInput().length(), commandsyntaxexception.getCursor()); MutableComponent ichatmutablecomponent = Component.empty().withStyle(ChatFormatting.GRAY).withStyle((chatmodifier) -> { @@ -157,16 +159,27 @@ }); if (i > 10) { -@@ -333,7 +394,7 @@ +@@ -333,8 +398,18 @@ } ichatmutablecomponent.append((Component) Component.translatable("command.context.here").withStyle(ChatFormatting.RED, ChatFormatting.ITALIC)); - source.sendFailure(ichatmutablecomponent); -+ commandlistenerwrapper.sendFailure(ichatmutablecomponent); ++ // Paper start - Add UnknownCommandEvent ++ // commandlistenerwrapper.sendFailure(ichatmutablecomponent); ++ builder ++ .append(net.kyori.adventure.text.Component.newline()) ++ .append(io.papermc.paper.adventure.PaperAdventure.asAdventure(ichatmutablecomponent)); } ++ org.bukkit.event.command.UnknownCommandEvent event = new org.bukkit.event.command.UnknownCommandEvent(commandlistenerwrapper.getBukkitSender(), s, org.spigotmc.SpigotConfig.unknownCommandMessage.isEmpty() ? null : builder.build()); ++ org.bukkit.Bukkit.getServer().getPluginManager().callEvent(event); ++ if (event.message() != null) { ++ commandlistenerwrapper.sendFailure(io.papermc.paper.adventure.PaperAdventure.asVanilla(event.message()), false); ++ // Paper end - Add UnknownCommandEvent ++ } return null; -@@ -368,7 +429,7 @@ + } +@@ -368,7 +443,7 @@ executioncontext1.close(); } finally { @@ -175,7 +188,7 @@ } } else { callback.accept(executioncontext); -@@ -377,11 +438,37 @@ +@@ -377,11 +452,37 @@ } public void sendCommands(ServerPlayer player) { @@ -214,7 +227,7 @@ player.connection.send(new ClientboundCommandsPacket(rootcommandnode)); } -@@ -390,9 +477,10 @@ +@@ -390,9 +491,10 @@ while (iterator.hasNext()) { CommandNode commandnode2 = (CommandNode) iterator.next(); @@ -226,7 +239,7 @@ argumentbuilder.requires((icompletionprovider) -> { return true; -@@ -415,7 +503,7 @@ +@@ -415,7 +517,7 @@ argumentbuilder.redirect((CommandNode) resultNodes.get(argumentbuilder.getRedirect())); } @@ -235,7 +248,7 @@ resultNodes.put(commandnode2, commandnode3); result.addChild(commandnode3); -@@ -481,7 +569,7 @@ +@@ -481,7 +583,7 @@ } private HolderLookup.RegistryLookup.Delegate createLookup(final HolderLookup.RegistryLookup original) { diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java index 267df83abe..a2b62970a5 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java @@ -950,7 +950,13 @@ public final class CraftServer implements Server { // Spigot start if (!org.spigotmc.SpigotConfig.unknownCommandMessage.isEmpty()) { - sender.sendMessage(org.spigotmc.SpigotConfig.unknownCommandMessage); + // Paper start + org.bukkit.event.command.UnknownCommandEvent event = new org.bukkit.event.command.UnknownCommandEvent(sender, commandLine, net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().deserialize(org.spigotmc.SpigotConfig.unknownCommandMessage)); + this.getPluginManager().callEvent(event); + if (event.message() != null) { + sender.sendMessage(event.message()); + } + // Paper end } // Spigot end