2015-07-27 19:50:04 +02:00
|
|
|
package com.plotsquared.general.commands;
|
|
|
|
|
|
|
|
import com.intellectualcrafters.plot.commands.CommandCategory;
|
|
|
|
import com.intellectualcrafters.plot.commands.RequiredType;
|
2015-07-26 20:50:54 +02:00
|
|
|
|
|
|
|
import java.lang.annotation.Annotation;
|
|
|
|
import java.util.ArrayList;
|
2015-07-27 06:30:50 +02:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2015-07-27 17:14:38 +02:00
|
|
|
public abstract class Command<E extends CommandCaller> extends CommandManager {
|
2015-07-26 20:50:54 +02:00
|
|
|
|
2015-07-27 15:10:14 +02:00
|
|
|
private RequiredType requiredType = RequiredType.NONE;
|
2015-07-26 20:50:54 +02:00
|
|
|
private String command, usage = "", description = "", permission = "";
|
2015-07-27 06:30:50 +02:00
|
|
|
private Set<String> aliases = new HashSet<>();
|
2015-07-26 20:50:54 +02:00
|
|
|
private CommandCategory category;
|
2015-07-27 17:14:38 +02:00
|
|
|
protected Argument<?>[] requiredArguments;
|
2015-07-26 20:50:54 +02:00
|
|
|
|
|
|
|
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;
|
2015-07-27 06:30:50 +02:00
|
|
|
this.aliases = new HashSet<>(Arrays.asList(aliases));
|
2015-07-26 20:50:54 +02:00
|
|
|
this.usage = usage;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Command(String command, String[] aliases) {
|
|
|
|
super(null, new ArrayList<Command>());
|
|
|
|
this.command = command;
|
2015-07-27 06:30:50 +02:00
|
|
|
this.aliases = new HashSet<>(Arrays.asList(aliases));
|
2015-07-26 20:50:54 +02:00
|
|
|
}
|
|
|
|
|
2015-07-27 15:10:14 +02:00
|
|
|
public Command(String command, String usage, String description, String permission, String[] aliases, RequiredType requiredType) {
|
2015-07-26 20:50:54 +02:00
|
|
|
super(null, new ArrayList<Command>());
|
|
|
|
this.command = command;
|
|
|
|
this.usage = usage;
|
|
|
|
this.description = description;
|
|
|
|
this.permission = permission;
|
2015-07-27 06:30:50 +02:00
|
|
|
this.aliases = new HashSet<>(Arrays.asList(aliases));
|
2015-07-26 20:50:54 +02:00
|
|
|
this.requiredType = requiredType;
|
|
|
|
}
|
|
|
|
|
2015-07-27 15:10:14 +02:00
|
|
|
final public RequiredType getRequiredType() {
|
2015-07-26 20:50:54 +02:00
|
|
|
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();
|
2015-07-27 06:30:50 +02:00
|
|
|
this.aliases = new HashSet<>(Arrays.asList(declaration.aliases()));
|
2015-07-26 20:50:54 +02:00
|
|
|
this.requiredType = declaration.requiredType();
|
|
|
|
this.category = declaration.category();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
final public String toString() {
|
|
|
|
return this.command;
|
|
|
|
}
|
|
|
|
|
2015-07-27 17:14:38 +02:00
|
|
|
public abstract boolean onCommand(E plr, String[] arguments);
|
2015-07-26 20:50:54 +02:00
|
|
|
|
2015-07-27 17:14:38 +02:00
|
|
|
final public int handle(E plr, String[] args) {
|
2015-07-26 20:50:54 +02:00
|
|
|
if (args.length == 0) {
|
2015-07-27 15:10:14 +02:00
|
|
|
return super.handle(plr, "");
|
2015-07-26 20:50:54 +02:00
|
|
|
}
|
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
for (String s : args) {
|
|
|
|
builder.append(s).append(" ");
|
|
|
|
}
|
|
|
|
String s = builder.substring(0, builder.length() - 1);
|
2015-07-27 15:10:14 +02:00
|
|
|
return super.handle(plr, s);
|
2015-07-26 20:50:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
final public String getCommand() {
|
|
|
|
return this.command;
|
|
|
|
}
|
|
|
|
|
|
|
|
final public String getUsage() {
|
2015-07-27 19:28:39 +02:00
|
|
|
if (this.usage.length() == 0) {
|
|
|
|
return "/{label} " + command;
|
|
|
|
}
|
2015-07-26 20:50:54 +02:00
|
|
|
return this.usage;
|
|
|
|
}
|
|
|
|
|
|
|
|
final public String getPermission() {
|
|
|
|
return this.permission;
|
|
|
|
}
|
|
|
|
|
|
|
|
final public String getDescription() {
|
|
|
|
return this.description;
|
|
|
|
}
|
|
|
|
|
2015-07-27 06:30:50 +02:00
|
|
|
final public Set<String> getAliases() {
|
2015-07-26 20:50:54 +02:00
|
|
|
return this.aliases;
|
|
|
|
}
|
|
|
|
|
2015-07-27 17:14:38 +02:00
|
|
|
final public Argument<?>[] getRequiredArguments() {
|
2015-07-26 20:50:54 +02:00
|
|
|
return this.requiredArguments;
|
|
|
|
}
|
|
|
|
|
|
|
|
final public CommandCategory getCategory() {
|
|
|
|
return this.category;
|
|
|
|
}
|
2015-07-27 15:10:14 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
if (this == obj) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (obj == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (getClass() != obj.getClass()) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-07-27 17:14:38 +02:00
|
|
|
final Command<?> other = (Command<?>) obj;
|
2015-07-27 15:10:14 +02:00
|
|
|
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;
|
|
|
|
}
|
2015-07-26 20:50:54 +02:00
|
|
|
}
|