feat: add way of supplying custom commanduser impls

dependabot/gradle/org.projectlombok-lombok-1.18.34
William 5 months ago
parent 823fcf9874
commit a3b9c1ab82
No known key found for this signature in database

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

@ -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<CommandSender> {
}
@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<CommandSender> {
private final CommandDispatcher<CommandSender> 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<CommandSender> {
@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));

@ -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<Object, CommandUser> 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 <S, T extends BaseCommand<S>> 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()
);
}

@ -48,6 +48,7 @@ public abstract class BaseCommand<S> {
private Predicate<S> condition;
@Nullable
private CommandExecutor<S> defaultExecutor;
private Uniform uniform;
private final List<CommandSyntax<S>> syntaxes = new ArrayList<>();
private final List<BaseCommand<S>> subCommands = new ArrayList<>();
@ -58,7 +59,8 @@ public abstract class BaseCommand<S> {
command.provide(this);
}
public BaseCommand(@NotNull String name, @NotNull String description, @NotNull List<String> aliases) {
public BaseCommand(@NotNull String name, @NotNull String description,
@NotNull List<String> aliases) {
this.name = name;
this.aliases = aliases;
this.description = description;
@ -69,7 +71,9 @@ public abstract class BaseCommand<S> {
}
@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<S> condition) {
this.condition = condition;
@ -107,12 +111,14 @@ public abstract class BaseCommand<S> {
public abstract void addSubCommand(@NotNull Command command);
@NotNull
public final LiteralCommandNode<S> build() {
public final LiteralCommandNode<S> build(@NotNull Uniform uniform) {
this.uniform = uniform;
return Graph.create(this).build();
}
@NotNull
public final LiteralArgumentBuilder<S> createBuilder() {
public final LiteralArgumentBuilder<S> createBuilder(@NotNull Uniform uniform) {
this.uniform = uniform;
return Graph.create(this).literal(this.name);
}

@ -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")
<S, T extends BaseCommand<S>> void register(T... commands);
@NotNull
Function<Object, CommandUser> getCommandUserSupplier();
void setCommandUserSupplier(@NotNull Function<Object, CommandUser> supplier);
}

@ -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<ServerCommandSource> {
});
}
@Override
@NotNull
protected CommandUser getUser(@NotNull Object user) {
return new FabricCommandUser((ServerCommandSource) user);
}
@Override
public void addSubCommand(@NotNull Command command) {
addSubCommand(new FabricCommand(command));
}
}

@ -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<FabricCommand> commands = Sets.newHashSet();
@Getter
@Setter
Function<Object, CommandUser> 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)))
);
}

@ -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<ServerCommandSource> {
super(name, aliases);
}
public FabricCommand(@NotNull String name, @NotNull String description, @NotNull List<String> aliases) {
public FabricCommand(@NotNull String name, @NotNull String description,
@NotNull List<String> aliases) {
super(name, description, aliases);
}
@ -79,12 +79,6 @@ public class FabricCommand extends BaseCommand<ServerCommandSource> {
});
}
@Override
@NotNull
protected CommandUser getUser(@NotNull Object user) {
return new FabricCommandUser((ServerCommandSource) user);
}
@Override
public void addSubCommand(@NotNull Command command) {
addSubCommand(new FabricCommand(command));

@ -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<FabricCommand> commands = Sets.newHashSet();
@Getter
@Setter
Function<Object, CommandUser> 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)))
);
}

