refactor: fix uniform on Bukkit

BukkitCommand no longer requires JavaPlugin
dependabot/gradle/org.projectlombok-lombok-1.18.34
William 5 months ago
parent 6a452524f4
commit 2f3edf0ca1
No known key found for this signature in database

@ -24,7 +24,6 @@ package net.william278.uniform.bukkit;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestion; import com.mojang.brigadier.suggestion.Suggestion;
import com.mojang.brigadier.tree.LiteralCommandNode;
import net.kyori.adventure.audience.Audience; import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.platform.bukkit.BukkitAudiences; import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
@ -34,32 +33,25 @@ import net.william278.uniform.Command;
import net.william278.uniform.CommandUser; import net.william278.uniform.CommandUser;
import org.bukkit.command.CommandException; import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.List; import java.util.List;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class BukkitCommand extends BaseCommand<CommandSender> { public class BukkitCommand extends BaseCommand<CommandSender> {
private final JavaPlugin plugin;
private BukkitAudiences audiences; private BukkitAudiences audiences;
public BukkitCommand(@NotNull Command command, @NotNull JavaPlugin plugin) { public BukkitCommand(@NotNull Command command) {
super(command); super(command);
this.plugin = plugin;
} }
public BukkitCommand(@NotNull JavaPlugin plugin, @NotNull String name, @NotNull String description, public BukkitCommand(@NotNull String name, @NotNull String description, @NotNull List<String> aliases) {
@NotNull List<String> aliases) {
super(name, description, aliases); super(name, description, aliases);
this.plugin = plugin;
} }
public BukkitCommand(@NotNull JavaPlugin plugin, @NotNull String name, @NotNull List<String> aliases) { public BukkitCommand(@NotNull String name, @NotNull List<String> aliases) {
super(name, aliases); super(name, aliases);
this.plugin = plugin;
} }
@NotNull @NotNull
@ -71,19 +63,11 @@ public class BukkitCommand extends BaseCommand<CommandSender> {
private static final int COMMAND_SUCCESS = com.mojang.brigadier.Command.SINGLE_SUCCESS; private static final int COMMAND_SUCCESS = com.mojang.brigadier.Command.SINGLE_SUCCESS;
private final LiteralCommandNode<CommandSender> commandNode; private final CommandDispatcher<CommandSender> dispatcher = new CommandDispatcher<>();
private final CommandDispatcher<CommandSender> dispatcher;
private final JavaPlugin plugin;
private BukkitAudiences audiences;
public Impl(@NotNull BukkitCommand command) { public Impl(@NotNull BukkitCommand command) {
super(command.getName()); super(command.getName());
this.audiences = command.audiences; this.dispatcher.register(command.createBuilder());
this.plugin = command.plugin;
// Register command, setup description and aliases
this.dispatcher = new CommandDispatcher<>();
this.commandNode = this.dispatcher.register(command.build().createBuilder());
this.setDescription(command.getDescription()); this.setDescription(command.getDescription());
this.setAliases(command.getAliases()); this.setAliases(command.getAliases());
} }
@ -91,9 +75,7 @@ public class BukkitCommand extends BaseCommand<CommandSender> {
@Override @Override
public boolean execute(@NotNull CommandSender commandSender, @NotNull String alias, @NotNull String[] args) { public boolean execute(@NotNull CommandSender commandSender, @NotNull String alias, @NotNull String[] args) {
try { try {
final String string = getInput(alias, args); return dispatcher.execute(getInput(args), commandSender) == COMMAND_SUCCESS;
System.out.println("Usage: \"" + Arrays.toString(dispatcher.getAllUsage(commandNode, commandSender, false)) + "\"");
return dispatcher.execute(string, commandSender) == COMMAND_SUCCESS;
} catch (CommandSyntaxException e) { } catch (CommandSyntaxException e) {
getAudience(commandSender).sendMessage(Component getAudience(commandSender).sendMessage(Component
.translatable("command.context.parse_error", NamedTextColor.RED) .translatable("command.context.parse_error", NamedTextColor.RED)
@ -105,7 +87,7 @@ public class BukkitCommand extends BaseCommand<CommandSender> {
return false; return false;
} catch (CommandException e) { } catch (CommandException e) {
getAudience(commandSender).sendMessage(Component.text(e.getMessage(), NamedTextColor.RED)); getAudience(commandSender).sendMessage(Component.text(e.getMessage(), NamedTextColor.RED));
return true; return false;
} }
} }
@ -113,7 +95,7 @@ public class BukkitCommand extends BaseCommand<CommandSender> {
@Override @Override
public List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) public List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args)
throws IllegalArgumentException { throws IllegalArgumentException {
final String passed = getInput(alias, args); final String passed = getInput(args);
return dispatcher.getCompletionSuggestions( return dispatcher.getCompletionSuggestions(
dispatcher.parse(passed, sender), dispatcher.parse(passed, sender),
passed.length() // Spigot API limitation - we can only TAB complete the full text length :( passed.length() // Spigot API limitation - we can only TAB complete the full text length :(
@ -124,30 +106,25 @@ public class BukkitCommand extends BaseCommand<CommandSender> {
@NotNull @NotNull
private Audience getAudience(@NotNull CommandSender sender) { private Audience getAudience(@NotNull CommandSender sender) {
if (audiences == null) { return BukkitUniform.getAudiences().sender(sender);
audiences = BukkitAudiences.create(plugin);
}
return audiences.sender(sender);
} }
@NotNull @NotNull
private String getInput(@NotNull String alias, @NotNull String[] args) { private String getInput(@NotNull String[] args) {
return args.length == 0 ? alias : "%s %s".formatted(alias, String.join(" ", args)); return args.length == 0 ? getName()
: "%s %s".formatted(getName(), String.join(" ", args));
} }
} }
@Override @Override
@NotNull @NotNull
protected CommandUser getUser(@NotNull Object user) { protected CommandUser getUser(@NotNull Object user) {
if (audiences == null) { return new BukkitCommandUser((CommandSender) user);
audiences = BukkitAudiences.create(plugin);
}
return new BukkitCommandUser((CommandSender) user, audiences);
} }
@Override @Override
public void addSubCommand(@NotNull Command command) { public void addSubCommand(@NotNull Command command) {
addSubCommand(new BukkitCommand(command, plugin)); addSubCommand(new BukkitCommand(command));
} }
} }

@ -22,7 +22,6 @@
package net.william278.uniform.bukkit; package net.william278.uniform.bukkit;
import net.kyori.adventure.audience.Audience; import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.william278.uniform.CommandUser; import net.william278.uniform.CommandUser;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -31,12 +30,12 @@ import org.jetbrains.annotations.Nullable;
import java.util.UUID; import java.util.UUID;
public record BukkitCommandUser(@NotNull CommandSender sender, @NotNull BukkitAudiences adv) implements CommandUser { public record BukkitCommandUser(@NotNull CommandSender sender) implements CommandUser {
@Override @Override
@NotNull @NotNull
public Audience getAudience() { public Audience getAudience() {
return adv.sender(sender); return BukkitUniform.getAudiences().sender(sender);
} }
@Override @Override

@ -40,6 +40,7 @@ package net.william278.uniform.bukkit;/*
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.william278.uniform.Command; import net.william278.uniform.Command;
import net.william278.uniform.Uniform; import net.william278.uniform.Uniform;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -59,15 +60,20 @@ import java.util.Locale;
public final class BukkitUniform implements Uniform<CommandSender, BukkitCommand> { public final class BukkitUniform implements Uniform<CommandSender, BukkitCommand> {
private static BukkitUniform INSTANCE; private static BukkitUniform INSTANCE;
private static BukkitAudiences AUDIENCES;
private static JavaPlugin PLUGIN;
private final JavaPlugin plugin;
private final CommandRegistration registrar; private final CommandRegistration registrar;
private BukkitUniform(@NotNull JavaPlugin plugin) { private BukkitUniform(@NotNull JavaPlugin plugin) {
this.plugin = plugin; PLUGIN = plugin;
this.registrar = new MorePaperLib(plugin).commandRegistration(); this.registrar = new MorePaperLib(plugin).commandRegistration();
} }
static BukkitAudiences getAudiences() {
return AUDIENCES != null ? AUDIENCES : (AUDIENCES = BukkitAudiences.create(PLUGIN));
}
/** /**
* Get the BukkitUniform instance for registering commands * Get the BukkitUniform instance for registering commands
* *
@ -89,7 +95,7 @@ public final class BukkitUniform implements Uniform<CommandSender, BukkitCommand
@Override @Override
public void register(@NotNull BukkitCommand... commands) { public void register(@NotNull BukkitCommand... commands) {
registrar.getServerCommandMap().registerAll( registrar.getServerCommandMap().registerAll(
plugin.getName().toLowerCase(Locale.ENGLISH), PLUGIN.getName().toLowerCase(Locale.ENGLISH).replaceAll("[^a-z0-9_]", ""),
Arrays.stream(commands).map(c -> (org.bukkit.command.Command) c.getImpl()).toList() Arrays.stream(commands).map(c -> (org.bukkit.command.Command) c.getImpl()).toList()
); );
} }
@ -102,9 +108,7 @@ public final class BukkitUniform implements Uniform<CommandSender, BukkitCommand
*/ */
@Override @Override
public void register(@NotNull Command... commands) { public void register(@NotNull Command... commands) {
register(Arrays.stream(commands) register(Arrays.stream(commands).map(BukkitCommand::new).toArray(BukkitCommand[]::new));
.map(command -> new BukkitCommand(command, plugin))
.toArray(BukkitCommand[]::new));
} }
} }

@ -22,9 +22,9 @@
package net.william278.uniform; package net.william278.uniform;
import com.mojang.brigadier.arguments.*; import com.mojang.brigadier.arguments.*;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.suggestion.SuggestionProvider; import com.mojang.brigadier.suggestion.SuggestionProvider;
import com.mojang.brigadier.tree.LiteralCommandNode; import com.mojang.brigadier.tree.LiteralCommandNode;
import lombok.AccessLevel;
import lombok.Getter; import lombok.Getter;
import net.william278.uniform.element.ArgumentElement; import net.william278.uniform.element.ArgumentElement;
import net.william278.uniform.element.CommandElement; import net.william278.uniform.element.CommandElement;
@ -36,16 +36,21 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.Predicate; import java.util.function.Predicate;
@Getter
@SuppressWarnings("unused") @SuppressWarnings("unused")
public abstract class BaseCommand<S> { public abstract class BaseCommand<S> {
@Getter
private final String name; private final String name;
@Getter
private final String description; private final String description;
@Getter
private final List<String> aliases; private final List<String> aliases;
@Nullable
private Predicate<S> condition;
@Nullable
private CommandExecutor<S> defaultExecutor;
private final List<CommandSyntax<S>> syntaxes = new ArrayList<>();
private final List<BaseCommand<S>> subCommands = new ArrayList<>();
public BaseCommand(@NotNull Command command) { public BaseCommand(@NotNull Command command) {
this.name = command.getName(); this.name = command.getName();
this.aliases = command.getAliases(); this.aliases = command.getAliases();
@ -63,17 +68,6 @@ public abstract class BaseCommand<S> {
this(name, "", aliases); this(name, "", aliases);
} }
@Nullable
@Getter(AccessLevel.PACKAGE)
private Predicate<S> condition;
@Nullable
@Getter(AccessLevel.PACKAGE)
private CommandExecutor<S> defaultExecutor;
@Getter(AccessLevel.PACKAGE)
private final List<CommandSyntax<S>> syntaxes = new ArrayList<>();
@Getter(AccessLevel.PACKAGE)
private final List<BaseCommand<S>> subCommands = new ArrayList<>();
@NotNull @NotNull
protected abstract CommandUser getUser(@NotNull Object user); protected abstract CommandUser getUser(@NotNull Object user);
@ -117,6 +111,11 @@ public abstract class BaseCommand<S> {
return Graph.create(this).build(); return Graph.create(this).build();
} }
@NotNull
public final LiteralArgumentBuilder<S> createBuilder() {
return Graph.create(this).literal(this.name);
}
@NotNull @NotNull
public static <S> LiteralElement<S> literal(@NotNull String name) { public static <S> LiteralElement<S> literal(@NotNull String name) {
return new LiteralElement<>(name); return new LiteralElement<>(name);

@ -21,6 +21,8 @@
package net.william278.uniform; 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.CommandNode;
import com.mojang.brigadier.tree.LiteralCommandNode; import com.mojang.brigadier.tree.LiteralCommandNode;
import net.william278.uniform.element.CommandElement; import net.william278.uniform.element.CommandElement;
@ -46,4 +48,14 @@ record Graph<S>(@NotNull Node<S> root) {
return literalNode; return literalNode;
} }
@NotNull
LiteralArgumentBuilder<S> literal(@NotNull String name) {
final LiteralArgumentBuilder<S> builder = LiteralArgumentBuilder.literal(name);
final CommandNode<S> command = this.root.build();
builder.executes(command.getCommand());
this.root.children().forEach(child -> builder.then(child.build()));
return builder;
}
} }

@ -4,8 +4,8 @@ plugins {
} }
dependencies { dependencies {
implementation(project(":paper")) // implementation(project(":paper"))
// implementation(project(":bukkit")) implementation(project(":bukkit"))
compileOnly 'io.papermc.paper:paper-api:1.20.6-R0.1-SNAPSHOT' compileOnly 'io.papermc.paper:paper-api:1.20.6-R0.1-SNAPSHOT'
} }

@ -21,7 +21,7 @@
package net.william278.uniform; package net.william278.uniform;
import net.william278.uniform.paper.PaperUniform; import net.william278.uniform.bukkit.BukkitUniform;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@SuppressWarnings("unused") @SuppressWarnings("unused")
@ -29,7 +29,7 @@ public class UniformExample extends JavaPlugin {
@Override @Override
public void onLoad() { public void onLoad() {
PaperUniform uniform = PaperUniform.getInstance(this); BukkitUniform uniform = BukkitUniform.getInstance(this);
uniform.register(new ExampleCommand()); uniform.register(new ExampleCommand());
} }

@ -52,15 +52,15 @@ public class FabricCommand extends BaseCommand<ServerCommandSource> {
super(name, description, aliases); super(name, description, aliases);
} }
protected static ArgumentElement<ServerCommandSource, Item> itemArg(String name) { public static ArgumentElement<ServerCommandSource, Item> item(String name) {
return registryArg(name, Registries.ITEM); return registry(name, Registries.ITEM);
} }
protected static ArgumentElement<ServerCommandSource, Block> blockArg(String name) { public static ArgumentElement<ServerCommandSource, Block> block(String name) {
return registryArg(name, Registries.BLOCK); return registry(name, Registries.BLOCK);
} }
protected static <T> ArgumentElement<ServerCommandSource, T> registryArg(String name, Registry<T> registry) { public static <T> ArgumentElement<ServerCommandSource, T> registry(String name, Registry<T> registry) {
return new ArgumentElement<>(name, reader -> { return new ArgumentElement<>(name, reader -> {
String itemId = reader.readString(); String itemId = reader.readString();
final Identifier id; final Identifier id;

@ -46,7 +46,7 @@ public final class FabricUniform implements Uniform<ServerCommandSource, FabricC
private FabricUniform() { private FabricUniform() {
CommandRegistrationCallback.EVENT.register((dispatcher, registry, environment) -> CommandRegistrationCallback.EVENT.register((dispatcher, registry, environment) ->
commands.forEach(command -> dispatcher.register(command.build().createBuilder())) commands.forEach(command -> dispatcher.register(command.createBuilder()))
); );
} }

@ -52,15 +52,15 @@ public class FabricCommand extends BaseCommand<ServerCommandSource> {
super(name, description, aliases); super(name, description, aliases);
} }
protected static ArgumentElement<ServerCommandSource, Item> itemArg(String name) { public static ArgumentElement<ServerCommandSource, Item> item(String name) {
return registryArg(name, Registries.ITEM); return registry(name, Registries.ITEM);
} }
protected static ArgumentElement<ServerCommandSource, Block> blockArg(String name) { public static ArgumentElement<ServerCommandSource, Block> block(String name) {
return registryArg(name, Registries.BLOCK); return registry(name, Registries.BLOCK);
} }
protected static <T> ArgumentElement<ServerCommandSource, T> registryArg(String name, Registry<T> registry) { public static <T> ArgumentElement<ServerCommandSource, T> registry(String name, Registry<T> registry) {
return new ArgumentElement<>(name, reader -> { return new ArgumentElement<>(name, reader -> {
String itemId = reader.readString(); String itemId = reader.readString();
final Identifier id; final Identifier id;

@ -46,7 +46,7 @@ public final class FabricUniform implements Uniform<ServerCommandSource, FabricC
private FabricUniform() { private FabricUniform() {
CommandRegistrationCallback.EVENT.register((dispatcher, registry, environment) -> CommandRegistrationCallback.EVENT.register((dispatcher, registry, environment) ->
commands.forEach(command -> dispatcher.register(command.build().createBuilder())) commands.forEach(command -> dispatcher.register(command.createBuilder()))
); );
} }

@ -50,7 +50,7 @@ public class PaperCommand extends BaseCommand<CommandSourceStack> {
super(name, description, aliases); super(name, description, aliases);
} }
protected static ArgumentElement<CommandSourceStack, Material> materialArg(String name) { public static ArgumentElement<CommandSourceStack, Material> material(String name) {
return new ArgumentElement<>(name, reader -> { return new ArgumentElement<>(name, reader -> {
String materialName = reader.readString(); String materialName = reader.readString();
Material material = Material.matchMaterial(materialName); Material material = Material.matchMaterial(materialName);
@ -66,7 +66,7 @@ public class PaperCommand extends BaseCommand<CommandSourceStack> {
}); });
} }
protected static ArgumentElement<CommandSourceStack, Collection<? extends Player>> playerArg(String name) { public static ArgumentElement<CommandSourceStack, Collection<? extends Player>> player(String name) {
return new ArgumentElement<>(name, reader -> { return new ArgumentElement<>(name, reader -> {
String playerName = reader.readString(); String playerName = reader.readString();
if (playerName.equals("@a")) { if (playerName.equals("@a")) {

@ -51,8 +51,8 @@ public class VelocityCommand extends BaseCommand<CommandSource> {
super(name, description, aliases); super(name, description, aliases);
} }
protected static ArgumentElement<CommandSource, RegisteredServer> serverArg(ProxyServer server, String name, public static ArgumentElement<CommandSource, RegisteredServer> server(ProxyServer server, String name,
SuggestionProvider<CommandSource> suggestionProvider) { SuggestionProvider<CommandSource> suggestionProvider) {
ArgumentType<RegisteredServer> argumentType = reader -> { ArgumentType<RegisteredServer> argumentType = reader -> {
String s = reader.readUnquotedString(); String s = reader.readUnquotedString();
RegisteredServer server1 = server.getServer(s).orElse(null); RegisteredServer server1 = server.getServer(s).orElse(null);
@ -64,8 +64,8 @@ public class VelocityCommand extends BaseCommand<CommandSource> {
return new ArgumentElement<>(name, argumentType, suggestionProvider); return new ArgumentElement<>(name, argumentType, suggestionProvider);
} }
protected static ArgumentElement<CommandSource, RegisteredServer> serverArg(ProxyServer server, String name) { public static ArgumentElement<CommandSource, RegisteredServer> server(ProxyServer server, String name) {
return serverArg(server, name, (context, builder) -> { return server(server, name, (context, builder) -> {
for (RegisteredServer server1 : server.getAllServers()) { for (RegisteredServer server1 : server.getAllServers()) {
builder.suggest(server1.getServerInfo().getName()); builder.suggest(server1.getServerInfo().getName());
} }
@ -73,8 +73,8 @@ public class VelocityCommand extends BaseCommand<CommandSource> {
}); });
} }
protected static ArgumentElement<CommandSource, CommandSource> sourceArg(ProxyServer server, String name, public static ArgumentElement<CommandSource, CommandSource> source(ProxyServer server, String name,
SuggestionProvider<CommandSource> suggestionProvider) { SuggestionProvider<CommandSource> suggestionProvider) {
ArgumentType<CommandSource> argumentType = reader -> { ArgumentType<CommandSource> argumentType = reader -> {
String s = reader.readUnquotedString(); String s = reader.readUnquotedString();
CommandSource source = server.getPlayer(s).orElse(null); CommandSource source = server.getPlayer(s).orElse(null);
@ -86,8 +86,8 @@ public class VelocityCommand extends BaseCommand<CommandSource> {
return new ArgumentElement<>(name, argumentType, suggestionProvider); return new ArgumentElement<>(name, argumentType, suggestionProvider);
} }
protected static ArgumentElement<CommandSource, CommandSource> sourceArg(ProxyServer server, String name) { public static ArgumentElement<CommandSource, CommandSource> source(ProxyServer server, String name) {
return sourceArg(server, name, (context, builder) -> { return source(server, name, (context, builder) -> {
for (Player source : server.getAllPlayers()) { for (Player source : server.getAllPlayers()) {
builder.suggest(source.getUsername()); builder.suggest(source.getUsername());
} }

Loading…
Cancel
Save