Simplify CommandFramework
Remove InternalCommand.java Remove InternalTabComplete.java Add SubCommand
Dieser Commit ist enthalten in:
Ursprung
f0bf20e960
Commit
ea4d6f2a0a
@ -1,89 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is a part of the SteamWar software.
|
|
||||||
*
|
|
||||||
* Copyright (C) 2020 SteamWar.de-Serverteam
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU Affero General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU Affero General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU Affero General Public License
|
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package de.steamwar.command;
|
|
||||||
|
|
||||||
import org.bukkit.command.CommandSender;
|
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.lang.reflect.Parameter;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
class InternalCommand {
|
|
||||||
|
|
||||||
private SWCommand swCommand;
|
|
||||||
private Method method;
|
|
||||||
private int increment = 0;
|
|
||||||
private Parameter[] parameters;
|
|
||||||
|
|
||||||
InternalCommand(SWCommand swCommand, Method method) {
|
|
||||||
this.swCommand = swCommand;
|
|
||||||
this.method = method;
|
|
||||||
parameters = method.getParameters();
|
|
||||||
|
|
||||||
increment = method.getAnnotation(SWCommand.Register.class).subCommand().length;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean invoke(CommandSender commandSender, String[] args) {
|
|
||||||
if (args.length < parameters.length - 1) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
Object[] objects = SWCommandUtils.generateArgumentArray(commandSender, method, parameters, args);
|
|
||||||
method.setAccessible(true);
|
|
||||||
method.invoke(swCommand, objects);
|
|
||||||
} catch (IllegalArgumentException | IllegalAccessException e) {
|
|
||||||
throw new SecurityException(e.getMessage(), e);
|
|
||||||
} catch (InvocationTargetException e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
List<String> tabComplete(String[] args) {
|
|
||||||
if (args.length > parameters.length - increment) {
|
|
||||||
if (parameters[parameters.length - 1].isVarArgs()) {
|
|
||||||
String name = parameters[parameters.length - 1].getType().getComponentType().getTypeName();
|
|
||||||
SWCommand.Mapper mapper = parameters[parameters.length - 1].getAnnotation(SWCommand.Mapper.class);
|
|
||||||
if (mapper != null) {
|
|
||||||
name = mapper.mapper();
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
return SWCommandUtils.MAPPER_FUNCTIONS.getOrDefault(name, SWCommandUtils.ERROR_FUNCTION).tabCompletes(args[args.length - 1]);
|
|
||||||
} catch (Exception e) {
|
|
||||||
// Ignored
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Collections.emptyList();
|
|
||||||
}
|
|
||||||
String name = parameters[args.length - increment + 1].getType().getTypeName();
|
|
||||||
SWCommand.Mapper mapper = parameters[args.length - increment + 1].getAnnotation(SWCommand.Mapper.class);
|
|
||||||
if (mapper != null) {
|
|
||||||
name = mapper.mapper();
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
return SWCommandUtils.MAPPER_FUNCTIONS.getOrDefault(name, SWCommandUtils.ERROR_FUNCTION).tabCompletes(args[args.length - 1]);
|
|
||||||
} catch (Exception e) {
|
|
||||||
return Collections.emptyList();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,55 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is a part of the SteamWar software.
|
|
||||||
*
|
|
||||||
* Copyright (C) 2020 SteamWar.de-Serverteam
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU Affero General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU Affero General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU Affero General Public License
|
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package de.steamwar.command;
|
|
||||||
|
|
||||||
import org.bukkit.command.CommandSender;
|
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.lang.reflect.Parameter;
|
|
||||||
|
|
||||||
class InternalTabComplete {
|
|
||||||
|
|
||||||
private SWCommand swCommand;
|
|
||||||
private Method method;
|
|
||||||
private Parameter[] parameters;
|
|
||||||
|
|
||||||
InternalTabComplete(SWCommand swCommand, Method method) {
|
|
||||||
this.swCommand = swCommand;
|
|
||||||
this.method = method;
|
|
||||||
parameters = method.getParameters();
|
|
||||||
}
|
|
||||||
|
|
||||||
SWCommandUtils.TabComplete invoke(CommandSender commandSender, String[] args) {
|
|
||||||
if (args.length < parameters.length - 1) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
Object[] objects = SWCommandUtils.generateArgumentArray(commandSender, method, parameters, args);
|
|
||||||
method.setAccessible(true);
|
|
||||||
return (SWCommandUtils.TabComplete) method.invoke(swCommand, objects);
|
|
||||||
} catch (IllegalArgumentException | IllegalAccessException e) {
|
|
||||||
throw new SecurityException(e.getMessage(), e);
|
|
||||||
} catch (InvocationTargetException e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -33,9 +33,7 @@ import java.util.function.Consumer;
|
|||||||
|
|
||||||
public abstract class SWCommand {
|
public abstract class SWCommand {
|
||||||
|
|
||||||
private final Set<InternalCommand> commandSet = new HashSet<>();
|
private final Set<SubCommand> commandSet = new HashSet<>();
|
||||||
private final Set<InternalTabComplete> tabCompleteSet = new HashSet<>();
|
|
||||||
private final Map<Integer, Set<String>> subCommandTabCompletes = new HashMap<>();
|
|
||||||
private Consumer<CommandSender> helpMessage = sender -> {
|
private Consumer<CommandSender> helpMessage = sender -> {
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -47,8 +45,8 @@ public abstract class SWCommand {
|
|||||||
SWCommandUtils.commandMap.register("steamwar", new Command(command, "", "/" + command, Arrays.asList(aliases)) {
|
SWCommandUtils.commandMap.register("steamwar", new Command(command, "", "/" + command, Arrays.asList(aliases)) {
|
||||||
@Override
|
@Override
|
||||||
public boolean execute(CommandSender sender, String alias, String[] args) {
|
public boolean execute(CommandSender sender, String alias, String[] args) {
|
||||||
for (InternalCommand internalCommand : commandSet) {
|
for (SubCommand subCommand : commandSet) {
|
||||||
if (internalCommand.invoke(sender, args)) {
|
if (subCommand.invoke(sender, args)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -58,17 +56,11 @@ public abstract class SWCommand {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
|
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
|
||||||
List<String> strings = new ArrayList<>(subCommandTabCompletes.getOrDefault(args.length, new HashSet<>()));
|
List<String> strings = new ArrayList<>();
|
||||||
for (InternalTabComplete internalTabComplete : tabCompleteSet) {
|
for (SubCommand subCommand : commandSet) {
|
||||||
SWCommandUtils.TabComplete tabComplete = internalTabComplete.invoke(sender, args);
|
List<String> tabCompletes = subCommand.tabComplete(sender, args);
|
||||||
if (tabComplete != null) {
|
if (tabCompletes != null) {
|
||||||
strings.addAll(tabComplete.tabCompletes);
|
strings.addAll(tabCompletes);
|
||||||
}
|
|
||||||
}
|
|
||||||
for (InternalCommand internalCommand : commandSet) {
|
|
||||||
List<String> stringList = internalCommand.tabComplete(args);
|
|
||||||
if (stringList != null) {
|
|
||||||
strings.addAll(stringList);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
strings = new ArrayList<>(strings);
|
strings = new ArrayList<>(strings);
|
||||||
@ -86,19 +78,10 @@ public abstract class SWCommand {
|
|||||||
if (register == null) {
|
if (register == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
for (int i = 0; i < register.subCommand().length; i++) {
|
|
||||||
subCommandTabCompletes.computeIfAbsent(i, integer -> new HashSet<>()).add(register.subCommand()[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!validMethod(method)) {
|
if (!validMethod(method)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (method.getReturnType() == Void.TYPE) {
|
commandSet.add(new SubCommand(this, method));
|
||||||
commandSet.add(new InternalCommand(this, method));
|
|
||||||
}
|
|
||||||
if (method.getReturnType() == SWCommandUtils.TabComplete.class) {
|
|
||||||
tabCompleteSet.add(new InternalTabComplete(this, method));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,7 +40,7 @@ class SWCommandUtils {
|
|||||||
|
|
||||||
static final Map<String, TypeMapper<?>> MAPPER_FUNCTIONS = new HashMap<>();
|
static final Map<String, TypeMapper<?>> MAPPER_FUNCTIONS = new HashMap<>();
|
||||||
|
|
||||||
static final TypeMapper<?> ERROR_FUNCTION = new TypeMapper<>() {
|
static final TypeMapper<?> ERROR_FUNCTION = new TypeMapper<Object>() {
|
||||||
@Override
|
@Override
|
||||||
public Object map(String s) {
|
public Object map(String s) {
|
||||||
throw new SecurityException();
|
throw new SecurityException();
|
||||||
@ -173,36 +173,31 @@ class SWCommandUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Object[] generateArgumentArray(CommandSender commandSender, Method method, Parameter[] parameters, String[] args) {
|
public static Object[] generateArgumentArray(TypeMapper<?>[] parameters, String[] args, boolean varArgs, String[] subCommand) {
|
||||||
Object[] arguments = new Object[args.length + 1];
|
Object[] arguments = new Object[parameters.length + 1];
|
||||||
boolean varArgs = false;
|
int index = 0;
|
||||||
if (parameters[parameters.length - 1].isVarArgs()) {
|
while (index < subCommand.length) {
|
||||||
varArgs = true;
|
if (!args[index].equals(subCommand[index])) {
|
||||||
arguments = new Object[parameters.length];
|
throw new SecurityException();
|
||||||
}
|
}
|
||||||
arguments[0] = parameters[0].getType().cast(commandSender);
|
index++;
|
||||||
|
|
||||||
SWCommand.Register register = method.getAnnotation(SWCommand.Register.class);
|
|
||||||
int subCommandIndex = 1;
|
|
||||||
for (String s : register.subCommand()) {
|
|
||||||
if (!args[subCommandIndex++].equals(s)) throw new SecurityException();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = subCommandIndex; i < parameters.length - (varArgs ? 1 : 0); i++) {
|
for (int i = 0; i < parameters.length - (varArgs ? 1 : 0); i++) {
|
||||||
arguments[i] = mapper(parameters[i]).apply(args[i - 1]);
|
arguments[i + 1] = parameters[i].map(args[index++]);
|
||||||
|
if (arguments[i + 1] == null) {
|
||||||
|
throw new SecurityException();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (varArgs) {
|
if (varArgs) {
|
||||||
Object[] varArgument = new Object[args.length - parameters.length + 2];
|
Object[] varArgument = new Object[args.length - parameters.length + 2];
|
||||||
arguments[arguments.length - 1] = varArgument;
|
arguments[arguments.length - 1] = varArgument;
|
||||||
Function<String, Object> mapper = mapper(parameters[parameters.length - 1]);
|
|
||||||
|
|
||||||
int index = 0;
|
for (int i = 0; i < varArgument.length; i++) {
|
||||||
for (int i = parameters.length - 2; i < args.length; i++) {
|
varArgument[i] = parameters[parameters.length - 1].map(args[index++]);
|
||||||
varArgument[index++] = mapper.apply(args[i]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return arguments;
|
return arguments;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
92
SpigotCore_Main/src/de/steamwar/command/SubCommand.java
Normale Datei
92
SpigotCore_Main/src/de/steamwar/command/SubCommand.java
Normale Datei
@ -0,0 +1,92 @@
|
|||||||
|
package de.steamwar.command;
|
||||||
|
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.lang.reflect.Parameter;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
class SubCommand {
|
||||||
|
|
||||||
|
private SWCommand swCommand;
|
||||||
|
private Method method;
|
||||||
|
private String[] subCommand;
|
||||||
|
private TypeMapper<?>[] arguments;
|
||||||
|
private boolean varArgs = false;
|
||||||
|
private Function<CommandSender, ?> commandSenderFunction;
|
||||||
|
|
||||||
|
public SubCommand(SWCommand swCommand, Method method) {
|
||||||
|
this.swCommand = swCommand;
|
||||||
|
this.method = method;
|
||||||
|
|
||||||
|
Parameter[] parameters = method.getParameters();
|
||||||
|
commandSenderFunction = sender -> parameters[0].getType().cast(sender);
|
||||||
|
SWCommand.Register register = method.getAnnotation(SWCommand.Register.class);
|
||||||
|
subCommand = register.subCommand();
|
||||||
|
|
||||||
|
arguments = new TypeMapper[parameters.length - 1];
|
||||||
|
for (int i = 1; i < parameters.length; i++) {
|
||||||
|
Parameter parameter = parameters[i];
|
||||||
|
Class<?> clazz = parameter.getType();
|
||||||
|
if (parameter.isVarArgs()) {
|
||||||
|
varArgs = true;
|
||||||
|
clazz = clazz.getComponentType();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (clazz.isEnum()) {
|
||||||
|
Class<Enum<?>> enumClass = (Class<Enum<?>>) clazz;
|
||||||
|
List<String> tabCompletes = new ArrayList<>();
|
||||||
|
for (Enum<?> enumConstant : enumClass.getEnumConstants()) {
|
||||||
|
tabCompletes.add(enumConstant.name().toLowerCase());
|
||||||
|
}
|
||||||
|
arguments[i] = new TypeMapper<Object>() {
|
||||||
|
@Override
|
||||||
|
public Object map(String s) {
|
||||||
|
return SWCommandUtils.ENUM_MAPPER.apply(enumClass, s);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> tabCompletes(String s) {
|
||||||
|
return tabCompletes;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
String name = clazz.getTypeName();
|
||||||
|
SWCommand.Mapper mapper = parameter.getAnnotation(SWCommand.Mapper.class);
|
||||||
|
if (mapper != null) {
|
||||||
|
name = mapper.mapper();
|
||||||
|
}
|
||||||
|
|
||||||
|
arguments[i] = SWCommandUtils.MAPPER_FUNCTIONS.getOrDefault(name, SWCommandUtils.ERROR_FUNCTION);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean invoke(CommandSender commandSender, String[] args) {
|
||||||
|
if (args.length < arguments.length - 1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Object[] objects = SWCommandUtils.generateArgumentArray(arguments, args, varArgs, subCommand);
|
||||||
|
objects[0] = commandSenderFunction.apply(commandSender);
|
||||||
|
method.setAccessible(true);
|
||||||
|
method.invoke(swCommand, objects);
|
||||||
|
} catch (IllegalArgumentException | IllegalAccessException e) {
|
||||||
|
throw new SecurityException(e.getMessage(), e);
|
||||||
|
} catch (InvocationTargetException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<String> tabComplete(CommandSender commandSender, String[] args) {
|
||||||
|
// TODO: implement tabCompleting for SubCommand
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
In neuem Issue referenzieren
Einen Benutzer sperren