-m \'So I hear you like commits ...\'

This commit is contained in:
Sauilitired
2015-07-27 19:50:04 +02:00
parent e50605d69a
commit 3a15254d8d
230 changed files with 1217 additions and 2125 deletions

View File

@ -0,0 +1,84 @@
package com.plotsquared.general.commands;
import com.intellectualcrafters.plot.object.ConsolePlayer;
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotId;
import com.intellectualcrafters.plot.util.MainUtil;
public abstract class Argument<T> {
private final String name;
private final T example;
public Argument(String name, T example) {
this.name = name;
this.example = example;
}
public abstract T parse(String in);
@Override
public final String toString() {
return this.getName();
}
public final String getName() {
return this.name;
}
public final T getExample() {
return this.example;
}
public static final Argument<Integer> Integer = new Argument<Integer>("int", 16) {
@Override
public Integer parse(String in) {
Integer value = null;
try {
value = java.lang.Integer.parseInt(in);
} catch(final Exception ignored) {}
return value;
}
};
public static final Argument<Boolean> Boolean = new Argument<Boolean>("boolean", true) {
@Override
public Boolean parse(String in) {
Boolean value = null;
if (in.equalsIgnoreCase("true") || in.equalsIgnoreCase("Yes") || in.equalsIgnoreCase("1")) {
value = true;
} else if (in.equalsIgnoreCase("false") || in.equalsIgnoreCase("No") || in.equalsIgnoreCase("0")) {
value = false;
}
return value;
}
};
public static final Argument<String> String = new Argument<String>("String", "Example") {
@Override
public String parse(String in) {
return in;
}
};
public static Argument<String> PlayerName = new Argument<String>("PlayerName", "Dinnerbone") {
@Override
public String parse(String in) {
return in.length() < 16 ? in : null;
}
};
public static Argument<PlotId> PlotID = new Argument<PlotId>("PlotID", new PlotId(-6, 3)) {
@Override
public PlotId parse(String in) {
return PlotId.fromString(in);
}
};
public static Argument<Plot> Plot = new Argument<Plot>("Plot", new Plot("plotworld", new PlotId(3, -6), null)) {
@Override
public Plot parse(String in) {
return MainUtil.getPlotFromString(ConsolePlayer.getConsole(), in, false);
}
};
}

View File

@ -0,0 +1,170 @@
package com.plotsquared.general.commands;
import com.intellectualcrafters.plot.commands.CommandCategory;
import com.intellectualcrafters.plot.commands.RequiredType;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public abstract class Command<E extends CommandCaller> extends CommandManager {
private RequiredType requiredType = RequiredType.NONE;
private String command, usage = "", description = "", permission = "";
private Set<String> aliases = new HashSet<>();
private CommandCategory category;
protected Argument<?>[] requiredArguments;
public Command() {
super(null, new ArrayList<Command>());
}
public Command(String command) {
super(null, new ArrayList<Command>());
this.command = command;
}
public Command(String command, String usage) {
super(null, new ArrayList<Command>());
this.command = command;
this.usage = usage;
}
public Command(String command, String usage, String description) {
super(null, new ArrayList<Command>());
this.command = command;
this.usage = usage;
this.description = description;
}
public Command(String command, String usage, String description, String permission) {
super(null, new ArrayList<Command>());
this.command = command;
this.usage = usage;
this.description = description;
this.permission = permission;
}
public Command(String command, String[] aliases, String usage) {
super(null, new ArrayList<Command>());
this.command = command;
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 = new HashSet<>(Arrays.asList(aliases));
}
public Command(String command, String usage, String description, String permission, String[] aliases, RequiredType requiredType) {
super(null, new ArrayList<Command>());
this.command = command;
this.usage = usage;
this.description = description;
this.permission = permission;
this.aliases = new HashSet<>(Arrays.asList(aliases));
this.requiredType = requiredType;
}
final public RequiredType getRequiredType() {
return this.requiredType;
}
final protected void create() {
Annotation annotation = getClass().getAnnotation(CommandDeclaration.class);
if (annotation == null) {
throw new RuntimeException("Command does not have a CommandDeclaration");
}
CommandDeclaration declaration = (CommandDeclaration) annotation;
this.command = declaration.command();
this.usage = declaration.usage();
this.description = declaration.description();
this.usage = declaration.usage();
this.permission = declaration.permission();
this.aliases = new HashSet<>(Arrays.asList(declaration.aliases()));
this.requiredType = declaration.requiredType();
this.category = declaration.category();
}
@Override
final public String toString() {
return this.command;
}
public abstract boolean onCommand(E plr, String[] arguments);
final public int handle(E plr, String[] args) {
if (args.length == 0) {
return super.handle(plr, "");
}
StringBuilder builder = new StringBuilder();
for (String s : args) {
builder.append(s).append(" ");
}
String s = builder.substring(0, builder.length() - 1);
return super.handle(plr, s);
}
final public String getCommand() {
return this.command;
}
final public String getUsage() {
if (this.usage.length() == 0) {
return "/{label} " + command;
}
return this.usage;
}
final public String getPermission() {
return this.permission;
}
final public String getDescription() {
return this.description;
}
final public Set<String> getAliases() {
return this.aliases;
}
final public Argument<?>[] getRequiredArguments() {
return this.requiredArguments;
}
final public CommandCategory getCategory() {
return this.category;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Command<?> other = (Command<?>) obj;
if (this.hashCode() != other.hashCode()) {
return false;
}
return this.command.equals(other.command);
}
private int hash;
@Override
public int hashCode() {
if (hash == 0) {
hash = getCommand().hashCode();
}
return hash;
}
}

