From d43e81aa461673f1f10a4233eba765c74dabd160 Mon Sep 17 00:00:00 2001 From: William Date: Sun, 16 Jun 2024 17:24:32 +0100 Subject: [PATCH] refactor: don't store Uniform within BaseCommand --- .../uniform/bukkit/BukkitCommand.java | 14 +++++++------- .../uniform/bukkit/BukkitUniform.java | 2 +- .../net/william278/uniform/BaseCommand.java | 11 +++++------ .../uniform/fabric/FabricCommand.java | 13 +++++++------ .../uniform/fabric/FabricUniform.java | 4 ++-- .../uniform/fabric/FabricCommand.java | 12 ++++++------ .../uniform/fabric/FabricUniform.java | 4 ++-- gradle.properties | 2 +- .../uniform/paper/LegacyPaperCommand.java | 16 +++++++--------- .../william278/uniform/paper/PaperCommand.java | 18 ++++++++---------- .../william278/uniform/paper/PaperUniform.java | 15 +++++++-------- .../uniform/velocity/VelocityCommand.java | 12 ++++++------ .../uniform/velocity/VelocityUniform.java | 4 ++-- 13 files changed, 61 insertions(+), 66 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 67b4557..8f19d52 100644 --- a/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitCommand.java +++ b/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitCommand.java @@ -46,11 +46,6 @@ public class BukkitCommand extends BaseCommand { super(command); } - private BukkitCommand(@NotNull Command command, @NotNull Uniform uniform) { - super(command); - this.uniform = uniform; - } - public BukkitCommand(@NotNull String name, @NotNull String description, @NotNull List aliases) { super(name, description, aliases); } @@ -72,7 +67,7 @@ public class BukkitCommand extends BaseCommand { public Impl(@NotNull Uniform uniform, @NotNull BukkitCommand command) { super(command.getName()); - this.dispatcher.register(command.createBuilder(uniform)); + this.dispatcher.register(command.createBuilder()); this.setDescription(command.getDescription()); this.setAliases(command.getAliases()); } @@ -122,7 +117,12 @@ public class BukkitCommand extends BaseCommand { @Override public void addSubCommand(@NotNull Command command) { - addSubCommand(new BukkitCommand(command, uniform)); + addSubCommand(new BukkitCommand(command)); + } + + @Override + public Uniform getUniform() { + return BukkitUniform.INSTANCE; } } 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 7778250..9343a02 100644 --- a/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitUniform.java +++ b/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitUniform.java @@ -46,7 +46,7 @@ import java.util.function.Function; @SuppressWarnings("unused") public final class BukkitUniform implements Uniform { - private static BukkitUniform INSTANCE; + static BukkitUniform INSTANCE; private static BukkitAudiences AUDIENCES; private static JavaPlugin PLUGIN; diff --git a/common/src/main/java/net/william278/uniform/BaseCommand.java b/common/src/main/java/net/william278/uniform/BaseCommand.java index 7d3d9ef..ae613e1 100644 --- a/common/src/main/java/net/william278/uniform/BaseCommand.java +++ b/common/src/main/java/net/william278/uniform/BaseCommand.java @@ -50,7 +50,6 @@ public abstract class BaseCommand { private Predicate condition; @Nullable private CommandExecutor defaultExecutor; - protected Uniform uniform; public BaseCommand(@NotNull Command command) { this.name = command.getName(); @@ -72,7 +71,7 @@ public abstract class BaseCommand { @NotNull public final CommandUser getUser(@NotNull Object user) { - return uniform.getCommandUserSupplier().apply(user); + return getUniform().getCommandUserSupplier().apply(user); } public final void setCondition(@NotNull Predicate condition) { @@ -110,15 +109,15 @@ public abstract class BaseCommand { public abstract void addSubCommand(@NotNull Command command); + public abstract Uniform getUniform(); + @NotNull - public final LiteralCommandNode build(@NotNull Uniform uniform) { - this.uniform = uniform; + public final LiteralCommandNode build() { return Graph.create(this).build(); } @NotNull - public final LiteralArgumentBuilder createBuilder(@NotNull Uniform uniform) { - this.uniform = uniform; + public final LiteralArgumentBuilder createBuilder() { return Graph.create(this).literal(this.name); } 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 bce5e84..1111c01 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 @@ -44,11 +44,6 @@ public class FabricCommand extends BaseCommand { super(command); } - private FabricCommand(@NotNull Command command, @NotNull Uniform uniform) { - super(command); - this.uniform = uniform; - } - public FabricCommand(@NotNull String name, @NotNull List aliases) { super(name, aliases); } @@ -86,6 +81,12 @@ public class FabricCommand extends BaseCommand { @Override public void addSubCommand(@NotNull Command command) { - addSubCommand(new FabricCommand(command, getUniform())); + addSubCommand(new FabricCommand(command)); } + + @Override + public Uniform getUniform() { + return FabricUniform.INSTANCE; + } + } 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 5eb038e..afdf7a9 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 @@ -44,7 +44,7 @@ import java.util.function.Function; @SuppressWarnings("unused") public final class FabricUniform implements Uniform { - private static FabricUniform INSTANCE; + static FabricUniform INSTANCE; private final Set commands = Sets.newHashSet(); @@ -54,7 +54,7 @@ public final class FabricUniform implements Uniform { private FabricUniform() { CommandRegistrationCallback.EVENT.register((dispatcher, registry, environment) -> - commands.forEach(command -> dispatcher.register(command.createBuilder(this))) + 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 b58a568..86982b9 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 @@ -44,11 +44,6 @@ public class FabricCommand extends BaseCommand { super(command); } - private FabricCommand(@NotNull Command command, @NotNull Uniform uniform) { - super(command); - this.uniform = uniform; - } - public FabricCommand(@NotNull String name, @NotNull List aliases) { super(name, aliases); } @@ -87,6 +82,11 @@ public class FabricCommand extends BaseCommand { @Override public void addSubCommand(@NotNull Command command) { - addSubCommand(new FabricCommand(command, getUniform())); + addSubCommand(new FabricCommand(command)); + } + + @Override + public Uniform getUniform() { + return FabricUniform.INSTANCE; } } 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 aa91316..17b0961 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 @@ -44,7 +44,7 @@ import java.util.function.Function; @SuppressWarnings("unused") public final class FabricUniform implements Uniform { - private static FabricUniform INSTANCE; + static FabricUniform INSTANCE; private final Set commands = Sets.newHashSet(); @@ -54,7 +54,7 @@ public final class FabricUniform implements Uniform { private FabricUniform() { CommandRegistrationCallback.EVENT.register((dispatcher, registry, environment) -> - commands.forEach(command -> dispatcher.register(command.createBuilder(this))) + commands.forEach(command -> dispatcher.register(command.createBuilder())) ); } diff --git a/gradle.properties b/gradle.properties index 3907cde..740b253 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,6 +3,6 @@ javaVersion=17 org.gradle.jvmargs='-Dfile.encoding=UTF-8' org.gradle.daemon=true -library_version=1.0.1 +library_version=1.0.2 library_archive=uniform library_description=Cross-platform wrapper for making Brigadier commands, based on BrigadierWrapper by Tofaa2, itself inspired by emortalmcs command system. \ No newline at end of file diff --git a/paper/src/main/java/net/william278/uniform/paper/LegacyPaperCommand.java b/paper/src/main/java/net/william278/uniform/paper/LegacyPaperCommand.java index 090459d..d298f08 100644 --- a/paper/src/main/java/net/william278/uniform/paper/LegacyPaperCommand.java +++ b/paper/src/main/java/net/william278/uniform/paper/LegacyPaperCommand.java @@ -52,11 +52,6 @@ public class LegacyPaperCommand extends BaseCommand aliases) { super(name, aliases); } @@ -67,13 +62,16 @@ public class LegacyPaperCommand extends BaseCommand event) { commands.forEach(command -> { // Register root command - final LiteralCommandNode built = command.build(uniform); + final LiteralCommandNode built = command.build(); event.getRoot().addChild(built); // Register aliases 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 e9a20c0..ac04b76 100644 --- a/paper/src/main/java/net/william278/uniform/paper/PaperCommand.java +++ b/paper/src/main/java/net/william278/uniform/paper/PaperCommand.java @@ -51,12 +51,6 @@ public class PaperCommand extends BaseCommand { super(command); } - private PaperCommand(@NotNull Command command, @NotNull Uniform uniform) { - super(command); - this.uniform = uniform; - } - - public PaperCommand(@NotNull String name, @NotNull List aliases) { super(name, aliases); } @@ -65,12 +59,11 @@ public class PaperCommand extends BaseCommand { super(name, description, aliases); } - static void register(@NotNull PaperUniform uniform, @NotNull JavaPlugin plugin, - @NotNull Set commands) { + static void register(@NotNull JavaPlugin plugin, @NotNull Set commands) { plugin.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS, (event) -> { commands.forEach(command -> event.registrar().register( plugin.getPluginMeta(), - command.build(uniform), + command.build(), command.getDescription(), command.getAliases() )); @@ -116,7 +109,12 @@ public class PaperCommand extends BaseCommand { @Override public void addSubCommand(@NotNull Command command) { - addSubCommand(new PaperCommand(command, getUniform())); + addSubCommand(new PaperCommand(command)); + } + + @Override + public Uniform getUniform() { + return PaperUniform.INSTANCE; } } diff --git a/paper/src/main/java/net/william278/uniform/paper/PaperUniform.java b/paper/src/main/java/net/william278/uniform/paper/PaperUniform.java index 80f6001..d680bfe 100644 --- a/paper/src/main/java/net/william278/uniform/paper/PaperUniform.java +++ b/paper/src/main/java/net/william278/uniform/paper/PaperUniform.java @@ -42,7 +42,7 @@ import java.util.function.Function; */ public final class PaperUniform implements Uniform { - private static PaperUniform INSTANCE; + static PaperUniform INSTANCE; private final Set legacyCommands = Sets.newHashSet(); private final Set commands = Sets.newHashSet(); @@ -56,14 +56,14 @@ public final class PaperUniform implements Uniform { // Modern (1.20.6+) Lifecycle event based Paper Brigadier API if (useModernApi) { this.commandUserSupplier = PaperCommand.USER_SUPPLIER; - PaperCommand.register(this, plugin, commands); + PaperCommand.register(plugin, commands); return; } // Legacy (1.17-1.20.4) event-based Paper Brigadier API this.commandUserSupplier = LegacyPaperCommand.USER_SUPPLIER; plugin.getServer().getPluginManager().registerEvents( - new LegacyPaperCommand.Registrar(this, plugin, legacyCommands), plugin + new LegacyPaperCommand.Registrar(plugin, legacyCommands), plugin ); } @@ -101,12 +101,11 @@ public final class PaperUniform implements Uniform { @Override public final > void register(T... commands) { Arrays.stream(commands).forEach(c -> { - if (c instanceof PaperCommand paper) { - this.commands.add(paper); - } else if (c instanceof LegacyPaperCommand legacy) { - this.legacyCommands.add(legacy); + if (useModernApi) { + this.commands.add((PaperCommand) c); + } else { + this.legacyCommands.add((LegacyPaperCommand) c); } - throw new IllegalArgumentException("Command type not supported"); }); } 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 673b5ec..3cebdd3 100644 --- a/velocity/src/main/java/net/william278/uniform/velocity/VelocityCommand.java +++ b/velocity/src/main/java/net/william278/uniform/velocity/VelocityCommand.java @@ -43,11 +43,6 @@ public class VelocityCommand extends BaseCommand { super(command); } - private VelocityCommand(@NotNull Command command, @NotNull Uniform uniform) { - super(command); - this.uniform = uniform; - } - public VelocityCommand(@NotNull String name, @NotNull List aliases) { super(name, aliases); } @@ -103,7 +98,12 @@ public class VelocityCommand extends BaseCommand { @Override public void addSubCommand(@NotNull Command command) { - addSubCommand(new VelocityCommand(command, getUniform())); + addSubCommand(new VelocityCommand(command)); + } + + @Override + public Uniform getUniform() { + return VelocityUniform.INSTANCE; } } diff --git a/velocity/src/main/java/net/william278/uniform/velocity/VelocityUniform.java b/velocity/src/main/java/net/william278/uniform/velocity/VelocityUniform.java index e2d6f93..50cfa71 100644 --- a/velocity/src/main/java/net/william278/uniform/velocity/VelocityUniform.java +++ b/velocity/src/main/java/net/william278/uniform/velocity/VelocityUniform.java @@ -43,7 +43,7 @@ import java.util.function.Function; @SuppressWarnings("unused") public final class VelocityUniform implements Uniform { - private static VelocityUniform INSTANCE; + static VelocityUniform INSTANCE; private final ProxyServer server; @@ -79,7 +79,7 @@ public final class VelocityUniform implements Uniform { @Override public final > void register(T... commands) { Arrays.stream(commands).map(c -> (VelocityCommand) c).forEach(c -> server.getCommandManager() - .register(c.getName(), new BrigadierCommand(c.build(this)))); + .register(c.getName(), new BrigadierCommand(c.build()))); } /**