Hotfix help system
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Dieser Commit ist enthalten in:
yoyosource 2023-03-05 21:54:35 +01:00
Ursprung 92cddbada1
Commit 6f7846956e
5 geänderte Dateien mit 40 neuen und 2 gelöschten Zeilen

Datei anzeigen

@ -40,6 +40,7 @@ public abstract class AbstractSWCommand<T> {
private boolean initialized = false; private boolean initialized = false;
protected final List<SubCommand<T>> commandList = new ArrayList<>(); protected final List<SubCommand<T>> commandList = new ArrayList<>();
protected final List<SubCommand<T>> helpCommandList = new ArrayList<>();
private final Map<String, AbstractTypeMapper<T, ?>> localTypeMapper = new HashMap<>(); private final Map<String, AbstractTypeMapper<T, ?>> localTypeMapper = new HashMap<>();
private final Map<String, AbstractValidator<T, ?>> localValidators = new HashMap<>(); private final Map<String, AbstractValidator<T, ?>> localValidators = new HashMap<>();
@ -83,7 +84,9 @@ public abstract class AbstractSWCommand<T> {
initialize(); initialize();
List<Runnable> errors = new ArrayList<>(); List<Runnable> errors = new ArrayList<>();
try { try {
if (!commandList.stream().anyMatch(s -> s.invoke(errors::add, sender, alias, args))) { if (commandList.stream().noneMatch(s -> s.invoke(errors::add, sender, alias, args))) {
errors.forEach(Runnable::run);
} else if (helpCommandList.stream().noneMatch(s -> s.invoke(errors::add, sender, alias, args))) {
errors.forEach(Runnable::run); errors.forEach(Runnable::run);
} }
} catch (CommandFrameworkException e) { } catch (CommandFrameworkException e) {
@ -158,6 +161,14 @@ public abstract class AbstractSWCommand<T> {
} }
Collections.sort(commandList); Collections.sort(commandList);
commandList.removeIf(subCommand -> {
if (subCommand.isHelp) {
helpCommandList.add(subCommand);
return true;
} else {
return false;
}
});
initialized = true; initialized = true;
} }

Datei anzeigen

@ -83,6 +83,20 @@ class CommandPart<T> {
this.next = next; this.next = next;
} }
public boolean isHelp() {
if (next == null) {
if (varArgType == null) {
return false;
}
if (varArgType != String.class) {
return false;
}
return typeMapper == SWCommandUtils.STRING_MAPPER;
} else {
return next.isHelp();
}
}
public void generateArgumentArray(Consumer<Runnable> errors, List<Object> current, T sender, String[] args, int startIndex) { public void generateArgumentArray(Consumer<Runnable> errors, List<Object> current, T sender, String[] args, int startIndex) {
if (varArgType != null) { if (varArgType != null) {
Object array = Array.newInstance(varArgType, args.length - startIndex); Object array = Array.newInstance(varArgType, args.length - startIndex);

Datei anzeigen

@ -51,6 +51,8 @@ public class SWCommandUtils {
} }
}; };
static final AbstractTypeMapper<Object, String> STRING_MAPPER = createMapper(s -> s, Collections::singletonList);
static { static {
addMapper(boolean.class, Boolean.class, createMapper(s -> { addMapper(boolean.class, Boolean.class, createMapper(s -> {
if (s.equalsIgnoreCase("true")) return true; if (s.equalsIgnoreCase("true")) return true;
@ -61,7 +63,7 @@ public class SWCommandUtils {
addMapper(double.class, Double.class, createMapper(numberMapper(Double::parseDouble), numberCompleter(Double::parseDouble, true))); addMapper(double.class, Double.class, createMapper(numberMapper(Double::parseDouble), numberCompleter(Double::parseDouble, true)));
addMapper(int.class, Integer.class, createMapper(numberMapper(Integer::parseInt), numberCompleter(Integer::parseInt, false))); addMapper(int.class, Integer.class, createMapper(numberMapper(Integer::parseInt), numberCompleter(Integer::parseInt, false)));
addMapper(long.class, Long.class, createMapper(numberMapper(Long::parseLong), numberCompleter(Long::parseLong, false))); addMapper(long.class, Long.class, createMapper(numberMapper(Long::parseLong), numberCompleter(Long::parseLong, false)));
MAPPER_FUNCTIONS.put(String.class.getTypeName(), createMapper(s -> s, Collections::singletonList)); MAPPER_FUNCTIONS.put(String.class.getTypeName(), STRING_MAPPER);
} }
public static <T extends AbstractTypeMapper<K, V>, K, V> void init(SWTypeMapperCreator<T, K, V> swTypeMapperCreator) { public static <T extends AbstractTypeMapper<K, V>, K, V> void init(SWTypeMapperCreator<T, K, V> swTypeMapperCreator) {

Datei anzeigen

@ -47,6 +47,8 @@ public class SubCommand<T> implements Comparable<SubCommand<T>> {
private CommandPart<T> commandPart; private CommandPart<T> commandPart;
boolean isHelp = false;
SubCommand(AbstractSWCommand<T> abstractSWCommand, Method method, String[] subCommand, Map<String, AbstractTypeMapper<T, ?>> localTypeMapper, Map<String, AbstractValidator<T, ?>> localValidator, String[] description, boolean noTabComplete) { SubCommand(AbstractSWCommand<T> abstractSWCommand, Method method, String[] subCommand, Map<String, AbstractTypeMapper<T, ?>> localTypeMapper, Map<String, AbstractValidator<T, ?>> localValidator, String[] description, boolean noTabComplete) {
this.abstractSWCommand = abstractSWCommand; this.abstractSWCommand = abstractSWCommand;
this.method = method; this.method = method;
@ -70,6 +72,7 @@ public class SubCommand<T> implements Comparable<SubCommand<T>> {
} }
commandPart = generateCommandPart(abstractSWCommand, subCommand, parameters, localTypeMapper, localValidator); commandPart = generateCommandPart(abstractSWCommand, subCommand, parameters, localTypeMapper, localValidator);
if (commandPart != null) isHelp = commandPart.isHelp();
senderPredicate = t -> parameters[0].getType().isAssignableFrom(t.getClass()); senderPredicate = t -> parameters[0].getType().isAssignableFrom(t.getClass());
senderFunction = t -> parameters[0].getType().cast(t); senderFunction = t -> parameters[0].getType().cast(t);

Datei anzeigen

@ -21,6 +21,9 @@ package de.steamwar.command;
import de.steamwar.command.dto.ExecutionIdentifier; import de.steamwar.command.dto.ExecutionIdentifier;
import de.steamwar.command.dto.TestSWCommand; import de.steamwar.command.dto.TestSWCommand;
import de.steamwar.command.dto.TestTypeMapper;
import java.util.Collections;
public class SubCMDSortingCommand extends TestSWCommand { public class SubCMDSortingCommand extends TestSWCommand {
@ -47,4 +50,9 @@ public class SubCMDSortingCommand extends TestSWCommand {
public void test(String s, String... args) { public void test(String s, String... args) {
throw new ExecutionIdentifier("Command with n parameters"); throw new ExecutionIdentifier("Command with n parameters");
} }
@ClassMapper(value = String.class, local = true)
public AbstractTypeMapper<String, String> stringMapper() {
return SWCommandUtils.createMapper(s -> s, Collections::singletonList);
}
} }