SteamWar/SpigotCore
Archiviert
13
0

Make HelpMessage more advanced

Dieser Commit ist enthalten in:
yoyosource 2021-03-29 22:14:52 +02:00
Ursprung e56d4c0e42
Commit 7e733fca7c
3 geänderte Dateien mit 18 neuen und 9 gelöschten Zeilen

Datei anzeigen

@ -36,8 +36,9 @@ public class TestCommand extends SWCommand {
}
// One Help Command, the first Parameter should be some kind of CommandSender
// The second argument can only be a varAgrs string of what arguments was tried to map
@RegisterHelp
public void testHelp(Player player) {
public void testHelp(Player player, String... args) {
player.sendMessage("This is your help message");
}

Datei anzeigen

@ -90,6 +90,12 @@ public abstract class SWCommand {
}
});
add(RegisterHelp.class, method, i -> i != 1, true, null, (anno, parameters) -> {
if (!parameters[parameters.length - 1].isVarArgs()) {
return;
}
if (parameters[parameters.length - 1].getType().getComponentType() != String.class) {
return;
}
commandHelpSet.add(new SubCommand(this, method, anno.value()));
});
}

Datei anzeigen

@ -24,10 +24,7 @@ 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.Collections;
import java.util.List;
import java.util.*;
import java.util.function.Function;
class SubCommand {
@ -63,7 +60,7 @@ class SubCommand {
for (Enum<?> enumConstant : enumClass.getEnumConstants()) {
tabCompletes.add(enumConstant.name().toLowerCase());
}
arguments[i] = SWCommandUtils.createMapper(s -> SWCommandUtils.ENUM_MAPPER.apply(enumClass, s), s -> tabCompletes);
arguments[i - 1] = SWCommandUtils.createMapper(s -> SWCommandUtils.ENUM_MAPPER.apply(enumClass, s), s -> tabCompletes);
continue;
}
@ -72,7 +69,7 @@ class SubCommand {
name = mapper.value();
}
arguments[i] = SWCommandUtils.MAPPER_FUNCTIONS.getOrDefault(name, SWCommandUtils.ERROR_FUNCTION);
arguments[i - 1] = SWCommandUtils.MAPPER_FUNCTIONS.getOrDefault(name, SWCommandUtils.ERROR_FUNCTION);
}
}
@ -80,6 +77,9 @@ class SubCommand {
if (args.length < arguments.length - 1) {
return false;
}
if (!varArgs && args.length > arguments.length) {
return false;
}
try {
Object[] objects = SWCommandUtils.generateArgumentArray(arguments, args, varArgs, subCommand);
objects[0] = commandSenderFunction.apply(commandSender);
@ -97,7 +97,7 @@ class SubCommand {
if (!varArgs && args.length < arguments.length - 1) {
return Collections.emptyList();
}
List<String> argsList = Arrays.asList(args);
List<String> argsList = new LinkedList<>(Arrays.asList(args));
for (String value : subCommand) {
String s = argsList.remove(0);
if (argsList.isEmpty()) return Collections.singletonList(value);
@ -107,7 +107,9 @@ class SubCommand {
String s = argsList.remove(0);
if (argsList.isEmpty()) return argument.tabCompletes(s);
try {
argument.map(s);
if (argument.map(s) == null) {
return Collections.emptyList();
}
} catch (Exception e) {
return Collections.emptyList();
}