use PluginCommand after all to stay reloadable

This commit is contained in:
xGinko 2024-07-09 02:50:18 +02:00
parent 7c56dfdb17
commit 20d426e315
9 changed files with 91 additions and 101 deletions

View File

@ -1,19 +1,14 @@
package me.xginko.villageroptimizer.commands;
import net.kyori.adventure.text.TextComponent;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.TabCompleter;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public abstract class SubCommand {
public abstract class SubCommand implements CommandExecutor, TabCompleter {
public abstract @NotNull String label();
public abstract @NotNull TextComponent description();
public abstract @NotNull TextComponent syntax();
public abstract @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args)
throws CommandException, IllegalArgumentException;
public abstract boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args);
}

View File

@ -3,14 +3,10 @@ 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.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginIdentifiableCommand;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.TabCompleter;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.reflections.Reflections;
import org.reflections.scanners.Scanners;
@ -22,17 +18,16 @@ import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
public abstract class VillagerOptimizerCommand extends Command
implements Enableable, Disableable, PluginIdentifiableCommand, CommandExecutor, TabCompleter {
public abstract class VillagerOptimizerCommand implements Enableable, Disableable, CommandExecutor, TabCompleter {
public static final Set<VillagerOptimizerCommand> COMMANDS = new HashSet<>();
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());
protected VillagerOptimizerCommand(
@NotNull String name, @NotNull String description, @NotNull String usageMessage, @NotNull List<String> aliases
) {
super(name, description, usageMessage, aliases);
public final String label;
protected VillagerOptimizerCommand(@NotNull String name) {
this.label = name;
}
public static void reloadCommands() {
@ -57,38 +52,17 @@ public abstract class VillagerOptimizerCommand extends Command
}
@Override
public @Nullable List<String> onTabComplete(
@NotNull CommandSender sender, @NotNull Command command, @NotNull String commandLabel, @NotNull String[] args
) {
return tabComplete(sender, commandLabel, args);
}
@Override
public boolean onCommand(
@NotNull CommandSender sender, @NotNull Command command, @NotNull String commandLabel, @NotNull String[] args
) {
return execute(sender, commandLabel, args);
}
@Override
public @NotNull Plugin getPlugin() {
return VillagerOptimizer.getInstance();
}
@Override
@SuppressWarnings({"deprecation", "DataFlowIssue"})
@SuppressWarnings("DataFlowIssue")
public void enable() {
VillagerOptimizer plugin = VillagerOptimizer.getInstance();
VillagerOptimizer.commandRegistration().getServerCommandMap()
.register(plugin.getDescription().getName().toLowerCase(), this);
plugin.getCommand(getName()).setExecutor(this);
plugin.getCommand(getName()).setTabCompleter(this);
PluginCommand pluginCommand = VillagerOptimizer.getInstance().getCommand(label);
pluginCommand.setExecutor(this);
pluginCommand.setTabCompleter(this);
}
@Override
@SuppressWarnings("DataFlowIssue")
public void disable() {
VillagerOptimizer.getInstance().getCommand(getName())
VillagerOptimizer.getInstance().getCommand(label)
.unregister(VillagerOptimizer.commandRegistration().getServerCommandMap());
}
}

View File

@ -13,15 +13,15 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.command.CommandException;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.Villager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@ -31,12 +31,7 @@ public class OptVillagersRadius extends VillagerOptimizerCommand {
private final int max_radius;
public OptVillagersRadius() {
super(
"optimizevillagers",
"Optmize villagers in a radius around you",
"/unoptimizevillagers <blockradius>",
Arrays.asList("optvils", "noai")
);
super("optimizevillagers");
Config config = VillagerOptimizer.config();
this.max_radius = config.getInt("optimization-methods.commands.optimizevillagers.max-block-radius", 100);
this.cooldown = config.getInt("optimization-methods.commands.optimizevillagers.cooldown-seconds", 600,
@ -45,14 +40,16 @@ public class OptVillagersRadius extends VillagerOptimizerCommand {
}
@Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args)
throws CommandException, IllegalArgumentException
{
public @Nullable List<String> onTabComplete(
@NotNull CommandSender sender, @NotNull Command command, @NotNull String commandLabel, @NotNull String[] args
) {
return args.length == 1 ? RADIUS_SUGGESTIONS : Collections.emptyList();
}
@Override
public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) {
public boolean onCommand(
@NotNull CommandSender sender, @NotNull Command command, @NotNull String commandLabel, @NotNull String[] args
) {
if (!sender.hasPermission(Permissions.Commands.OPTIMIZE_RADIUS.get())) {
KyoriUtil.sendMessage(sender, VillagerOptimizer.getLang(sender).no_permission);
return true;

View File

@ -12,15 +12,15 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.command.CommandException;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.Villager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@ -29,25 +29,22 @@ public class UnOptVillagersRadius extends VillagerOptimizerCommand {
private final int max_radius;
public UnOptVillagersRadius() {
super(
"unoptimizevillagers",
"Unoptmize villagers in a radius around you",
"/optimizevillagers <blockradius>",
Arrays.asList("unoptvils", "noaiundo")
);
super("unoptimizevillagers");
this.max_radius = VillagerOptimizer.config()
.getInt("optimization-methods.commands.unoptimizevillagers.max-block-radius", 100);
}
@Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args)
throws CommandException, IllegalArgumentException
{
public @Nullable List<String> onTabComplete(
@NotNull CommandSender sender, @NotNull Command command, @NotNull String commandLabel, @NotNull String[] args
) {
return args.length == 1 ? RADIUS_SUGGESTIONS : Collections.emptyList();
}
@Override
public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) {
public boolean onCommand(
@NotNull CommandSender sender, @NotNull Command command, @NotNull String commandLabel, @NotNull String[] args
) {
if (!sender.hasPermission(Permissions.Commands.UNOPTIMIZE_RADIUS.get())) {
KyoriUtil.sendMessage(sender, VillagerOptimizer.getLang(sender).no_permission);
return true;

View File

@ -10,9 +10,10 @@ import me.xginko.villageroptimizer.utils.KyoriUtil;
import me.xginko.villageroptimizer.utils.Util;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.command.CommandException;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.Collections;
@ -25,20 +26,15 @@ public class VillagerOptimizerCmd extends VillagerOptimizerCommand {
private final List<String> tabCompletes;
public VillagerOptimizerCmd() {
super(
"villageroptimizer",
"VillagerOptimizer admin commands",
"/villageroptimizer [ reload, version, disable ]",
Arrays.asList("voptimizer", "vo")
);
super("villageroptimizer");
subCommands = Arrays.asList(new ReloadSubCmd(), new VersionSubCmd(), new DisableSubCmd());
tabCompletes = subCommands.stream().map(SubCommand::label).collect(Collectors.toList());
}
@Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args)
throws CommandException, IllegalArgumentException
{
public @Nullable List<String> onTabComplete(
@NotNull CommandSender sender, @NotNull Command command, @NotNull String commandLabel, @NotNull String[] args
) {
if (args.length == 1) {
return tabCompletes;
}
@ -46,7 +42,7 @@ public class VillagerOptimizerCmd extends VillagerOptimizerCommand {
if (args.length >= 2) {
for (SubCommand subCommand : subCommands) {
if (args[0].equalsIgnoreCase(subCommand.label())) {
return subCommand.tabComplete(sender, alias, args);
return subCommand.onTabComplete(sender, command, commandLabel, args);
}
}
}
@ -55,11 +51,13 @@ public class VillagerOptimizerCmd extends VillagerOptimizerCommand {
}
@Override
public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) {
public boolean onCommand(
@NotNull CommandSender sender, @NotNull Command command, @NotNull String commandLabel, @NotNull String[] args
) {
if (args.length >= 1) {
for (SubCommand subCommand : subCommands) {
if (args[0].equalsIgnoreCase(subCommand.label())) {
return subCommand.execute(sender, commandLabel, args);
return subCommand.onCommand(sender, command, commandLabel, args);
}
}
}

View File

@ -4,14 +4,15 @@ import me.xginko.villageroptimizer.VillagerOptimizer;
import me.xginko.villageroptimizer.commands.SubCommand;
import me.xginko.villageroptimizer.enums.Permissions;
import me.xginko.villageroptimizer.modules.VillagerOptimizerModule;
import me.xginko.villageroptimizer.utils.Util;
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.CommandException;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collections;
import java.util.List;
@ -34,14 +35,16 @@ public class DisableSubCmd extends SubCommand {
}
@Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args)
throws CommandException, IllegalArgumentException
{
public @Nullable List<String> onTabComplete(
@NotNull CommandSender sender, @NotNull Command command, @NotNull String commandLabel, @NotNull String[] args
) {
return Collections.emptyList();
}
@Override
public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) {
public boolean onCommand(
@NotNull CommandSender sender, @NotNull Command command, @NotNull String commandLabel, @NotNull String[] args
) {
if (!sender.hasPermission(Permissions.Commands.DISABLE.get())) {
KyoriUtil.sendMessage(sender, VillagerOptimizer.getLang(sender).no_permission);
return true;

View File

@ -3,14 +3,15 @@ package me.xginko.villageroptimizer.commands.villageroptimizer.subcommands;
import me.xginko.villageroptimizer.VillagerOptimizer;
import me.xginko.villageroptimizer.commands.SubCommand;
import me.xginko.villageroptimizer.enums.Permissions;
import me.xginko.villageroptimizer.utils.Util;
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.CommandException;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collections;
import java.util.List;
@ -33,14 +34,16 @@ public class ReloadSubCmd extends SubCommand {
}
@Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args)
throws CommandException, IllegalArgumentException
{
public @Nullable List<String> onTabComplete(
@NotNull CommandSender sender, @NotNull Command command, @NotNull String commandLabel, @NotNull String[] args
) {
return Collections.emptyList();
}
@Override
public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) {
public boolean onCommand(
@NotNull CommandSender sender, @NotNull Command command, @NotNull String commandLabel, @NotNull String[] args
) {
if (!sender.hasPermission(Permissions.Commands.RELOAD.get())) {
KyoriUtil.sendMessage(sender, VillagerOptimizer.getLang(sender).no_permission);
return true;

View File

@ -4,16 +4,17 @@ import io.papermc.paper.plugin.configuration.PluginMeta;
import me.xginko.villageroptimizer.VillagerOptimizer;
import me.xginko.villageroptimizer.commands.SubCommand;
import me.xginko.villageroptimizer.enums.Permissions;
import me.xginko.villageroptimizer.utils.Util;
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.CommandException;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.PluginDescriptionFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collections;
import java.util.List;
@ -36,14 +37,16 @@ public class VersionSubCmd extends SubCommand {
}
@Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args)
throws CommandException, IllegalArgumentException
{
public @Nullable List<String> onTabComplete(
@NotNull CommandSender sender, @NotNull Command command, @NotNull String commandLabel, @NotNull String[] args
) {
return Collections.emptyList();
}
@Override
public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) {
public boolean onCommand(
@NotNull CommandSender sender, @NotNull Command command, @NotNull String commandLabel, @NotNull String[] args
) {
if (!sender.hasPermission(Permissions.Commands.VERSION.get())) {
KyoriUtil.sendMessage(sender, VillagerOptimizer.getLang(sender).no_permission);
return true;

View File

@ -6,3 +6,23 @@ description: ${project.description}
website: ${project.url}
api-version: '1.16'
folia-supported: true
commands:
villageroptimizer:
usage: /villageroptimizer [ reload, version, disable ]
description: VillagerOptimizer admin commands
aliases:
- voptimizer
- vo
optimizevillagers:
usage: /optimizevillagers <blockradius>
description: Optmize villagers in a radius around you
aliases:
- optvils
- noai
unoptimizevillagers:
usage: /unoptimizevillagers <blockradius>
description: Unoptmize villagers in a radius around you
aliases:
- unoptvils
- noaiundo