more command management

This commit is contained in:
xGinko 2024-07-09 03:21:03 +02:00
parent 20d426e315
commit ca563700b3
5 changed files with 52 additions and 57 deletions

View File

@ -5,10 +5,32 @@ import org.bukkit.command.CommandExecutor;
import org.bukkit.command.TabCompleter; import org.bukkit.command.TabCompleter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
public abstract class SubCommand implements CommandExecutor, TabCompleter { public abstract class SubCommand implements CommandExecutor, TabCompleter {
public abstract @NotNull String label(); private final String label;
public abstract @NotNull TextComponent description(); private final TextComponent syntax, description;
public abstract @NotNull TextComponent syntax();
public SubCommand(String label, TextComponent syntax, TextComponent description) {
this.label = label;
this.syntax = syntax;
this.description = description;
}
public @NotNull String mergeArgs(@NotNull String[] args, int start) {
return String.join(" ", Arrays.copyOfRange(args, start, args.length));
}
public @NotNull String label() {
return label;
}
public @NotNull TextComponent syntax() {
return syntax;
}
public @NotNull TextComponent description() {
return description;
}
} }

View File

@ -3,6 +3,7 @@ package me.xginko.villageroptimizer.commands;
import me.xginko.villageroptimizer.VillagerOptimizer; import me.xginko.villageroptimizer.VillagerOptimizer;
import me.xginko.villageroptimizer.utils.Disableable; import me.xginko.villageroptimizer.utils.Disableable;
import me.xginko.villageroptimizer.utils.Enableable; import me.xginko.villageroptimizer.utils.Enableable;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.PluginCommand; import org.bukkit.command.PluginCommand;
import org.bukkit.command.TabCompleter; import org.bukkit.command.TabCompleter;
@ -16,7 +17,6 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
public abstract class VillagerOptimizerCommand implements Enableable, Disableable, CommandExecutor, TabCompleter { public abstract class VillagerOptimizerCommand implements Enableable, Disableable, CommandExecutor, TabCompleter {
@ -24,45 +24,43 @@ public abstract class VillagerOptimizerCommand implements Enableable, Disableabl
public static final List<String> RADIUS_SUGGESTIONS = Arrays.asList("5", "10", "25", "50"); public static final List<String> RADIUS_SUGGESTIONS = Arrays.asList("5", "10", "25", "50");
public static final Reflections COMMANDS_PACKAGE = new Reflections(VillagerOptimizerCommand.class.getPackage().getName()); public static final Reflections COMMANDS_PACKAGE = new Reflections(VillagerOptimizerCommand.class.getPackage().getName());
public final String label; public final PluginCommand pluginCommand;
protected VillagerOptimizerCommand(@NotNull String name) { protected VillagerOptimizerCommand(@NotNull String name) throws CommandException {
this.label = name; PluginCommand pluginCommand = VillagerOptimizer.getInstance().getCommand(name);
if (pluginCommand != null) this.pluginCommand = pluginCommand;
else throw new CommandException("Command cannot be enabled because it's not defined in the plugin.yml.");
} }
public static void reloadCommands() { public static void reloadCommands() {
COMMANDS.forEach(Disableable::disable); COMMANDS.forEach(Disableable::disable);
COMMANDS.clear(); COMMANDS.clear();
COMMANDS.addAll(COMMANDS_PACKAGE.get(Scanners.SubTypes.of(VillagerOptimizerCommand.class).asClass()) COMMANDS_PACKAGE.get(Scanners.SubTypes.of(VillagerOptimizerCommand.class).asClass())
.stream() .stream()
.filter(clazz -> !clazz.isInterface() && !Modifier.isAbstract(clazz.getModifiers())) .filter(clazz -> !clazz.isInterface() && !Modifier.isAbstract(clazz.getModifiers()))
.map(clazz -> { .map(clazz -> {
try { try {
return (VillagerOptimizerCommand) clazz.getDeclaredConstructor().newInstance(); return (VillagerOptimizerCommand) clazz.getDeclaredConstructor().newInstance();
} catch (Throwable t) { } catch (Throwable t) {
VillagerOptimizer.logger().warn("Failed initialising command '{}'. This should not happen.", clazz.getSimpleName()); VillagerOptimizer.logger().error("Failed initialising command '{}'.", clazz.getSimpleName(), t);
return null; return null;
} }
}) })
.filter(Objects::nonNull) .filter(Objects::nonNull)
.collect(Collectors.toSet())); .forEach(COMMANDS::add);
COMMANDS.forEach(Enableable::enable); COMMANDS.forEach(Enableable::enable);
} }
@Override @Override
@SuppressWarnings("DataFlowIssue")
public void enable() { public void enable() {
PluginCommand pluginCommand = VillagerOptimizer.getInstance().getCommand(label);
pluginCommand.setExecutor(this); pluginCommand.setExecutor(this);
pluginCommand.setTabCompleter(this); pluginCommand.setTabCompleter(this);
} }
@Override @Override
@SuppressWarnings("DataFlowIssue")
public void disable() { public void disable() {
VillagerOptimizer.getInstance().getCommand(label) pluginCommand.unregister(VillagerOptimizer.commandRegistration().getServerCommandMap());
.unregister(VillagerOptimizer.commandRegistration().getServerCommandMap());
} }
} }

