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.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<CommandSender> {
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<String> aliases) {
public BukkitCommand(@NotNull String name, @NotNull String description, @NotNull List<String> 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);
this.plugin = plugin;
}
@NotNull
@ -71,19 +63,11 @@ public class BukkitCommand extends BaseCommand<CommandSender> {
private static final int COMMAND_SUCCESS = com.mojang.brigadier.Command.SINGLE_SUCCESS;
private final LiteralCommandNode<CommandSender> commandNode;
private final CommandDispatcher<CommandSender> dispatcher;
private final JavaPlugin plugin;
private BukkitAudiences audiences;
private final CommandDispatcher<CommandSender> 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<CommandSender> {
@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<CommandSender> {
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<CommandSender> {
@Override
public List<String> 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<CommandSender> {
@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));
}
}

@ -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

@ -40,6 +40,7 @@ package net.william278.uniform.bukkit;/*
* 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.Uniform;
import org.bukkit.command.CommandSender;
@ -59,15 +60,20 @@ import java.util.Locale;
public final class BukkitUniform implements Uniform<CommandSender, BukkitCommand> {
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<CommandSender, BukkitCommand
@Override
public void register(@NotNull BukkitCommand... commands) {
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()
);
}
@ -102,9 +108,7 @@ public final class BukkitUniform implements Uniform<CommandSender, BukkitCommand
*/
@Override
public void register(@NotNull Command... commands) {
register(Arrays.stream(commands)
.map(command -> new BukkitCommand(command, plugin))
.toArray(BukkitCommand[]::new));
register(Arrays.stream(commands).map(BukkitCommand::new).toArray(BukkitCommand[]::new));
}
}

@ -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<S> {
@Getter
private final String name;
@Getter
private final String description;
@Getter
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) {
this.name = command.getName();
this.aliases = command.getAliases();
@ -63,17 +68,6 @@ public abstract class BaseCommand<S> {
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
protected abstract CommandUser getUser(@NotNull Object user);
@ -117,6 +111,11 @@ public abstract class BaseCommand<S> {
return Graph.create(this).build();
}
@NotNull
public final LiteralArgumentBuilder<S> createBuilder() {
return Graph.create(this).literal(this.name);
}
@NotNull
public static <S> LiteralElement<S> literal(@NotNull String name) {
return new LiteralElement<>(name);

@ -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<S>(@NotNull Node<S> root) {
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 {
implementation(project(":paper"))
// implementation(project(":bukkit"))
// implementation(project(":paper"))
implementation(project(":bukkit"))
compileOnly 'io.papermc.paper:paper-api:1.20.6-R0.1-SNAPSHOT'
}

@ -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());
}

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

@ -46,7 +46,7 @@ public final class FabricUniform implements Uniform<ServerCommandSource, FabricC
private FabricUniform() {
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);
}
protected static ArgumentElement<ServerCommandSource, Item> itemArg(String name) {
return registryArg(name, Registries.ITEM);
public static ArgumentElement<ServerCommandSource, Item> item(String name) {
return registry(name, Registries.ITEM);
}
protected static ArgumentElement<ServerCommandSource, Block> blockArg(String name) {
return registryArg(name, Registries.BLOCK);
public static ArgumentElement<ServerCommandSource, Block> block(String name) {
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 -> {
String itemId = reader.readString();
final Identifier id;

@ -46,7 +46,7 @@ public final class FabricUniform implements Uniform<ServerCommandSource, FabricC
private FabricUniform() {
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);
}
protected static ArgumentElement<CommandSourceStack, Material> materialArg(String name) {
public static ArgumentElement<CommandSourceStack, Material> 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<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 -> {
String playerName = reader.readString();
if (playerName.equals("@a")) {

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

Loading…
Cancel
Save