From 25d54ae229a825f141b552696669ec5031f559d6 Mon Sep 17 00:00:00 2001 From: KennyTV Date: Sat, 9 May 2020 09:46:15 +0200 Subject: [PATCH] Add runnable list to be executed before the full init --- .../java/us/myles/ViaVersion/ViaManager.java | 25 +++++++++++++++++-- .../exception/InformativeException.java | 2 +- .../us/myles/ViaVersion/VelocityPlugin.java | 4 +++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/common/src/main/java/us/myles/ViaVersion/ViaManager.java b/common/src/main/java/us/myles/ViaVersion/ViaManager.java index 4b901cba0..709fd1bfb 100644 --- a/common/src/main/java/us/myles/ViaVersion/ViaManager.java +++ b/common/src/main/java/us/myles/ViaVersion/ViaManager.java @@ -15,7 +15,9 @@ import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.TabCompleteThread; import us.myles.ViaVersion.protocols.protocol1_9to1_8.ViaIdleThread; import us.myles.ViaVersion.update.UpdateUtil; +import java.util.ArrayList; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.UUID; @@ -28,6 +30,7 @@ public class ViaManager { private final ViaCommandHandler commandHandler; private final ViaPlatformLoader loader; private final Set subPlatforms = new HashSet<>(); + private List enableListeners = new ArrayList<>(); private TaskId mappingLoadingTask; private boolean debug; @@ -48,10 +51,13 @@ public class ViaManager { platform.onReload(); } // Check for updates - if (platform.getConf().isCheckForUpdates()) + if (platform.getConf().isCheckForUpdates()) { UpdateUtil.sendUpdateMessage(); + } + // Force class load ProtocolRegistry.init(); + // Inject try { injector.inject(); @@ -60,11 +66,17 @@ public class ViaManager { e.printStackTrace(); return; } + // Mark as injected System.setProperty("ViaVersion", platform.getPluginVersion()); + + for (Runnable listener : enableListeners) { + listener.run(); + } + enableListeners = null; + // If successful platform.runSync(this::onServerLoaded); - } public void onServerLoaded() { @@ -195,6 +207,15 @@ public class ViaManager { return platform.getConnectionManager().getConnectedClient(playerUUID); } + /** + * Adds a runnable to be executed when ViaVersion has finished its init before the full server load. + * + * @param runnable runnable to be executed + */ + public void addEnableListener(Runnable runnable) { + enableListeners.add(runnable); + } + public static final class ViaManagerBuilder { private ViaPlatform platform; private ViaInjector injector; diff --git a/common/src/main/java/us/myles/ViaVersion/exception/InformativeException.java b/common/src/main/java/us/myles/ViaVersion/exception/InformativeException.java index 433c45e65..cec74fff3 100644 --- a/common/src/main/java/us/myles/ViaVersion/exception/InformativeException.java +++ b/common/src/main/java/us/myles/ViaVersion/exception/InformativeException.java @@ -5,7 +5,7 @@ import java.util.Map; public class InformativeException extends Exception { private final Map info = new HashMap<>(); - private int sources = 0; + private int sources; public InformativeException(Throwable cause) { super(cause); diff --git a/velocity/src/main/java/us/myles/ViaVersion/VelocityPlugin.java b/velocity/src/main/java/us/myles/ViaVersion/VelocityPlugin.java index 70bbd800f..9f48d3ebc 100644 --- a/velocity/src/main/java/us/myles/ViaVersion/VelocityPlugin.java +++ b/velocity/src/main/java/us/myles/ViaVersion/VelocityPlugin.java @@ -2,6 +2,7 @@ package us.myles.ViaVersion; import com.google.gson.JsonObject; import com.google.inject.Inject; +import com.velocitypowered.api.event.PostOrder; import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; import com.velocitypowered.api.plugin.Plugin; @@ -82,7 +83,10 @@ public class VelocityPlugin implements ViaPlatform { if (proxy.getPluginManager().getPlugin("ViaBackwards").isPresent()) { MappingDataLoader.enableMappingsCache(); } + } + @Subscribe(order = PostOrder.LAST) + public void onProxyLateInit(ProxyInitializeEvent e) { Via.getManager().init(); }