Add runnable list to be executed before the full init

This commit is contained in:
KennyTV 2020-05-09 09:46:15 +02:00
parent 045c35243f
commit 25d54ae229
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
3 changed files with 28 additions and 3 deletions

View File

@ -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<String> subPlatforms = new HashSet<>();
private List<Runnable> 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;

View File

@ -5,7 +5,7 @@ import java.util.Map;
public class InformativeException extends Exception {
private final Map<String, Object> info = new HashMap<>();
private int sources = 0;
private int sources;
public InformativeException(Throwable cause) {
super(cause);

View File

@ -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<Player> {
if (proxy.getPluginManager().getPlugin("ViaBackwards").isPresent()) {
MappingDataLoader.enableMappingsCache();
}
}
@Subscribe(order = PostOrder.LAST)
public void onProxyLateInit(ProxyInitializeEvent e) {
Via.getManager().init();
}