View File

@ -0,0 +1,14 @@
package com.plotsquared.general.commands;
import com.intellectualcrafters.plot.commands.RequiredType;
import com.intellectualcrafters.plot.config.C;
public interface CommandCaller {
void sendMessage(final String message);
void sendMessage(C c, String ... args);
boolean hasPermission(final String perm);
RequiredType getSuperCaller();
}

View File

@ -0,0 +1,28 @@
package com.plotsquared.general.commands;
import com.intellectualcrafters.plot.commands.CommandCategory;
import com.intellectualcrafters.plot.commands.RequiredType;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface CommandDeclaration {
String command();
String[] aliases() default {};
String permission() default "";
String usage() default "";
String description() default "";
RequiredType requiredType() default RequiredType.NONE;
CommandCategory category();
}

View File

@ -0,0 +1,31 @@
package com.plotsquared.general.commands;
import java.lang.reflect.Field;
public class CommandHandlingOutput {
public static int CALLER_OF_WRONG_TYPE = -6;
public static int NOT_COMMAND = -5;
public static int NOT_FOUND = -4;
public static int NOT_PERMITTED = -3;
public static int ERROR = -2;
public static int WRONG_USAGE = -1;
public static int SUCCESS = 1;
public static String nameField(int code) {
Field[] fields = CommandHandlingOutput.class.getDeclaredFields();
for (final Field field : fields) {
if (field.getGenericType() == Integer.TYPE) {
try {
if ((Integer) field.get(CommandHandlingOutput.class) == code) {
return field.getName();
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
return "null??";
}
}

View File

@ -0,0 +1,141 @@
package com.plotsquared.general.commands;
import com.intellectualcrafters.plot.config.C;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
@SuppressWarnings("unused")
public class CommandManager<T extends CommandCaller> {
protected final ConcurrentHashMap<String, Command<T>> commands;
protected final Character initialCharacter;
public CommandManager() {
this('/', new ArrayList<Command<T>>());
}
public CommandManager(Character initialCharacter, List<Command<T>> commands) {
this.commands = new ConcurrentHashMap<>();
for (Command<T> command : commands) {
addCommand(command);
}
this.initialCharacter = initialCharacter;
}
final public void addCommand(final Command<T> 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<T> command) {
try {
command.create();
} catch(final Exception e) {
e.printStackTrace();
return false;
}
if (command.getCommand() != null) {
addCommand(command);
return true;
}
return false;
}
final public ArrayList<Command<T>> getCommands() {
ArrayList<Command<T>> result = new ArrayList<>(this.commands.values());
Collections.sort(result, new Comparator<Command<T>>() {
@Override
public int compare(Command<T> a, Command<T> b) {
if (a == b) {
return 0;
}
if (a == null) {
return -1;
}
if (b == null) {
return 1;
}
return a.getCommand().compareTo(b.getCommand());
}
});
return result;
}
final public ArrayList<String> getCommandLabels(ArrayList<Command<T>> cmds) {
ArrayList<String> labels = new ArrayList<>(cmds.size());
for (Command<T> cmd : cmds) {
labels.add(cmd.getCommand());
}
return labels;
}
public int handle(T plr, String input) {
if (initialCharacter != null && !input.startsWith(initialCharacter + "")) {
return CommandHandlingOutput.NOT_COMMAND;
}
input = initialCharacter == null ? input : input.substring(1);
String[] parts = input.split(" ");
String[] args;
String command = parts[0].toLowerCase();
if (parts.length == 1) {
args = new String[0];
} else {
args = new String[parts.length - 1];
System.arraycopy(parts, 1, args, 0, args.length);
}
Command<T> cmd = null;
cmd = commands.get(command);
if (cmd == null) {
return CommandHandlingOutput.NOT_FOUND;
}
if (!cmd.getRequiredType().allows(plr)) {
return CommandHandlingOutput.CALLER_OF_WRONG_TYPE;
}
if (!plr.hasPermission(cmd.getPermission())) {
return CommandHandlingOutput.NOT_PERMITTED;
}
Argument<?>[] requiredArguments = cmd.getRequiredArguments();
if (requiredArguments != null && requiredArguments.length > 0) {
boolean success = true;
if (args.length < requiredArguments.length) {
success = false;
} else {
for (int i = 0; i < requiredArguments.length; i++) {
if (requiredArguments[i].parse(args[i]) == null) {
success = false;
break;
}
}
}
if (!success) {
String usage = cmd.getUsage();
C.COMMAND_SYNTAX.send(plr, cmd.getUsage());
return CommandHandlingOutput.WRONG_USAGE;
}
}
try {
boolean a = cmd.onCommand(plr, args);
if (!a) {
String usage = cmd.getUsage();
if (usage != null && !usage.isEmpty()) {
plr.sendMessage(usage);
}
return CommandHandlingOutput.WRONG_USAGE;
}
} catch(final Throwable t) {
t.printStackTrace();
return CommandHandlingOutput.ERROR;
}
return CommandHandlingOutput.SUCCESS;
}
final public char getInitialCharacter() {
return this.initialCharacter;
}
}