From 03ebe22352f88c1d8d4872351a788f74fb5c1fb0 Mon Sep 17 00:00:00 2001 From: Brianna O'Keefe Date: Wed, 27 Feb 2019 09:46:14 -0500 Subject: [PATCH] Fixed command manager. --- .../command/AbstractCommand.java | 14 +++++++++- .../command/CommandManager.java | 26 +++++++++---------- .../command/commands/CommandClearChat.java | 7 +++-- .../command/commands/CommandRandomPlayer.java | 2 +- .../command/commands/CommandToggleChat.java | 2 +- .../commands/CommandUltimateModeration.java | 4 +-- .../command/commands/CommandVanish.java | 2 +- 7 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/songoda/ultimatemoderation/command/AbstractCommand.java b/src/main/java/com/songoda/ultimatemoderation/command/AbstractCommand.java index ace52c0..5ffa88e 100644 --- a/src/main/java/com/songoda/ultimatemoderation/command/AbstractCommand.java +++ b/src/main/java/com/songoda/ultimatemoderation/command/AbstractCommand.java @@ -9,8 +9,9 @@ import java.util.List; public abstract class AbstractCommand { - private final AbstractCommand parent; + private AbstractCommand parent = null; private final boolean noConsole; + private boolean hasArgs = false; private String command; private List subCommand = new ArrayList<>(); @@ -25,6 +26,13 @@ public abstract class AbstractCommand { this.noConsole = noConsole; } + protected AbstractCommand(boolean noConsole, boolean hasArgs, String... command) { + this.command = Arrays.asList(command).get(0); + + this.hasArgs = hasArgs; + this.noConsole = noConsole; + } + public AbstractCommand getParent() { return parent; } @@ -51,6 +59,10 @@ public abstract class AbstractCommand { public abstract String getDescription(); + public boolean hasArgs() { + return hasArgs; + } + public boolean isNoConsole() { return noConsole; } diff --git a/src/main/java/com/songoda/ultimatemoderation/command/CommandManager.java b/src/main/java/com/songoda/ultimatemoderation/command/CommandManager.java index f49f7ce..4622e85 100644 --- a/src/main/java/com/songoda/ultimatemoderation/command/CommandManager.java +++ b/src/main/java/com/songoda/ultimatemoderation/command/CommandManager.java @@ -1,8 +1,10 @@ package com.songoda.ultimatemoderation.command; +import com.songoda.epicspawners.References; import com.songoda.ultimatemoderation.UltimateModeration; import com.songoda.ultimatemoderation.command.commands.*; import com.songoda.ultimatemoderation.utils.Methods; +import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; @@ -52,22 +54,20 @@ public class CommandManager implements CommandExecutor { @Override public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) { for (AbstractCommand abstractCommand : commands) { - if (abstractCommand.getCommand() == null) - continue; - if (!abstractCommand.getCommand().equalsIgnoreCase(command.getName())) return false; - - if (strings.length == 0) { - processRequirements(abstractCommand, commandSender, strings); - return true; - } - - String cmd = strings[0]; - String cmd2 = strings.length >= 2 ? String.join(" ", strings[0], strings[1]) : null; - for (String sub : abstractCommand.getSubCommand()) { - if (cmd.equalsIgnoreCase(sub) || (cmd2 != null && cmd2.equalsIgnoreCase(sub))) { + if (abstractCommand.getCommand() != null && abstractCommand.getCommand().equalsIgnoreCase(command.getName().toLowerCase())) { + if (strings.length == 0 || abstractCommand.hasArgs()) { processRequirements(abstractCommand, commandSender, strings); return true; } + } else if (strings.length != 0 && abstractCommand.getParent() != null && abstractCommand.getParent().getCommand().equalsIgnoreCase(command.getName())) { + String cmd = strings[0]; + String cmd2 = strings.length >= 2 ? String.join(" ", strings[0], strings[1]) : null; + for (String cmds : abstractCommand.getSubCommand()) { + if (cmd.equalsIgnoreCase(cmds) || (cmd2 != null && cmd2.equalsIgnoreCase(cmds))) { + processRequirements(abstractCommand, commandSender, strings); + return true; + } + } } } commandSender.sendMessage(instance.getReferences().getPrefix() + Methods.formatText("&7The command you entered does not exist or is spelt incorrectly.")); diff --git a/src/main/java/com/songoda/ultimatemoderation/command/commands/CommandClearChat.java b/src/main/java/com/songoda/ultimatemoderation/command/commands/CommandClearChat.java index 30a025a..c36faee 100644 --- a/src/main/java/com/songoda/ultimatemoderation/command/commands/CommandClearChat.java +++ b/src/main/java/com/songoda/ultimatemoderation/command/commands/CommandClearChat.java @@ -13,12 +13,15 @@ import java.util.List; public class CommandClearChat extends AbstractCommand { public CommandClearChat() { - super(null, true, "ClearChat"); - addSubCommand("force"); + super(true, true,"ClearChat"); } @Override protected ReturnType runCommand(UltimateModeration instance, CommandSender sender, String... args) { + + if (args.length != 0 && !args[0].equalsIgnoreCase("force")) + return ReturnType.SYNTAX_ERROR; + for (Player player : Bukkit.getOnlinePlayers()) { if (!player.hasPermission("um.clearchat.bypass") || isForced(args)) { String[] toSend = new String[250]; diff --git a/src/main/java/com/songoda/ultimatemoderation/command/commands/CommandRandomPlayer.java b/src/main/java/com/songoda/ultimatemoderation/command/commands/CommandRandomPlayer.java index 2ed391d..8d6a643 100644 --- a/src/main/java/com/songoda/ultimatemoderation/command/commands/CommandRandomPlayer.java +++ b/src/main/java/com/songoda/ultimatemoderation/command/commands/CommandRandomPlayer.java @@ -12,7 +12,7 @@ import java.util.*; public class CommandRandomPlayer extends AbstractCommand { public CommandRandomPlayer() { - super(null, true, "RandomPlayer"); + super(true, false,"RandomPlayer"); } @Override diff --git a/src/main/java/com/songoda/ultimatemoderation/command/commands/CommandToggleChat.java b/src/main/java/com/songoda/ultimatemoderation/command/commands/CommandToggleChat.java index a6868ae..9a7c1c0 100644 --- a/src/main/java/com/songoda/ultimatemoderation/command/commands/CommandToggleChat.java +++ b/src/main/java/com/songoda/ultimatemoderation/command/commands/CommandToggleChat.java @@ -19,7 +19,7 @@ public class CommandToggleChat extends AbstractCommand { private boolean toggled = true; public CommandToggleChat() { - super(null, false, "togglechat"); + super(false, false, "togglechat"); } @Override diff --git a/src/main/java/com/songoda/ultimatemoderation/command/commands/CommandUltimateModeration.java b/src/main/java/com/songoda/ultimatemoderation/command/commands/CommandUltimateModeration.java index 9eddf9c..061ff1a 100644 --- a/src/main/java/com/songoda/ultimatemoderation/command/commands/CommandUltimateModeration.java +++ b/src/main/java/com/songoda/ultimatemoderation/command/commands/CommandUltimateModeration.java @@ -10,9 +10,7 @@ import java.util.List; public class CommandUltimateModeration extends AbstractCommand { public CommandUltimateModeration() { - super(null, false, "UltimateModeration"); - addSubCommand("reload"); - addSubCommand("settings"); + super(false, false, "UltimateModeration"); } @Override diff --git a/src/main/java/com/songoda/ultimatemoderation/command/commands/CommandVanish.java b/src/main/java/com/songoda/ultimatemoderation/command/commands/CommandVanish.java index 1252ce9..edee7bb 100644 --- a/src/main/java/com/songoda/ultimatemoderation/command/commands/CommandVanish.java +++ b/src/main/java/com/songoda/ultimatemoderation/command/commands/CommandVanish.java @@ -22,7 +22,7 @@ public class CommandVanish extends AbstractCommand { private static List inVanish = new ArrayList<>(); public CommandVanish() { - super(null, true, "Vanish"); + super(true, false, "Vanish"); } @Override