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); 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) { public BukkitCommand(@NotNull String name, @NotNull String description, @NotNull List<String> aliases) {
super(name, description, aliases); super(name, description, aliases);
} }
@ -72,7 +67,7 @@ public class BukkitCommand extends BaseCommand<CommandSender> {
public Impl(@NotNull Uniform uniform, @NotNull BukkitCommand command) { public Impl(@NotNull Uniform uniform, @NotNull BukkitCommand command) {
super(command.getName()); super(command.getName());
this.dispatcher.register(command.createBuilder(uniform)); this.dispatcher.register(command.createBuilder());
this.setDescription(command.getDescription()); this.setDescription(command.getDescription());
this.setAliases(command.getAliases()); this.setAliases(command.getAliases());
} }
@ -122,7 +117,12 @@ public class BukkitCommand extends BaseCommand<CommandSender> {
@Override @Override
public void addSubCommand(@NotNull Command command) { 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") @SuppressWarnings("unused")
public final class BukkitUniform implements Uniform { public final class BukkitUniform implements Uniform {
private static BukkitUniform INSTANCE; static BukkitUniform INSTANCE;
private static BukkitAudiences AUDIENCES; private static BukkitAudiences AUDIENCES;
private static JavaPlugin PLUGIN; private static JavaPlugin PLUGIN;

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

@ -44,11 +44,6 @@ public class FabricCommand extends BaseCommand<ServerCommandSource> {
super(command); super(command);
} }
private FabricCommand(@NotNull Command command, @NotNull Uniform uniform) {
super(command);
this.uniform = uniform;
}
public FabricCommand(@NotNull String name, @NotNull List<String> aliases) { public FabricCommand(@NotNull String name, @NotNull List<String> aliases) {
super(name, aliases); super(name, aliases);
} }
@ -86,6 +81,12 @@ public class FabricCommand extends BaseCommand<ServerCommandSource> {
@Override @Override
public void addSubCommand(@NotNull Command command) { 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") @SuppressWarnings("unused")
public final class FabricUniform implements Uniform { public final class FabricUniform implements Uniform {
private static FabricUniform INSTANCE; static FabricUniform INSTANCE;
private final Set<FabricCommand> commands = Sets.newHashSet(); private final Set<FabricCommand> commands = Sets.newHashSet();
@ -54,7 +54,7 @@ public final class FabricUniform implements Uniform {
private FabricUniform() { private FabricUniform() {
CommandRegistrationCallback.EVENT.register((dispatcher, registry, environment) -> 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); super(command);
} }
private FabricCommand(@NotNull Command command, @NotNull Uniform uniform) {
super(command);
this.uniform = uniform;
}
public FabricCommand(@NotNull String name, @NotNull List<String> aliases) { public FabricCommand(@NotNull String name, @NotNull List<String> aliases) {
super(name, aliases); super(name, aliases);
} }
@ -87,6 +82,11 @@ public class FabricCommand extends BaseCommand<ServerCommandSource> {
@Override @Override
public void addSubCommand(@NotNull Command command) { 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") @SuppressWarnings("unused")
public final class FabricUniform implements Uniform { public final class FabricUniform implements Uniform {
private static FabricUniform INSTANCE; static FabricUniform INSTANCE;
private final Set<FabricCommand> commands = Sets.newHashSet(); private final Set<FabricCommand> commands = Sets.newHashSet();
@ -54,7 +54,7 @@ public final class FabricUniform implements Uniform {
private FabricUniform() { private FabricUniform() {
CommandRegistrationCallback.EVENT.register((dispatcher, registry, environment) -> 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.jvmargs='-Dfile.encoding=UTF-8'
org.gradle.daemon=true org.gradle.daemon=true
library_version=1.0.1 library_version=1.0.2
library_archive=uniform library_archive=uniform
library_description=Cross-platform wrapper for making Brigadier commands, based on BrigadierWrapper by Tofaa2, itself inspired by emortalmcs command system. 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); super(command);
} }
private LegacyPaperCommand(@NotNull Command command, @NotNull Uniform uniform) {
super(command);
this.uniform = uniform;
}
public LegacyPaperCommand(@NotNull String name, @NotNull List<String> aliases) { public LegacyPaperCommand(@NotNull String name, @NotNull List<String> aliases) {
super(name, aliases); super(name, aliases);
} }
@ -67,13 +62,16 @@ public class LegacyPaperCommand extends BaseCommand<BukkitBrigadierCommandSource
@Override @Override
public void addSubCommand(@NotNull Command command) { public void addSubCommand(@NotNull Command command) {
addSubCommand(new LegacyPaperCommand(command, getUniform())); addSubCommand(new LegacyPaperCommand(command));
}
@Override
public Uniform getUniform() {
return PaperUniform.INSTANCE;
} }
@AllArgsConstructor @AllArgsConstructor
static class Registrar implements Listener { static class Registrar implements Listener {
@NotNull
private final PaperUniform uniform;
@NotNull @NotNull
private final JavaPlugin plugin; private final JavaPlugin plugin;
@NotNull @NotNull
@ -83,7 +81,7 @@ public class LegacyPaperCommand extends BaseCommand<BukkitBrigadierCommandSource
public void commandRegisterEvent(CommandRegisteredEvent<BukkitBrigadierCommandSource> event) { public void commandRegisterEvent(CommandRegisteredEvent<BukkitBrigadierCommandSource> event) {
commands.forEach(command -> { commands.forEach(command -> {
// Register root command // Register root command
final LiteralCommandNode<BukkitBrigadierCommandSource> built = command.build(uniform); final LiteralCommandNode<BukkitBrigadierCommandSource> built = command.build();
event.getRoot().addChild(built); event.getRoot().addChild(built);
// Register aliases // Register aliases

@ -51,12 +51,6 @@ public class PaperCommand extends BaseCommand<CommandSourceStack> {
super(command); super(command);
} }
private PaperCommand(@NotNull Command command, @NotNull Uniform uniform) {
super(command);
this.uniform = uniform;
}
public PaperCommand(@NotNull String name, @NotNull List<String> aliases) { public PaperCommand(@NotNull String name, @NotNull List<String> aliases) {
super(name, aliases); super(name, aliases);
} }
@ -65,12 +59,11 @@ public class PaperCommand extends BaseCommand<CommandSourceStack> {
super(name, description, aliases); super(name, description, aliases);
} }
static void register(@NotNull PaperUniform uniform, @NotNull JavaPlugin plugin, static void register(@NotNull JavaPlugin plugin, @NotNull Set<PaperCommand> commands) {
@NotNull Set<PaperCommand> commands) {
plugin.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS, (event) -> { plugin.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS, (event) -> {
commands.forEach(command -> event.registrar().register( commands.forEach(command -> event.registrar().register(
plugin.getPluginMeta(), plugin.getPluginMeta(),
command.build(uniform), command.build(),
command.getDescription(), command.getDescription(),
command.getAliases() command.getAliases()
)); ));
@ -116,7 +109,12 @@ public class PaperCommand extends BaseCommand<CommandSourceStack> {
@Override @Override
public void addSubCommand(@NotNull Command command) { 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 { public final class PaperUniform implements Uniform {
private static PaperUniform INSTANCE; static PaperUniform INSTANCE;
private final Set<LegacyPaperCommand> legacyCommands = Sets.newHashSet(); private final Set<LegacyPaperCommand> legacyCommands = Sets.newHashSet();
private final Set<PaperCommand> commands = 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 // Modern (1.20.6+) Lifecycle event based Paper Brigadier API
if (useModernApi) { if (useModernApi) {
this.commandUserSupplier = PaperCommand.USER_SUPPLIER; this.commandUserSupplier = PaperCommand.USER_SUPPLIER;
PaperCommand.register(this, plugin, commands); PaperCommand.register(plugin, commands);
return; return;
} }
// Legacy (1.17-1.20.4) event-based Paper Brigadier API // Legacy (1.17-1.20.4) event-based Paper Brigadier API
this.commandUserSupplier = LegacyPaperCommand.USER_SUPPLIER; this.commandUserSupplier = LegacyPaperCommand.USER_SUPPLIER;
plugin.getServer().getPluginManager().registerEvents( 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 @Override
public final <S, T extends BaseCommand<S>> void register(T... commands) { public final <S, T extends BaseCommand<S>> void register(T... commands) {
Arrays.stream(commands).forEach(c -> { Arrays.stream(commands).forEach(c -> {
if (c instanceof PaperCommand paper) { if (useModernApi) {
this.commands.add(paper); this.commands.add((PaperCommand) c);
} else if (c instanceof LegacyPaperCommand legacy) { } else {
this.legacyCommands.add(legacy); this.legacyCommands.add((LegacyPaperCommand) c);
} }
throw new IllegalArgumentException("Command type not supported");
}); });
} }

@ -43,11 +43,6 @@ public class VelocityCommand extends BaseCommand<CommandSource> {
super(command); super(command);
} }
private VelocityCommand(@NotNull Command command, @NotNull Uniform uniform) {
super(command);
this.uniform = uniform;
}
public VelocityCommand(@NotNull String name, @NotNull List<String> aliases) { public VelocityCommand(@NotNull String name, @NotNull List<String> aliases) {
super(name, aliases); super(name, aliases);
} }
@ -103,7 +98,12 @@ public class VelocityCommand extends BaseCommand<CommandSource> {
@Override @Override
public void addSubCommand(@NotNull Command command) { 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") @SuppressWarnings("unused")
public final class VelocityUniform implements Uniform { public final class VelocityUniform implements Uniform {
private static VelocityUniform INSTANCE; static VelocityUniform INSTANCE;
private final ProxyServer server; private final ProxyServer server;
@ -79,7 +79,7 @@ public final class VelocityUniform implements Uniform {
@Override @Override
public final <S, T extends BaseCommand<S>> void register(T... commands) { public final <S, T extends BaseCommand<S>> void register(T... commands) {
Arrays.stream(commands).map(c -> (VelocityCommand) c).forEach(c -> server.getCommandManager() 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