SteamWar/SpigotCore
Archiviert
13
0

Add SWCommand.Register.description to simplify help commands
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Dieser Commit ist enthalten in:
yoyosource 2021-11-20 18:56:38 +01:00
Ursprung 0ff834a4d0
Commit f1e5e39156
3 geänderte Dateien mit 82 neuen und 7 gelöschten Zeilen

Datei anzeigen

@ -46,6 +46,11 @@ dependencies {
compileOnly files("${project.rootDir}/lib/Spigot-1.15.jar") compileOnly files("${project.rootDir}/lib/Spigot-1.15.jar")
compileOnly files("${project.rootDir}/lib/WorldEdit-1.12.jar") compileOnly files("${project.rootDir}/lib/WorldEdit-1.12.jar")
implementation 'net.wesjd:anvilgui:1.4.0-SNAPSHOT' implementation 'net.wesjd:anvilgui:1.4.0-SNAPSHOT'
compileOnly 'org.projectlombok:lombok:1.18.6'
testCompileOnly 'org.projectlombok:lombok:1.18.6'
annotationProcessor 'org.projectlombok:lombok:1.18.6'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.6'
} }
processResources { processResources {

Datei anzeigen

@ -19,14 +19,18 @@
package de.steamwar.command; package de.steamwar.command;
import de.steamwar.message.Message;
import lombok.Setter;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.lang.annotation.*; import java.lang.annotation.*;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Parameter; import java.lang.reflect.Parameter;
import java.util.*; import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.IntPredicate; import java.util.function.IntPredicate;
import java.util.logging.Level; import java.util.logging.Level;
@ -41,6 +45,9 @@ public abstract class SWCommand {
private final Map<String, TypeMapper<?>> localTypeMapper = new HashMap<>(); private final Map<String, TypeMapper<?>> localTypeMapper = new HashMap<>();
private final Map<String, GuardChecker> localGuardChecker = new HashMap<>(); private final Map<String, GuardChecker> localGuardChecker = new HashMap<>();
@Setter
private Message message = null;
protected SWCommand(String command) { protected SWCommand(String command) {
this(command, new String[0]); this(command, new String[0]);
} }
@ -82,7 +89,7 @@ public abstract class SWCommand {
} }
private synchronized void createMapping() { private synchronized void createMapping() {
Method[] methods = getClass().getDeclaredMethods(); List<Method> methods = methods();
for (Method method : methods) { for (Method method : methods) {
addMapper(Mapper.class, method, i -> i == 0, false, TypeMapper.class, (anno, typeMapper) -> { addMapper(Mapper.class, method, i -> i == 0, false, TypeMapper.class, (anno, typeMapper) -> {
(anno.local() ? localTypeMapper : SWCommandUtils.MAPPER_FUNCTIONS).putIfAbsent(anno.value(), typeMapper); (anno.local() ? localTypeMapper : SWCommandUtils.MAPPER_FUNCTIONS).putIfAbsent(anno.value(), typeMapper);
@ -109,7 +116,7 @@ public abstract class SWCommand {
Bukkit.getLogger().log(Level.WARNING, () -> "The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument"); Bukkit.getLogger().log(Level.WARNING, () -> "The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument");
return; return;
} }
commandHelpList.add(new SubCommand(this, method, anno.value(), new HashMap<>(), localGuardChecker, true)); commandHelpList.add(new SubCommand(this, method, anno.value(), new HashMap<>(), localGuardChecker, true, null));
}); });
} }
for (Method method : methods) { for (Method method : methods) {
@ -131,7 +138,7 @@ public abstract class SWCommand {
return; return;
} }
} }
commandList.add(new SubCommand(this, method, anno.value(), localTypeMapper, localGuardChecker, false)); commandList.add(new SubCommand(this, method, anno.value(), localTypeMapper, localGuardChecker, false, anno.description()));
}); });
this.commandList.sort((o1, o2) -> { this.commandList.sort((o1, o2) -> {
@ -143,7 +150,15 @@ public abstract class SWCommand {
o2.varArgType != null ? Integer.MAX_VALUE : o2.arguments.length); o2.varArgType != null ? Integer.MAX_VALUE : o2.arguments.length);
} }
}); });
commandHelpList.sort(Comparator.comparingInt(o -> -o.subCommand.length)); commandHelpList.sort((o1, o2) -> {
int compare = Integer.compare(-o1.subCommand.length, -o2.subCommand.length);
if (compare != 0) {
return compare;
} else {
return Integer.compare(o1.method.getDeclaringClass() == SWCommand.class ? 1 : 0,
o2.method.getDeclaringClass() == SWCommand.class ? 1 : 0);
}
});
} }
initialized = true; initialized = true;
} }
@ -190,6 +205,12 @@ public abstract class SWCommand {
}); });
} }
private List<Method> methods() {
List<Method> methods = new ArrayList<>(Arrays.asList(getClass().getDeclaredMethods()));
methods.addAll(Arrays.asList(SWCommand.class.getDeclaredMethods()));
return methods;
}
public void unregister() { public void unregister() {
SWCommandUtils.knownCommandMap.remove(command.getName()); SWCommandUtils.knownCommandMap.remove(command.getName());
command.getAliases().forEach(SWCommandUtils.knownCommandMap::remove); command.getAliases().forEach(SWCommandUtils.knownCommandMap::remove);
@ -200,6 +221,51 @@ public abstract class SWCommand {
SWCommandUtils.commandMap.register("steamwar", this.command); SWCommandUtils.commandMap.register("steamwar", this.command);
} }
@Register(help = true)
private void internalHelp(Player p, String... args) {
if (message == null) {
return;
}
try {
message.sendPrefixless("COMMAND_HELP_HEAD", p, command.getName());
} catch (Exception e) {
Bukkit.getLogger().log(Level.WARNING, "Failed to send help message", e);
return;
}
AtomicInteger atomicInteger = new AtomicInteger();
if (args.length != 0) {
commandList.forEach(subCommand -> {
List<String> tabCompletes = subCommand.tabComplete(p, args);
if (tabCompletes == null || tabCompletes.isEmpty()) {
atomicInteger.incrementAndGet();
return;
}
boolean hasTabCompletes = tabCompletes.stream()
.anyMatch(s -> s.toLowerCase().startsWith(args[args.length - 1].toLowerCase()));
if (hasTabCompletes) {
try {
message.sendPrefixless(subCommand.description, p);
} catch (Exception e) {
Bukkit.getLogger().log(Level.WARNING, "Failed to send description of registered method '" + subCommand.method + "' with description '" + subCommand.description + "'", e);
}
} else {
atomicInteger.incrementAndGet();
}
});
}
if (args.length == 0 || atomicInteger.get() == commandList.size()) {
commandList.forEach(subCommand -> {
if (subCommand.guardChecker == null || subCommand.guardChecker.guard(p, GuardCheckType.TAB_COMPLETE, new String[0], null) == GuardResult.ALLOWED) {
try {
message.sendPrefixless(subCommand.description, p);
} catch (Exception e) {
Bukkit.getLogger().log(Level.WARNING, "Failed to send description of registered method '" + subCommand.method + "' with description '" + subCommand.description + "'", e);
}
}
});
}
}
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD}) @Target({ElementType.METHOD})
@Repeatable(Register.Registeres.class) @Repeatable(Register.Registeres.class)
@ -208,6 +274,8 @@ public abstract class SWCommand {
boolean help() default false; boolean help() default false;
String description() default "";
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD}) @Target({ElementType.METHOD})
@interface Registeres { @interface Registeres {

Datei anzeigen

@ -35,20 +35,22 @@ import static de.steamwar.command.SWCommandUtils.*;
class SubCommand { class SubCommand {
private SWCommand swCommand; private SWCommand swCommand;
private Method method; Method method;
String description;
String[] subCommand; String[] subCommand;
TypeMapper<?>[] arguments; TypeMapper<?>[] arguments;
GuardChecker[] guards; GuardChecker[] guards;
private Predicate<CommandSender> commandSenderPredicate; private Predicate<CommandSender> commandSenderPredicate;
private Function<CommandSender, ?> commandSenderFunction; private Function<CommandSender, ?> commandSenderFunction;
private GuardChecker guardChecker; GuardChecker guardChecker;
Class<?> varArgType = null; Class<?> varArgType = null;
private boolean help; private boolean help;
SubCommand(SWCommand swCommand, Method method, String[] subCommand, Map<String, TypeMapper<?>> localTypeMapper, Map<String, GuardChecker> localGuardChecker, boolean help) { SubCommand(SWCommand swCommand, Method method, String[] subCommand, Map<String, TypeMapper<?>> localTypeMapper, Map<String, GuardChecker> localGuardChecker, boolean help, String description) {
this.swCommand = swCommand; this.swCommand = swCommand;
this.method = method; this.method = method;
this.help = help; this.help = help;
this.description = description;
Parameter[] parameters = method.getParameters(); Parameter[] parameters = method.getParameters();
commandSenderPredicate = sender -> parameters[0].getType().isAssignableFrom(sender.getClass()); commandSenderPredicate = sender -> parameters[0].getType().isAssignableFrom(sender.getClass());