/*
* 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 .
*/
package de.steamwar.command;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
public class SubCommand implements Comparable> {
private AbstractSWCommand abstractSWCommand;
Method method;
String[] description;
String[] subCommand;
private Predicate senderPredicate;
private Function senderFunction;
AbstractValidator validator;
boolean noTabComplete;
private Parameter[] parameters;
private CommandPart commandPart;
boolean isHelp = false;
SubCommand(AbstractSWCommand abstractSWCommand, Method method, String[] subCommand, Map> localTypeMapper, Map> localValidator, String[] description, boolean noTabComplete) {
this.abstractSWCommand = abstractSWCommand;
this.method = method;
try {
this.method.setAccessible(true);
} catch (SecurityException e) {
throw new SecurityException(e.getMessage(), e);
}
this.subCommand = subCommand;
this.description = description;
this.noTabComplete = noTabComplete;
parameters = method.getParameters();
AbstractSWCommand.Validator validator = parameters[0].getAnnotation(AbstractSWCommand.Validator.class);
if (validator != null) {
this.validator = (AbstractValidator) SWCommandUtils.getValidator(validator, parameters[0].getType(), localValidator);
if (validator.invert()) {
AbstractValidator current = this.validator;
this.validator = (sender, value, messageSender) -> !current.validate(sender, value, messageSender);
}
}
commandPart = generateCommandPart(abstractSWCommand, subCommand, parameters, localTypeMapper, localValidator);
if (commandPart != null) isHelp = commandPart.isHelp();
senderPredicate = t -> parameters[0].getType().isAssignableFrom(t.getClass());
senderFunction = t -> parameters[0].getType().cast(t);
}
@Override
public int compareTo(SubCommand o) {
int tLength = parameters.length + subCommand.length;
int oLength = o.parameters.length + o.subCommand.length;
boolean tVarArgs = parameters[parameters.length - 1].isVarArgs();
boolean oVarArgs = o.parameters[o.parameters.length - 1].isVarArgs();
if (tVarArgs) tLength *= -1;
if (oVarArgs) oLength *= -1;
if (tVarArgs && oVarArgs) return Integer.compare(tLength, oLength);
return -Integer.compare(tLength, oLength);
}
boolean invoke(Consumer errors, T sender, String alias, String[] args) {
try {
if (!senderPredicate.test(sender)) {
return false;
}
if (commandPart == null) {
if (args.length != 0) {
return false;
}
if (validator != null) {
if (!validator.validate(sender, sender, (s, objectArgs) -> {
abstractSWCommand.sendMessage(sender, s, objectArgs);
})) {
return false;
}
}
method.invoke(abstractSWCommand, senderFunction.apply(sender));
} else {
List