From 2f3edf0ca11997d4643234ba3dcc775cea8543ed Mon Sep 17 00:00:00 2001 From: William Date: Sat, 15 Jun 2024 19:26:01 +0100 Subject: [PATCH] refactor: fix uniform on Bukkit BukkitCommand no longer requires JavaPlugin --- .../uniform/bukkit/BukkitCommand.java | 51 +++++-------------- .../uniform/bukkit/BukkitCommandUser.java | 5 +- .../uniform/bukkit/BukkitUniform.java | 16 +++--- .../net/william278/uniform/BaseCommand.java | 29 +++++------ .../java/net/william278/uniform/Graph.java | 12 +++++ example-plugin/build.gradle | 4 +- .../william278/uniform/UniformExample.java | 4 +- .../uniform/fabric/FabricCommand.java | 10 ++-- .../uniform/fabric/FabricUniform.java | 2 +- .../uniform/fabric/FabricCommand.java | 10 ++-- .../uniform/fabric/FabricUniform.java | 2 +- .../uniform/paper/PaperCommand.java | 4 +- .../uniform/velocity/VelocityCommand.java | 16 +++--- 13 files changed, 78 insertions(+), 87 deletions(-) diff --git a/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitCommand.java b/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitCommand.java index 8d2e4e3..c410fa9 100644 --- a/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitCommand.java +++ b/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitCommand.java @@ -24,7 +24,6 @@ package net.william278.uniform.bukkit; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.suggestion.Suggestion; -import com.mojang.brigadier.tree.LiteralCommandNode; import net.kyori.adventure.audience.Audience; import net.kyori.adventure.platform.bukkit.BukkitAudiences; import net.kyori.adventure.text.Component; @@ -34,32 +33,25 @@ import net.william278.uniform.Command; import net.william278.uniform.CommandUser; import org.bukkit.command.CommandException; import org.bukkit.command.CommandSender; -import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; -import java.util.Arrays; import java.util.List; @SuppressWarnings("unused") public class BukkitCommand extends BaseCommand { - private final JavaPlugin plugin; private BukkitAudiences audiences; - public BukkitCommand(@NotNull Command command, @NotNull JavaPlugin plugin) { + public BukkitCommand(@NotNull Command command) { super(command); - this.plugin = plugin; } - public BukkitCommand(@NotNull JavaPlugin plugin, @NotNull String name, @NotNull String description, - @NotNull List aliases) { + public BukkitCommand(@NotNull String name, @NotNull String description, @NotNull List aliases) { super(name, description, aliases); - this.plugin = plugin; } - public BukkitCommand(@NotNull JavaPlugin plugin, @NotNull String name, @NotNull List aliases) { + public BukkitCommand(@NotNull String name, @NotNull List aliases) { super(name, aliases); - this.plugin = plugin; } @NotNull @@ -71,19 +63,11 @@ public class BukkitCommand extends BaseCommand { private static final int COMMAND_SUCCESS = com.mojang.brigadier.Command.SINGLE_SUCCESS; - private final LiteralCommandNode commandNode; - private final CommandDispatcher dispatcher; - private final JavaPlugin plugin; - private BukkitAudiences audiences; + private final CommandDispatcher dispatcher = new CommandDispatcher<>(); public Impl(@NotNull BukkitCommand command) { super(command.getName()); - this.audiences = command.audiences; - this.plugin = command.plugin; - - // Register command, setup description and aliases - this.dispatcher = new CommandDispatcher<>(); - this.commandNode = this.dispatcher.register(command.build().createBuilder()); + this.dispatcher.register(command.createBuilder()); this.setDescription(command.getDescription()); this.setAliases(command.getAliases()); } @@ -91,9 +75,7 @@ public class BukkitCommand extends BaseCommand { @Override public boolean execute(@NotNull CommandSender commandSender, @NotNull String alias, @NotNull String[] args) { try { - final String string = getInput(alias, args); - System.out.println("Usage: \"" + Arrays.toString(dispatcher.getAllUsage(commandNode, commandSender, false)) + "\""); - return dispatcher.execute(string, commandSender) == COMMAND_SUCCESS; + return dispatcher.execute(getInput(args), commandSender) == COMMAND_SUCCESS; } catch (CommandSyntaxException e) { getAudience(commandSender).sendMessage(Component .translatable("command.context.parse_error", NamedTextColor.RED) @@ -105,7 +87,7 @@ public class BukkitCommand extends BaseCommand { return false; } catch (CommandException e) { getAudience(commandSender).sendMessage(Component.text(e.getMessage(), NamedTextColor.RED)); - return true; + return false; } } @@ -113,7 +95,7 @@ public class BukkitCommand extends BaseCommand { @Override public List tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException { - final String passed = getInput(alias, args); + final String passed = getInput(args); return dispatcher.getCompletionSuggestions( dispatcher.parse(passed, sender), passed.length() // Spigot API limitation - we can only TAB complete the full text length :( @@ -124,30 +106,25 @@ public class BukkitCommand extends BaseCommand { @NotNull private Audience getAudience(@NotNull CommandSender sender) { - if (audiences == null) { - audiences = BukkitAudiences.create(plugin); - } - return audiences.sender(sender); + return BukkitUniform.getAudiences().sender(sender); } @NotNull - private String getInput(@NotNull String alias, @NotNull String[] args) { - return args.length == 0 ? alias : "%s %s".formatted(alias, String.join(" ", args)); + private String getInput(@NotNull String[] args) { + return args.length == 0 ? getName() + : "%s %s".formatted(getName(), String.join(" ", args)); } } @Override @NotNull protected CommandUser getUser(@NotNull Object user) { - if (audiences == null) { - audiences = BukkitAudiences.create(plugin); - } - return new BukkitCommandUser((CommandSender) user, audiences); + return new BukkitCommandUser((CommandSender) user); } @Override public void addSubCommand(@NotNull Command command) { - addSubCommand(new BukkitCommand(command, plugin)); + addSubCommand(new BukkitCommand(command)); } } diff --git a/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitCommandUser.java b/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitCommandUser.java index b1cd941..8eca4f4 100644 --- a/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitCommandUser.java +++ b/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitCommandUser.java @@ -22,7 +22,6 @@ package net.william278.uniform.bukkit; import net.kyori.adventure.audience.Audience; -import net.kyori.adventure.platform.bukkit.BukkitAudiences; import net.william278.uniform.CommandUser; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -31,12 +30,12 @@ import org.jetbrains.annotations.Nullable; import java.util.UUID; -public record BukkitCommandUser(@NotNull CommandSender sender, @NotNull BukkitAudiences adv) implements CommandUser { +public record BukkitCommandUser(@NotNull CommandSender sender) implements CommandUser { @Override @NotNull public Audience getAudience() { - return adv.sender(sender); + return BukkitUniform.getAudiences().sender(sender); } @Override diff --git a/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitUniform.java b/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitUniform.java index 85d8517..42ed0b4 100644 --- a/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitUniform.java +++ b/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitUniform.java @@ -40,6 +40,7 @@ package net.william278.uniform.bukkit;/* * along with this program. If not, see . */ +import net.kyori.adventure.platform.bukkit.BukkitAudiences; import net.william278.uniform.Command; import net.william278.uniform.Uniform; import org.bukkit.command.CommandSender; @@ -59,15 +60,20 @@ import java.util.Locale; public final class BukkitUniform implements Uniform { private static BukkitUniform INSTANCE; + private static BukkitAudiences AUDIENCES; + private static JavaPlugin PLUGIN; - private final JavaPlugin plugin; private final CommandRegistration registrar; private BukkitUniform(@NotNull JavaPlugin plugin) { - this.plugin = plugin; + PLUGIN = plugin; this.registrar = new MorePaperLib(plugin).commandRegistration(); } + static BukkitAudiences getAudiences() { + return AUDIENCES != null ? AUDIENCES : (AUDIENCES = BukkitAudiences.create(PLUGIN)); + } + /** * Get the BukkitUniform instance for registering commands * @@ -89,7 +95,7 @@ public final class BukkitUniform implements Uniform (org.bukkit.command.Command) c.getImpl()).toList() ); } @@ -102,9 +108,7 @@ public final class BukkitUniform implements Uniform new BukkitCommand(command, plugin)) - .toArray(BukkitCommand[]::new)); + register(Arrays.stream(commands).map(BukkitCommand::new).toArray(BukkitCommand[]::new)); } } diff --git a/common/src/main/java/net/william278/uniform/BaseCommand.java b/common/src/main/java/net/william278/uniform/BaseCommand.java index 7015142..42c4bd4 100644 --- a/common/src/main/java/net/william278/uniform/BaseCommand.java +++ b/common/src/main/java/net/william278/uniform/BaseCommand.java @@ -22,9 +22,9 @@ package net.william278.uniform; import com.mojang.brigadier.arguments.*; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.suggestion.SuggestionProvider; import com.mojang.brigadier.tree.LiteralCommandNode; -import lombok.AccessLevel; import lombok.Getter; import net.william278.uniform.element.ArgumentElement; import net.william278.uniform.element.CommandElement; @@ -36,16 +36,21 @@ import java.util.ArrayList; import java.util.List; import java.util.function.Predicate; +@Getter @SuppressWarnings("unused") public abstract class BaseCommand { - @Getter private final String name; - @Getter private final String description; - @Getter private final List aliases; + @Nullable + private Predicate condition; + @Nullable + private CommandExecutor defaultExecutor; + private final List> syntaxes = new ArrayList<>(); + private final List> subCommands = new ArrayList<>(); + public BaseCommand(@NotNull Command command) { this.name = command.getName(); this.aliases = command.getAliases(); @@ -63,17 +68,6 @@ public abstract class BaseCommand { this(name, "", aliases); } - @Nullable - @Getter(AccessLevel.PACKAGE) - private Predicate condition; - @Nullable - @Getter(AccessLevel.PACKAGE) - private CommandExecutor defaultExecutor; - @Getter(AccessLevel.PACKAGE) - private final List> syntaxes = new ArrayList<>(); - @Getter(AccessLevel.PACKAGE) - private final List> subCommands = new ArrayList<>(); - @NotNull protected abstract CommandUser getUser(@NotNull Object user); @@ -117,6 +111,11 @@ public abstract class BaseCommand { return Graph.create(this).build(); } + @NotNull + public final LiteralArgumentBuilder createBuilder() { + return Graph.create(this).literal(this.name); + } + @NotNull public static LiteralElement literal(@NotNull String name) { return new LiteralElement<>(name); diff --git a/common/src/main/java/net/william278/uniform/Graph.java b/common/src/main/java/net/william278/uniform/Graph.java index d22a7fa..8a01e08 100644 --- a/common/src/main/java/net/william278/uniform/Graph.java +++ b/common/src/main/java/net/william278/uniform/Graph.java @@ -21,6 +21,8 @@ package net.william278.uniform; +import com.mojang.brigadier.Command; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.tree.CommandNode; import com.mojang.brigadier.tree.LiteralCommandNode; import net.william278.uniform.element.CommandElement; @@ -46,4 +48,14 @@ record Graph(@NotNull Node root) { return literalNode; } + @NotNull + LiteralArgumentBuilder literal(@NotNull String name) { + final LiteralArgumentBuilder builder = LiteralArgumentBuilder.literal(name); + final CommandNode command = this.root.build(); + builder.executes(command.getCommand()); + this.root.children().forEach(child -> builder.then(child.build())); + return builder; + } + + } \ No newline at end of file diff --git a/example-plugin/build.gradle b/example-plugin/build.gradle index b0b5da9..3ab567c 100644 --- a/example-plugin/build.gradle +++ b/example-plugin/build.gradle @@ -4,8 +4,8 @@ plugins { } dependencies { - implementation(project(":paper")) -// implementation(project(":bukkit")) + // implementation(project(":paper")) + implementation(project(":bukkit")) compileOnly 'io.papermc.paper:paper-api:1.20.6-R0.1-SNAPSHOT' } diff --git a/example-plugin/src/main/java/net/william278/uniform/UniformExample.java b/example-plugin/src/main/java/net/william278/uniform/UniformExample.java index ead198e..2f41020 100644 --- a/example-plugin/src/main/java/net/william278/uniform/UniformExample.java +++ b/example-plugin/src/main/java/net/william278/uniform/UniformExample.java @@ -21,7 +21,7 @@ package net.william278.uniform; -import net.william278.uniform.paper.PaperUniform; +import net.william278.uniform.bukkit.BukkitUniform; import org.bukkit.plugin.java.JavaPlugin; @SuppressWarnings("unused") @@ -29,7 +29,7 @@ public class UniformExample extends JavaPlugin { @Override public void onLoad() { - PaperUniform uniform = PaperUniform.getInstance(this); + BukkitUniform uniform = BukkitUniform.getInstance(this); uniform.register(new ExampleCommand()); } diff --git a/fabric-1.20.1/src/main/java/net/william278/uniform/fabric/FabricCommand.java b/fabric-1.20.1/src/main/java/net/william278/uniform/fabric/FabricCommand.java index 00744ac..8488b7e 100644 --- a/fabric-1.20.1/src/main/java/net/william278/uniform/fabric/FabricCommand.java +++ b/fabric-1.20.1/src/main/java/net/william278/uniform/fabric/FabricCommand.java @@ -52,15 +52,15 @@ public class FabricCommand extends BaseCommand { super(name, description, aliases); } - protected static ArgumentElement itemArg(String name) { - return registryArg(name, Registries.ITEM); + public static ArgumentElement item(String name) { + return registry(name, Registries.ITEM); } - protected static ArgumentElement blockArg(String name) { - return registryArg(name, Registries.BLOCK); + public static ArgumentElement block(String name) { + return registry(name, Registries.BLOCK); } - protected static ArgumentElement registryArg(String name, Registry registry) { + public static ArgumentElement registry(String name, Registry registry) { return new ArgumentElement<>(name, reader -> { String itemId = reader.readString(); final Identifier id; diff --git a/fabric-1.20.1/src/main/java/net/william278/uniform/fabric/FabricUniform.java b/fabric-1.20.1/src/main/java/net/william278/uniform/fabric/FabricUniform.java index 376e1ac..266d9c1 100644 --- a/fabric-1.20.1/src/main/java/net/william278/uniform/fabric/FabricUniform.java +++ b/fabric-1.20.1/src/main/java/net/william278/uniform/fabric/FabricUniform.java @@ -46,7 +46,7 @@ public final class FabricUniform implements Uniform - commands.forEach(command -> dispatcher.register(command.build().createBuilder())) + commands.forEach(command -> dispatcher.register(command.createBuilder())) ); } diff --git a/fabric-1.20.6/src/main/java/net/william278/uniform/fabric/FabricCommand.java b/fabric-1.20.6/src/main/java/net/william278/uniform/fabric/FabricCommand.java index 06f375e..d17108e 100644 --- a/fabric-1.20.6/src/main/java/net/william278/uniform/fabric/FabricCommand.java +++ b/fabric-1.20.6/src/main/java/net/william278/uniform/fabric/FabricCommand.java @@ -52,15 +52,15 @@ public class FabricCommand extends BaseCommand { super(name, description, aliases); } - protected static ArgumentElement itemArg(String name) { - return registryArg(name, Registries.ITEM); + public static ArgumentElement item(String name) { + return registry(name, Registries.ITEM); } - protected static ArgumentElement blockArg(String name) { - return registryArg(name, Registries.BLOCK); + public static ArgumentElement block(String name) { + return registry(name, Registries.BLOCK); } - protected static ArgumentElement registryArg(String name, Registry registry) { + public static ArgumentElement registry(String name, Registry registry) { return new ArgumentElement<>(name, reader -> { String itemId = reader.readString(); final Identifier id; diff --git a/fabric-1.20.6/src/main/java/net/william278/uniform/fabric/FabricUniform.java b/fabric-1.20.6/src/main/java/net/william278/uniform/fabric/FabricUniform.java index d0a9f29..7c19a94 100644 --- a/fabric-1.20.6/src/main/java/net/william278/uniform/fabric/FabricUniform.java +++ b/fabric-1.20.6/src/main/java/net/william278/uniform/fabric/FabricUniform.java @@ -46,7 +46,7 @@ public final class FabricUniform implements Uniform - commands.forEach(command -> dispatcher.register(command.build().createBuilder())) + commands.forEach(command -> dispatcher.register(command.createBuilder())) ); } diff --git a/paper/src/main/java/net/william278/uniform/paper/PaperCommand.java b/paper/src/main/java/net/william278/uniform/paper/PaperCommand.java index 70e1dfe..dc09e89 100644 --- a/paper/src/main/java/net/william278/uniform/paper/PaperCommand.java +++ b/paper/src/main/java/net/william278/uniform/paper/PaperCommand.java @@ -50,7 +50,7 @@ public class PaperCommand extends BaseCommand { super(name, description, aliases); } - protected static ArgumentElement materialArg(String name) { + public static ArgumentElement material(String name) { return new ArgumentElement<>(name, reader -> { String materialName = reader.readString(); Material material = Material.matchMaterial(materialName); @@ -66,7 +66,7 @@ public class PaperCommand extends BaseCommand { }); } - protected static ArgumentElement> playerArg(String name) { + public static ArgumentElement> player(String name) { return new ArgumentElement<>(name, reader -> { String playerName = reader.readString(); if (playerName.equals("@a")) { diff --git a/velocity/src/main/java/net/william278/uniform/velocity/VelocityCommand.java b/velocity/src/main/java/net/william278/uniform/velocity/VelocityCommand.java index f52a605..a29bd52 100644 --- a/velocity/src/main/java/net/william278/uniform/velocity/VelocityCommand.java +++ b/velocity/src/main/java/net/william278/uniform/velocity/VelocityCommand.java @@ -51,8 +51,8 @@ public class VelocityCommand extends BaseCommand { super(name, description, aliases); } - protected static ArgumentElement serverArg(ProxyServer server, String name, - SuggestionProvider suggestionProvider) { + public static ArgumentElement server(ProxyServer server, String name, + SuggestionProvider suggestionProvider) { ArgumentType argumentType = reader -> { String s = reader.readUnquotedString(); RegisteredServer server1 = server.getServer(s).orElse(null); @@ -64,8 +64,8 @@ public class VelocityCommand extends BaseCommand { return new ArgumentElement<>(name, argumentType, suggestionProvider); } - protected static ArgumentElement serverArg(ProxyServer server, String name) { - return serverArg(server, name, (context, builder) -> { + public static ArgumentElement server(ProxyServer server, String name) { + return server(server, name, (context, builder) -> { for (RegisteredServer server1 : server.getAllServers()) { builder.suggest(server1.getServerInfo().getName()); } @@ -73,8 +73,8 @@ public class VelocityCommand extends BaseCommand { }); } - protected static ArgumentElement sourceArg(ProxyServer server, String name, - SuggestionProvider suggestionProvider) { + public static ArgumentElement source(ProxyServer server, String name, + SuggestionProvider suggestionProvider) { ArgumentType argumentType = reader -> { String s = reader.readUnquotedString(); CommandSource source = server.getPlayer(s).orElse(null); @@ -86,8 +86,8 @@ public class VelocityCommand extends BaseCommand { return new ArgumentElement<>(name, argumentType, suggestionProvider); } - protected static ArgumentElement sourceArg(ProxyServer server, String name) { - return sourceArg(server, name, (context, builder) -> { + public static ArgumentElement source(ProxyServer server, String name) { + return source(server, name, (context, builder) -> { for (Player source : server.getAllPlayers()) { builder.suggest(source.getUsername()); }