refactor: don't store Uniform within BaseCommand

dependabot/gradle/org.projectlombok-lombok-1.18.34 1.0.2
William 5 months ago
parent 39ebab832a
commit d43e81aa46
No known key found for this signature in database

@ -46,11 +46,6 @@ public class BukkitCommand extends BaseCommand<CommandSender> {
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<String> aliases) {
super(name, description, aliases);
}
@ -72,7 +67,7 @@ public class BukkitCommand extends BaseCommand<CommandSender> {
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<CommandSender> {
@Override
public void addSubCommand(@NotNull Command command) {
addSubCommand(new BukkitCommand(command, uniform));
addSubCommand(new BukkitCommand(command));
}
@Override
public Uniform getUniform() {
return BukkitUniform.INSTANCE;
}
}

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

@ -50,7 +50,6 @@ public abstract class BaseCommand<S> {
private Predicate<S> condition;
@Nullable
private CommandExecutor<S> defaultExecutor;
protected Uniform uniform;
public BaseCommand(@NotNull Command command) {
this.name = command.getName();
@ -72,7 +71,7 @@ public abstract class BaseCommand<S> {
@NotNull
public final CommandUser getUser(@NotNull Object user) {
return uniform.getCommandUserSupplier().apply(user);
return getUniform().getCommandUserSupplier().apply(user);
}
public final void setCondition(@NotNull Predicate<S> condition) {
@ -110,15 +109,15 @@ public abstract class BaseCommand<S> {
public abstract void addSubCommand(@NotNull Command command);
public abstract Uniform getUniform();
@NotNull
public final LiteralCommandNode<S> build(@NotNull Uniform uniform) {
this.uniform = uniform;
public final LiteralCommandNode<S> build() {
return Graph.create(this).build();
}
@NotNull
public final LiteralArgumentBuilder<S> createBuilder(@NotNull Uniform uniform) {
this.uniform = uniform;
public final LiteralArgumentBuilder<S> createBuilder() {
return Graph.create(this).literal(this.name);
}

@ -44,11 +44,6 @@ public class FabricCommand extends BaseCommand<ServerCommandSource> {
super(command);
}
private FabricCommand(@NotNull Command command, @NotNull Uniform uniform) {
super(command);
this.uniform = uniform;
}
public FabricCommand(@NotNull String name, @NotNull List<String> aliases) {
super(name, aliases);
}
@ -86,6 +81,12 @@ public class FabricCommand extends BaseCommand<ServerCommandSource> {
@Override
public void addSubCommand(@NotNull Command command) {
addSubCommand(new FabricCommand(command, getUniform()));
addSubCommand(new FabricCommand(command));
}
@Override
public Uniform getUniform() {
return FabricUniform.INSTANCE;
}
}

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

@ -44,11 +44,6 @@ public class FabricCommand extends BaseCommand<ServerCommandSource> {
super(command);
}
private FabricCommand(@NotNull Command command, @NotNull Uniform uniform) {
super(command);
this.uniform = uniform;
}
public FabricCommand(@NotNull String name, @NotNull List<String> aliases) {
super(name, aliases);
}
@ -87,6 +82,11 @@ public class FabricCommand extends BaseCommand<ServerCommandSource> {
@Override
public void addSubCommand(@NotNull Command command) {
addSubCommand(new FabricCommand(command, getUniform()));
addSubCommand(new FabricCommand(command));
}
@Override
public Uniform getUniform() {
return FabricUniform.INSTANCE;
}
}

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

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

@ -52,11 +52,6 @@ public class LegacyPaperCommand extends BaseCommand<BukkitBrigadierCommandSource
super(command);
}
private LegacyPaperCommand(@NotNull Command command, @NotNull Uniform uniform) {
super(command);
this.uniform = uniform;
}
public LegacyPaperCommand(@NotNull String name, @NotNull List<String> aliases) {
super(name, aliases);
}
@ -67,13 +62,16 @@ public class LegacyPaperCommand extends BaseCommand<BukkitBrigadierCommandSource
@Override
public void addSubCommand(@NotNull Command command) {
addSubCommand(new LegacyPaperCommand(command, getUniform()));
addSubCommand(new LegacyPaperCommand(command));
}
@Override
public Uniform getUniform() {
return PaperUniform.INSTANCE;
}
@AllArgsConstructor
static class Registrar implements Listener {
@NotNull
private final PaperUniform uniform;
@NotNull
private final JavaPlugin plugin;
@NotNull
@ -83,7 +81,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(uniform);
final LiteralCommandNode<BukkitBrigadierCommandSource> built = command.build();
event.getRoot().addChild(built);
// Register aliases

@ -51,12 +51,6 @@ public class PaperCommand extends BaseCommand<CommandSourceStack> {
super(command);
}
private PaperCommand(@NotNull Command command, @NotNull Uniform uniform) {
super(command);
this.uniform = uniform;
}
public PaperCommand(@NotNull String name, @NotNull List<String> aliases) {
super(name, aliases);
}
@ -65,12 +59,11 @@ public class PaperCommand extends BaseCommand<CommandSourceStack> {
super(name, description, aliases);
}
static void register(@NotNull PaperUniform uniform, @NotNull JavaPlugin plugin,
@NotNull Set<PaperCommand> commands) {
static void register(@NotNull JavaPlugin plugin, @NotNull Set<PaperCommand> 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<CommandSourceStack> {
@Override
public void addSubCommand(@NotNull Command command) {
addSubCommand(new PaperCommand(command, getUniform()));
addSubCommand(new PaperCommand(command));
}
@Override
public Uniform getUniform() {
return PaperUniform.INSTANCE;
}
}

@ -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<LegacyPaperCommand> legacyCommands = Sets.newHashSet();
private final Set<PaperCommand> 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 <S, T extends BaseCommand<S>> 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");
});
}

@ -43,11 +43,6 @@ public class VelocityCommand extends BaseCommand<CommandSource> {
super(command);
}
private VelocityCommand(@NotNull Command command, @NotNull Uniform uniform) {
super(command);
this.uniform = uniform;
}
public VelocityCommand(@NotNull String name, @NotNull List<String> aliases) {
super(name, aliases);
}
@ -103,7 +98,12 @@ public class VelocityCommand extends BaseCommand<CommandSource> {
@Override
public void addSubCommand(@NotNull Command command) {
addSubCommand(new VelocityCommand(command, getUniform()));
addSubCommand(new VelocityCommand(command));
}
@Override
public Uniform getUniform() {
return VelocityUniform.INSTANCE;
}
}

@ -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 <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(this))));
.register(c.getName(), new BrigadierCommand(c.build())));
}
/**

Loading…
Cancel
Save