aboutsummaryrefslogtreecommitdiff
path: root/spark-fabric/src
diff options
context:
space:
mode:
Diffstat (limited to 'spark-fabric/src')
-rw-r--r--spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkMod.java32
-rw-r--r--spark-fabric/src/main/java/me/lucko/spark/fabric/placeholder/SparkFabricPlaceholderApi.java2
-rw-r--r--spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricClientSparkPlugin.java27
-rw-r--r--spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricServerSparkPlugin.java41
4 files changed, 70 insertions, 32 deletions
diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkMod.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkMod.java
index 1dd4fd8..fdb359e 100644
--- a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkMod.java
+++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkMod.java
@@ -20,14 +20,19 @@
package me.lucko.spark.fabric;
+import com.mojang.brigadier.CommandDispatcher;
+
import me.lucko.spark.fabric.plugin.FabricClientSparkPlugin;
import me.lucko.spark.fabric.plugin.FabricServerSparkPlugin;
import net.fabricmc.api.ModInitializer;
+import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.minecraft.client.MinecraftClient;
+import net.minecraft.server.MinecraftServer;
+import net.minecraft.server.command.ServerCommandSource;
import java.nio.file.Path;
import java.util.Objects;
@@ -38,6 +43,8 @@ public class FabricSparkMod implements ModInitializer {
private ModContainer container;
private Path configDirectory;
+ private FabricServerSparkPlugin activeServerPlugin = null;
+
@Override
public void onInitialize() {
FabricSparkMod.mod = this;
@@ -47,8 +54,12 @@ public class FabricSparkMod implements ModInitializer {
.orElseThrow(() -> new IllegalStateException("Unable to get container for spark"));
this.configDirectory = loader.getConfigDir().resolve("spark");
- // load hooks
- ServerLifecycleEvents.SERVER_STARTING.register(server -> FabricServerSparkPlugin.register(this, server));
+ // lifecycle hooks
+ ServerLifecycleEvents.SERVER_STARTING.register(this::initializeServer);
+ ServerLifecycleEvents.SERVER_STOPPING.register(this::onServerStopping);
+
+ // events to propagate to active server plugin
+ CommandRegistrationCallback.EVENT.register(this::onCommandRegister);
}
// called be entrypoint defined in fabric.mod.json
@@ -57,6 +68,23 @@ public class FabricSparkMod implements ModInitializer {
FabricClientSparkPlugin.register(FabricSparkMod.mod, MinecraftClient.getInstance());
}
+ public void initializeServer(MinecraftServer server) {
+ this.activeServerPlugin = FabricServerSparkPlugin.register(this, server);
+ }
+
+ public void onServerStopping(MinecraftServer stoppingServer) {
+ if (this.activeServerPlugin != null) {
+ this.activeServerPlugin.disable();
+ this.activeServerPlugin = null;
+ }
+ }
+
+ public void onCommandRegister(CommandDispatcher<ServerCommandSource> dispatcher, boolean isDedicated) {
+ if (this.activeServerPlugin != null) {
+ this.activeServerPlugin.registerCommands(dispatcher);
+ }
+ }
+
public String getVersion() {
return this.container.getMetadata().getVersion().getFriendlyString();
}
diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/placeholder/SparkFabricPlaceholderApi.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/placeholder/SparkFabricPlaceholderApi.java
index 359630d..9171cbb 100644
--- a/spark-fabric/src/main/java/me/lucko/spark/fabric/placeholder/SparkFabricPlaceholderApi.java
+++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/placeholder/SparkFabricPlaceholderApi.java
@@ -2,11 +2,13 @@ package me.lucko.spark.fabric.placeholder;
import eu.pb4.placeholders.PlaceholderAPI;
import eu.pb4.placeholders.PlaceholderResult;
+
import me.lucko.spark.common.SparkPlatform;
import me.lucko.spark.common.command.modules.HealthModule;
import me.lucko.spark.common.monitor.cpu.CpuMonitor;
import me.lucko.spark.common.monitor.tick.TickStatistics;
import me.lucko.spark.common.util.RollingAverage;
+
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.minecraft.text.Text;
diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricClientSparkPlugin.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricClientSparkPlugin.java
index 0948225..c173a0b 100644
--- a/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricClientSparkPlugin.java
+++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricClientSparkPlugin.java
@@ -55,16 +55,6 @@ public class FabricClientSparkPlugin extends FabricSparkPlugin implements Sugges
public static void register(FabricSparkMod mod, MinecraftClient client) {
FabricClientSparkPlugin plugin = new FabricClientSparkPlugin(mod, client);
plugin.enable();
-
- // ensure commands are registered
- plugin.scheduler.scheduleWithFixedDelay(plugin::checkCommandRegistered, 10, 10, TimeUnit.SECONDS);
-
- // register shutdown hook
- ClientLifecycleEvents.CLIENT_STOPPING.register(stoppingClient -> {
- if (stoppingClient == plugin.minecraft) {
- plugin.disable();
- }
- });
}
private final MinecraftClient minecraft;
@@ -75,6 +65,23 @@ public class FabricClientSparkPlugin extends FabricSparkPlugin implements Sugges
this.minecraft = minecraft;
}
+ @Override
+ public void enable() {
+ super.enable();
+
+ // ensure commands are registered
+ this.scheduler.scheduleWithFixedDelay(this::checkCommandRegistered, 10, 10, TimeUnit.SECONDS);
+
+ // events
+ ClientLifecycleEvents.CLIENT_STOPPING.register(this::onDisable);
+ }
+
+ private void onDisable(MinecraftClient stoppingClient) {
+ if (stoppingClient == this.minecraft) {
+ disable();
+ }
+ }
+
private CommandDispatcher<CommandSource> getPlayerCommandDispatcher() {
return Optional.ofNullable(this.minecraft.player)
.map(player -> player.networkHandler)
diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricServerSparkPlugin.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricServerSparkPlugin.java
index 8d38b1c..617564a 100644
--- a/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricServerSparkPlugin.java
+++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricServerSparkPlugin.java
@@ -21,6 +21,7 @@
package me.lucko.spark.fabric.plugin;
import com.mojang.brigadier.Command;
+import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.SuggestionProvider;
@@ -36,10 +37,8 @@ import me.lucko.spark.fabric.FabricPlatformInfo;
import me.lucko.spark.fabric.FabricSparkMod;
import me.lucko.spark.fabric.FabricTickHook;
import me.lucko.spark.fabric.FabricTickReporter;
-
import me.lucko.spark.fabric.placeholder.SparkFabricPlaceholderApi;
-import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
-import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
+
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.MinecraftServer;
@@ -52,25 +51,10 @@ import java.util.stream.Stream;
public class FabricServerSparkPlugin extends FabricSparkPlugin implements Command<ServerCommandSource>, SuggestionProvider<ServerCommandSource> {
- public static void register(FabricSparkMod mod, MinecraftServer server) {
+ public static FabricServerSparkPlugin register(FabricSparkMod mod, MinecraftServer server) {
FabricServerSparkPlugin plugin = new FabricServerSparkPlugin(mod, server);
plugin.enable();
-
- // register commands
- registerCommands(server.getCommandManager().getDispatcher(), plugin, plugin, "spark");
- CommandRegistrationCallback.EVENT.register((dispatcher, isDedicated) -> registerCommands(dispatcher, plugin, plugin, "spark"));
-
-
- if (FabricLoader.getInstance().isModLoaded("placeholder-api")) {
- new SparkFabricPlaceholderApi(plugin.platform);
- }
-
- // register shutdown hook
- ServerLifecycleEvents.SERVER_STOPPING.register(stoppingServer -> {
- if (stoppingServer == plugin.server) {
- plugin.disable();
- }
- });
+ return plugin;
}
private final MinecraftServer server;
@@ -81,6 +65,23 @@ public class FabricServerSparkPlugin extends FabricSparkPlugin implements Comman
}
@Override
+ public void enable() {
+ super.enable();
+
+ // register commands
+ registerCommands(this.server.getCommandManager().getDispatcher());
+
+ // placeholders
+ if (FabricLoader.getInstance().isModLoaded("placeholder-api")) {
+ new SparkFabricPlaceholderApi(this.platform);
+ }
+ }
+
+ public void registerCommands(CommandDispatcher<ServerCommandSource> dispatcher) {
+ registerCommands(dispatcher, this, this, "spark");
+ }
+
+ @Override
public int run(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
String[] args = processArgs(context, false);
if (args == null) {