@ -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<BukkitBrigadierCommandSource> {
static final Function<Object, CommandUser> USER_SUPPLIER = (user) -> new LegacyPaperCommandUser(
(BukkitBrigadierCommandSource) user
);
public LegacyPaperCommand(@NotNull Command command) {
super(command);
}
@ -54,12 +59,6 @@ public class LegacyPaperCommand extends BaseCommand<BukkitBrigadierCommandSource
super(name, description, aliases);
}
@Override
@NotNull
protected CommandUser getUser(@NotNull Object user) {
return new LegacyPaperCommandUser((BukkitBrigadierCommandSource) user);
}
@Override
public void addSubCommand(@NotNull Command command) {
addSubCommand(new LegacyPaperCommand(command));
@ -67,6 +66,8 @@ public class LegacyPaperCommand extends BaseCommand<BukkitBrigadierCommandSource
@AllArgsConstructor
static class Registrar implements Listener {
@NotNull
private final PaperUniform uniform;
@NotNull
private final JavaPlugin plugin;
@NotNull
@ -76,7 +77,7 @@ public class LegacyPaperCommand extends BaseCommand<BukkitBrigadierCommandSource
public void commandRegisterEvent(CommandRegisteredEvent<BukkitBrigadierCommandSource> event) {
commands.forEach(command -> {
// Register root command
final LiteralCommandNode<BukkitBrigadierCommandSource> built = command.build();
final LiteralCommandNode<BukkitBrigadierCommandSource> built = command.build(uniform);
event.getRoot().addChild(built);
// Register aliases

@ -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<CommandSourceStack> {
static final Function<Object, CommandUser> USER_SUPPLIER = (user) -> new PaperCommandUser(
(CommandSourceStack) user
);
public PaperCommand(@NotNull Command command) {
super(command);
}
@ -53,11 +58,12 @@ public class PaperCommand extends BaseCommand<CommandSourceStack> {
super(name, description, aliases);
}
static void register(@NotNull JavaPlugin plugin, @NotNull Set<PaperCommand> commands) {
static void register(@NotNull PaperUniform uniform, @NotNull JavaPlugin plugin,
@NotNull Set<PaperCommand> 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<CommandSourceStack> {
});
}
@Override
@NotNull
protected CommandUser getUser(@NotNull Object user) {
return new PaperCommandUser((CommandSourceStack) user);
}
@Override
public void addSubCommand(@NotNull Command command) {
addSubCommand(new PaperCommand(command));

@ -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<LegacyPaperCommand> legacyCommands = Sets.newHashSet();
private final Set<PaperCommand> commands = Sets.newHashSet();
private final boolean useModernApi = isUseModernApi();
@Getter
@Setter
Function<Object, CommandUser> 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;
}

@ -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<CommandSource> {
super(name, aliases);
}
public VelocityCommand(@NotNull String name, @NotNull String description, @NotNull List<String> aliases) {
public VelocityCommand(@NotNull String name, @NotNull String description,
@NotNull List<String> aliases) {
super(name, description, aliases);
}
public static ArgumentElement<CommandSource, RegisteredServer> server(ProxyServer server, String name,
SuggestionProvider<CommandSource> suggestionProvider) {
SuggestionProvider<CommandSource> suggestionProvider) {
ArgumentType<RegisteredServer> argumentType = reader -> {
String s = reader.readUnquotedString();
RegisteredServer server1 = server.getServer(s).orElse(null);
@ -74,7 +74,7 @@ public class VelocityCommand extends BaseCommand<CommandSource> {
}
public static ArgumentElement<CommandSource, CommandSource> source(ProxyServer server, String name,
SuggestionProvider<CommandSource> suggestionProvider) {
SuggestionProvider<CommandSource> suggestionProvider) {
ArgumentType<CommandSource> argumentType = reader -> {
String s = reader.readUnquotedString();
CommandSource source = server.getPlayer(s).orElse(null);
@ -95,12 +95,6 @@ public class VelocityCommand extends BaseCommand<CommandSource> {
});
}
@Override
@NotNull
protected CommandUser getUser(@NotNull Object user) {
return new VelocityCommandUser((CommandSource) user);
}
@Override
public void addSubCommand(@NotNull Command command) {
addSubCommand(new VelocityCommand(command));

@ -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<Object, CommandUser> 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 <S, T extends BaseCommand<S>> 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))));
}
/**

Loading…
Cancel
Save