Better command validation, fixes #1895

This commit is contained in:
PikaMug 2022-03-13 05:15:08 -04:00
parent 77b79f0689
commit afcc682261
27 changed files with 134 additions and 111 deletions

View File

@ -37,6 +37,8 @@ public abstract class QuestsSubCommand {
public abstract String getSyntax();
public abstract int getMaxArguments();
public abstract void execute(CommandSender commandSender, String[] args);
public List<String> tabComplete(CommandSender commandSender, String[] args) {

View File

@ -69,6 +69,9 @@ public class QuestadminCommandHandler {
}
for (Map.Entry<String, QuestsSubCommand> cmd : subCommands.entrySet()) {
if (args[0].equalsIgnoreCase(cmd.getKey()) || args[0].equalsIgnoreCase(cmd.getValue().getNameI18N())) {
if (args.length < cmd.getValue().getMaxArguments()) {
cs.sendMessage(getAdminCommandUsage(args[0]));
}
cmd.getValue().execute(cs, args);
return true;
}
@ -179,4 +182,9 @@ public class QuestadminCommandHandler {
+ (translateSubCommands ? Lang.get("COMMAND_QUESTADMIN_RELOAD") : "reload") + ChatColor.RED));
}
}
private String getAdminCommandUsage(final String cmd) {
return ChatColor.RED + Lang.get("usage") + ": " + ChatColor.YELLOW + "/questadmin "
+ Lang.get(Lang.getKeyFromPrefix("COMMAND_QUESTADMIN_", cmd) + "_HELP");
}
}

View File

