This commit is contained in:
boy0001
2015-07-27 23:10:14 +10:00
parent dce25ba07a
commit a39ec22c8c
87 changed files with 785 additions and 752 deletions

View File

@@ -1,7 +1,9 @@
package com.intellectualsites.commands;
import com.intellectualcrafters.plot.object.ConsolePlayer;
import com.intellectualcrafters.plot.object.PlotId;
import org.bukkit.entity.Player;
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.util.MainUtil;
public abstract class Argument<T> {
@@ -28,7 +30,7 @@ public abstract class Argument<T> {
return this.example;
}
public static final Argument<Integer> Integer = new Argument<java.lang.Integer>("int", 16) {
public static final Argument<Integer> Integer = new Argument<Integer>("int", 16) {
@Override
public Integer parse(String in) {
Integer value = null;
@@ -39,7 +41,7 @@ public abstract class Argument<T> {
}
};
public static final Argument<Boolean> Boolean = new Argument<java.lang.Boolean>("boolean", true) {
public static final Argument<Boolean> Boolean = new Argument<Boolean>("boolean", true) {
@Override
public Boolean parse(String in) {
Boolean value = null;
@@ -52,33 +54,31 @@ public abstract class Argument<T> {
}
};
public static final Argument<String> String = new Argument<java.lang.String>("String", "Example") {
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<java.lang.String>("PlayerName", "Dinnerbone") {
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<com.intellectualcrafters.plot.object.PlotId>("PlotID", new PlotId(3, -32)) {
public static Argument<PlotId> PlotID = new Argument<PlotId>("PlotID", new PlotId(-6, 3)) {
@Override
public PlotId parse(String in) {
PlotId plotID;
try {
String[] parts = in.split(";");
int i1 = java.lang.Integer.parseInt(parts[0]);
int i2 = java.lang.Integer.parseInt(parts[1]);
plotID = new PlotId(i1, i2);
} catch(final Exception e) {
return null;
}
return plotID;
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

@@ -7,10 +7,13 @@ import java.util.HashSet;
import java.util.Set;
import com.intellectualcrafters.plot.commands.CommandCategory;
import com.intellectualcrafters.plot.commands.RequiredType;
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotPlayer;
public abstract class Command extends CommandManager {
private Class requiredType = Object.class;
private RequiredType requiredType = RequiredType.NONE;
private String command, usage = "", description = "", permission = "";
private Set<String> aliases = new HashSet<>();
private CommandCategory category;
@@ -59,7 +62,7 @@ public abstract class Command extends CommandManager {
this.aliases = new HashSet<>(Arrays.asList(aliases));
}
public Command(String command, String usage, String description, String permission, String[] aliases, Class requiredType) {
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;
@@ -69,7 +72,7 @@ public abstract class Command extends CommandManager {
this.requiredType = requiredType;
}
final public Class getRequiredType() {
final public RequiredType getRequiredType() {
return this.requiredType;
}
@@ -93,23 +96,19 @@ public abstract class Command extends CommandManager {
final public String toString() {
return this.command;
}
@Override
final public int hashCode() {
return this.command.hashCode();
}
public abstract boolean onCommand(CommandCaller caller, String[] arguments);
public abstract boolean onCommand(PlotPlayer plr, String[] arguments);
final public int handle(CommandCaller caller, String[] args) {
final public int handle(PlotPlayer plr, String[] args) {
if (args.length == 0) {
return super.handle(caller, "");
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(caller, s);
return super.handle(plr, s);
}
final public String getCommand() {
@@ -139,4 +138,32 @@ public abstract class Command extends CommandManager {
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

@@ -1,19 +0,0 @@
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,6 +1,7 @@
package com.intellectualsites.commands;
import com.intellectualcrafters.plot.commands.CommandCategory;
import com.intellectualcrafters.plot.commands.RequiredType;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
@@ -21,7 +22,7 @@ public @interface CommandDeclaration {
String description() default "";
Class requiredType() default Object.class;
RequiredType requiredType() default RequiredType.NONE;
CommandCategory category();
}

View File

@@ -7,7 +7,11 @@ import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import com.intellectualsites.commands.util.StringUtil;
import com.intellectualcrafters.plot.commands.RequiredType;
import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.object.ConsolePlayer;
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.util.MainUtil;
@SuppressWarnings("unused")
public class CommandManager {
@@ -68,8 +72,8 @@ public class CommandManager {
return result;
}
public int handle(CommandCaller caller, String input) {
if (initialCharacter != null && !StringUtil.startsWith(initialCharacter, input)) {
public int handle(PlotPlayer plr, String input) {
if (initialCharacter != null && !input.startsWith(initialCharacter + "")) {
return CommandHandlingOutput.NOT_COMMAND;
}
input = initialCharacter == null ? input : input.substring(1);
@@ -87,10 +91,10 @@ public class CommandManager {
if (cmd == null) {
return CommandHandlingOutput.NOT_FOUND;
}
if (!cmd.getRequiredType().isInstance(caller.getSuperCaller())) {
if (!cmd.getRequiredType().allows(plr)) {
return CommandHandlingOutput.CALLER_OF_WRONG_TYPE;
}
if (!caller.hasPermission(cmd.getPermission())) {
if (!plr.hasPermission(cmd.getPermission())) {
return CommandHandlingOutput.NOT_PERMITTED;
}
Argument[] requiredArguments = cmd.getRequiredArguments();
@@ -108,16 +112,16 @@ public class CommandManager {
}
if (!success) {
String usage = cmd.getUsage();
caller.sendRequiredArgumentsList(this, cmd, requiredArguments);
C.COMMAND_SYNTAX.send(plr, cmd.getUsage());
return CommandHandlingOutput.WRONG_USAGE;
}
}
try {
boolean a = cmd.onCommand(caller, args);
boolean a = cmd.onCommand(plr, args);
if (!a) {
String usage = cmd.getUsage();
if (usage != null && !usage.isEmpty()) {
caller.message(usage);
MainUtil.sendMessage(plr, usage);
}
return CommandHandlingOutput.WRONG_USAGE;
}

View File

@@ -1,26 +0,0 @@
package com.intellectualsites.commands.util;
public class StringUtil {
public static final String[] emptyArray = new String[0];
public static boolean startsWith(char c, String s) {
return !(s == null || s.isEmpty()) && s.toCharArray()[0] == c;
}
public static boolean inArray(String s, String[] a, boolean matchCase) {
for (String aS : a) {
if (matchCase) {
if (s.equals(aS)) {
return true;
}
} else {
if (s.equalsIgnoreCase(aS)) {
return true;
}
}
}
return false;
}
}