From 8317096182bc2e36daebd542a36ddba61b2ceedd Mon Sep 17 00:00:00 2001 From: TPGamesNL Date: Sat, 19 Jun 2021 17:02:08 +0200 Subject: [PATCH 1/4] committee --- src/main/java/ch/njol/skript/Skript.java | 67 ++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 5 deletions(-) diff --git a/src/main/java/ch/njol/skript/Skript.java b/src/main/java/ch/njol/skript/Skript.java index 41853c32380..b3ba5637037 100644 --- a/src/main/java/ch/njol/skript/Skript.java +++ b/src/main/java/ch/njol/skript/Skript.java @@ -23,6 +23,7 @@ import java.io.InputStream; import java.lang.Thread.UncaughtExceptionHandler; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.net.URL; @@ -56,6 +57,7 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Material; +import org.bukkit.Server; import org.bukkit.command.CommandSender; import org.bukkit.command.PluginCommand; import org.bukkit.entity.Player; @@ -64,6 +66,7 @@ import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.server.PluginDisableEvent; import org.bukkit.event.server.ServerCommandEvent; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginDescriptionFile; @@ -177,6 +180,7 @@ public final class Skript extends JavaPlugin implements Listener { private static Skript instance = null; private static boolean disabled = false; + private static boolean partDisabled = false; public static Skript getInstance() { final Skript i = instance; @@ -288,6 +292,7 @@ private static boolean checkServerPlatform() { @Override public void onEnable() { + Bukkit.getPluginManager().registerEvents(this, this); if (disabled) { Skript.error(m_invalid_reload.toString()); setEnabled(false); @@ -972,16 +977,68 @@ public static Metrics getMetrics() { public static void closeOnDisable(final Closeable closeable) { closeOnDisable.add(closeable); } - + + @SuppressWarnings("unused") + @EventHandler + public void onPluginDisable(PluginDisableEvent event) { + Plugin plugin = event.getPlugin(); + PluginDescriptionFile descriptionFile = plugin.getDescription(); + if (descriptionFile.getDepend().contains("Skript") || descriptionFile.getSoftDepend().contains("Skript")) { + // An addon being disabled, check if server is being stopped + if (!isServerRunning()) { + beforeDisable(); + } + } + } + + private boolean isServerRunning() { + Server server = Bukkit.getServer(); + Class clazz = server.getClass(); + + Method serverMethod; + try { + serverMethod = clazz.getMethod("getServer"); + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } + + Object mcServer; + try { + mcServer = serverMethod.invoke(server); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException(e); + } + + Method isRunningMethod; + try { + isRunningMethod = mcServer.getClass().getMethod("isRunning"); + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } + + try { + return (boolean) isRunningMethod.invoke(mcServer); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException(e); + } + } + + public void beforeDisable() { + partDisabled = true; + EvtSkript.onSkriptStop(); // TODO [code style] warn user about delays in Skript stop events + + ScriptLoader.disableScripts(); + } + @Override public void onDisable() { if (disabled) return; disabled = true; - - EvtSkript.onSkriptStop(); // TODO [code style] warn user about delays in Skript stop events - - ScriptLoader.disableScripts(); + + if (!partDisabled) { + beforeDisable(); + } Bukkit.getScheduler().cancelTasks(this); From 635671322969ecad26f3431680398c61556c1db0 Mon Sep 17 00:00:00 2001 From: TPGamesNL Date: Sun, 20 Jun 2021 15:23:50 +0200 Subject: [PATCH 2/4] Update files --- src/main/java/ch/njol/skript/Skript.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/main/java/ch/njol/skript/Skript.java b/src/main/java/ch/njol/skript/Skript.java index b3ba5637037..5f112bb02bf 100644 --- a/src/main/java/ch/njol/skript/Skript.java +++ b/src/main/java/ch/njol/skript/Skript.java @@ -991,7 +991,10 @@ public void onPluginDisable(PluginDisableEvent event) { } } - private boolean isServerRunning() { + private static final Method IS_RUNNING; + private static final Object MC_SERVER; + + static { Server server = Bukkit.getServer(); Class clazz = server.getClass(); @@ -1002,22 +1005,22 @@ private boolean isServerRunning() { throw new RuntimeException(e); } - Object mcServer; try { - mcServer = serverMethod.invoke(server); + MC_SERVER = serverMethod.invoke(server); } catch (IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); } - Method isRunningMethod; try { - isRunningMethod = mcServer.getClass().getMethod("isRunning"); + IS_RUNNING = MC_SERVER.getClass().getMethod("isRunning"); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } + } + private boolean isServerRunning() { try { - return (boolean) isRunningMethod.invoke(mcServer); + return (boolean) IS_RUNNING.invoke(MC_SERVER); } catch (IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); } From eef136b1199231a4d1e11ce84ead7e628efffc41 Mon Sep 17 00:00:00 2001 From: TPGamesNL Date: Sun, 20 Jun 2021 15:49:40 +0200 Subject: [PATCH 3/4] Update Skript.java --- src/main/java/ch/njol/skript/Skript.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/Skript.java b/src/main/java/ch/njol/skript/Skript.java index 5f112bb02bf..03f7ed4cbd9 100644 --- a/src/main/java/ch/njol/skript/Skript.java +++ b/src/main/java/ch/njol/skript/Skript.java @@ -1026,7 +1026,7 @@ private boolean isServerRunning() { } } - public void beforeDisable() { + private void beforeDisable() { partDisabled = true; EvtSkript.onSkriptStop(); // TODO [code style] warn user about delays in Skript stop events From 59903e03bd7ce5eb66e18f2631a45db0930263ed Mon Sep 17 00:00:00 2001 From: TPGamesNL Date: Mon, 21 Jun 2021 16:58:59 +0200 Subject: [PATCH 4/4] Update Skript.java --- src/main/java/ch/njol/skript/Skript.java | 49 +++++++++++++++--------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/src/main/java/ch/njol/skript/Skript.java b/src/main/java/ch/njol/skript/Skript.java index 03f7ed4cbd9..1e4638a0586 100644 --- a/src/main/java/ch/njol/skript/Skript.java +++ b/src/main/java/ch/njol/skript/Skript.java @@ -991,34 +991,45 @@ public void onPluginDisable(PluginDisableEvent event) { } } - private static final Method IS_RUNNING; - private static final Object MC_SERVER; + private static final boolean IS_STOPPING_EXISTS; + @Nullable + private static Method IS_RUNNING; + @Nullable + private static Object MC_SERVER; static { - Server server = Bukkit.getServer(); - Class clazz = server.getClass(); + IS_STOPPING_EXISTS = methodExists(Server.class, "isStopping"); - Method serverMethod; - try { - serverMethod = clazz.getMethod("getServer"); - } catch (NoSuchMethodException e) { - throw new RuntimeException(e); - } + if (!IS_STOPPING_EXISTS) { + Server server = Bukkit.getServer(); + Class clazz = server.getClass(); - try { - MC_SERVER = serverMethod.invoke(server); - } catch (IllegalAccessException | InvocationTargetException e) { - throw new RuntimeException(e); - } + Method serverMethod; + try { + serverMethod = clazz.getMethod("getServer"); + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } - try { - IS_RUNNING = MC_SERVER.getClass().getMethod("isRunning"); - } catch (NoSuchMethodException e) { - throw new RuntimeException(e); + try { + MC_SERVER = serverMethod.invoke(server); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException(e); + } + + try { + IS_RUNNING = MC_SERVER.getClass().getMethod("isRunning"); + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } } } + @SuppressWarnings("ConstantConditions") private boolean isServerRunning() { + if (IS_STOPPING_EXISTS) + return !Bukkit.getServer().isStopping(); + try { return (boolean) IS_RUNNING.invoke(MC_SERVER); } catch (IllegalAccessException | InvocationTargetException e) {