2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Sun, 19 Apr 2020 18:15:29 -0400
2024-01-21 13:56:22 +01:00
Subject: [PATCH] Brigadier Mojang API
2021-06-11 14:02:28 +02:00
Adds AsyncPlayerSendCommandsEvent
- Allows modifying on a per command basis what command data they see.
Adds CommandRegisteredEvent
- Allows manipulating the CommandNode to add more children/metadata for the client
2021-06-14 03:06:38 +02:00
diff --git a/build.gradle.kts b/build.gradle.kts
2024-04-28 02:27:32 +02:00
index e9498f78cb6c0973a820f093ff7a31bef44ba27f..db2d67c98c62aa90591fea82e8fb07270699d96c 100644
2021-06-14 03:06:38 +02:00
--- a/build.gradle.kts
+++ b/build.gradle.kts
2024-04-27 22:27:01 +02:00
@@ -13,6 +13,7 @@ val alsoShade: Configuration by configurations.creating
2021-06-14 03:06:38 +02:00
dependencies {
2021-11-26 07:08:46 +01:00
implementation(project(":paper-api"))
+ implementation(project(":paper-mojangapi"))
2021-06-14 03:06:38 +02:00
// Paper start
2021-11-02 19:02:16 +01:00
implementation("org.jline:jline-terminal-jansi:3.21.0")
implementation("net.minecrell:terminalconsoleappender:1.3.0")
2022-10-27 19:10:28 +02:00
diff --git a/src/main/java/com/mojang/brigadier/exceptions/CommandSyntaxException.java b/src/main/java/com/mojang/brigadier/exceptions/CommandSyntaxException.java
2024-01-21 13:56:22 +01:00
index 3370731ee064d2693b972a0765c13dd4fd69f66a..09d486a05179b9d878e1c33725b4e614c3544da9 100644
2022-10-27 19:10:28 +02:00
--- a/src/main/java/com/mojang/brigadier/exceptions/CommandSyntaxException.java
+++ b/src/main/java/com/mojang/brigadier/exceptions/CommandSyntaxException.java
@@ -5,7 +5,7 @@ package com.mojang.brigadier.exceptions;
import com.mojang.brigadier.Message;
-public class CommandSyntaxException extends Exception {
2024-01-21 13:56:22 +01:00
+public class CommandSyntaxException extends Exception implements net.kyori.adventure.util.ComponentMessageThrowable { // Paper - Brigadier API
2022-10-27 19:10:28 +02:00
public static final int CONTEXT_AMOUNT = 10;
public static boolean ENABLE_COMMAND_STACK_TRACES = true;
public static BuiltInExceptionProvider BUILT_IN_EXCEPTIONS = new BuiltInExceptions();
@@ -73,4 +73,11 @@ public class CommandSyntaxException extends Exception {
public int getCursor() {
return cursor;
}
+
2024-01-21 13:56:22 +01:00
+ // Paper start - Brigadier API
2022-10-27 19:10:28 +02:00
+ @Override
+ public @org.jetbrains.annotations.Nullable net.kyori.adventure.text.Component componentMessage() {
+ return io.papermc.paper.brigadier.PaperBrigadier.componentFromMessage(this.message);
+ }
2024-01-21 13:56:22 +01:00
+ // Paper end - Brigadier API
2022-10-27 19:10:28 +02:00
}
2022-08-07 01:22:51 +02:00
diff --git a/src/main/java/com/mojang/brigadier/tree/CommandNode.java b/src/main/java/com/mojang/brigadier/tree/CommandNode.java
2024-04-27 08:17:58 +02:00
index da6250df1c5f3385b683cffde47754bca4606f5e..14ccd0c8f721e9be7dca8a5dcb8ef95b5cd82731 100644
2022-08-07 01:22:51 +02:00
--- a/src/main/java/com/mojang/brigadier/tree/CommandNode.java
+++ b/src/main/java/com/mojang/brigadier/tree/CommandNode.java
@@ -34,6 +34,7 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
private final RedirectModifier<S> modifier;
private final boolean forks;
private Command<S> command;
2024-04-27 08:17:58 +02:00
+ public CommandNode<CommandSourceStack> clientNode; // Paper - Brigadier API
2022-08-07 01:22:51 +02:00
// CraftBukkit start
public void removeCommand(String name) {
this.children.remove(name);
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/commands/CommandSourceStack.java b/src/main/java/net/minecraft/commands/CommandSourceStack.java
2024-04-24 07:43:09 +02:00
index d9fc3c25bef251df6a53ee47ec224b07240a931c..2a22827f44dd0d524c22264447959a6979e9f0de 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/commands/CommandSourceStack.java
+++ b/src/main/java/net/minecraft/commands/CommandSourceStack.java
2023-12-05 23:21:44 +01:00
@@ -45,7 +45,7 @@ import net.minecraft.world.phys.Vec2;
2021-06-11 14:02:28 +02:00
import net.minecraft.world.phys.Vec3;
import com.mojang.brigadier.tree.CommandNode; // CraftBukkit
2023-12-05 23:21:44 +01:00
-public class CommandSourceStack implements ExecutionCommandSource<CommandSourceStack>, SharedSuggestionProvider {
2024-01-21 13:56:22 +01:00
+public class CommandSourceStack implements ExecutionCommandSource<CommandSourceStack>, SharedSuggestionProvider, com.destroystokyo.paper.brigadier.BukkitBrigadierCommandSource { // Paper - Brigadier API
2021-06-11 14:02:28 +02:00
2022-06-08 03:31:24 +02:00
public static final SimpleCommandExceptionType ERROR_NOT_PLAYER = new SimpleCommandExceptionType(Component.translatable("permissions.requires.player"));
public static final SimpleCommandExceptionType ERROR_NOT_ENTITY = new SimpleCommandExceptionType(Component.translatable("permissions.requires.entity"));
2023-12-05 23:21:44 +01:00
@@ -170,6 +170,26 @@ public class CommandSourceStack implements ExecutionCommandSource<CommandSourceS
2022-12-07 20:22:28 +01:00
return this.textName;
2021-06-11 14:02:28 +02:00
}
2024-01-21 13:56:22 +01:00
+ // Paper start - Brigadier API
2021-06-11 14:02:28 +02:00
+ @Override
+ public org.bukkit.entity.Entity getBukkitEntity() {
+ return getEntity() != null ? getEntity().getBukkitEntity() : null;
+ }
+
+ @Override
+ public org.bukkit.World getBukkitWorld() {
+ return getLevel() != null ? getLevel().getWorld() : null;
+ }
+
+ @Override
+ public org.bukkit.Location getBukkitLocation() {
+ Vec3 pos = getPosition();
+ org.bukkit.World world = getBukkitWorld();
2022-07-21 09:26:36 +02:00
+ Vec2 rot = getRotation();
2022-09-30 21:03:42 +02:00
+ return world != null && pos != null ? new org.bukkit.Location(world, pos.x, pos.y, pos.z, rot != null ? rot.y : 0, rot != null ? rot.x : 0) : null;
2021-06-11 14:02:28 +02:00
+ }
2024-01-21 13:56:22 +01:00
+ // Paper end - Brigadier API
2021-06-11 14:02:28 +02:00
+
@Override
public boolean hasPermission(int level) {
// CraftBukkit start
diff --git a/src/main/java/net/minecraft/commands/Commands.java b/src/main/java/net/minecraft/commands/Commands.java
2024-04-27 08:17:58 +02:00
index b754c0b3e2cd878fca5f702daca64f837ec83451..f15c388434a0a501f86868de35cc138756975027 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/commands/Commands.java
+++ b/src/main/java/net/minecraft/commands/Commands.java
2024-04-27 08:17:58 +02:00
@@ -488,6 +488,7 @@ public class Commands {
2021-06-11 14:02:28 +02:00
bukkit.add(node.getName());
}
2024-01-21 13:56:22 +01:00
// Paper start - Perf: Async command map building
+ new com.destroystokyo.paper.event.brigadier.AsyncPlayerSendCommandsEvent<CommandSourceStack>(player.getBukkitEntity(), (RootCommandNode) rootcommandnode, false).callEvent(); // Paper - Brigadier API
2022-08-08 13:32:17 +02:00
net.minecraft.server.MinecraftServer.getServer().execute(() -> {
2021-08-26 18:41:22 +02:00
runSync(player, bukkit, rootcommandnode);
2021-06-11 14:02:28 +02:00
});
2024-04-27 08:17:58 +02:00
@@ -495,6 +496,7 @@ public class Commands {
2021-06-11 14:02:28 +02:00
2021-08-26 18:41:22 +02:00
private void runSync(ServerPlayer player, Collection<String> bukkit, RootCommandNode<SharedSuggestionProvider> rootcommandnode) {
2024-01-21 13:56:22 +01:00
// Paper end - Perf: Async command map building
+ new com.destroystokyo.paper.event.brigadier.AsyncPlayerSendCommandsEvent<CommandSourceStack>(player.getBukkitEntity(), (RootCommandNode) rootcommandnode, false).callEvent(); // Paper - Brigadier API
2021-08-26 18:41:22 +02:00
PlayerCommandSendEvent event = new PlayerCommandSendEvent(player.getBukkitEntity(), new LinkedHashSet<>(bukkit));
2021-06-11 14:02:28 +02:00
event.getPlayer().getServer().getPluginManager().callEvent(event);
2024-04-27 08:17:58 +02:00
@@ -513,6 +515,11 @@ public class Commands {
2022-08-07 01:22:51 +02:00
while (iterator.hasNext()) {
CommandNode<CommandSourceStack> commandnode2 = (CommandNode) iterator.next();
2024-01-21 13:56:22 +01:00
+ // Paper start - Brigadier API
2022-08-07 01:22:51 +02:00
+ if (commandnode2.clientNode != null) {
+ commandnode2 = commandnode2.clientNode;
+ }
2024-01-21 13:56:22 +01:00
+ // Paper end - Brigadier API
2022-08-07 01:22:51 +02:00
if ( !org.spigotmc.SpigotConfig.sendNamespaced && commandnode2.getName().contains( ":" ) ) continue; // Spigot
if (commandnode2.canUse(source)) {
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
2024-04-25 23:21:18 +02:00
index b277029f20f27431d62f05bf8fbd65095c5b15a8..afe102a677546dd16b91433c7a50c62a05a00b20 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
2024-04-25 23:21:18 +02:00
@@ -766,19 +766,34 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
2024-02-05 12:31:51 +01:00
builder.suggest(completion.suggestion(), PaperAdventure.asVanilla(completion.tooltip()));
2022-08-01 21:56:28 +02:00
}
2021-06-11 14:02:28 +02:00
}
2024-02-05 12:31:51 +01:00
- this.connection.send(new ClientboundCommandSuggestionsPacket(packet.getId(), builder.buildFuture().join()));
+ // Paper start - Brigadier API
+ com.mojang.brigadier.suggestion.Suggestions suggestions = builder.buildFuture().join();
+ com.destroystokyo.paper.event.brigadier.AsyncPlayerSendSuggestionsEvent suggestEvent = new com.destroystokyo.paper.event.brigadier.AsyncPlayerSendSuggestionsEvent(this.getCraftPlayer(), suggestions, packet.getCommand());
+ suggestEvent.setCancelled(suggestions.isEmpty());
+ if (suggestEvent.callEvent()) {
2024-04-24 07:43:09 +02:00
+ this.connection.send(new ClientboundCommandSuggestionsPacket(packet.getId(), limitTo(suggestEvent.getSuggestions(), ServerGamePacketListenerImpl.MAX_COMMAND_SUGGESTIONS)));
2024-02-05 12:31:51 +01:00
+ }
+ // Paper end - Brigadier API
}
}
2024-04-24 07:43:09 +02:00
+ // Paper start - brig API
+ private static Suggestions limitTo(final Suggestions suggestions, final int size) {
+ return suggestions.getList().size() <= size ? suggestions : new Suggestions(suggestions.getRange(), suggestions.getList().subList(0, size));
+ }
+ // Paper end - brig API
2024-02-05 12:31:51 +01:00
2024-04-24 07:43:09 +02:00
private void sendServerSuggestions(final ServerboundCommandSuggestionPacket packet, final StringReader stringreader) {
// Paper end - AsyncTabCompleteEvent
2024-02-05 12:31:51 +01:00
ParseResults<CommandSourceStack> parseresults = this.server.getCommands().getDispatcher().parse(stringreader, this.player.createCommandSourceStack());
this.server.getCommands().getDispatcher().getCompletionSuggestions(parseresults).thenAccept((suggestions) -> {
- if (suggestions.isEmpty()) return; // CraftBukkit - don't send through empty suggestions - prevents [<args>] from showing for plugins with nothing more to offer
2024-04-24 07:43:09 +02:00
- Suggestions suggestions1 = suggestions.getList().size() <= 1000 ? suggestions : new Suggestions(suggestions.getRange(), suggestions.getList().subList(0, 1000));
-
- this.send(new ClientboundCommandSuggestionsPacket(packet.getId(), suggestions1));
2024-02-05 12:31:51 +01:00
+ // Paper start - Brigadier API
+ com.destroystokyo.paper.event.brigadier.AsyncPlayerSendSuggestionsEvent suggestEvent = new com.destroystokyo.paper.event.brigadier.AsyncPlayerSendSuggestionsEvent(this.getCraftPlayer(), suggestions, packet.getCommand());
+ suggestEvent.setCancelled(suggestions.isEmpty());
+ if (suggestEvent.callEvent()) {
2024-04-24 07:43:09 +02:00
+ this.send(new ClientboundCommandSuggestionsPacket(packet.getId(), limitTo(suggestEvent.getSuggestions(), ServerGamePacketListenerImpl.MAX_COMMAND_SUGGESTIONS)));
2024-02-05 12:31:51 +01:00
+ }
+ // Paper end - Brigadier API
2022-08-01 21:56:28 +02:00
});
2024-02-05 12:31:51 +01:00
}
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/command/BukkitCommandWrapper.java b/src/main/java/org/bukkit/craftbukkit/command/BukkitCommandWrapper.java
2022-08-07 01:22:51 +02:00
index 83d81b9371902b0302d13e53b31c15fac4e67966..d113e54a30db16e2ad955170df6030d15de530d6 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/command/BukkitCommandWrapper.java
+++ b/src/main/java/org/bukkit/craftbukkit/command/BukkitCommandWrapper.java
2022-07-29 09:11:11 +02:00
@@ -20,7 +20,7 @@ import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
2021-06-11 14:02:28 +02:00
import org.bukkit.craftbukkit.CraftServer;
-public class BukkitCommandWrapper implements com.mojang.brigadier.Command<CommandSourceStack>, Predicate<CommandSourceStack>, SuggestionProvider<CommandSourceStack> {
+public class BukkitCommandWrapper implements com.mojang.brigadier.Command<CommandSourceStack>, Predicate<CommandSourceStack>, SuggestionProvider<CommandSourceStack>, com.destroystokyo.paper.brigadier.BukkitBrigadierCommand<CommandSourceStack> { // Paper
private final CraftServer server;
private final Command command;
2022-08-07 01:22:51 +02:00
@@ -31,10 +31,24 @@ public class BukkitCommandWrapper implements com.mojang.brigadier.Command<Comman
2021-06-11 14:02:28 +02:00
}
public LiteralCommandNode<CommandSourceStack> register(CommandDispatcher<CommandSourceStack> dispatcher, String label) {
- return dispatcher.register(
- LiteralArgumentBuilder.<CommandSourceStack>literal(label).requires(this).executes(this)
- .then(RequiredArgumentBuilder.<CommandSourceStack, String>argument("args", StringArgumentType.greedyString()).suggests(this).executes(this))
- );
+ // Paper start - Expose Brigadier to Paper-MojangAPI
+ com.mojang.brigadier.tree.RootCommandNode<CommandSourceStack> root = dispatcher.getRoot();
+ LiteralCommandNode<CommandSourceStack> literal = LiteralArgumentBuilder.<CommandSourceStack>literal(label).requires(this).executes(this).build();
2022-08-07 01:22:51 +02:00
+ LiteralCommandNode<CommandSourceStack> defaultNode = literal;
2021-06-11 14:02:28 +02:00
+ com.mojang.brigadier.tree.ArgumentCommandNode<CommandSourceStack, String> defaultArgs = RequiredArgumentBuilder.<CommandSourceStack, String>argument("args", StringArgumentType.greedyString()).suggests(this).executes(this).build();
+ literal.addChild(defaultArgs);
+ com.destroystokyo.paper.event.brigadier.CommandRegisteredEvent<CommandSourceStack> event = new com.destroystokyo.paper.event.brigadier.CommandRegisteredEvent<>(label, this, this.command, root, literal, defaultArgs);
+ if (!event.callEvent()) {
+ return null;
+ }
+ literal = event.getLiteral();
2022-08-07 01:22:51 +02:00
+ if (event.isRawCommand()) {
+ defaultNode.clientNode = literal;
+ literal = defaultNode;
+ }
2021-06-11 14:02:28 +02:00
+ root.addChild(literal);
+ return literal;
+ // Paper end
}
@Override