aboutsummaryrefslogtreecommitdiff
path: root/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkMod.java
diff options
context:
space:
mode:
Diffstat (limited to 'spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkMod.java')
-rw-r--r--spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkMod.java35
1 files changed, 32 insertions, 3 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..ad419f7 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,21 @@
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.v2.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.command.CommandRegistryAccess;
+import net.minecraft.server.MinecraftServer;
+import net.minecraft.server.command.CommandManager.RegistrationEnvironment;
+import net.minecraft.server.command.ServerCommandSource;
import java.nio.file.Path;
import java.util.Objects;
@@ -38,6 +45,8 @@ public class FabricSparkMod implements ModInitializer {
private ModContainer container;
private Path configDirectory;
+ private FabricServerSparkPlugin activeServerPlugin = null;
+
@Override
public void onInitialize() {
FabricSparkMod.mod = this;
@@ -47,16 +56,36 @@ 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));
+ // server event hooks
+ ServerLifecycleEvents.SERVER_STARTING.register(this::initializeServer);
+ ServerLifecycleEvents.SERVER_STOPPING.register(this::onServerStopping);
+ CommandRegistrationCallback.EVENT.register(this::onServerCommandRegister);
}
- // called be entrypoint defined in fabric.mod.json
+ // client (called be entrypoint defined in fabric.mod.json)
public static void initializeClient() {
Objects.requireNonNull(FabricSparkMod.mod, "mod");
FabricClientSparkPlugin.register(FabricSparkMod.mod, MinecraftClient.getInstance());
}
+ // server
+ 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 onServerCommandRegister(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess access, RegistrationEnvironment env) {
+ if (this.activeServerPlugin != null) {
+ this.activeServerPlugin.registerCommands(dispatcher);
+ }
+ }
+
public String getVersion() {
return this.container.getMetadata().getVersion().getFriendlyString();
}