Fixed command manager.

This commit is contained in:
Brianna O'Keefe 2019-02-27 09:46:14 -05:00
parent abfa0b9cee
commit 03ebe22352
7 changed files with 35 additions and 22 deletions

View File

@ -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<String> 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;
}

View File

@ -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."));

View File

@ -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];

View File

@ -12,7 +12,7 @@ import java.util.*;
public class CommandRandomPlayer extends AbstractCommand {
public CommandRandomPlayer() {
super(null, true, "RandomPlayer");
super(true, false,"RandomPlayer");
}
@Override

View File

@ -19,7 +19,7 @@ public class CommandToggleChat extends AbstractCommand {
private boolean toggled = true;
public CommandToggleChat() {
super(null, false, "togglechat");
super(false, false, "togglechat");
}
@Override

View File

@ -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

View File

@ -22,7 +22,7 @@ public class CommandVanish extends AbstractCommand {
private static List<UUID> inVanish = new ArrayList<>();
public CommandVanish() {
super(null, true, "Vanish");
super(true, false, "Vanish");
}
@Override