@ -61,6 +61,11 @@ public class QuestadminFinishCommand extends QuestsSubCommand {
return "/questadmin finish";
}
@Override
public int getMaxArguments() {
return 3;
}
@Override
public void execute(CommandSender cs, String[] args) {
if (cs.hasPermission("quests.admin.*") || cs.hasPermission("quests.admin.finish")) {

View File

@ -61,6 +61,11 @@ public class QuestadminGiveCommand extends QuestsSubCommand {
return "/questadmin give";
}
@Override
public int getMaxArguments() {
return 3;
}
@Override
public void execute(CommandSender cs, String[] args) {
if (cs.hasPermission("quests.admin.*") || cs.hasPermission("quests.admin.give")) {

View File

@ -59,6 +59,11 @@ public class QuestadminGivepointsCommand extends QuestsSubCommand {
return "/questadmin givepoints";
}
@Override
public int getMaxArguments() {
return 3;
}
@Override
public void execute(CommandSender cs, String[] args) {
if (cs.hasPermission("quests.admin.*") || cs.hasPermission("quests.admin.givepoints")) {

View File

@ -61,6 +61,11 @@ public class QuestadminNextstageCommand extends QuestsSubCommand {
return "/questadmin nextstage";
}
@Override
public int getMaxArguments() {
return 3;
}
@Override
public void execute(CommandSender cs, String[] args) {
if (cs.hasPermission("quests.admin.*") || cs.hasPermission("quests.admin.nextstage")) {

View File

@ -59,6 +59,11 @@ public class QuestadminPointsCommand extends QuestsSubCommand {
return "/questadmin points";
}
@Override
public int getMaxArguments() {
return 3;
}
@Override
public void execute(CommandSender cs, String[] args) {
if (cs.hasPermission("quests.admin.*") || cs.hasPermission("quests.admin.points")) {

View File

@ -59,6 +59,11 @@ public class QuestadminPointsallCommand extends QuestsSubCommand {
return "/questadmin pointsall";
}
@Override
public int getMaxArguments() {
return 2;
}
@Override
public void execute(CommandSender cs, String[] args) {
if (cs.hasPermission("quests.admin.*") || cs.hasPermission("quests.admin.pointsall")) {

View File

@ -61,6 +61,11 @@ public class QuestadminQuitCommand extends QuestsSubCommand {
return "/questadmin quit";
}
@Override
public int getMaxArguments() {
return 3;
}
@Override
public void execute(CommandSender cs, String[] args) {
if (cs.hasPermission("quests.admin.*") || cs.hasPermission("quests.admin.quit")) {

View File

@ -52,6 +52,11 @@ public class QuestadminReloadCommand extends QuestsSubCommand {
return "/questadmin reload";
}
@Override
public int getMaxArguments() {
return 1;
}
@Override
public void execute(CommandSender cs, String[] args) {
if (cs.hasPermission("quests.admin.*") || cs.hasPermission("quests.admin.reload")) {

View File

@ -60,6 +60,11 @@ public class QuestadminRemoveCommand extends QuestsSubCommand {
return "/questadmin remove";
}
@Override
public int getMaxArguments() {
return 3;
}
@Override
public void execute(CommandSender cs, String[] args) {
if (cs.hasPermission("quests.admin.*") && cs.hasPermission("quests.admin.remove")) {

View File

@ -61,6 +61,11 @@ public class QuestadminResetCommand extends QuestsSubCommand {
return "/questadmin reset";
}
@Override
public int getMaxArguments() {
return 2;
}
@Override
public void execute(CommandSender cs, String[] args) {
if (cs.hasPermission("quests.admin.*") || cs.hasPermission("quests.admin.reset")) {

View File

@ -61,6 +61,11 @@ public class QuestadminSetstageCommand extends QuestsSubCommand {
return "/questadmin setstage";
}
@Override
public int getMaxArguments() {
return 4;
}
@Override
public void execute(CommandSender cs, String[] args) {
if (cs.hasPermission("quests.admin.*") || cs.hasPermission("quests.admin.setstage")) {

View File

@ -60,6 +60,11 @@ public class QuestadminStatsCommand extends QuestsSubCommand {
return "/questadmin stats";
}
@Override
public int getMaxArguments() {
return 2;
}
@Override
public void execute(CommandSender cs, String[] args) {
if (cs.hasPermission("quests.admin.*") && cs.hasPermission("quests.admin.stats")) {

View File

@ -59,6 +59,11 @@ public class QuestadminTakepointsCommand extends QuestsSubCommand {
return "/questadmin takepoints";
}
@Override
public int getMaxArguments() {
return 3;
}
@Override
public void execute(CommandSender cs, String[] args) {
if (cs.hasPermission("quests.admin.*") || cs.hasPermission("quests.admin.takepoints")) {

View File

@ -64,6 +64,9 @@ public class QuestsCommandHandler {
}
for (Map.Entry<String, QuestsSubCommand> cmd : subCommands.entrySet()) {
if (args[0].equalsIgnoreCase(cmd.getKey()) || args[0].equalsIgnoreCase(cmd.getValue().getNameI18N())) {
if (args.length < cmd.getValue().getMaxArguments()) {
cs.sendMessage(getCommandUsage(args[0]));
}
cmd.getValue().execute(cs, args);
return true;
}
@ -114,4 +117,9 @@ public class QuestsCommandHandler {
+ Lang.get("COMMAND_QUESTADMIN_HELP"));
}
}
private String getCommandUsage(final String cmd) {
return ChatColor.RED + Lang.get("usage") + ": " + ChatColor.YELLOW + "/quests "
+ Lang.get(Lang.getKeyFromPrefix("COMMAND_", cmd) + "_HELP");
}
}

View File

@ -52,6 +52,11 @@ public class QuestsActionsCommand extends QuestsSubCommand {
return "/quests actions";
}
@Override
public int getMaxArguments() {
return 1;
}
@Override
public void execute(CommandSender cs, String[] args) {
if (cs.hasPermission("quests.events.*") || cs.hasPermission("quests.actions.*")

View File

@ -52,6 +52,11 @@ public class QuestsConditionsCommand extends QuestsSubCommand {
return "/quests conditions";
}
@Override
public int getMaxArguments() {
return 1;
}
@Override
public void execute(CommandSender cs, String[] args) {
if (cs.hasPermission("quests.conditions.*") || cs.hasPermission("quests.conditions.editor")

View File

@ -56,6 +56,11 @@ public class QuestsEditorCommand extends QuestsSubCommand {
return "/quests editor";
}
@Override
public int getMaxArguments() {
return 1;
}
@Override
public void execute(CommandSender cs, String[] args) {
if (cs.hasPermission("quests.editor.*") || cs.hasPermission("quests.editor.editor")

View File

@ -50,6 +50,11 @@ public class QuestsInfoCommand extends QuestsSubCommand {
return "/quests info";
}
@Override
public int getMaxArguments() {
return 1;
}
@Override
public void execute(CommandSender cs, String[] args) {
if (cs.hasPermission("quests.info")) {

View File

@ -58,6 +58,11 @@ public class QuestsJournalCommand extends QuestsSubCommand {
return "/quests journal";
}
@Override
public int getMaxArguments() {
return 1;
}
@SuppressWarnings("deprecation")
@Override
public void execute(CommandSender cs, String[] args) {

View File

@ -55,6 +55,11 @@ public class QuestsListCommand extends QuestsSubCommand {
return "/quests list";
}
@Override
public int getMaxArguments() {
return 1;
}
@Override
public void execute(CommandSender cs, String[] args) {
if (cs.hasPermission(getPermission())) {

View File

@ -58,6 +58,11 @@ public class QuestsQuitCommand extends QuestsSubCommand {
return "/quests quit";
}
@Override
public int getMaxArguments() {
return 2;
}
@Override
public void execute(CommandSender cs, String[] args) {
if (assertNonPlayer(cs)) {

View File

@ -56,6 +56,11 @@ public class QuestsStatsCommand extends QuestsSubCommand {
return "/quests stats";
}
@Override
public int getMaxArguments() {
return 1;
}
@Override
public void execute(CommandSender cs, String[] args) {
if (assertNonPlayer(cs)) {

View File

@ -58,6 +58,11 @@ public class QuestsTakeCommand extends QuestsSubCommand {
return "/quests take";
}
@Override
public int getMaxArguments() {
return 2;
}
@Override
public void execute(CommandSender cs, String[] args) {
if (assertNonPlayer(cs)) {

View File

@ -61,6 +61,11 @@ public class QuestsTopCommand extends QuestsSubCommand {
return "/quests top";
}
@Override
public int getMaxArguments() {
return 2;
}
@Override
public void execute(CommandSender cs, String[] args) {
if (cs.hasPermission(getPermission())) {

View File

@ -25,18 +25,13 @@ import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CommandManager implements TabExecutor {
private final Quests plugin;
private final Map<String, Integer> commandSizes = new HashMap<>();
private final Map<String, Integer> adminCommandSizes = new HashMap<>();
public CommandManager(final Quests plugin) {
this.plugin = plugin;
init();
}
@Override
@ -52,16 +47,11 @@ public class CommandManager implements TabExecutor {
return true;
}
}
final String error = validateCommand(cmd.getName(), args);
if (error != null) {
cs.sendMessage(error);
return true;
}
if (cmd.getName().equalsIgnoreCase("quest")) {
return new QuestCommandHandler(plugin).check(cs, args);
} else if (cmd.getName().equalsIgnoreCase("quests")) {
return new QuestsCommandHandler(plugin).check(cs, args);
} else if (cmd.getName().equalsIgnoreCase("questadmin")) {
} else if (cmd.getName().equalsIgnoreCase("questadmin") || cmd.getName().equalsIgnoreCase("questsadmin")) {
return new QuestadminCommandHandler(plugin).check(cs, args);
}
return false;
@ -82,104 +72,4 @@ public class CommandManager implements TabExecutor {
}
return null;
}
private void init() {
// [] - required
// {} - optional
if (plugin.getSettings().canTranslateSubCommands()) {
commandSizes.put(Lang.get("COMMAND_LIST"), 1); // list {page}
commandSizes.put(Lang.get("COMMAND_TAKE"), 2); // take [quest]
commandSizes.put(Lang.get("COMMAND_QUIT"), 2); // quit [quest]
commandSizes.put(Lang.get("COMMAND_EDITOR"), 1); // editor
commandSizes.put(Lang.get("COMMAND_EVENTS_EDITOR"), 1); // actions
commandSizes.put(Lang.get("COMMAND_CONDITIONS_EDITOR"), 1); // conditions
commandSizes.put(Lang.get("COMMAND_STATS"), 1); // stats
commandSizes.put(Lang.get("COMMAND_TOP"), 2); // top {number}
commandSizes.put(Lang.get("COMMAND_INFO"), 1); // info
commandSizes.put(Lang.get("COMMAND_JOURNAL"), 1); // journal
adminCommandSizes.put(Lang.get("COMMAND_QUESTADMIN_STATS"), 2); // stats [player]
adminCommandSizes.put(Lang.get("COMMAND_QUESTADMIN_GIVE"), 3); // give [player] [quest]
adminCommandSizes.put(Lang.get("COMMAND_QUESTADMIN_QUIT"), 3); // quit [player] [quest]
adminCommandSizes.put(Lang.get("COMMAND_QUESTADMIN_REMOVE"), 3); // remove [player] [quest]
adminCommandSizes.put(Lang.get("COMMAND_QUESTADMIN_POINTS"), 3); // points [player] [amount]
adminCommandSizes.put(Lang.get("COMMAND_QUESTADMIN_TAKEPOINTS"), 3); // takepoints [player] [amount]
adminCommandSizes.put(Lang.get("COMMAND_QUESTADMIN_GIVEPOINTS"), 3); // givepoints [player] [amount]
adminCommandSizes.put(Lang.get("COMMAND_QUESTADMIN_POINTSALL"), 2); // pointsall [amount]
adminCommandSizes.put(Lang.get("COMMAND_QUESTADMIN_FINISH"), 3); // finish [player] [quest]
adminCommandSizes.put(Lang.get("COMMAND_QUESTADMIN_NEXTSTAGE"), 3); // nextstage [player] [quest]
adminCommandSizes.put(Lang.get("COMMAND_QUESTADMIN_SETSTAGE"), 4); // setstage [player] [quest] [stage]
adminCommandSizes.put(Lang.get("COMMAND_QUESTADMIN_RESET"), 2); // reset [player]
adminCommandSizes.put(Lang.get("COMMAND_QUESTADMIN_RELOAD"), 1); // reload
} else {
commandSizes.put("list", 1); // list {page}
commandSizes.put("take", 2); // take [quest]
commandSizes.put("quit", 2); // quit [quest]
commandSizes.put("editor", 1); // editor
commandSizes.put("actions", 1); // actions
commandSizes.put("events", 1); // LEGACY - events
commandSizes.put("conditions", 1); // conditions
commandSizes.put("stats", 1); // stats
commandSizes.put("top", 2); // top [number]
commandSizes.put("info", 1); // info
commandSizes.put("journal", 1); // journal
adminCommandSizes.put("stats", 2); // stats [player]
adminCommandSizes.put("give", 3); // give [player] [quest]
adminCommandSizes.put("quit", 3); // quit [player] [quest]
adminCommandSizes.put("remove", 3); // remove [player] [quest]
adminCommandSizes.put("points", 3); // points [player] [amount]
adminCommandSizes.put("takepoints", 3); // takepoints [player] [amount]
adminCommandSizes.put("givepoints", 3); // givepoints [player] [amount]
adminCommandSizes.put("pointsall", 2); // pointsall [amount]
adminCommandSizes.put("finish", 3); // finish [player] [quest]
adminCommandSizes.put("nextstage", 3); // nextstage [player] [quest]
adminCommandSizes.put("setstage", 4); // setstage [player] [quest] [stage]
adminCommandSizes.put("reset", 2); // reset [player]
adminCommandSizes.put("reload", 1); // reload
}
}
public String validateCommand(final String cmd, final String[] args) {
if (cmd.equalsIgnoreCase("quest") || args.length == 0) {
return null;
}
if (cmd.equalsIgnoreCase("quests")) {
String subCmd = args[0].toLowerCase();
if (subCmd.equals("action")) {
subCmd = "actions";
} else if (subCmd.equals("condition")) {
subCmd = "conditions";
}
if (commandSizes.containsKey(subCmd)) {
final int min = commandSizes.get(subCmd);
if (args.length < min) {
return getQuestsCommandUsage(subCmd);
} else {
return null;
}
}
return ChatColor.YELLOW + Lang.get("questsUnknownCommand");
} else if (cmd.equalsIgnoreCase("questsadmin") || cmd.equalsIgnoreCase("questadmin")) {
final String subCmd = args[0].toLowerCase();
if (adminCommandSizes.containsKey(subCmd)) {
final int min = adminCommandSizes.get(subCmd);
if (args.length < min) {
return getQuestadminCommandUsage(subCmd);
} else {
return null;
}
}
return ChatColor.YELLOW + Lang.get("questsUnknownAdminCommand");
}
return "NULL";
}
public String getQuestsCommandUsage(final String cmd) {
return ChatColor.RED + Lang.get("usage") + ": " + ChatColor.YELLOW + "/quests "
+ Lang.get(Lang.getKeyFromPrefix("COMMAND_", cmd) + "_HELP");
}
public String getQuestadminCommandUsage(final String cmd) {
return ChatColor.RED + Lang.get("usage") + ": " + ChatColor.YELLOW + "/questadmin "
+ Lang.get(Lang.getKeyFromPrefix("COMMAND_QUESTADMIN_", cmd) + "_HELP");
}
}