From 8a31be11f5fa8400496c383d4423a6e29ff54fbe Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 22 Oct 2021 18:27:21 +0200 Subject: [PATCH] Add potential StartUpTime decrease by lazy loading commands only on usage --- .../src/de/steamwar/command/SWCommand.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index ab72fc5..5f0f9d2 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -34,6 +34,7 @@ import java.util.stream.Collectors; public abstract class SWCommand { + private boolean initialized = false; private final Command command; private final List commandList = new ArrayList<>(); private final List 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 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 void add(Class annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class returnType, BiConsumer consumer) {