Skip to content

Commit

Permalink
Work
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen1212055 committed Aug 4, 2022
1 parent 2a9645b commit 31e1982
Show file tree
Hide file tree
Showing 2 changed files with 432 additions and 340 deletions.
146 changes: 13 additions & 133 deletions patches/api/0390-Brigadier-based-command-API.patch
Original file line number Diff line number Diff line change
Expand Up @@ -379,139 +379,6 @@ index 0000000000000000000000000000000000000000..8be98ea599c864782198d0deddb22a68
+
+
+}
diff --git a/src/main/java/io/papermc/paper/command/brigadier/bukkit/BukkitCommandNode.java b/src/main/java/io/papermc/paper/command/brigadier/bukkit/BukkitCommandNode.java
new file mode 100644
index 0000000000000000000000000000000000000000..5c56e986ffe037498b339714c6c1db81452ece5e
--- /dev/null
+++ b/src/main/java/io/papermc/paper/command/brigadier/bukkit/BukkitCommandNode.java
@@ -0,0 +1,127 @@
+package io.papermc.paper.command.brigadier.bukkit;
+
+import co.aikar.timings.Timing;
+import com.mojang.brigadier.arguments.StringArgumentType;
+import com.mojang.brigadier.builder.RequiredArgumentBuilder;
+import com.mojang.brigadier.context.CommandContext;
+import com.mojang.brigadier.exceptions.CommandSyntaxException;
+import com.mojang.brigadier.suggestion.SuggestionProvider;
+import com.mojang.brigadier.suggestion.Suggestions;
+import com.mojang.brigadier.suggestion.SuggestionsBuilder;
+import com.mojang.brigadier.tree.LiteralCommandNode;
+import io.papermc.paper.command.brigadier.CommandSourceStack;
+import org.bukkit.Bukkit;
+import org.bukkit.ChatColor;
+import org.bukkit.Location;
+import org.bukkit.command.Command;
+import org.bukkit.command.CommandException;
+import org.bukkit.command.CommandSender;
+import org.jetbrains.annotations.ApiStatus;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.logging.Level;
+
+@ApiStatus.Internal
+public class BukkitCommandNode extends LiteralCommandNode<CommandSourceStack> {
+
+ private final Command command;
+
+ private BukkitCommandNode(String literal, Command command, BukkitBrigCommand bukkitBrigCommand) {
+ super(
+ literal, bukkitBrigCommand, s -> command.testPermissionSilent(s.getBukkitSender()),
+ null, null, false
+ );
+ this.command = command;
+ }
+
+ public static BukkitCommandNode of(String name, Command command) {
+ BukkitBrigCommand bukkitBrigCommand = new BukkitBrigCommand(command, name);
+ BukkitCommandNode commandNode = new BukkitCommandNode(name, command, bukkitBrigCommand);
+ commandNode.addChild(
+ RequiredArgumentBuilder.<CommandSourceStack, String>argument("args", StringArgumentType.greedyString()).suggests(BukkitBrigSuggestionProvider.INSTANCE).executes(bukkitBrigCommand).build()
+ );
+
+ return commandNode;
+ }
+
+ public Command getBukkitCommand() {
+ return this.command;
+ }
+
+ static class BukkitBrigCommand implements com.mojang.brigadier.Command<CommandSourceStack> {
+
+ private final org.bukkit.command.Command command;
+ private final String literal;
+
+ BukkitBrigCommand(org.bukkit.command.Command command, String literal) {
+ this.command = command;
+ this.literal = literal;
+ }
+
+ @Override
+ public int run(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
+ CommandSender sender = context.getSource().getBukkitSender();
+
+ // Plugins do weird things to workaround normal registration
+ if (this.command.timings == null) {
+ this.command.timings = co.aikar.timings.TimingsManager.getCommandTiming(null, this.command);
+ }
+
+ String[] args = org.apache.commons.lang3.StringUtils.split(context.getInput(), ' '); // fix adjacent spaces (from console/plugins) causing empty array elements
+
+ try (Timing ignored = this.command.timings.startTiming()) {
+ // Note: we don't return the result of target.execute as thats success / failure, we return handled (true) or not handled (false)
+ this.command.execute(sender, this.literal, Arrays.copyOfRange(args, 1, args.length));
+ }
+
+ // return true as command was handled
+ return 1;
+ }
+ }
+
+ static class BukkitBrigSuggestionProvider implements SuggestionProvider<CommandSourceStack> {
+
+ public static final SuggestionProvider<CommandSourceStack> INSTANCE = new BukkitBrigSuggestionProvider();
+
+ @Override
+ public CompletableFuture<Suggestions> getSuggestions(CommandContext<CommandSourceStack> context, SuggestionsBuilder builder) throws CommandSyntaxException {
+ // Paper start
+ org.bukkit.command.CommandSender sender = context.getSource().getBukkitSender();
+ if (!(sender instanceof org.bukkit.entity.Player player)) {
+ return CompletableFuture.completedFuture(builder.build());
+ }
+
+ String message = builder.getInput();
+ List<String> results = null;
+ Location pos = context.getSource().getBukkitLocation();
+ try {
+ if (message.startsWith("/")) {
+ // Trim leading '/' if present (won't always be present in command blocks)
+ message = message.substring(1);
+ }
+ if (pos == null) {
+ results = Bukkit.getCommandMap().tabComplete(player, message);
+ } else {
+ results = Bukkit.getCommandMap().tabComplete(player, message, pos.clone());
+ }
+ } catch (CommandException ex) {
+ player.sendMessage(ChatColor.RED + "An internal error occurred while attempting to tab-complete this command");
+ Bukkit.getServer().getLogger().log(Level.SEVERE, "Exception when " + player.getName() + " attempted to tab complete " + message, ex);
+ }
+
+ // Paper end
+
+ // Defaults to sub nodes, but we have just one giant args node, so offset accordingly
+ builder = builder.createOffset(builder.getInput().lastIndexOf(' ') + 1);
+
+ for (String s : results) {
+ builder.suggest(s);
+ }
+
+ return builder.buildFuture();
+ }
+ }
+
+}
diff --git a/src/main/java/org/bukkit/Bukkit.java b/src/main/java/org/bukkit/Bukkit.java
index 92a1462261029e804da73da2743bbd68e57841e9..6f67001ee605400969514a074faf4c703a79bab1 100644
--- a/src/main/java/org/bukkit/Bukkit.java
Expand Down Expand Up @@ -562,6 +429,19 @@ index 864c263bbd4dd6dd7c37a74b39b1a40a884d0731..d5c10484497a091c46923e186f0a075c
public interface CommandMap {

/**
diff --git a/src/main/java/org/bukkit/command/FormattedCommandAlias.java b/src/main/java/org/bukkit/command/FormattedCommandAlias.java
index 9d4f553c04784cca63901a56a7aea62a5cae1d72..abe256e1e45ce28036da4aa1586715bc8a1a3414 100644
--- a/src/main/java/org/bukkit/command/FormattedCommandAlias.java
+++ b/src/main/java/org/bukkit/command/FormattedCommandAlias.java
@@ -117,7 +117,7 @@ public class FormattedCommandAlias extends Command {
index = formatString.indexOf('$', index);
}

- return formatString;
+ return formatString.trim(); // Paper - Causes an extra space at the end, breaks with brig commands
}

@NotNull
diff --git a/src/main/java/org/bukkit/command/SimpleCommandMap.java b/src/main/java/org/bukkit/command/SimpleCommandMap.java
index b8623575b1c1b565560c2dd6438190716845a652..499b2f33747c5dd960ff869aabcccb87ca4e3fdb 100644
--- a/src/main/java/org/bukkit/command/SimpleCommandMap.java
Expand Down
Loading

0 comments on commit 31e1982

Please sign in to comment.