new-1.1
This commit is contained in:
parent
c6b53cb30b
commit
601f3e2ea5
3
pom.xml
3
pom.xml
|
@ -26,6 +26,9 @@
|
||||||
<configuration>
|
<configuration>
|
||||||
<source>${java.version}</source>
|
<source>${java.version}</source>
|
||||||
<target>${java.version}</target>
|
<target>${java.version}</target>
|
||||||
|
<compilerArgs>
|
||||||
|
<arg>-parameters</arg>
|
||||||
|
</compilerArgs>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
|
|
|
@ -4,6 +4,7 @@ import com.io.yutian.elementoriginlib.lang.Lang;
|
||||||
import com.io.yutian.elementoriginlib.listener.GuiHandlerListener;
|
import com.io.yutian.elementoriginlib.listener.GuiHandlerListener;
|
||||||
import com.io.yutian.elementoriginlib.listener.PlayerChatInputListener;
|
import com.io.yutian.elementoriginlib.listener.PlayerChatInputListener;
|
||||||
import com.io.yutian.elementoriginlib.logger.Logger;
|
import com.io.yutian.elementoriginlib.logger.Logger;
|
||||||
|
import com.io.yutian.elementoriginlib.manager.CommandManager;
|
||||||
import com.io.yutian.elementoriginlib.redis.RedisIO;
|
import com.io.yutian.elementoriginlib.redis.RedisIO;
|
||||||
import net.byteflux.libby.*;
|
import net.byteflux.libby.*;
|
||||||
import net.byteflux.libby.logging.LogLevel;
|
import net.byteflux.libby.logging.LogLevel;
|
||||||
|
@ -19,6 +20,8 @@ public final class ElementOriginLib extends JavaPlugin {
|
||||||
|
|
||||||
private static RedisIO redisIO;
|
private static RedisIO redisIO;
|
||||||
|
|
||||||
|
private static CommandManager commandManager;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
instance = this;
|
instance = this;
|
||||||
|
@ -28,6 +31,9 @@ public final class ElementOriginLib extends JavaPlugin {
|
||||||
new GuiHandlerListener(this);
|
new GuiHandlerListener(this);
|
||||||
new PlayerChatInputListener(this);
|
new PlayerChatInputListener(this);
|
||||||
|
|
||||||
|
commandManager = new CommandManager(this);
|
||||||
|
commandManager.registerPluginCommand("elementoriginlib");
|
||||||
|
|
||||||
Lang.registerLangFile(this);
|
Lang.registerLangFile(this);
|
||||||
Lang.reload();
|
Lang.reload();
|
||||||
|
|
||||||
|
|
|
@ -1,8 +0,0 @@
|
||||||
package com.io.yutian.elementoriginlib.command;
|
|
||||||
|
|
||||||
@FunctionalInterface
|
|
||||||
public interface Command<S extends CommandContext> {
|
|
||||||
|
|
||||||
void run(S context);
|
|
||||||
|
|
||||||
}
|
|
|
@ -7,18 +7,25 @@ import java.util.Map;
|
||||||
|
|
||||||
public class CommandContext {
|
public class CommandContext {
|
||||||
|
|
||||||
|
private Object commandInstance;
|
||||||
|
|
||||||
private String command;
|
private String command;
|
||||||
private String label;
|
private String label;
|
||||||
private CommandSender sender;
|
private CommandSender sender;
|
||||||
private Map<String, ArgumentValue> argumentsValues;
|
private Map<String, ArgumentValue> argumentsValues;
|
||||||
|
|
||||||
public CommandContext(String command, String label, CommandSender sender, Map<String, ArgumentValue> argumentsValues) {
|
public CommandContext(Object commandInstance, String command, String label, CommandSender sender, Map<String, ArgumentValue> argumentsValues) {
|
||||||
|
this.commandInstance = commandInstance;
|
||||||
this.command = command;
|
this.command = command;
|
||||||
this.label = label;
|
this.label = label;
|
||||||
this.sender = sender;
|
this.sender = sender;
|
||||||
this.argumentsValues = argumentsValues;
|
this.argumentsValues = argumentsValues;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Object getCommandInstance() {
|
||||||
|
return commandInstance;
|
||||||
|
}
|
||||||
|
|
||||||
public String getCommand() {
|
public String getCommand() {
|
||||||
return command;
|
return command;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,225 @@
|
||||||
|
package com.io.yutian.elementoriginlib.command;
|
||||||
|
|
||||||
|
import com.io.yutian.elementoriginlib.command.argument.Argument;
|
||||||
|
import com.io.yutian.elementoriginlib.command.argument.ArgumentType;
|
||||||
|
import com.io.yutian.elementoriginlib.command.interfaces.Command;
|
||||||
|
import com.io.yutian.elementoriginlib.command.interfaces.CommandArgument;
|
||||||
|
import com.io.yutian.elementoriginlib.command.interfaces.SubCommand;
|
||||||
|
import com.io.yutian.elementoriginlib.exception.command.CommandRegisterException;
|
||||||
|
import com.io.yutian.elementoriginlib.logger.Logger;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.lang.reflect.Parameter;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class CommandEntity {
|
||||||
|
|
||||||
|
private static final Logger LOGGER = Logger.getLogger(CommandEntity.class);
|
||||||
|
|
||||||
|
private Object instance;
|
||||||
|
|
||||||
|
private String command;
|
||||||
|
private String permission;
|
||||||
|
|
||||||
|
private List<CommandEntry> childrens = new ArrayList<>();
|
||||||
|
|
||||||
|
public CommandEntity(Object instance, String command, String permission, List<CommandEntry> childrens) {
|
||||||
|
this.instance = instance;
|
||||||
|
this.command = command;
|
||||||
|
this.permission = permission;
|
||||||
|
this.childrens = childrens;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object getInstance() {
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addChild(CommandEntry child) {
|
||||||
|
childrens.add(child);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCommand() {
|
||||||
|
return command;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPermission() {
|
||||||
|
return permission;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<CommandEntry> getChildrens() {
|
||||||
|
return childrens;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CommandEntity parseFromClass(Class clazz) {
|
||||||
|
if (clazz == null) {
|
||||||
|
LOGGER.warn("Class is null");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (!clazz.isAnnotationPresent(Command.class)) {
|
||||||
|
LOGGER.warn("Class " + clazz.getName() + " is not annotated with @Command");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Object instance;
|
||||||
|
try {
|
||||||
|
instance = clazz.getConstructor(null).newInstance();
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOGGER.warn("无法实例化类 " + clazz);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Command commandAnnotation = (Command) clazz.getAnnotation(Command.class);
|
||||||
|
String command = commandAnnotation.value();
|
||||||
|
String permission = commandAnnotation.permission();
|
||||||
|
|
||||||
|
List<CommandEntry> allEntries = new ArrayList<>();
|
||||||
|
Map<String, CommandEntry> pathToEntryMap = new HashMap<>();
|
||||||
|
List<CommandEntry> rootEntries = new ArrayList<>();
|
||||||
|
|
||||||
|
try {
|
||||||
|
for (Method method : clazz.getDeclaredMethods()) {
|
||||||
|
if (!method.isAnnotationPresent(SubCommand.class)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
CommandEntry commandEntry = parseFromMethod(method);
|
||||||
|
if (commandEntry != null) {
|
||||||
|
allEntries.add(commandEntry);
|
||||||
|
|
||||||
|
String path = commandEntry.getFullName();
|
||||||
|
String parentPath = getParentPath(path);
|
||||||
|
|
||||||
|
if (pathToEntryMap.containsKey(path)) {
|
||||||
|
LOGGER.warn("命令路径冲突: 子命令 '" + path + "' 已经定义, 不能再定义父命令 '" + parentPath + "'");
|
||||||
|
throw new CommandRegisterException("命令路径冲突: 子命令 '" + path + "' 已经定义, 不能再定义父命令 '" + parentPath + "'");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!parentPath.isEmpty() && pathToEntryMap.containsKey(parentPath)) {
|
||||||
|
LOGGER.warn("命令路径冲突: 子命令 '" + path + "' 已经定义, 不能再定义父命令 '" + parentPath + "'");
|
||||||
|
throw new CommandRegisterException("命令路径冲突: 子命令 '" + path + "' 已经定义, 不能再定义父命令 '" + parentPath + "'");
|
||||||
|
}
|
||||||
|
|
||||||
|
pathToEntryMap.put(commandEntry.getFullName(), commandEntry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (CommandEntry entry : allEntries) {
|
||||||
|
String fullPath = entry.getFullName();
|
||||||
|
int lastDotIndex = fullPath.lastIndexOf('.');
|
||||||
|
|
||||||
|
if (lastDotIndex == -1) {
|
||||||
|
rootEntries.add(entry);
|
||||||
|
} else {
|
||||||
|
String parentPath = fullPath.substring(0, lastDotIndex);
|
||||||
|
CommandEntry parentEntry = pathToEntryMap.get(parentPath);
|
||||||
|
|
||||||
|
if (parentEntry == null) {
|
||||||
|
parentEntry = createParentNodes(pathToEntryMap, parentPath);
|
||||||
|
if (parentPath.indexOf('.') == -1) {
|
||||||
|
rootEntries.add(parentEntry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
parentEntry.getChildrens().add(entry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new CommandEntity(instance, command, permission, rootEntries);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String getParentPath(String path) {
|
||||||
|
int lastDotIndex = path.lastIndexOf('.');
|
||||||
|
if (lastDotIndex == -1) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return path.substring(0, lastDotIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static CommandEntry createParentNodes(Map<String, CommandEntry> pathToEntryMap, String currentPath) {
|
||||||
|
if (pathToEntryMap.containsKey(currentPath)) {
|
||||||
|
return pathToEntryMap.get(currentPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
int lastDotIndex = currentPath.lastIndexOf('.');
|
||||||
|
CommandEntry parentEntry;
|
||||||
|
|
||||||
|
if (lastDotIndex == -1) {
|
||||||
|
parentEntry = new CommandEntry(currentPath, currentPath);
|
||||||
|
} else {
|
||||||
|
String grandParentPath = currentPath.substring(0, lastDotIndex);
|
||||||
|
CommandEntry grandParentEntry = createParentNodes(pathToEntryMap, grandParentPath);
|
||||||
|
parentEntry = new CommandEntry(currentPath, currentPath.substring(lastDotIndex + 1));
|
||||||
|
grandParentEntry.getChildrens().add(parentEntry);
|
||||||
|
}
|
||||||
|
pathToEntryMap.put(currentPath, parentEntry);
|
||||||
|
return parentEntry;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static CommandEntry parseFromMethod(Method method) {
|
||||||
|
SubCommand subCommand = method.getAnnotation(SubCommand.class);
|
||||||
|
String path = subCommand.value();
|
||||||
|
String permission = subCommand.permission();
|
||||||
|
String[] senderRequireArray = subCommand.senderRequire();
|
||||||
|
List<SenderRequire> senderRequireList = new ArrayList<>();
|
||||||
|
for (String senderRequire : senderRequireArray) {
|
||||||
|
SenderRequire senderRequire1 = SenderRequires.get(senderRequire);
|
||||||
|
if (senderRequire1 == null) {
|
||||||
|
LOGGER.warn("Sender require " + senderRequire + " is null");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
senderRequireList.add(SenderRequires.get(senderRequire));
|
||||||
|
}
|
||||||
|
|
||||||
|
String subName = path;
|
||||||
|
|
||||||
|
if (path.contains(".")) {
|
||||||
|
int lastDotIndex = path.lastIndexOf('.');
|
||||||
|
if (lastDotIndex == -1 || lastDotIndex > path.length() - 1) {
|
||||||
|
LOGGER.warn("Invalid command name '" + path + "'");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
subName = path.substring(path.lastIndexOf('.') + 1);
|
||||||
|
}
|
||||||
|
List<Argument> arguments = new ArrayList<>();
|
||||||
|
for (Parameter parameter : method.getParameters()) {
|
||||||
|
if (!parameter.isAnnotationPresent(CommandArgument.class)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
CommandArgument commandArgument = parameter.getAnnotation(CommandArgument.class);
|
||||||
|
String name = commandArgument.name();
|
||||||
|
String argumentName = name.isEmpty() ? parameter.getName() : name;
|
||||||
|
boolean required = commandArgument.required();
|
||||||
|
String suggestionType = commandArgument.suggestionType();
|
||||||
|
Class<?> type = parameter.getType();
|
||||||
|
ArgumentType argumentType = ArgumentType.get(parameter.getType());
|
||||||
|
if (argumentType == null) {
|
||||||
|
LOGGER.warn("Argument type " + parameter.getType().getName() + " is null");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Argument argument = new Argument(argumentName, argumentType);
|
||||||
|
if (!suggestionType.isEmpty()) {
|
||||||
|
Suggest suggest = Suggests.getSuggest(suggestionType);
|
||||||
|
if (suggest == null) {
|
||||||
|
LOGGER.warn("Suggest type " + suggestionType + " is null");
|
||||||
|
} else {
|
||||||
|
argument.suggest(suggest);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
arguments.add(argument);
|
||||||
|
}
|
||||||
|
return new CommandEntry(path, subName, permission, senderRequireList, arguments, method);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "CommandEntity{" +
|
||||||
|
"command='" + command + '\'' +
|
||||||
|
", permission='" + permission + '\'' +
|
||||||
|
", childrens=" + childrens +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,227 @@
|
||||||
|
package com.io.yutian.elementoriginlib.command;
|
||||||
|
|
||||||
|
import com.io.yutian.elementoriginlib.command.argument.Argument;
|
||||||
|
import com.io.yutian.elementoriginlib.command.argument.ArgumentValue;
|
||||||
|
import com.io.yutian.elementoriginlib.command.interfaces.CommandArgument;
|
||||||
|
import com.io.yutian.elementoriginlib.logger.Logger;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
|
import java.lang.invoke.MethodHandle;
|
||||||
|
import java.lang.invoke.MethodHandles;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.lang.reflect.Parameter;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class CommandEntry {
|
||||||
|
|
||||||
|
private static final Logger LOGGER = Logger.getLogger(CommandEntry.class);
|
||||||
|
|
||||||
|
private String fullName;
|
||||||
|
private String name;
|
||||||
|
private String permission;
|
||||||
|
private List<SenderRequire> senderRequires = new ArrayList<>();
|
||||||
|
|
||||||
|
private List<Argument> arguments = new ArrayList<>();
|
||||||
|
|
||||||
|
private List<CommandEntry> childrens = new ArrayList<>();
|
||||||
|
|
||||||
|
private long depth = 0;
|
||||||
|
|
||||||
|
private final Method method;
|
||||||
|
private final MethodHandle methodHandle;
|
||||||
|
|
||||||
|
private Class<?>[] parameterTypes;
|
||||||
|
|
||||||
|
public CommandEntry(String fullName, String name, String permission, List<SenderRequire> senderRequires, List<Argument> arguments, Method method) {
|
||||||
|
this.fullName = fullName;
|
||||||
|
this.name = name;
|
||||||
|
this.permission = permission;
|
||||||
|
this.senderRequires = senderRequires;
|
||||||
|
this.arguments = arguments;
|
||||||
|
this.method = method;
|
||||||
|
try {
|
||||||
|
this.methodHandle = MethodHandles.lookup().unreflect(this.method);
|
||||||
|
this.parameterTypes = methodHandle.type().parameterArray();
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
this.depth = fullName.chars().filter(ch -> ch == '.').count();
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommandEntry(String fullName, String name) {
|
||||||
|
this.fullName = fullName;
|
||||||
|
this.name = name;
|
||||||
|
this.method = null;
|
||||||
|
this.methodHandle = null;
|
||||||
|
this.parameterTypes = new Class<?>[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
public void invoke(CommandContext commandContext) {
|
||||||
|
if (method == null || methodHandle == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Object targetInstance = commandContext.getCommandInstance();
|
||||||
|
if (targetInstance == null) {
|
||||||
|
LOGGER.warn("该命令的实例为空,无法执行命令: " + fullName);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Class<?>[] parameterTypes = method.getParameterTypes();
|
||||||
|
Object[] parameters = new Object[parameterTypes.length];
|
||||||
|
|
||||||
|
int argumentIndex = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < parameterTypes.length; i++) {
|
||||||
|
Parameter parameter = method.getParameters()[i];
|
||||||
|
if (parameter.isAnnotationPresent(CommandArgument.class)) {
|
||||||
|
String paramName = arguments.get(argumentIndex).getName();
|
||||||
|
ArgumentValue argumentValue = commandContext.getArgumentsValue(paramName);
|
||||||
|
if (argumentValue != null) {
|
||||||
|
parameters[i] = argumentValue.getValue();
|
||||||
|
} else {
|
||||||
|
parameters[i] = null;
|
||||||
|
}
|
||||||
|
argumentIndex++;
|
||||||
|
} else if (parameter.getType() == CommandContext.class) {
|
||||||
|
parameters[i] = commandContext;
|
||||||
|
} else {
|
||||||
|
parameters[i] = getDefaultForType(parameterTypes[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Object[] parms = new Object[parameters.length+1];
|
||||||
|
parms[0] = targetInstance;
|
||||||
|
System.arraycopy(parameters, 0, parms, 1, parameters.length);
|
||||||
|
methodHandle.invokeWithArguments(parms);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
LOGGER.error("执行命令时出现异常: " + fullName);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
LOGGER.error("执行命令时出现异常: " + fullName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean canInvoke(CommandSender sender) {
|
||||||
|
if (senderRequires != null && senderRequires.size() > 0) {
|
||||||
|
for (SenderRequire senderRequire : senderRequires) {
|
||||||
|
if (senderRequire.test(sender)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasPermission(CommandSender sender) {
|
||||||
|
if (permission != null && !permission.isEmpty()) {
|
||||||
|
return sender.isOp() || sender.hasPermission(permission);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object[] convertToPrimitive(Object[] parameters) {
|
||||||
|
Object[] convertedParams = new Object[parameters.length];
|
||||||
|
for (int i = 0; i < parameters.length; i++) {
|
||||||
|
Object param = parameters[i];
|
||||||
|
Class<?> parameterType = parameterTypes[i+1];
|
||||||
|
if (parameterType.isPrimitive()) {
|
||||||
|
if (param instanceof Integer) {
|
||||||
|
convertedParams[i] = ((Integer) param).intValue();
|
||||||
|
} else if (param instanceof Double) {
|
||||||
|
convertedParams[i] = ((Double) param).doubleValue();
|
||||||
|
} else if (param instanceof Boolean) {
|
||||||
|
convertedParams[i] = ((Boolean) param).booleanValue();
|
||||||
|
} else if (param instanceof Character) {
|
||||||
|
convertedParams[i] = ((Character) param).charValue();
|
||||||
|
} else if (param instanceof Byte) {
|
||||||
|
convertedParams[i] = ((Byte) param).byteValue();
|
||||||
|
} else if (param instanceof Short) {
|
||||||
|
convertedParams[i] = ((Short) param).shortValue();
|
||||||
|
} else if (param instanceof Long) {
|
||||||
|
convertedParams[i] = ((Long) param).longValue();
|
||||||
|
} else if (param instanceof Float) {
|
||||||
|
convertedParams[i] = ((Float) param).floatValue();
|
||||||
|
} else {
|
||||||
|
convertedParams[i] = param;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return convertedParams;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object getDefaultForType(Class<?> parameterType) {
|
||||||
|
if (parameterType.isPrimitive()) {
|
||||||
|
if (parameterType == int.class) {
|
||||||
|
return 0;
|
||||||
|
} else if (parameterType == double.class) {
|
||||||
|
return 0.0;
|
||||||
|
} else if (parameterType == boolean.class) {
|
||||||
|
return false;
|
||||||
|
} else if (parameterType == char.class) {
|
||||||
|
return '\u0000';
|
||||||
|
} else if (parameterType == byte.class) {
|
||||||
|
return (byte) 0;
|
||||||
|
} else if (parameterType == short.class) {
|
||||||
|
return (short) 0;
|
||||||
|
} else if (parameterType == long.class) {
|
||||||
|
return 0L;
|
||||||
|
} else if (parameterType == float.class) {
|
||||||
|
return 0.0f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isEmptyEntry() {
|
||||||
|
return method == null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addChild(CommandEntry child) {
|
||||||
|
childrens.add(child);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFullName() {
|
||||||
|
return fullName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPermission() {
|
||||||
|
return permission;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<SenderRequire> getSenderRequires() {
|
||||||
|
return senderRequires;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Argument> getArguments() {
|
||||||
|
return arguments;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<CommandEntry> getChildrens() {
|
||||||
|
return childrens;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getDepth() {
|
||||||
|
return depth;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "CommandEntry{" +
|
||||||
|
"fullName='" + fullName + '\'' +
|
||||||
|
", name='" + name + '\'' +
|
||||||
|
", permission='" + permission + '\'' +
|
||||||
|
", arguments=" + arguments +
|
||||||
|
", childrens=" + childrens +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,91 +0,0 @@
|
||||||
package com.io.yutian.elementoriginlib.command;
|
|
||||||
|
|
||||||
import com.io.yutian.elementoriginlib.command.argument.Argument;
|
|
||||||
import org.bukkit.command.CommandSender;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.function.Predicate;
|
|
||||||
|
|
||||||
public class CommandNode {
|
|
||||||
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
private List<CommandNode> childrens = new ArrayList<>();
|
|
||||||
private List<Argument> arguments = new ArrayList<>();
|
|
||||||
|
|
||||||
private List<String> alias = new ArrayList<>();
|
|
||||||
|
|
||||||
private Predicate<CommandSender> commandSenderPredicate = (commandSender -> true);
|
|
||||||
private Command command;
|
|
||||||
|
|
||||||
public CommandNode(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public CommandNode(String name, String[] alias) {
|
|
||||||
this.name = name;
|
|
||||||
this.alias = Arrays.asList(alias);
|
|
||||||
}
|
|
||||||
|
|
||||||
public CommandNode(String name, List<String> alias) {
|
|
||||||
this.name = name;
|
|
||||||
this.alias = alias;
|
|
||||||
}
|
|
||||||
|
|
||||||
public CommandNode permission(Predicate<CommandSender> commandSenderPredicate) {
|
|
||||||
this.commandSenderPredicate = commandSenderPredicate;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<String> getAlias() {
|
|
||||||
return alias;
|
|
||||||
}
|
|
||||||
|
|
||||||
public CommandNode setAlias(List<String> alias) {
|
|
||||||
this.alias = alias;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public CommandNode addAilas(String alias) {
|
|
||||||
this.alias.add(alias);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public CommandNode addArgument(Argument argument) {
|
|
||||||
arguments.add(argument);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public CommandNode addChildren(CommandNode commandNode) {
|
|
||||||
this.childrens.add(commandNode);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<CommandNode> getChildrens() {
|
|
||||||
return childrens;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Argument> getArguments() {
|
|
||||||
return arguments;
|
|
||||||
}
|
|
||||||
|
|
||||||
public CommandNode executes(Command command) {
|
|
||||||
this.command = command;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Command getCommand() {
|
|
||||||
return command;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static CommandNode node(String name) {
|
|
||||||
return new CommandNode(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,7 +0,0 @@
|
||||||
package com.io.yutian.elementoriginlib.command;
|
|
||||||
|
|
||||||
public interface IAlias {
|
|
||||||
|
|
||||||
String[] getAlias();
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,67 +0,0 @@
|
||||||
package com.io.yutian.elementoriginlib.command;
|
|
||||||
|
|
||||||
import com.io.yutian.elementoriginlib.command.argument.Argument;
|
|
||||||
import org.bukkit.command.CommandSender;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public abstract class ICommand {
|
|
||||||
|
|
||||||
private String name;
|
|
||||||
private String description;
|
|
||||||
|
|
||||||
private List<CommandNode> commandNodes = new ArrayList<>();
|
|
||||||
private List<Argument> arguments = new ArrayList<>();
|
|
||||||
|
|
||||||
public ICommand(String name) {
|
|
||||||
this(name, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ICommand(String name, String description) {
|
|
||||||
this.name = name;
|
|
||||||
this.description = description;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void executes(CommandContext commandContext) {
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean emptyExecutes(CommandSender commandSender) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean hasPermission(CommandSender sender) {
|
|
||||||
return sender.isOp() || sender.hasPermission(getPermissionPrefix()+"."+name);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getPermissionPrefix() {
|
|
||||||
return "command."+name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ICommand addArgument(Argument argument) {
|
|
||||||
arguments.add(argument);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ICommand addCommandNode(CommandNode commandNode) {
|
|
||||||
this.commandNodes.add(commandNode);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<CommandNode> getCommandNodes() {
|
|
||||||
return commandNodes;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Argument> getArguments() {
|
|
||||||
return arguments;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getDescription() {
|
|
||||||
return description;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -6,6 +6,6 @@ public interface ICommandManager {
|
||||||
|
|
||||||
String getName();
|
String getName();
|
||||||
|
|
||||||
List<ICommand> getCommands();
|
List<CommandEntity> getCommands();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
package com.io.yutian.elementoriginlib.command;
|
||||||
|
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface SenderRequire {
|
||||||
|
|
||||||
|
boolean test(CommandSender sender);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,29 @@
|
||||||
|
package com.io.yutian.elementoriginlib.command;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class SenderRequires {
|
||||||
|
|
||||||
|
public static final SenderRequire CONSOLE = (sender) -> sender instanceof org.bukkit.command.ConsoleCommandSender;
|
||||||
|
|
||||||
|
public static final SenderRequire PLAYER = (sender) -> sender instanceof org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
private static Map<String, SenderRequire> requireMap = new HashMap<>();
|
||||||
|
|
||||||
|
public static void register(String name, SenderRequire require) {
|
||||||
|
requireMap.put(name, require);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SenderRequire get(String name) {
|
||||||
|
return requireMap.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
static {
|
||||||
|
|
||||||
|
register("console", CONSOLE);
|
||||||
|
register("player", PLAYER);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,295 +0,0 @@
|
||||||
package com.io.yutian.elementoriginlib.command;
|
|
||||||
|
|
||||||
import com.io.yutian.elementoriginlib.command.argument.Argument;
|
|
||||||
import com.io.yutian.elementoriginlib.command.argument.ArgumentValue;
|
|
||||||
import com.io.yutian.elementoriginlib.command.handler.CommandHandler;
|
|
||||||
import com.io.yutian.elementoriginlib.lang.Lang;
|
|
||||||
import org.bukkit.command.Command;
|
|
||||||
import org.bukkit.command.CommandSender;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
import java.util.stream.Stream;
|
|
||||||
|
|
||||||
public class SimpleCommandHandler extends Command {
|
|
||||||
|
|
||||||
private ICommand iCommand;
|
|
||||||
|
|
||||||
public SimpleCommandHandler(String name, ICommand iCommand) {
|
|
||||||
super(name);
|
|
||||||
this.iCommand = iCommand;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
|
|
||||||
executes(sender, commandLabel, args);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void executes(CommandSender sender, String commandLabel, String[] args) {
|
|
||||||
if (!iCommand.hasPermission(sender)) {
|
|
||||||
sender.sendMessage(Lang.get("command-no-permission"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
List<CommandNode> commandNodes = iCommand.getCommandNodes();
|
|
||||||
StringBuilder stringBuilder = new StringBuilder();
|
|
||||||
for (int i = 0; i < args.length; i++) {
|
|
||||||
stringBuilder.append(args[i]);
|
|
||||||
if (i < args.length - 1) {
|
|
||||||
stringBuilder.append(" ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
String commandString = stringBuilder.toString();
|
|
||||||
if (commandNodes.size() == 0) {
|
|
||||||
Map<String, ArgumentValue> map = new HashMap<>();
|
|
||||||
if (iCommand.getArguments().size() > 0) {
|
|
||||||
int argSize = args.length;
|
|
||||||
List<Argument> arguments = iCommand.getArguments();
|
|
||||||
int k = 0;
|
|
||||||
if (arguments.get(arguments.size()-1).isOptional()) {
|
|
||||||
k++;
|
|
||||||
}
|
|
||||||
if (argSize < arguments.size()-k) {
|
|
||||||
sender.sendMessage(Lang.get("command-short-arg"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (int l = 0; l < arguments.size(); l++) {
|
|
||||||
Argument a = arguments.get(l);
|
|
||||||
int index = l;
|
|
||||||
if (index >= args.length) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
String arg = args[index];
|
|
||||||
if (!a.getArgumentsType().test(arg)) {
|
|
||||||
sender.sendMessage(Lang.get("command-unknown-arg", index+1, arg));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
map = parseArgumentValue(sender, arguments, args, -1);
|
|
||||||
}
|
|
||||||
iCommand.executes(new CommandContext(commandString, commandLabel, sender, map));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int nodeSize = args.length;
|
|
||||||
|
|
||||||
if (commandNodes.size() > 0 && nodeSize == 0) {
|
|
||||||
if (!iCommand.emptyExecutes(sender)) {
|
|
||||||
sender.sendMessage(Lang.get("command-short-arg"));
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
String mainNode = args[0];
|
|
||||||
|
|
||||||
Stream<CommandNode> nodeStream = commandNodes.stream().filter((n) -> {
|
|
||||||
return n.getName().equalsIgnoreCase(mainNode) || n.getAlias().contains(mainNode);
|
|
||||||
});
|
|
||||||
Optional<CommandNode> nodeOptional = nodeStream.findFirst();
|
|
||||||
if (!nodeOptional.isPresent()) {
|
|
||||||
sender.sendMessage(Lang.get("command-unknown-arg", 1, mainNode));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
CommandNode node = nodeOptional.get();
|
|
||||||
|
|
||||||
if (node.getChildrens().size() > 0) {
|
|
||||||
checkClidren(commandString, commandLabel, sender, 0, args, node);
|
|
||||||
} else {
|
|
||||||
if (node.getCommand() != null) {
|
|
||||||
Map<String, ArgumentValue> map = new HashMap<>();
|
|
||||||
if (node.getArguments().size() > 0) {
|
|
||||||
int argSize = args.length - 1;
|
|
||||||
List<Argument> arguments = node.getArguments();
|
|
||||||
int k = 0;
|
|
||||||
if (arguments.get(arguments.size()-1).isOptional()) {
|
|
||||||
k++;
|
|
||||||
}
|
|
||||||
if (argSize < arguments.size()-k) {
|
|
||||||
sender.sendMessage(Lang.get("command-short-arg"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (int l = 0; l < arguments.size(); l++) {
|
|
||||||
Argument a = arguments.get(l);
|
|
||||||
int index = l + 1;
|
|
||||||
if (index >= args.length) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
String arg = args[index];
|
|
||||||
if (!a.getArgumentsType().test(arg)) {
|
|
||||||
sender.sendMessage(Lang.get("command-error-arg", index+1, arg));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
map = parseArgumentValue(sender, node.getArguments(), args, 0);
|
|
||||||
}
|
|
||||||
node.getCommand().run(new CommandContext(commandString, commandLabel, sender, map));
|
|
||||||
} else {
|
|
||||||
sender.sendMessage(Lang.get("command-unknown-arg", 2, mainNode));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void checkClidren(String commandString, String commandLabel, CommandSender sender, int i, String[] args, CommandNode node) {
|
|
||||||
i++;
|
|
||||||
if (i >= args.length) {
|
|
||||||
if (node.getCommand() == null) {
|
|
||||||
sender.sendMessage(Lang.get("command-short-arg"));
|
|
||||||
} else {
|
|
||||||
node.getCommand().run(new CommandContext(commandString, commandLabel, sender, new HashMap<>()));
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
String s = args[i];
|
|
||||||
Stream<CommandNode> nodeStream = node.getChildrens().stream().filter((n) -> {
|
|
||||||
return n.getName().equalsIgnoreCase(s) || n.getAlias().contains(s);
|
|
||||||
});
|
|
||||||
Optional<CommandNode> nodeOptional = nodeStream.findFirst();
|
|
||||||
if (!nodeOptional.isPresent()) {
|
|
||||||
sender.sendMessage(Lang.get("command-unknown-arg", i+1, s));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
CommandNode node1 = nodeOptional.get();
|
|
||||||
|
|
||||||
if (node1.getChildrens().size() > 0) {
|
|
||||||
checkClidren(commandString, commandLabel, sender, i, args, node1);
|
|
||||||
} else {
|
|
||||||
if (node1.getCommand() != null) {
|
|
||||||
Map<String, ArgumentValue> map = new HashMap<>();
|
|
||||||
if (node1.getArguments().size() > 0) {
|
|
||||||
int argSize = args.length - i - 1;
|
|
||||||
List<Argument> arguments = node1.getArguments();
|
|
||||||
int k = 0;
|
|
||||||
if (arguments.get(arguments.size()-1).isOptional()) {
|
|
||||||
k++;
|
|
||||||
}
|
|
||||||
if (argSize < arguments.size()-k) {
|
|
||||||
sender.sendMessage(Lang.get("command-short-arg"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (int l = 0; l < arguments.size(); l++) {
|
|
||||||
Argument a = arguments.get(l);
|
|
||||||
int index = i + l + 1;
|
|
||||||
if (index >= args.length) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
String arg = args[index];
|
|
||||||
if (!a.getArgumentsType().test(arg)) {
|
|
||||||
sender.sendMessage(Lang.get("command-unknown-arg", index+1, arg));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
map = parseArgumentValue(sender, node1.getArguments(), args, i);
|
|
||||||
}
|
|
||||||
node1.getCommand().run(new CommandContext(commandString, commandLabel, sender, map));
|
|
||||||
} else {
|
|
||||||
sender.sendMessage(Lang.get("command-unknown-arg", i+1, s));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private Map<String, ArgumentValue> parseArgumentValue(CommandSender commandSender, List<Argument> argumentList, String[] args, int i) {
|
|
||||||
Map<String, ArgumentValue> map = new HashMap<>();
|
|
||||||
List<Argument> arguments = argumentList;
|
|
||||||
for (int l = 0; l < arguments.size(); l++) {
|
|
||||||
Argument a = arguments.get(l);
|
|
||||||
if (i+1+l >= args.length) {
|
|
||||||
if (a.isOptional()) {
|
|
||||||
map.put(a.getName(), new ArgumentValue(a.getDefaultValue()));
|
|
||||||
}
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
String arg = args[i+1+l];
|
|
||||||
if (!a.getArgumentsType().test(arg)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
ArgumentValue argumentValue = new ArgumentValue(a.getArgumentsType().get(arg));
|
|
||||||
map.put(a.getName(), argumentValue);
|
|
||||||
}
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
|
|
||||||
return onTabComplete(sender, alias, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<String> onTabComplete(CommandSender sender, String alias, String[] args) {
|
|
||||||
List<String> list = new ArrayList<>();
|
|
||||||
int index = args.length;
|
|
||||||
String arg = args[index-1];
|
|
||||||
if (!iCommand.hasPermission(sender)) {
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
if (iCommand instanceof ITabCompleter) {
|
|
||||||
ITabCompleter tabCompleter = (ITabCompleter) iCommand;
|
|
||||||
return tabCompleter.onTabComplete(sender, args, index-1, arg);
|
|
||||||
} else {
|
|
||||||
Map<Integer, List<String>> map = new HashMap<>();
|
|
||||||
if (iCommand.getCommandNodes().size() > 0) {
|
|
||||||
List<String> list1 = new ArrayList<>();
|
|
||||||
for (CommandNode node : iCommand.getCommandNodes()) {
|
|
||||||
list1.add(node.getName());
|
|
||||||
list1.addAll(node.getAlias());
|
|
||||||
if (index >= 2) {
|
|
||||||
if (!node.getName().equalsIgnoreCase(args[0])) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (node.getChildrens().size() > 0) {
|
|
||||||
getTabComplete(node, 1, map);
|
|
||||||
} else if (node.getArguments().size() > 0) {
|
|
||||||
List<Argument> arguments = node.getArguments();
|
|
||||||
for (int l = 0; l < arguments.size(); l++) {
|
|
||||||
Argument argument = arguments.get(l);
|
|
||||||
if (argument.getSuggest() != null) {
|
|
||||||
map.put(2+l, argument.getSuggest().getSuggest());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
map.put(2+l, Arrays.asList("<"+argument.getName()+">"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
map.put(1, list1);
|
|
||||||
return CommandHandler.preseSuggest(map.getOrDefault(index, list), arg);
|
|
||||||
} else if (iCommand.getArguments().size() > 0) {
|
|
||||||
List<Argument> arguments = iCommand.getArguments();
|
|
||||||
for (int l = 0; l < arguments.size(); l++) {
|
|
||||||
Argument argument = arguments.get(l);
|
|
||||||
if (argument.getSuggest() != null) {
|
|
||||||
map.put(0+l+1, argument.getSuggest().getSuggest());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
map.put(0+l+1, Arrays.asList("<"+argument.getName()+">"));
|
|
||||||
}
|
|
||||||
return CommandHandler.preseSuggest(map.getOrDefault(index, list), arg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return CommandHandler.preseSuggest(list, arg);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void getTabComplete(CommandNode node, int i, Map<Integer, List<String>> map) {
|
|
||||||
i++;
|
|
||||||
List<String> list = map.getOrDefault(i, new ArrayList<>());
|
|
||||||
for (CommandNode c : node.getChildrens()) {
|
|
||||||
list.add(c.getName());
|
|
||||||
if (c.getChildrens().size() > 0) {
|
|
||||||
getTabComplete(c, i, map);
|
|
||||||
} else if (c.getArguments().size() > 0) {
|
|
||||||
List<Argument> arguments = c.getArguments();
|
|
||||||
for (int l = 0; l < arguments.size(); l++) {
|
|
||||||
Argument argument = arguments.get(l);
|
|
||||||
if (argument.getSuggest() != null) {
|
|
||||||
map.put(i+l+1, argument.getSuggest().getSuggest());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
map.put(i+l+1, Arrays.asList("<"+argument.getName()+">"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
map.put(i, list);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,7 +1,6 @@
|
||||||
package com.io.yutian.elementoriginlib.command;
|
package com.io.yutian.elementoriginlib.command;
|
||||||
|
|
||||||
import com.io.yutian.elementoriginlib.command.handler.CommandHandler;
|
import com.io.yutian.elementoriginlib.command.handler.CommandHandler;
|
||||||
import com.io.yutian.elementoriginlib.command.list.CommandHelp;
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandMap;
|
import org.bukkit.command.CommandMap;
|
||||||
|
@ -23,65 +22,46 @@ public class SimpleCommandManager implements ICommandManager {
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private List<ICommand> commands;
|
private List<Class<?>> commandClasses = new ArrayList<>();
|
||||||
|
@NotNull
|
||||||
|
private List<CommandEntity> commands = new ArrayList<>();
|
||||||
|
|
||||||
|
|
||||||
public SimpleCommandManager(Plugin plugin, String name) {
|
public SimpleCommandManager(Plugin plugin, String name) {
|
||||||
this(plugin, name, new ArrayList<>());
|
this(plugin, name, new ArrayList<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
public SimpleCommandManager(Plugin plugin, String name, @NotNull List<ICommand> commands) {
|
public SimpleCommandManager(Plugin plugin, String name, @NotNull List<Class<?>> commandClasses) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.commands = commands;
|
this.commandClasses = commandClasses;
|
||||||
register(new CommandHelp(this));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void register(@NotNull ICommand command) {
|
public void register(@NotNull Class<?> commandClass) {
|
||||||
|
if (commandClass == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
CommandEntity command = CommandEntity.parseFromClass(commandClass);
|
||||||
if (command == null) {
|
if (command == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
commandClasses.add(commandClass);
|
||||||
commands.add(command);
|
commands.add(command);
|
||||||
if (command instanceof IAlias alias) {
|
|
||||||
String[] array = alias.getAlias();
|
|
||||||
for (String s : array) {
|
|
||||||
registerPluginBukkitCommand(plugin, s, command);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void unregisterAll() {
|
public void unregisterAll() {
|
||||||
for (ICommand command : commands) {
|
commands.clear();
|
||||||
if (command instanceof IAlias) {
|
|
||||||
unregister(command);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void unregister(ICommand command) {
|
public void unregister(Class<?> commandClass) {
|
||||||
if (!(command instanceof IAlias)) {
|
if (commandClass == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
commands.remove(commandClass);
|
||||||
Map<String, Command> map = (Map<String, Command>) commandMap.getClass().getMethod("getKnownCommands").invoke(commandMap);
|
|
||||||
for (String name : ((IAlias) command).getAlias()) {
|
|
||||||
map.remove(name);
|
|
||||||
Command bukkitCommand = bukkitCommandMap.get(name);
|
|
||||||
bukkitCommand.unregister(commandMap);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void registerPluginCommand(@NotNull Plugin plugin, String commandName) {
|
public void registerPluginCommand(@NotNull String commandName) {
|
||||||
Bukkit.getPluginCommand(commandName).setExecutor(new CommandHandler(plugin, this));
|
Bukkit.getPluginCommand(commandName).setExecutor(new CommandHandler(this));
|
||||||
}
|
|
||||||
|
|
||||||
protected static void registerPluginBukkitCommand(Plugin plugin, String name, ICommand command) {
|
|
||||||
SimpleCommandHandler simpleCommandHandler = new SimpleCommandHandler(name, command);
|
|
||||||
bukkitCommandMap.put(name, simpleCommandHandler);
|
|
||||||
commandMap.register(plugin.getName(), simpleCommandHandler);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Plugin getPlugin() {
|
public Plugin getPlugin() {
|
||||||
|
@ -96,7 +76,7 @@ public class SimpleCommandManager implements ICommandManager {
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public List<ICommand> getCommands() {
|
public List<CommandEntity> getCommands() {
|
||||||
return commands;
|
return commands;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,8 +4,10 @@ import org.bukkit.Bukkit;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class Suggests {
|
public class Suggests {
|
||||||
|
|
||||||
|
@ -25,4 +27,25 @@ public class Suggests {
|
||||||
return list;
|
return list;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private static final Map<String, Suggest> suggests = new HashMap<>();
|
||||||
|
|
||||||
|
public static void registerSuggest(String name, Suggest suggest) {
|
||||||
|
suggests.put(name, suggest);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Suggest getSuggest(String name) {
|
||||||
|
return suggests.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void unregisterSuggest(String name) {
|
||||||
|
suggests.remove(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
static {
|
||||||
|
|
||||||
|
registerSuggest("world_list", WORLD_LIST);
|
||||||
|
registerSuggest("player_list", PLAYER_LIST);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,8 +49,4 @@ public class Argument {
|
||||||
return suggest;
|
return suggest;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Argument argument(String name, ArgumentType type) {
|
|
||||||
return new Argument(name, type);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
package com.io.yutian.elementoriginlib.command.argument;
|
||||||
|
|
||||||
|
public class ArgumentNode extends Argument {
|
||||||
|
|
||||||
|
private static final ArgumentType<Object> NODE = new ArgumentType<>("node", null, null);
|
||||||
|
|
||||||
|
public ArgumentNode(String name) {
|
||||||
|
super(name, NODE);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -1,10 +1,52 @@
|
||||||
package com.io.yutian.elementoriginlib.command.argument;
|
package com.io.yutian.elementoriginlib.command.argument;
|
||||||
|
|
||||||
|
import com.io.yutian.elementoriginlib.util.ClassUtil;
|
||||||
|
import com.io.yutian.elementoriginlib.util.StringUtil;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
public class ArgumentType<T> {
|
public class ArgumentType<T> {
|
||||||
|
|
||||||
|
private static final ArgumentType<String> STRING = new ArgumentType<>("string", (s) -> true, (s) -> s);
|
||||||
|
|
||||||
|
private static final ArgumentType<Integer> INTEGER = new ArgumentType<>("integer", StringUtil::isInt, Integer::parseInt);
|
||||||
|
|
||||||
|
private static final ArgumentType<Double> DOUBLE = new ArgumentType<>("double", StringUtil::isDouble, Double::parseDouble);
|
||||||
|
|
||||||
|
private static final ArgumentType<Boolean> BOOLEAN = new ArgumentType<>("boolean", StringUtil::isBoolean, Boolean::parseBoolean);
|
||||||
|
|
||||||
|
private static final ArgumentType<String[]> STRING_ARRAY = new ArgumentType<>("string[]", (s) -> true, (s) -> s.split(","));
|
||||||
|
|
||||||
|
private static final ArgumentType<Integer[]> INTEGER_ARRAY = new ArgumentType<>("integer[]", (s) -> true, (s) -> {
|
||||||
|
String[] arr = s.split(",");
|
||||||
|
Integer[] result = new Integer[arr.length];
|
||||||
|
for (int i = 0; i < arr.length; i++) {
|
||||||
|
result[i] = Integer.parseInt(arr[i]);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
});
|
||||||
|
private static final ArgumentType<Double[]> DOUBLE_ARRAY = new ArgumentType<>("double[]", (s) -> true, (s) -> {
|
||||||
|
String[] arr = s.split(",");
|
||||||
|
Double[] result = new Double[arr.length];
|
||||||
|
for (int i = 0; i < arr.length; i++) {
|
||||||
|
result[i] = Double.parseDouble(arr[i]);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
});
|
||||||
|
private static final ArgumentType<Boolean[]> BOOLEAN_ARRAY = new ArgumentType<>("boolean[]", (s) -> true, (s) -> {
|
||||||
|
String[] arr = s.split(",");
|
||||||
|
Boolean[] result = new Boolean[arr.length];
|
||||||
|
for (int i = 0; i < arr.length; i++) {
|
||||||
|
result[i] = Boolean.parseBoolean(arr[i]);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
});
|
||||||
|
|
||||||
|
private static final Map<Class<?>, ArgumentType<?>> ARGUMENT_TYPES = new HashMap<>();
|
||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
private Predicate<String> predicate;
|
private Predicate<String> predicate;
|
||||||
|
@ -28,4 +70,49 @@ public class ArgumentType<T> {
|
||||||
return function.apply(t);
|
return function.apply(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static {
|
||||||
|
register(String.class, STRING);
|
||||||
|
register(Integer.class, INTEGER);
|
||||||
|
register(Double.class, DOUBLE);
|
||||||
|
register(Boolean.class, BOOLEAN);
|
||||||
|
register(String[].class, STRING_ARRAY);
|
||||||
|
register(Integer[].class, INTEGER_ARRAY);
|
||||||
|
register(Double[].class, DOUBLE_ARRAY);
|
||||||
|
register(Boolean[].class, BOOLEAN_ARRAY);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> void register(Class<T> clazz, ArgumentType<T> type) {
|
||||||
|
ARGUMENT_TYPES.put(clazz, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<Class<?>, ArgumentType<?>> getArgumentTypes() {
|
||||||
|
return ARGUMENT_TYPES;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> ArgumentType<T> get(Class<T> clazz) {
|
||||||
|
if (clazz.isEnum()) {
|
||||||
|
return new EnumArgumentType<>("enum_"+clazz.getSimpleName(), (Class<? extends Enum>)clazz);
|
||||||
|
}
|
||||||
|
Class<?> c = ClassUtil.PRIMITIVE_TO_WRAPPER.getOrDefault(clazz, clazz);
|
||||||
|
return (ArgumentType<T>) ARGUMENT_TYPES.get(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class EnumArgumentType<T extends Enum<T>> extends ArgumentType<T> {
|
||||||
|
|
||||||
|
private final Class<T> clazz;
|
||||||
|
|
||||||
|
public EnumArgumentType(String name, Class<T> clazz) {
|
||||||
|
super(name, (s)->{
|
||||||
|
try {
|
||||||
|
Enum.valueOf(clazz, s);
|
||||||
|
return true;
|
||||||
|
} catch (Exception e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}, (s)-> Enum.valueOf(clazz, s));
|
||||||
|
this.clazz = clazz;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +0,0 @@
|
||||||
package com.io.yutian.elementoriginlib.command.argument;
|
|
||||||
|
|
||||||
import com.io.yutian.elementoriginlib.util.StringUtil;
|
|
||||||
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
public class ArgumentTypes {
|
|
||||||
|
|
||||||
public static final ArgumentType<String> STRING = new ArgumentType<>("string", (s) -> true, (s) -> s);
|
|
||||||
|
|
||||||
public static final ArgumentType<Integer> INTEGER = new ArgumentType<>("integer", StringUtil::isInt, Integer::parseInt);
|
|
||||||
|
|
||||||
public static final ArgumentType<Double> DOUBLE = new ArgumentType<>("double", StringUtil::isDouble, Double::parseDouble);
|
|
||||||
|
|
||||||
public static final ArgumentType<UUID> UUID = new ArgumentType<>("uuid", StringUtil::isUUID, java.util.UUID::fromString);
|
|
||||||
|
|
||||||
public static final ArgumentType<Boolean> BOOLEAN = new ArgumentType<>("boolean", StringUtil::isBoolean, Boolean::parseBoolean);
|
|
||||||
|
|
||||||
}
|
|
|
@ -52,10 +52,6 @@ public class ArgumentValue {
|
||||||
return (Boolean) value;
|
return (Boolean) value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public UUID getUUID() {
|
|
||||||
return (UUID) value;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "ArgumentValue{" +
|
return "ArgumentValue{" +
|
||||||
|
|
|
@ -8,213 +8,128 @@ import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandExecutor;
|
import org.bukkit.command.CommandExecutor;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.command.TabCompleter;
|
import org.bukkit.command.TabCompleter;
|
||||||
import org.bukkit.plugin.Plugin;
|
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
|
||||||
|
|
||||||
public class CommandHandler implements CommandExecutor, TabCompleter {
|
public class CommandHandler implements CommandExecutor, TabCompleter {
|
||||||
|
|
||||||
private ICommandManager commandManager;
|
private final ICommandManager commandManager;
|
||||||
|
|
||||||
public CommandHandler(Plugin plugin, ICommandManager commandManager) {
|
public CommandHandler(ICommandManager commandManager) {
|
||||||
this.commandManager = commandManager;
|
this.commandManager = commandManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void execute(CommandSender sender, String label, String[] args) {
|
public void execute(CommandSender sender, String label, String[] args) {
|
||||||
if (args.length == 0) {
|
if (args.length == 0) {
|
||||||
execute(sender, label, new String[]{"help", "1"});
|
sender.sendMessage(Lang.get("command.short-arg"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
List<ICommand> commands = commandManager.getCommands();
|
String commandName = args[0];
|
||||||
String command = args[0];
|
Optional<CommandEntity> entityOptional = commandManager.getCommands().stream()
|
||||||
Stream<ICommand> stream = commands.stream().filter((c) -> c.getName().equalsIgnoreCase(command));
|
.filter(entity -> entity.getCommand().equalsIgnoreCase(commandName))
|
||||||
Optional<ICommand> optional = stream.findFirst();
|
.findFirst();
|
||||||
if (!optional.isPresent()) {
|
|
||||||
sender.sendMessage(Lang.get("command-unknown", command));
|
if (!entityOptional.isPresent()) {
|
||||||
return;
|
sender.sendMessage(Lang.get("command.unknown", commandName));
|
||||||
}
|
|
||||||
ICommand iCommand = optional.get();
|
|
||||||
if (!iCommand.hasPermission(sender)) {
|
|
||||||
sender.sendMessage(Lang.get("command-no-permission"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
List<CommandNode> commandNodes = iCommand.getCommandNodes();
|
|
||||||
StringBuilder stringBuilder = new StringBuilder();
|
|
||||||
for (int i = 0; i < args.length; i++) {
|
|
||||||
stringBuilder.append(args[i]);
|
|
||||||
if (i < args.length - 1) {
|
|
||||||
stringBuilder.append(" ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
String commandString = stringBuilder.toString();
|
|
||||||
if (commandNodes.size() == 0) {
|
|
||||||
Map<String, ArgumentValue> map = new HashMap<>();
|
|
||||||
if (iCommand.getArguments().size() > 0) {
|
|
||||||
int argSize = args.length - 1;
|
|
||||||
List<Argument> arguments = iCommand.getArguments();
|
|
||||||
int k = 0;
|
|
||||||
if (arguments.get(arguments.size()-1).isOptional()) {
|
|
||||||
k++;
|
|
||||||
}
|
|
||||||
if (argSize < arguments.size()-k) {
|
|
||||||
sender.sendMessage(Lang.get("command-short-arg"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (int l = 0; l < arguments.size(); l++) {
|
|
||||||
Argument a = arguments.get(l);
|
|
||||||
int index = l + 1;
|
|
||||||
if (index >= args.length) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
String arg = args[index];
|
|
||||||
if (!a.getArgumentsType().test(arg)) {
|
|
||||||
sender.sendMessage(Lang.get("command-unknown-arg", index+1, arg));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
map = parseArgumentValue(sender, arguments, args, 0);
|
|
||||||
}
|
|
||||||
iCommand.executes(new CommandContext(commandString, label, sender, map));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int nodeSize = args.length - 1;
|
CommandEntity commandEntity = entityOptional.get();
|
||||||
|
handleCommand(commandEntity, sender, label, args, commandEntity.getChildrens(), 1, new StringBuilder());
|
||||||
if (commandNodes.size() > 0 && nodeSize == 0) {
|
|
||||||
if (!iCommand.emptyExecutes(sender)) {
|
|
||||||
sender.sendMessage(Lang.get("command-short-arg"));
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
String mainNode = args[1];
|
|
||||||
Stream<CommandNode> nodeStream = commandNodes.stream().filter((n) -> n.getName().equalsIgnoreCase(mainNode) || n.getAlias().contains(mainNode));
|
|
||||||
Optional<CommandNode> nodeOptional = nodeStream.findFirst();
|
|
||||||
if (!nodeOptional.isPresent()) {
|
|
||||||
sender.sendMessage(Lang.get("command-unknown-arg", 2, mainNode));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
CommandNode node = nodeOptional.get();
|
|
||||||
|
|
||||||
if (node.getChildrens().size() > 0) {
|
|
||||||
checkClidren(commandString, label, sender, 1, args, node);
|
|
||||||
} else {
|
|
||||||
if (node.getCommand() != null) {
|
|
||||||
Map<String, ArgumentValue> map = new HashMap<>();
|
|
||||||
if (node.getArguments().size() > 0) {
|
|
||||||
int argSize = args.length - 2;
|
|
||||||
List<Argument> arguments = node.getArguments();
|
|
||||||
int k = 0;
|
|
||||||
if (arguments.get(arguments.size()-1).isOptional()) {
|
|
||||||
k++;
|
|
||||||
}
|
|
||||||
if (argSize < arguments.size()-k) {
|
|
||||||
sender.sendMessage(Lang.get("command-short-arg"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (int l = 0; l < arguments.size(); l++) {
|
|
||||||
Argument a = arguments.get(l);
|
|
||||||
int index = l + 1;
|
|
||||||
if (index >= args.length) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (index+1 >= args.length) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
String arg = args[index+1];
|
|
||||||
if (!a.getArgumentsType().test(arg)) {
|
|
||||||
sender.sendMessage(Lang.get("command-error-arg", index+1, arg));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
map = parseArgumentValue(sender, node.getArguments(), args, 1);
|
|
||||||
}
|
|
||||||
node.getCommand().run(new CommandContext(commandString, label, sender, map));
|
|
||||||
} else {
|
|
||||||
sender.sendMessage(Lang.get("command-unknown-arg", 3, mainNode));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, ArgumentValue> parseArgumentValue(CommandSender commandSender, List<Argument> argumentList, String[] args, int i) {
|
private void handleCommand(CommandEntity commandEntity, CommandSender sender, String label, String[] args, List<CommandEntry> entries, int index, StringBuilder fullCommand) {
|
||||||
Map<String, ArgumentValue> map = new HashMap<>();
|
if (index >= args.length) {
|
||||||
List<Argument> arguments = argumentList;
|
sender.sendMessage(Lang.get("command.short-arg"));
|
||||||
for (int l = 0; l < arguments.size(); l++) {
|
return;
|
||||||
Argument a = arguments.get(l);
|
}
|
||||||
if (i+1+l >= args.length) {
|
|
||||||
if (a.isOptional()) {
|
String currentArg = args[index];
|
||||||
map.put(a.getName(), new ArgumentValue(a.getDefaultValue()));
|
Optional<CommandEntry> optionalEntry = entries.stream()
|
||||||
}
|
.filter(entry -> entry.getName().equalsIgnoreCase(currentArg))
|
||||||
return map;
|
.findFirst();
|
||||||
|
|
||||||
|
if (!optionalEntry.isPresent()) {
|
||||||
|
sender.sendMessage(Lang.get("command.unknown-arg", index + 1, currentArg));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
CommandEntry entry = optionalEntry.get();
|
||||||
|
fullCommand.append(currentArg);
|
||||||
|
|
||||||
|
if (!entry.canInvoke(sender)) {
|
||||||
|
sender.sendMessage(Lang.get("command.error-sender-type"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!entry.hasPermission(sender)) {
|
||||||
|
sender.sendMessage(Lang.get("command.no-permission"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!entry.getChildrens().isEmpty()) {
|
||||||
|
fullCommand.append(" ");
|
||||||
|
handleCommand(commandEntity, sender, label, args, entry.getChildrens(), index + 1, fullCommand);
|
||||||
|
} else {
|
||||||
|
if (!validateArguments(sender, entry.getArguments(), args, index + 1)) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
String arg = args[i+1+l];
|
Map<String, ArgumentValue> parsedArguments = parseArgumentValue(sender, entry.getArguments(), args, index + 1);
|
||||||
if (!a.getArgumentsType().test(arg)) {
|
entry.invoke(new CommandContext(commandEntity.getInstance(), fullCommand.toString(), label, sender, parsedArguments));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean validateArguments(CommandSender sender, List<Argument> arguments, String[] args, int startIndex) {
|
||||||
|
for (int i = 0; i < arguments.size(); i++) {
|
||||||
|
Argument argument = arguments.get(i);
|
||||||
|
int argIndex = startIndex + i;
|
||||||
|
if (argIndex >= args.length) {
|
||||||
|
if (argument.isOptional()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
sender.sendMessage(Lang.get("command.short-arg"));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
String arg = args[argIndex];
|
||||||
|
if (!argument.getArgumentsType().test(arg)) {
|
||||||
|
sender.sendMessage(Lang.get("command.error-arg", argIndex + 1, arg));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, ArgumentValue> parseArgumentValue(CommandSender sender, List<Argument> arguments, String[] args, int startIndex) {
|
||||||
|
Map<String, ArgumentValue> parsedArguments = new HashMap<>();
|
||||||
|
List<Object> methodParams = new ArrayList<>();
|
||||||
|
|
||||||
|
for (Argument argument : arguments) {
|
||||||
|
int argIndex = startIndex + arguments.indexOf(argument);
|
||||||
|
|
||||||
|
if (argIndex >= args.length) {
|
||||||
|
if (argument.isOptional()) {
|
||||||
|
parsedArguments.put(argument.getName(), new ArgumentValue(argument.getDefaultValue()));
|
||||||
|
} else {
|
||||||
|
sender.sendMessage(Lang.get("command.short-arg", argument.getName()));
|
||||||
|
return null;
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
ArgumentValue argumentValue = new ArgumentValue(a.getArgumentsType().get(arg));
|
|
||||||
map.put(a.getName(), argumentValue);
|
|
||||||
}
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void checkClidren(String commandString, String label, CommandSender sender, int i, String[] args, CommandNode node) {
|
String rawValue = args[argIndex];
|
||||||
i++;
|
if (!argument.getArgumentsType().test(rawValue)) {
|
||||||
if (i >= args.length) {
|
sender.sendMessage(Lang.get("command.error-arg", argIndex + 1, rawValue));
|
||||||
if (node.getCommand() == null) {
|
return null;
|
||||||
sender.sendMessage(Lang.get("command-short-arg"));
|
|
||||||
} else {
|
|
||||||
node.getCommand().run(new CommandContext(commandString, label, sender, new HashMap<>()));
|
|
||||||
}
|
}
|
||||||
return;
|
Object parsedValue = argument.getArgumentsType().get(rawValue);
|
||||||
}
|
parsedArguments.put(argument.getName(), new ArgumentValue(parsedValue));
|
||||||
String s = args[i];
|
|
||||||
Stream<CommandNode> nodeStream = node.getChildrens().stream().filter((n) -> n.getName().equalsIgnoreCase(s) || n.getAlias().contains(s));
|
|
||||||
Optional<CommandNode> nodeOptional = nodeStream.findFirst();
|
|
||||||
if (!nodeOptional.isPresent()) {
|
|
||||||
sender.sendMessage(Lang.get("command-unknown-arg", i+1, s));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
CommandNode node1 = nodeOptional.get();
|
|
||||||
|
|
||||||
if (node1.getChildrens().size() > 0) {
|
methodParams.add(parsedValue);
|
||||||
checkClidren(commandString, label, sender, i, args, node1);
|
|
||||||
} else {
|
|
||||||
if (node1.getCommand() != null) {
|
|
||||||
Map<String, ArgumentValue> map = new HashMap<>();
|
|
||||||
if (node1.getArguments().size() > 0) {
|
|
||||||
int argSize = args.length - i - 1;
|
|
||||||
List<Argument> arguments = node1.getArguments();
|
|
||||||
int k = 0;
|
|
||||||
if (arguments.get(arguments.size()-1).isOptional()) {
|
|
||||||
k++;
|
|
||||||
}
|
|
||||||
if (argSize < arguments.size()-k) {
|
|
||||||
sender.sendMessage(Lang.get("command-short-arg"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (int l = 0; l < arguments.size(); l++) {
|
|
||||||
Argument a = arguments.get(l);
|
|
||||||
int index = i + l + 1;
|
|
||||||
if (index >= args.length) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
String arg = args[index];
|
|
||||||
if (!a.getArgumentsType().test(arg)) {
|
|
||||||
sender.sendMessage(Lang.get("command-unknown-arg", index+1, arg));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
map = parseArgumentValue(sender, node1.getArguments(), args, i);
|
|
||||||
}
|
|
||||||
node1.getCommand().run(new CommandContext(commandString, label, sender, map));
|
|
||||||
} else {
|
|
||||||
sender.sendMessage(Lang.get("command-unknown-arg", i+1, s));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return parsedArguments;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -225,117 +140,74 @@ public class CommandHandler implements CommandExecutor, TabCompleter {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
||||||
List<String> list = new ArrayList<>();
|
if (args.length == 1) {
|
||||||
int index = args.length;
|
return commandManager.getCommands().stream()
|
||||||
String arg = args[index-1];
|
.map(CommandEntity::getCommand)
|
||||||
List<ICommand> commands = commandManager.getCommands();
|
.collect(Collectors.toList());
|
||||||
if (index == 1) {
|
}
|
||||||
List<ICommand> commandList = commands.stream().filter((c)->c.getName().startsWith(arg)).collect(Collectors.toList());
|
|
||||||
if (commandList.size() > 0) {
|
String commandName = args[0];
|
||||||
commandList.forEach(c-> {
|
Optional<CommandEntity> entityOptional = commandManager.getCommands().stream()
|
||||||
if (c.hasPermission(sender)) {
|
.filter(entity -> entity.getCommand().equalsIgnoreCase(commandName))
|
||||||
list.add(c.getName());
|
.findFirst();
|
||||||
}
|
|
||||||
});
|
if (!entityOptional.isPresent()) {
|
||||||
return list;
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
|
CommandEntity commandEntity = entityOptional.get();
|
||||||
|
List<CommandEntry> entries = commandEntity.getChildrens();
|
||||||
|
|
||||||
|
int depth = args.length;
|
||||||
|
String currentArg = args[depth - 1];
|
||||||
|
return getSuggestions(sender, entries, args, 1, depth - 1, currentArg);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private List<String> getSuggestions(CommandSender sender, List<CommandEntry> entries, String[] args, int start, int currentDepth, String currentArg) {
|
||||||
|
int maxDepth = -1;
|
||||||
|
CommandEntry commandEntry = null;
|
||||||
|
for (int i = start; i < currentDepth; i++) {
|
||||||
|
String arg = args[i];
|
||||||
|
Optional<CommandEntry> optionalEntry = entries.stream()
|
||||||
|
.filter(entry -> entry.getName().equalsIgnoreCase(arg))
|
||||||
|
.findFirst();
|
||||||
|
|
||||||
|
if (!optionalEntry.isPresent()) {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
commandList = commands.stream().filter((c)->c.getName().contains(arg)).collect(Collectors.toList());
|
|
||||||
if (commandList.size() > 0) {
|
commandEntry = optionalEntry.get();
|
||||||
commandList.forEach(c-> {
|
|
||||||
if (c.hasPermission(sender)) {
|
entries = commandEntry.getChildrens();
|
||||||
list.add(c.getName());
|
|
||||||
}
|
if (entries.size() > 0) {
|
||||||
});
|
maxDepth = i + 1;
|
||||||
return list;
|
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
Optional<ICommand> iCommandOptional = commands.stream().filter((c)->c.getName().equalsIgnoreCase(args[0])).findFirst();
|
|
||||||
if (!iCommandOptional.isPresent()) {
|
if (entries.size() == 0) {
|
||||||
return list;
|
List<Argument> arguments = commandEntry.getArguments();
|
||||||
|
int index = (int) (currentDepth - commandEntry.getDepth() - 2);
|
||||||
|
if (index >= arguments.size()) {
|
||||||
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
ICommand iCommand = iCommandOptional.get();
|
if (!arguments.isEmpty()) {
|
||||||
if (!iCommand.hasPermission(sender)) {
|
Argument argument = arguments.get(index);
|
||||||
return list;
|
Suggest suggest = argument.getSuggest();
|
||||||
}
|
if (suggest != null) {
|
||||||
if (iCommand instanceof ITabCompleter) {
|
return suggest.getSuggest();
|
||||||
ITabCompleter tabCompleter = (ITabCompleter) iCommand;
|
} else {
|
||||||
return tabCompleter.onTabComplete(sender, args, index-2, arg);
|
return Collections.singletonList(new StringBuilder().append("<").append(argument.getName()).append(">").toString());
|
||||||
} else {
|
|
||||||
Map<Integer, List<String>> map = new HashMap<>();
|
|
||||||
if (iCommand.getCommandNodes().size() > 0) {
|
|
||||||
List<String> list1 = new ArrayList<>();
|
|
||||||
for (CommandNode node : iCommand.getCommandNodes()) {
|
|
||||||
list1.add(node.getName());
|
|
||||||
list1.addAll(node.getAlias());
|
|
||||||
if (index >= 2) {
|
|
||||||
if (!node.getName().equalsIgnoreCase(args[1])) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (node.getChildrens().size() > 0) {
|
|
||||||
getTabComplete(node, 2, map);
|
|
||||||
} else if (node.getArguments().size() > 0) {
|
|
||||||
List<Argument> arguments = node.getArguments();
|
|
||||||
for (int l = 0; l < arguments.size(); l++) {
|
|
||||||
Argument argument = arguments.get(l);
|
|
||||||
if (argument.getSuggest() != null) {
|
|
||||||
map.put(2+l+1, argument.getSuggest().getSuggest());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
map.put(2+l+1, Arrays.asList("<"+argument.getName()+">"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
map.put(2, list1);
|
|
||||||
return preseSuggest(map.getOrDefault(index, list), arg);
|
|
||||||
} else if (iCommand.getArguments().size() > 0) {
|
|
||||||
List<Argument> arguments = iCommand.getArguments();
|
|
||||||
for (int l = 0; l < arguments.size(); l++) {
|
|
||||||
Argument argument = arguments.get(l);
|
|
||||||
if (argument.getSuggest() != null) {
|
|
||||||
map.put(1+l+1, argument.getSuggest().getSuggest());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
map.put(1+l+1, Arrays.asList("<"+argument.getName()+">"));
|
|
||||||
}
|
|
||||||
return preseSuggest(map.getOrDefault(index, list), arg);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return preseSuggest(list, arg);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static List<String> preseSuggest(List<String> list, String arg) {
|
return entries.stream()
|
||||||
List<String> newList = new ArrayList<>();
|
.filter(entry -> entry.getName().toLowerCase().startsWith(currentArg.toLowerCase()))
|
||||||
List<String> list1 = list.stream().filter((c)->c.startsWith(arg)||c.toLowerCase().startsWith(arg.toLowerCase())).collect(Collectors.toList());
|
.filter(entry -> entry.canInvoke(sender) && entry.hasPermission(sender))
|
||||||
List<String> list2 = list.stream().filter((c)->c.contains(arg)||c.toLowerCase().contains(arg.toLowerCase())).collect(Collectors.toList());
|
.map(CommandEntry::getName)
|
||||||
List<String> list3 = list.stream().filter((c)->c.equalsIgnoreCase(arg)|| c.equalsIgnoreCase(arg)).collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
newList.addAll(list1);
|
|
||||||
newList.addAll(list2);
|
|
||||||
newList.addAll(list3);
|
|
||||||
return newList;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void getTabComplete(CommandNode node, int i, Map<Integer, List<String>> map) {
|
|
||||||
i++;
|
|
||||||
List<String> list = map.getOrDefault(i, new ArrayList<>());
|
|
||||||
for (CommandNode c : node.getChildrens()) {
|
|
||||||
list.add(c.getName());
|
|
||||||
if (c.getChildrens().size() > 0) {
|
|
||||||
getTabComplete(c, i, map);
|
|
||||||
} else if (c.getArguments().size() > 0) {
|
|
||||||
List<Argument> arguments = c.getArguments();
|
|
||||||
for (int l = 0; l < arguments.size(); l++) {
|
|
||||||
Argument argument = arguments.get(l);
|
|
||||||
if (argument.getSuggest() != null) {
|
|
||||||
map.put(i+l+1, argument.getSuggest().getSuggest());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
map.put(i+l+1, Arrays.asList("<"+argument.getName()+">"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
map.put(i, list);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
package com.io.yutian.elementoriginlib.command.interfaces;
|
||||||
|
|
||||||
|
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 Command {
|
||||||
|
|
||||||
|
String value();
|
||||||
|
|
||||||
|
String permission() default "command.elementoriginlib.default";
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.io.yutian.elementoriginlib.command.interfaces;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Target(ElementType.PARAMETER)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface CommandArgument {
|
||||||
|
|
||||||
|
String name() default "";
|
||||||
|
|
||||||
|
boolean required() default true;
|
||||||
|
|
||||||
|
String suggestionType() default "";
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.io.yutian.elementoriginlib.command.interfaces;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Target(ElementType.METHOD)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface SubCommand {
|
||||||
|
|
||||||
|
String value() default "";
|
||||||
|
|
||||||
|
String permission() default "command.elementoriginlib.default";
|
||||||
|
|
||||||
|
String[] senderRequire() default { "console", "player" };
|
||||||
|
|
||||||
|
}
|
|
@ -1,117 +0,0 @@
|
||||||
package com.io.yutian.elementoriginlib.command.list;
|
|
||||||
|
|
||||||
import com.io.yutian.elementoriginlib.command.CommandContext;
|
|
||||||
import com.io.yutian.elementoriginlib.command.CommandNode;
|
|
||||||
import com.io.yutian.elementoriginlib.command.ICommand;
|
|
||||||
import com.io.yutian.elementoriginlib.command.ICommandManager;
|
|
||||||
import com.io.yutian.elementoriginlib.command.argument.Argument;
|
|
||||||
import com.io.yutian.elementoriginlib.command.argument.ArgumentTypes;
|
|
||||||
import com.io.yutian.elementoriginlib.lang.Lang;
|
|
||||||
import com.io.yutian.elementoriginlib.util.ComponentBuilder;
|
|
||||||
import com.io.yutian.elementoriginlib.list.PageList;
|
|
||||||
import net.kyori.adventure.text.Component;
|
|
||||||
import net.kyori.adventure.text.event.ClickEvent;
|
|
||||||
import net.kyori.adventure.text.event.HoverEvent;
|
|
||||||
import org.bukkit.command.CommandSender;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
import java.util.stream.Stream;
|
|
||||||
|
|
||||||
public class CommandHelp extends ICommand {
|
|
||||||
|
|
||||||
private ICommandManager commandManager;
|
|
||||||
@Nullable
|
|
||||||
private String alias;
|
|
||||||
|
|
||||||
public CommandHelp(ICommandManager commandManager) {
|
|
||||||
this(commandManager, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public CommandHelp(ICommandManager commandManager, String alias) {
|
|
||||||
super("help");
|
|
||||||
this.commandManager = commandManager;
|
|
||||||
this.alias = alias;
|
|
||||||
addArgument(Argument.argument("page", ArgumentTypes.INTEGER).optional(1));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean hasPermission(CommandSender sender) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void executes(CommandContext commandContext) {
|
|
||||||
String commandAlias = alias != null ? alias : commandContext.getLabel();
|
|
||||||
CommandSender sender = commandContext.getSender();
|
|
||||||
int page = commandContext.getArgumentsValue("page").getInt();
|
|
||||||
if (page <= 0) {
|
|
||||||
sender.sendMessage(Lang.get("command.help.page-error"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
List<ICommand> commands = commandManager.getCommands();
|
|
||||||
Stream<ICommand> stream = commands.stream().filter((c) -> c.hasPermission(sender));
|
|
||||||
List<ICommand> list = stream.collect(Collectors.toList());
|
|
||||||
PageList<ICommand> pageList = new PageList<>(list, 8);
|
|
||||||
if (page > pageList.size()) {
|
|
||||||
sender.sendMessage(Lang.get("command.help.page-error"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
sender.sendMessage(" ");
|
|
||||||
List<ICommand> commandList = pageList.getList(page);
|
|
||||||
sender.sendMessage("§7======[ §e§l"+commandManager.getName()+" §7]======");
|
|
||||||
for (ICommand command : commandList) {
|
|
||||||
StringBuilder stringBuilder = new StringBuilder("§6/"+commandAlias+" "+command.getName());
|
|
||||||
stringBuilder.append("§f");
|
|
||||||
if (command.getCommandNodes().size() > 0) {
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
sb.append(" [");
|
|
||||||
int i = 0;
|
|
||||||
for (CommandNode node : command.getCommandNodes()) {
|
|
||||||
sb.append(node.getName());
|
|
||||||
if (i + 1 < command.getCommandNodes().size()) {
|
|
||||||
sb.append("/");
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
sb.append("]");
|
|
||||||
stringBuilder.append(sb);
|
|
||||||
} else {
|
|
||||||
for (Argument argument : command.getArguments()) {
|
|
||||||
stringBuilder.append(" ");
|
|
||||||
stringBuilder.append("<"+argument.getName()+">");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (command.getDescription() != null) {
|
|
||||||
stringBuilder.append(" ");
|
|
||||||
stringBuilder.append("§7- §f"+command.getDescription());
|
|
||||||
}
|
|
||||||
sender.sendMessage(stringBuilder.toString());
|
|
||||||
}
|
|
||||||
ComponentBuilder componentBuilder = new ComponentBuilder();
|
|
||||||
boolean hasUpPage = page > 1;
|
|
||||||
boolean hasNextPage = page < pageList.size();
|
|
||||||
componentBuilder.add("§7=====");
|
|
||||||
if (hasUpPage) {
|
|
||||||
componentBuilder.add(" §7["+getColor(true)+"◀§7] ", ClickEvent.clickEvent(ClickEvent.Action.RUN_COMMAND, "/"+commandAlias+" help "+(page-1)), HoverEvent.hoverEvent(HoverEvent.Action.SHOW_TEXT, Component.text("§f上一页")));
|
|
||||||
} else {
|
|
||||||
componentBuilder.add(" §7["+getColor(false)+"◀§7] ");
|
|
||||||
}
|
|
||||||
componentBuilder.add("§7====");
|
|
||||||
componentBuilder.add("(§a"+page+"§f/§e"+pageList.size()+"§7)");
|
|
||||||
componentBuilder.add("§7====");
|
|
||||||
if (hasNextPage) {
|
|
||||||
componentBuilder.add(" §7["+getColor(true)+"▶§7] ", ClickEvent.clickEvent(ClickEvent.Action.RUN_COMMAND, "/"+commandAlias+" help "+(page+1)), HoverEvent.hoverEvent(HoverEvent.Action.SHOW_TEXT, Component.text("§f下一页")));
|
|
||||||
} else {
|
|
||||||
componentBuilder.add(" §7["+getColor(false)+"▶§7] ");
|
|
||||||
}
|
|
||||||
componentBuilder.add("§7=====");
|
|
||||||
sender.sendMessage(componentBuilder.build());
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getColor(boolean hasPage) {
|
|
||||||
return hasPage ? "§a" : "§c";
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -0,0 +1,34 @@
|
||||||
|
package com.io.yutian.elementoriginlib.command.list;
|
||||||
|
|
||||||
|
import com.io.yutian.elementoriginlib.command.CommandContext;
|
||||||
|
import com.io.yutian.elementoriginlib.command.interfaces.Command;
|
||||||
|
import com.io.yutian.elementoriginlib.command.interfaces.SubCommand;
|
||||||
|
import com.io.yutian.elementoriginlib.command.interfaces.CommandArgument;
|
||||||
|
|
||||||
|
@Command("test")
|
||||||
|
public class CommandTest {
|
||||||
|
|
||||||
|
@SubCommand("list")
|
||||||
|
public void list(CommandContext context, @CommandArgument int page) {
|
||||||
|
System.out.println(context.getSender());
|
||||||
|
System.out.println("list:"+page);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SubCommand("item.load")
|
||||||
|
public void item_load(@CommandArgument(suggestionType = "world_list") String id, int page, double k, CommandContext context) {
|
||||||
|
System.out.println(context.getSender());
|
||||||
|
System.out.println("load:"+id);
|
||||||
|
System.out.println(page);
|
||||||
|
System.out.println(k);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SubCommand("item.save")
|
||||||
|
public void item_save(@CommandArgument String id) {
|
||||||
|
System.out.println("save:"+id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SubCommand("item.test")
|
||||||
|
public void item_test(@CommandArgument String id) {
|
||||||
|
System.out.println(id);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.io.yutian.elementoriginlib.exception.command;
|
||||||
|
|
||||||
|
public class CommandRegisterException extends RuntimeException {
|
||||||
|
|
||||||
|
public CommandRegisterException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommandRegisterException(String s) {
|
||||||
|
super(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommandRegisterException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommandRegisterException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,22 +1,24 @@
|
||||||
package com.io.yutian.elementoriginlib.item.stat;
|
package com.io.yutian.elementoriginlib.item.stat;
|
||||||
|
|
||||||
import com.io.yutian.elementoriginlib.item.stat.list.IDStat;
|
import com.io.yutian.elementoriginlib.item.stat.list.ItemStatId;
|
||||||
import com.io.yutian.elementoriginlib.item.stat.type.StringStat;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class ItemStats {
|
public class ItemStats {
|
||||||
|
|
||||||
private static Map<String, ItemStat<?>> itemStats = new HashMap<>();
|
private static Map<Class<? extends ItemStat>, ItemStat<?>> itemStats = new HashMap<>();
|
||||||
|
|
||||||
public static final StringStat ID = register(new IDStat());
|
static {
|
||||||
|
register(ItemStatId.class, new ItemStatId());
|
||||||
|
}
|
||||||
|
|
||||||
public static <I extends ItemStat> I register(I itemStat) {
|
public static <I extends ItemStat> I register(Class<I> clazz, I itemStat) {
|
||||||
if (itemStats.containsKey(itemStat.getId())) {
|
if (itemStats.containsKey(itemStat.getId())) {
|
||||||
return itemStat;
|
return itemStat;
|
||||||
}
|
}
|
||||||
itemStats.put(itemStat.getId(), itemStat);
|
itemStats.put(clazz, itemStat);
|
||||||
return itemStat;
|
return itemStat;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,8 +26,19 @@ public class ItemStats {
|
||||||
itemStats.remove(itemStat.getId());
|
itemStats.remove(itemStat.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public static ItemStat getItemStat(Class<? extends ItemStat> clazz) {
|
||||||
|
return itemStats.get(clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
public static ItemStat getItemStat(String id) {
|
public static ItemStat getItemStat(String id) {
|
||||||
return itemStats.get(id);
|
for (ItemStat itemStat : itemStats.values()) {
|
||||||
|
if (itemStat.getId().equalsIgnoreCase(id)) {
|
||||||
|
return itemStat;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,9 @@ package com.io.yutian.elementoriginlib.item.stat.list;
|
||||||
|
|
||||||
import com.io.yutian.elementoriginlib.item.stat.type.StringStat;
|
import com.io.yutian.elementoriginlib.item.stat.type.StringStat;
|
||||||
|
|
||||||
public class IDStat extends StringStat {
|
public class ItemStatId extends StringStat {
|
||||||
|
|
||||||
public IDStat() {
|
public ItemStatId() {
|
||||||
super("id", "Id");
|
super("id", "Id");
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,10 +6,7 @@ import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class Lang {
|
public class Lang {
|
||||||
|
|
||||||
|
@ -29,6 +26,10 @@ public class Lang {
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Optional<String> getOptional(String key) {
|
||||||
|
return Optional.ofNullable(langMap.get(key));
|
||||||
|
}
|
||||||
|
|
||||||
public static void reload(Plugin plugin) {
|
public static void reload(Plugin plugin) {
|
||||||
if (!langFileMap.containsKey(plugin)) {
|
if (!langFileMap.containsKey(plugin)) {
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
package com.io.yutian.elementoriginlib.manager;
|
||||||
|
|
||||||
|
import com.io.yutian.elementoriginlib.command.SimpleCommandManager;
|
||||||
|
import com.io.yutian.elementoriginlib.command.list.CommandTest;
|
||||||
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
|
public class CommandManager extends SimpleCommandManager {
|
||||||
|
|
||||||
|
public CommandManager(Plugin plugin) {
|
||||||
|
super(plugin, "elementoriginlib");
|
||||||
|
// register(new CommandHelp(this, "help"));
|
||||||
|
register(CommandTest.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -49,6 +49,15 @@ public class ClassUtil {
|
||||||
PRIMITIVE_TO_WRAPPER.put(long.class, Long.class);
|
PRIMITIVE_TO_WRAPPER.put(long.class, Long.class);
|
||||||
PRIMITIVE_TO_WRAPPER.put(float.class, Float.class);
|
PRIMITIVE_TO_WRAPPER.put(float.class, Float.class);
|
||||||
PRIMITIVE_TO_WRAPPER.put(double.class, Double.class);
|
PRIMITIVE_TO_WRAPPER.put(double.class, Double.class);
|
||||||
|
PRIMITIVE_TO_WRAPPER.put(int[].class, Integer[].class);
|
||||||
|
PRIMITIVE_TO_WRAPPER.put(long[].class, Long[].class);
|
||||||
|
PRIMITIVE_TO_WRAPPER.put(float[].class, Float[].class);
|
||||||
|
PRIMITIVE_TO_WRAPPER.put(short[].class, Short[].class);
|
||||||
|
PRIMITIVE_TO_WRAPPER.put(byte[].class, Byte[].class);
|
||||||
|
PRIMITIVE_TO_WRAPPER.put(char[].class, Character[].class);
|
||||||
|
PRIMITIVE_TO_WRAPPER.put(boolean[].class, Boolean[].class);
|
||||||
|
PRIMITIVE_TO_WRAPPER.put(double[].class, Double[].class);
|
||||||
|
PRIMITIVE_TO_WRAPPER.put(void.class, Void.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,9 +4,12 @@ command:
|
||||||
short-arg: "&f缺少参数"
|
short-arg: "&f缺少参数"
|
||||||
unknown-arg: "&f未知的参数 &8(&7第$0个参数&8) &7&o[$1]"
|
unknown-arg: "&f未知的参数 &8(&7第$0个参数&8) &7&o[$1]"
|
||||||
error-arg: "&f参数错误 &8(&7第$0个参数&8) &7&o[$1]"
|
error-arg: "&f参数错误 &8(&7第$0个参数&8) &7&o[$1]"
|
||||||
no-player: "&f该指令只能由玩家执行"
|
error-sender-type: "&f无法执行此命令"
|
||||||
player-no-online: "&f玩家 &e$0 &f不存在或不在线"
|
player-no-online: "&f玩家 &e$0 &f不存在或不在线"
|
||||||
no-permission: "&f你没有执行该命令的权限"
|
no-permission: "&f你没有执行该命令的权限"
|
||||||
|
help:
|
||||||
|
description: "获得指令帮助"
|
||||||
|
page-error: "页码错误"
|
||||||
playerchatinput:
|
playerchatinput:
|
||||||
timeout: "&c输入超时"
|
timeout: "&c输入超时"
|
||||||
cancel: "&c输入已取消"
|
cancel: "&c输入已取消"
|
|
@ -3,3 +3,6 @@ version: '1.0'
|
||||||
main: com.io.yutian.elementoriginlib.ElementOriginLib
|
main: com.io.yutian.elementoriginlib.ElementOriginLib
|
||||||
api-version: '1.20'
|
api-version: '1.20'
|
||||||
authors: [ SuperYuTian ]
|
authors: [ SuperYuTian ]
|
||||||
|
commands:
|
||||||
|
elementoriginlib:
|
||||||
|
aliases: [ eol ]
|
Loading…
Reference in New Issue
Block a user