more command management
This commit is contained in:
parent
20d426e315
commit
ca563700b3
@ -5,10 +5,32 @@ import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.TabCompleter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public abstract class SubCommand implements CommandExecutor, TabCompleter {
|
||||
|
||||
public abstract @NotNull String label();
|
||||
public abstract @NotNull TextComponent description();
|
||||
public abstract @NotNull TextComponent syntax();
|
||||
private final String label;
|
||||
private final TextComponent syntax, description;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package me.xginko.villageroptimizer.commands;
|
||||
import me.xginko.villageroptimizer.VillagerOptimizer;
|
||||
import me.xginko.villageroptimizer.utils.Disableable;
|
||||
import me.xginko.villageroptimizer.utils.Enableable;
|
||||
import org.bukkit.command.CommandException;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.PluginCommand;
|
||||
import org.bukkit.command.TabCompleter;
|
||||
@ -16,7 +17,6 @@ import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
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 Reflections COMMANDS_PACKAGE = new Reflections(VillagerOptimizerCommand.class.getPackage().getName());
|
||||
|
||||
public final String label;
|
||||
public final PluginCommand pluginCommand;
|
||||
|
||||
protected VillagerOptimizerCommand(@NotNull String name) {
|
||||
this.label = name;
|
||||
protected VillagerOptimizerCommand(@NotNull String name) throws CommandException {
|
||||
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() {
|
||||
COMMANDS.forEach(Disableable::disable);
|
||||
COMMANDS.clear();
|
||||
|
||||
COMMANDS.addAll(COMMANDS_PACKAGE.get(Scanners.SubTypes.of(VillagerOptimizerCommand.class).asClass())
|
||||
COMMANDS_PACKAGE.get(Scanners.SubTypes.of(VillagerOptimizerCommand.class).asClass())
|
||||
.stream()
|
||||
.filter(clazz -> !clazz.isInterface() && !Modifier.isAbstract(clazz.getModifiers()))
|
||||
.map(clazz -> {
|
||||
try {
|
||||
return (VillagerOptimizerCommand) clazz.getDeclaredConstructor().newInstance();
|
||||
} 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;
|
||||
}
|
||||
})
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toSet()));
|
||||
.forEach(COMMANDS::add);
|
||||
|
||||
COMMANDS.forEach(Enableable::enable);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("DataFlowIssue")
|
||||
public void enable() {
|
||||
PluginCommand pluginCommand = VillagerOptimizer.getInstance().getCommand(label);
|
||||
pluginCommand.setExecutor(this);
|
||||
pluginCommand.setTabCompleter(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("DataFlowIssue")
|
||||
public void disable() {
|
||||
VillagerOptimizer.getInstance().getCommand(label)
|
||||
.unregister(VillagerOptimizer.commandRegistration().getServerCommandMap());
|
||||
pluginCommand.unregister(VillagerOptimizer.commandRegistration().getServerCommandMap());
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,6 @@ import me.xginko.villageroptimizer.modules.VillagerOptimizerModule;
|
||||
import me.xginko.villageroptimizer.utils.KyoriUtil;
|
||||
import me.xginko.villageroptimizer.utils.Util;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@ -19,19 +18,12 @@ import java.util.List;
|
||||
|
||||
public class DisableSubCmd extends SubCommand {
|
||||
|
||||
@Override
|
||||
public @NotNull String label() {
|
||||
return "disable";
|
||||
}
|
||||
|
||||
@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);
|
||||
public DisableSubCmd() {
|
||||
super(
|
||||
"disable",
|
||||
Component.text("/villageroptimizer disable").color(Util.PL_COLOR),
|
||||
Component.text("Disable all plugin tasks and listeners.").color(NamedTextColor.GRAY)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -6,7 +6,6 @@ import me.xginko.villageroptimizer.enums.Permissions;
|
||||
import me.xginko.villageroptimizer.utils.KyoriUtil;
|
||||
import me.xginko.villageroptimizer.utils.Util;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@ -18,19 +17,11 @@ import java.util.List;
|
||||
|
||||
public class ReloadSubCmd extends SubCommand {
|
||||
|
||||
@Override
|
||||
public @NotNull String label() {
|
||||
return "reload";
|
||||
}
|
||||
|
||||
@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);
|
||||
public ReloadSubCmd() {
|
||||
super(
|
||||
"reload",
|
||||
Component.text("/villageroptimizer reload").color(Util.PL_COLOR),
|
||||
Component.text("Reload the plugin configuration.").color(NamedTextColor.GRAY));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -7,7 +7,6 @@ import me.xginko.villageroptimizer.enums.Permissions;
|
||||
import me.xginko.villageroptimizer.utils.KyoriUtil;
|
||||
import me.xginko.villageroptimizer.utils.Util;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.event.ClickEvent;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import org.bukkit.command.Command;
|
||||
@ -21,19 +20,12 @@ import java.util.List;
|
||||
|
||||
public class VersionSubCmd extends SubCommand {
|
||||
|
||||
@Override
|
||||
public @NotNull String label() {
|
||||
return "version";
|
||||
}
|
||||
|
||||
@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);
|
||||
public VersionSubCmd() {
|
||||
super(
|
||||
"version",
|
||||
Component.text("/villageroptimizer version").color(Util.PL_COLOR),
|
||||
Component.text("Show the plugin version.").color(NamedTextColor.GRAY)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
x
Reference in New Issue
Block a user