From 9f105a55bd114df9e09230ad5252f6cfd480c9c4 Mon Sep 17 00:00:00 2001 From: james58899 Date: Fri, 20 Nov 2020 13:10:33 +0000 Subject: Ensure scheduler is closed at server stop (#75, #78) Co-authored-by: Luck --- .../spark/forge/plugin/ForgeClientSparkPlugin.java | 6 ++++-- .../spark/forge/plugin/ForgeServerSparkPlugin.java | 14 +++++++++----- .../me/lucko/spark/forge/plugin/ForgeSparkPlugin.java | 19 +++++++++++++++---- 3 files changed, 28 insertions(+), 11 deletions(-) (limited to 'spark-forge/src/main/java/me') diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeClientSparkPlugin.java b/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeClientSparkPlugin.java index 66fd3ab..7b32f5b 100644 --- a/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeClientSparkPlugin.java +++ b/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeClientSparkPlugin.java @@ -54,11 +54,13 @@ import java.util.stream.Stream; public class ForgeClientSparkPlugin extends ForgeSparkPlugin implements SuggestionProvider { public static void register(ForgeSparkMod mod, FMLClientSetupEvent event) { - Minecraft minecraft = event.getMinecraftSupplier().get(); + ForgeClientSparkPlugin plugin = new ForgeClientSparkPlugin(mod, event.getMinecraftSupplier().get()); + plugin.enable(); - ForgeClientSparkPlugin plugin = new ForgeClientSparkPlugin(mod, minecraft); + // register listeners MinecraftForge.EVENT_BUS.register(plugin); + // ensure commands are registered plugin.scheduler.scheduleWithFixedDelay(plugin::checkCommandRegistered, 10, 10, TimeUnit.SECONDS); } diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeServerSparkPlugin.java b/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeServerSparkPlugin.java index 3433e3b..42309c1 100644 --- a/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeServerSparkPlugin.java +++ b/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeServerSparkPlugin.java @@ -58,18 +58,17 @@ public class ForgeServerSparkPlugin extends ForgeSparkPlugin implements Command< public static void register(ForgeSparkMod mod, RegisterCommandsEvent event) { ForgeServerSparkPlugin plugin = new ForgeServerSparkPlugin(mod, ServerLifecycleHooks::getCurrentServer); + plugin.enable(); + + // register listeners MinecraftForge.EVENT_BUS.register(plugin); + // register commands & permissions CommandDispatcher dispatcher = event.getDispatcher(); registerCommands(dispatcher, plugin, plugin, "spark"); PermissionAPI.registerNode("spark", DefaultPermissionLevel.OP, "Access to the spark command"); } - @SubscribeEvent - public void onDisable(FMLServerStoppingEvent event) { - this.platform.disable(); - } - private final Supplier server; public ForgeServerSparkPlugin(ForgeSparkMod mod, Supplier server) { @@ -77,6 +76,11 @@ public class ForgeServerSparkPlugin extends ForgeSparkPlugin implements Command< this.server = server; } + @SubscribeEvent + public void onDisable(FMLServerStoppingEvent event) { + disable(); + } + @Override public int run(CommandContext context) throws CommandSyntaxException { String[] args = processArgs(context); diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeSparkPlugin.java b/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeSparkPlugin.java index 920ca24..17bc3e1 100644 --- a/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeSparkPlugin.java +++ b/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeSparkPlugin.java @@ -20,7 +20,6 @@ package me.lucko.spark.forge.plugin; -import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.mojang.brigadier.Command; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.StringArgumentType; @@ -65,13 +64,25 @@ public abstract class ForgeSparkPlugin implements SparkPlugin { protected ForgeSparkPlugin(ForgeSparkMod mod) { this.mod = mod; - this.scheduler = Executors.newSingleThreadScheduledExecutor( - new ThreadFactoryBuilder().setNameFormat("spark-forge-async-worker").build() - ); + this.scheduler = Executors.newSingleThreadScheduledExecutor(r -> { + Thread thread = Executors.defaultThreadFactory().newThread(r); + thread.setName("spark-forge-async-worker"); + thread.setDaemon(true); + return thread; + }); this.platform = new SparkPlatform(this); this.platform.enable(); } + public void enable() { + this.platform.enable(); + } + + public void disable() { + this.platform.disable(); + this.scheduler.shutdown(); + } + public abstract boolean hasPermission(ICommandSource sender, String permission); @Override -- cgit