SteamWar/SpigotCore
Archiviert
13
0

Add potential StartUpTime decrease by lazy loading commands only on usage #121

Zusammengeführt
Lixfel hat 4 Commits von StartTime nach master 2021-10-22 20:11:38 +02:00 zusammengeführt
Nur Änderungen aus Commit 8a31be11f5 werden angezeigt - Alle Commits anzeigen

Datei anzeigen

@ -34,6 +34,7 @@ import java.util.stream.Collectors;
public abstract class SWCommand {
private boolean initialized = false;
private final Command command;
private final List<SubCommand> commandList = new ArrayList<>();
private final List<SubCommand> commandHelpList = new ArrayList<>();
@ -47,6 +48,9 @@ public abstract class SWCommand {
this.command = new Command(command, "", "/" + command, Arrays.asList(aliases)) {
@Override
public boolean execute(CommandSender sender, String alias, String[] args) {
if (!initialized) {
createMapping();
}
if (commandList.stream().anyMatch(s -> s.invoke(sender, args))) return false;
commandHelpList.stream().anyMatch(s -> s.invoke(sender, args));
return false;
@ -54,6 +58,9 @@ public abstract class SWCommand {
@Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
if (!initialized) {
createMapping();
}
String string = args[args.length - 1].toLowerCase();
return commandList.stream()
.map(s -> s.tabComplete(sender, args))
@ -66,7 +73,11 @@ public abstract class SWCommand {
};
unregister();
register();
}
private synchronized void createMapping() {
Bukkit.getLogger().log(Level.INFO, () -> "Initializing: " + getClass().getTypeName());
long time = System.currentTimeMillis();
Method[] methods = getClass().getDeclaredMethods();
for (Method method : methods) {
addMapper(Mapper.class, method, i -> i == 0, false, TypeMapper.class, (anno, typeMapper) -> {
@ -123,6 +134,8 @@ public abstract class SWCommand {
});
commandHelpList.sort(Comparator.comparingInt(o -> -o.subCommand.length));
}
Bukkit.getLogger().log(Level.INFO, () -> "Load time for " + getClass().getTypeName() + " was " + (System.currentTimeMillis() - time) + "ms");
initialized = true;
}
private <T extends Annotation> void add(Class<T> annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class<?> returnType, BiConsumer<T, Parameter[]> consumer) {