aboutsummaryrefslogtreecommitdiff
path: root/spark-forge
diff options
context:
space:
mode:
authorjames58899 <james59988@gmail.com>2020-11-20 13:10:33 +0000
committerLuck <git@lucko.me>2020-11-20 13:10:33 +0000
commit9f105a55bd114df9e09230ad5252f6cfd480c9c4 (patch)
tree831aacdbf57ff303b6af1eead8b4e2e31ca93c10 /spark-forge
parentd9be8f7ce9d33dfa908ca5de04d45054f854d93e (diff)
downloadspark-9f105a55bd114df9e09230ad5252f6cfd480c9c4.tar.gz
spark-9f105a55bd114df9e09230ad5252f6cfd480c9c4.tar.bz2
spark-9f105a55bd114df9e09230ad5252f6cfd480c9c4.zip
Ensure scheduler is closed at server stop (#75, #78)
Co-authored-by: Luck <git@lucko.me>
Diffstat (limited to 'spark-forge')
-rw-r--r--spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeClientSparkPlugin.java6
-rw-r--r--spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeServerSparkPlugin.java14
-rw-r--r--spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeSparkPlugin.java19
3 files changed, 28 insertions, 11 deletions
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<ISuggestionProvider> {
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<CommandSource> 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<MinecraftServer> server;
public ForgeServerSparkPlugin(ForgeSparkMod mod, Supplier<MinecraftServer> 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<CommandSource> 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