diff --git a/README.md b/README.md index acd8859..ec0de8e 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,8 @@ public class ExampleCommand extends PaperCommand { addSyntax((context) -> { context.getSource().getBukkitSender().sendMessage("Woah!!!!"); String arg = context.getArgument("message", String.class); - context.getSource().getBukkitSender().sendMessage(MiniMessage.miniMessage().deserialize(arg)); + context.getSource().getBukkitSender() + .sendMessage(MiniMessage.miniMessage().deserialize(arg)); }, stringArg("message")); } } 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 c410fa9..75ca8d6 100644 --- a/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitCommand.java +++ b/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitCommand.java @@ -30,7 +30,7 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import net.william278.uniform.BaseCommand; import net.william278.uniform.Command; -import net.william278.uniform.CommandUser; +import net.william278.uniform.Uniform; import org.bukkit.command.CommandException; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; @@ -55,8 +55,8 @@ public class BukkitCommand extends BaseCommand { } @NotNull - Impl getImpl() { - return new Impl(this); + Impl getImpl(@NotNull Uniform uniform) { + return new Impl(uniform, this); } static final class Impl extends org.bukkit.command.Command { @@ -65,9 +65,9 @@ public class BukkitCommand extends BaseCommand { private final CommandDispatcher dispatcher = new CommandDispatcher<>(); - public Impl(@NotNull BukkitCommand command) { + public Impl(@NotNull Uniform uniform, @NotNull BukkitCommand command) { super(command.getName()); - this.dispatcher.register(command.createBuilder()); + this.dispatcher.register(command.createBuilder(uniform)); this.setDescription(command.getDescription()); this.setAliases(command.getAliases()); } @@ -111,17 +111,10 @@ public class BukkitCommand extends BaseCommand { @NotNull private String getInput(@NotNull String[] args) { - return args.length == 0 ? getName() - : "%s %s".formatted(getName(), String.join(" ", args)); + return args.length == 0 ? getName() : "%s %s".formatted(getName(), String.join(" ", args)); } } - @Override - @NotNull - protected CommandUser getUser(@NotNull Object user) { - return new BukkitCommandUser((CommandSender) user); - } - @Override public void addSubCommand(@NotNull Command command) { addSubCommand(new BukkitCommand(command)); 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 693c0ea..7778250 100644 --- a/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitUniform.java +++ b/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitUniform.java @@ -21,10 +21,14 @@ package net.william278.uniform.bukkit; +import lombok.Getter; +import lombok.Setter; import net.kyori.adventure.platform.bukkit.BukkitAudiences; import net.william278.uniform.BaseCommand; import net.william278.uniform.Command; +import net.william278.uniform.CommandUser; import net.william278.uniform.Uniform; +import org.bukkit.command.CommandSender; import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; import space.arim.morepaperlib.MorePaperLib; @@ -32,6 +36,7 @@ import space.arim.morepaperlib.commands.CommandRegistration; import java.util.Arrays; import java.util.Locale; +import java.util.function.Function; /** * A class for registering commands with the Bukkit server's CommandMap @@ -47,6 +52,10 @@ public final class BukkitUniform implements Uniform { private final CommandRegistration registrar; + @Getter + @Setter + Function commandUserSupplier = (user) -> new BukkitCommandUser((CommandSender) user); + private BukkitUniform(@NotNull JavaPlugin plugin) { PLUGIN = plugin; this.registrar = new MorePaperLib(plugin).commandRegistration(); @@ -81,7 +90,8 @@ public final class BukkitUniform implements Uniform { public final > void register(T... commands) { registrar.getServerCommandMap().registerAll( PLUGIN.getName().toLowerCase(Locale.ENGLISH).replaceAll("[^a-z0-9_]", ""), - Arrays.stream(commands).map(c -> (org.bukkit.command.Command) ((BukkitCommand) c).getImpl()).toList() + Arrays.stream(commands).map(c -> (BukkitCommand) c) + .map(c -> (org.bukkit.command.Command) c.getImpl(this)).toList() ); } diff --git a/common/src/main/java/net/william278/uniform/BaseCommand.java b/common/src/main/java/net/william278/uniform/BaseCommand.java index 42c4bd4..fec0a34 100644 --- a/common/src/main/java/net/william278/uniform/BaseCommand.java +++ b/common/src/main/java/net/william278/uniform/BaseCommand.java @@ -48,6 +48,7 @@ public abstract class BaseCommand { private Predicate condition; @Nullable private CommandExecutor defaultExecutor; + private Uniform uniform; private final List> syntaxes = new ArrayList<>(); private final List> subCommands = new ArrayList<>(); @@ -58,7 +59,8 @@ public abstract class BaseCommand { command.provide(this); } - public BaseCommand(@NotNull String name, @NotNull String description, @NotNull List aliases) { + public BaseCommand(@NotNull String name, @NotNull String description, + @NotNull List aliases) { this.name = name; this.aliases = aliases; this.description = description; @@ -69,7 +71,9 @@ public abstract class BaseCommand { } @NotNull - protected abstract CommandUser getUser(@NotNull Object user); + public final CommandUser getUser(@NotNull Object user) { + return uniform.getCommandUserSupplier().apply(user); + } public final void setCondition(@NotNull Predicate condition) { this.condition = condition; @@ -107,12 +111,14 @@ public abstract class BaseCommand { public abstract void addSubCommand(@NotNull Command command); @NotNull - public final LiteralCommandNode build() { + public final LiteralCommandNode build(@NotNull Uniform uniform) { + this.uniform = uniform; return Graph.create(this).build(); } @NotNull - public final LiteralArgumentBuilder createBuilder() { + public final LiteralArgumentBuilder createBuilder(@NotNull Uniform uniform) { + this.uniform = uniform; return Graph.create(this).literal(this.name); } diff --git a/common/src/main/java/net/william278/uniform/Uniform.java b/common/src/main/java/net/william278/uniform/Uniform.java index d0883ba..8300975 100644 --- a/common/src/main/java/net/william278/uniform/Uniform.java +++ b/common/src/main/java/net/william278/uniform/Uniform.java @@ -21,6 +21,10 @@ package net.william278.uniform; +import org.jetbrains.annotations.NotNull; + +import java.util.function.Function; + public interface Uniform { void register(Command... commands); @@ -28,4 +32,9 @@ public interface Uniform { @SuppressWarnings("unchecked") > void register(T... commands); + @NotNull + Function getCommandUserSupplier(); + + void setCommandUserSupplier(@NotNull Function supplier); + } 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 8488b7e..185a329 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 @@ -31,7 +31,6 @@ import net.minecraft.util.Identifier; import net.minecraft.util.InvalidIdentifierException; import net.william278.uniform.BaseCommand; import net.william278.uniform.Command; -import net.william278.uniform.CommandUser; import net.william278.uniform.element.ArgumentElement; import org.jetbrains.annotations.NotNull; @@ -79,15 +78,8 @@ public class FabricCommand extends BaseCommand { }); } - @Override - @NotNull - protected CommandUser getUser(@NotNull Object user) { - return new FabricCommandUser((ServerCommandSource) user); - } - @Override public void addSubCommand(@NotNull Command command) { addSubCommand(new FabricCommand(command)); } - } 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 c5ef156..5eb038e 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 @@ -22,14 +22,19 @@ package net.william278.uniform.fabric; import com.google.common.collect.Sets; +import lombok.Getter; +import lombok.Setter; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; +import net.minecraft.server.command.ServerCommandSource; import net.william278.uniform.BaseCommand; import net.william278.uniform.Command; +import net.william278.uniform.CommandUser; import net.william278.uniform.Uniform; import org.jetbrains.annotations.NotNull; import java.util.Arrays; import java.util.Set; +import java.util.function.Function; /** * A class for registering commands with the Fabric (1.20.1) server @@ -43,9 +48,13 @@ public final class FabricUniform implements Uniform { private final Set commands = Sets.newHashSet(); + @Getter + @Setter + Function commandUserSupplier = (user) -> new FabricCommandUser((ServerCommandSource) user); + private FabricUniform() { CommandRegistrationCallback.EVENT.register((dispatcher, registry, environment) -> - commands.forEach(command -> dispatcher.register(command.createBuilder())) + commands.forEach(command -> dispatcher.register(command.createBuilder(this))) ); } 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 d17108e..96e967f 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 @@ -31,7 +31,6 @@ import net.minecraft.util.Identifier; import net.minecraft.util.InvalidIdentifierException; import net.william278.uniform.BaseCommand; import net.william278.uniform.Command; -import net.william278.uniform.CommandUser; import net.william278.uniform.element.ArgumentElement; import org.jetbrains.annotations.NotNull; @@ -48,7 +47,8 @@ public class FabricCommand extends BaseCommand { super(name, aliases); } - public FabricCommand(@NotNull String name, @NotNull String description, @NotNull List aliases) { + public FabricCommand(@NotNull String name, @NotNull String description, + @NotNull List aliases) { super(name, description, aliases); } @@ -79,12 +79,6 @@ public class FabricCommand extends BaseCommand { }); } - @Override - @NotNull - protected CommandUser getUser(@NotNull Object user) { - return new FabricCommandUser((ServerCommandSource) user); - } - @Override public void addSubCommand(@NotNull Command command) { addSubCommand(new FabricCommand(command)); 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 4f4c9a3..aa91316 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 @@ -22,14 +22,19 @@ package net.william278.uniform.fabric; import com.google.common.collect.Sets; +import lombok.Getter; +import lombok.Setter; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; +import net.minecraft.server.command.ServerCommandSource; import net.william278.uniform.BaseCommand; import net.william278.uniform.Command; +import net.william278.uniform.CommandUser; import net.william278.uniform.Uniform; import org.jetbrains.annotations.NotNull; import java.util.Arrays; import java.util.Set; +import java.util.function.Function; /** * A class for registering commands with the Fabric (1.20.6) server @@ -43,9 +48,13 @@ public final class FabricUniform implements Uniform { private final Set commands = Sets.newHashSet(); + @Getter + @Setter + Function commandUserSupplier = (user) -> new FabricCommandUser((ServerCommandSource) user); + private FabricUniform() { CommandRegistrationCallback.EVENT.register((dispatcher, registry, environment) -> - commands.forEach(command -> dispatcher.register(command.createBuilder())) + commands.forEach(command -> dispatcher.register(command.createBuilder(this))) ); } 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 43658d8..61267a8 100644 --- a/paper/src/main/java/net/william278/uniform/paper/LegacyPaperCommand.java +++ b/paper/src/main/java/net/william278/uniform/paper/LegacyPaperCommand.java @@ -38,10 +38,15 @@ import org.jetbrains.annotations.NotNull; import java.util.List; import java.util.Locale; import java.util.Set; +import java.util.function.Function; @SuppressWarnings({"removal", "deprecation", "UnstableApiUsage"}) public class LegacyPaperCommand extends BaseCommand { + static final Function USER_SUPPLIER = (user) -> new LegacyPaperCommandUser( + (BukkitBrigadierCommandSource) user + ); + public LegacyPaperCommand(@NotNull Command command) { super(command); } @@ -54,12 +59,6 @@ public class LegacyPaperCommand extends BaseCommand event) { commands.forEach(command -> { // Register root command - final LiteralCommandNode built = command.build(); + final LiteralCommandNode built = command.build(uniform); 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 8d86bbd..d210f7a 100644 --- a/paper/src/main/java/net/william278/uniform/paper/PaperCommand.java +++ b/paper/src/main/java/net/william278/uniform/paper/PaperCommand.java @@ -37,10 +37,15 @@ import org.jetbrains.annotations.NotNull; import java.util.Collection; import java.util.List; import java.util.Set; +import java.util.function.Function; @SuppressWarnings({"unused", "UnstableApiUsage"}) public class PaperCommand extends BaseCommand { + static final Function USER_SUPPLIER = (user) -> new PaperCommandUser( + (CommandSourceStack) user + ); + public PaperCommand(@NotNull Command command) { super(command); } @@ -53,11 +58,12 @@ public class PaperCommand extends BaseCommand { super(name, description, aliases); } - static void register(@NotNull JavaPlugin plugin, @NotNull Set commands) { + static void register(@NotNull PaperUniform uniform, @NotNull JavaPlugin plugin, + @NotNull Set commands) { plugin.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS, (event) -> { commands.forEach(command -> event.registrar().register( plugin.getPluginMeta(), - command.build(), + command.build(uniform), command.getDescription(), command.getAliases() )); @@ -101,12 +107,6 @@ public class PaperCommand extends BaseCommand { }); } - @Override - @NotNull - protected CommandUser getUser(@NotNull Object user) { - return new PaperCommandUser((CommandSourceStack) user); - } - @Override public void addSubCommand(@NotNull Command command) { addSubCommand(new PaperCommand(command)); 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 8924ea3..80f6001 100644 --- a/paper/src/main/java/net/william278/uniform/paper/PaperUniform.java +++ b/paper/src/main/java/net/william278/uniform/paper/PaperUniform.java @@ -22,14 +22,18 @@ package net.william278.uniform.paper; import com.google.common.collect.Sets; +import lombok.Getter; +import lombok.Setter; import net.william278.uniform.BaseCommand; import net.william278.uniform.Command; +import net.william278.uniform.CommandUser; import net.william278.uniform.Uniform; import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; import java.util.Arrays; import java.util.Set; +import java.util.function.Function; /** * A class for registering commands with the Paper server's command manager @@ -42,26 +46,27 @@ public final class PaperUniform implements Uniform { private final Set legacyCommands = Sets.newHashSet(); private final Set commands = Sets.newHashSet(); + private final boolean useModernApi = isUseModernApi(); + + @Getter + @Setter + Function commandUserSupplier; private PaperUniform(@NotNull JavaPlugin plugin) { - if (isUseModernApi()) { - PaperCommand.register(plugin, commands); + // Modern (1.20.6+) Lifecycle event based Paper Brigadier API + if (useModernApi) { + this.commandUserSupplier = PaperCommand.USER_SUPPLIER; + PaperCommand.register(this, 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(plugin, legacyCommands), plugin + new LegacyPaperCommand.Registrar(this, plugin, legacyCommands), plugin ); } - private static boolean isUseModernApi() { - try { - Class.forName("io.papermc.paper.command.brigadier.CommandSourceStack"); - return true; - } catch (ClassNotFoundException e) { - return false; - } - } - /** * Get the PaperUniform instance for registering commands * @@ -74,6 +79,15 @@ public final class PaperUniform implements Uniform { return INSTANCE != null ? INSTANCE : (INSTANCE = new PaperUniform(plugin)); } + // Check if the modern Paper API is available + private static boolean isUseModernApi() { + try { + Class.forName("io.papermc.paper.command.brigadier.CommandSourceStack"); + return true; + } catch (ClassNotFoundException e) { + return false; + } + } /** * Register a command with the server's command manager @@ -103,7 +117,7 @@ public final class PaperUniform implements Uniform { * @since 1.0 */ public void register(@NotNull Command... commands) { - if (isUseModernApi()) { + if (useModernApi) { register(Arrays.stream(commands).map(PaperCommand::new).toArray(PaperCommand[]::new)); return; } 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 a29bd52..af47b8b 100644 --- a/velocity/src/main/java/net/william278/uniform/velocity/VelocityCommand.java +++ b/velocity/src/main/java/net/william278/uniform/velocity/VelocityCommand.java @@ -30,7 +30,6 @@ import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.server.RegisteredServer; import net.william278.uniform.BaseCommand; import net.william278.uniform.Command; -import net.william278.uniform.CommandUser; import net.william278.uniform.element.ArgumentElement; import org.jetbrains.annotations.NotNull; @@ -47,12 +46,13 @@ public class VelocityCommand extends BaseCommand { super(name, aliases); } - public VelocityCommand(@NotNull String name, @NotNull String description, @NotNull List aliases) { + public VelocityCommand(@NotNull String name, @NotNull String description, + @NotNull List aliases) { super(name, description, aliases); } public static ArgumentElement server(ProxyServer server, String name, - SuggestionProvider suggestionProvider) { + SuggestionProvider suggestionProvider) { ArgumentType argumentType = reader -> { String s = reader.readUnquotedString(); RegisteredServer server1 = server.getServer(s).orElse(null); @@ -74,7 +74,7 @@ public class VelocityCommand extends BaseCommand { } public static ArgumentElement source(ProxyServer server, String name, - SuggestionProvider suggestionProvider) { + SuggestionProvider suggestionProvider) { ArgumentType argumentType = reader -> { String s = reader.readUnquotedString(); CommandSource source = server.getPlayer(s).orElse(null); @@ -95,12 +95,6 @@ public class VelocityCommand extends BaseCommand { }); } - @Override - @NotNull - protected CommandUser getUser(@NotNull Object user) { - return new VelocityCommandUser((CommandSource) user); - } - @Override public void addSubCommand(@NotNull Command command) { addSubCommand(new VelocityCommand(command)); 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 41bb503..e2d6f93 100644 --- a/velocity/src/main/java/net/william278/uniform/velocity/VelocityUniform.java +++ b/velocity/src/main/java/net/william278/uniform/velocity/VelocityUniform.java @@ -22,13 +22,18 @@ package net.william278.uniform.velocity; import com.velocitypowered.api.command.BrigadierCommand; +import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.proxy.ProxyServer; +import lombok.Getter; +import lombok.Setter; import net.william278.uniform.BaseCommand; import net.william278.uniform.Command; +import net.william278.uniform.CommandUser; import net.william278.uniform.Uniform; import org.jetbrains.annotations.NotNull; import java.util.Arrays; +import java.util.function.Function; /** * A class for registering commands with the Velocity server's command manager @@ -42,6 +47,10 @@ public final class VelocityUniform implements Uniform { private final ProxyServer server; + @Getter + @Setter + Function commandUserSupplier = (user) -> new VelocityCommandUser((CommandSource) user); + private VelocityUniform(@NotNull ProxyServer server) { this.server = server; } @@ -70,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()))); + .register(c.getName(), new BrigadierCommand(c.build(this)))); } /**