Add your command caller back

This commit is contained in:
boy0001
2015-07-27 14:30:50 +10:00
parent 6df9024ff1
commit 56fbeb60e5
75 changed files with 152 additions and 125 deletions

View File

@@ -1,16 +1,18 @@
package com.intellectualsites.commands;
import com.intellectualcrafters.plot.commands.CommandCategory;
import com.intellectualsites.commands.callers.CommandCaller;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import com.intellectualcrafters.plot.commands.CommandCategory;
public abstract class Command extends CommandManager {
private Class requiredType = Object.class;
private String command, usage = "", description = "", permission = "";
private String[] aliases = new String[0];
private Set<String> aliases = new HashSet<>();
private CommandCategory category;
protected Argument[] requiredArguments;
@@ -47,14 +49,14 @@ public abstract class Command extends CommandManager {
public Command(String command, String[] aliases, String usage) {
super(null, new ArrayList<Command>());
this.command = command;
this.aliases = aliases;
this.aliases = new HashSet<>(Arrays.asList(aliases));
this.usage = usage;
}
public Command(String command, String[] aliases) {
super(null, new ArrayList<Command>());
this.command = command;
this.aliases = aliases;
this.aliases = new HashSet<>(Arrays.asList(aliases));
}
public Command(String command, String usage, String description, String permission, String[] aliases, Class requiredType) {
@@ -63,7 +65,7 @@ public abstract class Command extends CommandManager {
this.usage = usage;
this.description = description;
this.permission = permission;
this.aliases = aliases;
this.aliases = new HashSet<>(Arrays.asList(aliases));
this.requiredType = requiredType;
}
@@ -82,7 +84,7 @@ public abstract class Command extends CommandManager {
this.description = declaration.description();
this.usage = declaration.usage();
this.permission = declaration.permission();
this.aliases = declaration.aliases();
this.aliases = new HashSet<>(Arrays.asList(declaration.aliases()));
this.requiredType = declaration.requiredType();
this.category = declaration.category();
}
@@ -126,7 +128,7 @@ public abstract class Command extends CommandManager {
return this.description;
}
final public String[] getAliases() {
final public Set<String> getAliases() {
return this.aliases;
}

View File

@@ -0,0 +1,19 @@
package com.intellectualsites.commands;
import com.intellectualcrafters.plot.config.C;
import com.intellectualsites.commands.Argument;
import com.intellectualsites.commands.Command;
import com.intellectualsites.commands.CommandManager;
public interface CommandCaller<T> {
boolean hasPermission(String permission);
void message(String message);
T getSuperCaller();
void message(C c, String ... args);
void sendRequiredArgumentsList(CommandManager manager, Command cmd, Argument[] required);
}

View File

@@ -1,16 +1,16 @@
package com.intellectualsites.commands;
import com.intellectualsites.commands.callers.CommandCaller;
import com.intellectualsites.commands.util.StringUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import com.intellectualsites.commands.util.StringUtil;
@SuppressWarnings("unused")
public class CommandManager {
protected final List<Command> commands;
protected final ConcurrentHashMap<String, Command> commands;
protected final Character initialCharacter;
public CommandManager() {
@@ -18,12 +18,18 @@ public class CommandManager {
}
public CommandManager(Character initialCharacter, List<Command> commands) {
this.commands = Collections.synchronizedList(commands);
this.commands = new ConcurrentHashMap<>();
for (Command command : commands) {
addCommand(command);
}
this.initialCharacter = initialCharacter;
}
final public void addCommand(final Command command) {
this.commands.add(command);
this.commands.put(command.getCommand().toLowerCase(), command);
for (String alias : command.getAliases()) {
this.commands.put(alias.toLowerCase(), command);
}
}
final public boolean createCommand(final Command command) {
@@ -34,24 +40,24 @@ public class CommandManager {
return false;
}
if (command.getCommand() != null) {
commands.add(command);
addCommand(command);
return true;
}
return false;
}
final public List<Command> getCommands() {
return this.commands;
final public Collection<Command> getCommands() {
return this.commands.values();
}
public int handle(CommandCaller caller, String input) {
if (initialCharacter != null && !StringUtil.startsWith(initialCharacter, input)) {
return CommandHandlingOutput.NOT_COMMAND;
}
input = initialCharacter == null ? input : StringUtil.replaceFirst(initialCharacter, input);
input = initialCharacter == null ? input : input.substring(1);
String[] parts = input.split(" ");
String[] args;
String command = parts[0];
String command = parts[0].toLowerCase();
if (parts.length == 1) {
args = new String[0];
} else {
@@ -59,12 +65,7 @@ public class CommandManager {
System.arraycopy(parts, 1, args, 0, args.length);
}
Command cmd = null;
for (Command c1 : this.commands) {
if (c1.getCommand().equalsIgnoreCase(command) || StringUtil.inArray(command, c1.getAliases(), false)) {
cmd = c1;
break;
}
}
cmd = commands.get(command);
if (cmd == null) {
return CommandHandlingOutput.NOT_FOUND;
}

View File

@@ -8,32 +8,6 @@ public class StringUtil {
return !(s == null || s.isEmpty()) && s.toCharArray()[0] == c;
}
public static String replaceFirst(char c, String s) {
if (s == null) {
return "";
}
if (s.isEmpty()) {
return s;
}
char[] chars = s.toCharArray();
char[] newChars = new char[chars.length];
int used = 0;
boolean found = false;
for (char cc : chars) {
if (!found && c == cc) {
found = true;
} else {
newChars[used++] = cc;
}
}
if (found) {
chars = new char[newChars.length - 1];
System.arraycopy(newChars, 0, chars, 0, chars.length);
return String.valueOf(chars);
}
return s;
}
public static boolean inArray(String s, String[] a, boolean matchCase) {
for (String aS : a) {
if (matchCase) {