View File

@ -7,7 +7,6 @@ import me.xginko.villageroptimizer.modules.VillagerOptimizerModule;
import me.xginko.villageroptimizer.utils.KyoriUtil; import me.xginko.villageroptimizer.utils.KyoriUtil;
import me.xginko.villageroptimizer.utils.Util; import me.xginko.villageroptimizer.utils.Util;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -19,19 +18,12 @@ import java.util.List;
public class DisableSubCmd extends SubCommand { public class DisableSubCmd extends SubCommand {
@Override public DisableSubCmd() {
public @NotNull String label() { super(
return "disable"; "disable",
} Component.text("/villageroptimizer disable").color(Util.PL_COLOR),
Component.text("Disable all plugin tasks and listeners.").color(NamedTextColor.GRAY)
@Override );
public @NotNull TextComponent description() {
return Component.text("Disable all plugin tasks and listeners.").color(NamedTextColor.GRAY);
}
@Override
public @NotNull TextComponent syntax() {
return Component.text("/villageroptimizer disable").color(Util.PL_COLOR);
} }
@Override @Override

View File

@ -6,7 +6,6 @@ import me.xginko.villageroptimizer.enums.Permissions;
import me.xginko.villageroptimizer.utils.KyoriUtil; import me.xginko.villageroptimizer.utils.KyoriUtil;
import me.xginko.villageroptimizer.utils.Util; import me.xginko.villageroptimizer.utils.Util;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -18,19 +17,11 @@ import java.util.List;
public class ReloadSubCmd extends SubCommand { public class ReloadSubCmd extends SubCommand {
@Override public ReloadSubCmd() {
public @NotNull String label() { super(
return "reload"; "reload",
} Component.text("/villageroptimizer reload").color(Util.PL_COLOR),
Component.text("Reload the plugin configuration.").color(NamedTextColor.GRAY));
@Override
public @NotNull TextComponent description() {
return Component.text("Reload the plugin configuration.").color(NamedTextColor.GRAY);
}
@Override
public @NotNull TextComponent syntax() {
return Component.text("/villageroptimizer reload").color(Util.PL_COLOR);
} }
@Override @Override

View File

@ -7,7 +7,6 @@ import me.xginko.villageroptimizer.enums.Permissions;
import me.xginko.villageroptimizer.utils.KyoriUtil; import me.xginko.villageroptimizer.utils.KyoriUtil;
import me.xginko.villageroptimizer.utils.Util; import me.xginko.villageroptimizer.utils.Util;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.ClickEvent; import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -21,19 +20,12 @@ import java.util.List;
public class VersionSubCmd extends SubCommand { public class VersionSubCmd extends SubCommand {
@Override public VersionSubCmd() {
public @NotNull String label() { super(
return "version"; "version",
} Component.text("/villageroptimizer version").color(Util.PL_COLOR),
Component.text("Show the plugin version.").color(NamedTextColor.GRAY)
@Override );
public @NotNull TextComponent description() {
return Component.text("Show the plugin version.").color(NamedTextColor.GRAY);
}
@Override
public @NotNull TextComponent syntax() {
return Component.text("/villageroptimizer version").color(Util.PL_COLOR);
} }
@Override @Override