diff options
author | Luck <git@lucko.me> | 2021-12-23 12:42:21 +0000 |
---|---|---|
committer | Luck <git@lucko.me> | 2021-12-23 12:42:43 +0000 |
commit | 703a1578ac26c7e73725b2584ac989abfc56cf37 (patch) | |
tree | 466dbf98160c0b33932f25fd10c78f5e1feb831e /spark-forge/src/main/java | |
parent | 51aaf93ef245f322beea40a60b8d8d6ce5519e03 (diff) | |
download | spark-703a1578ac26c7e73725b2584ac989abfc56cf37.tar.gz spark-703a1578ac26c7e73725b2584ac989abfc56cf37.tar.bz2 spark-703a1578ac26c7e73725b2584ac989abfc56cf37.zip |
Tidy up mod lifecycle
Diffstat (limited to 'spark-forge/src/main/java')
3 files changed, 55 insertions, 28 deletions
diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeSparkMod.java b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeSparkMod.java index 02a2ebc..3321d12 100644 --- a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeSparkMod.java +++ b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeSparkMod.java @@ -24,7 +24,7 @@ import me.lucko.spark.forge.plugin.ForgeClientSparkPlugin; import me.lucko.spark.forge.plugin.ForgeServerSparkPlugin; import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.event.RegisterCommandsEvent; +import net.minecraftforge.event.server.ServerAboutToStartEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.IExtensionPoint; import net.minecraftforge.fml.ModContainer; @@ -61,15 +61,15 @@ public class ForgeSparkMod { this.configDirectory = FMLPaths.CONFIGDIR.get().resolve(this.container.getModId()); } - @SubscribeEvent - public void registerCommands(RegisterCommandsEvent e) { - ForgeServerSparkPlugin.register(this, e); - } - public void clientInit(FMLClientSetupEvent e) { ForgeClientSparkPlugin.register(this, e); } + @SubscribeEvent + public void serverInit(ServerAboutToStartEvent e) { + ForgeServerSparkPlugin.register(this, e); + } + public Path getConfigDirectory() { if (this.configDirectory == null) { throw new IllegalStateException("Config directory not set"); 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 95bb2d1..4d1bebe 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 @@ -58,12 +58,6 @@ public class ForgeClientSparkPlugin extends ForgeSparkPlugin implements Suggesti public static void register(ForgeSparkMod mod, FMLClientSetupEvent event) { ForgeClientSparkPlugin plugin = new ForgeClientSparkPlugin(mod, Minecraft.getInstance()); plugin.enable(); - - // register listeners - MinecraftForge.EVENT_BUS.register(plugin); - - // ensure commands are registered - plugin.scheduler.scheduleWithFixedDelay(plugin::checkCommandRegistered, 10, 10, TimeUnit.SECONDS); } private final Minecraft minecraft; @@ -74,6 +68,17 @@ public class ForgeClientSparkPlugin extends ForgeSparkPlugin implements Suggesti this.minecraft = minecraft; } + @Override + public void enable() { + super.enable(); + + // ensure commands are registered + this.scheduler.scheduleWithFixedDelay(this::checkCommandRegistered, 10, 10, TimeUnit.SECONDS); + + // register listeners + MinecraftForge.EVENT_BUS.register(this); + } + private CommandDispatcher<SharedSuggestionProvider> getPlayerCommandDispatcher() { return Optional.ofNullable(this.minecraft.player) .map(player -> player.connection) 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 4a2416f..c03bb05 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 @@ -45,9 +45,9 @@ import net.minecraft.server.level.ServerPlayer; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.RegisterCommandsEvent; import net.minecraftforge.event.TickEvent; +import net.minecraftforge.event.server.ServerAboutToStartEvent; import net.minecraftforge.event.server.ServerStoppingEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; -import net.minecraftforge.server.ServerLifecycleHooks; import net.minecraftforge.server.permission.PermissionAPI; import net.minecraftforge.server.permission.events.PermissionGatherEvent; import net.minecraftforge.server.permission.nodes.PermissionNode; @@ -59,35 +59,48 @@ import java.util.Collections; import java.util.List; import java.util.Map; import java.util.concurrent.CompletableFuture; -import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.stream.Stream; public class ForgeServerSparkPlugin extends ForgeSparkPlugin implements Command<CommandSourceStack>, SuggestionProvider<CommandSourceStack> { - public static void register(ForgeSparkMod mod, RegisterCommandsEvent event) { - ForgeServerSparkPlugin plugin = new ForgeServerSparkPlugin(mod, ServerLifecycleHooks::getCurrentServer); + public static void register(ForgeSparkMod mod, ServerAboutToStartEvent event) { + ForgeServerSparkPlugin plugin = new ForgeServerSparkPlugin(mod, event.getServer()); plugin.enable(); - - // register listeners - MinecraftForge.EVENT_BUS.register(plugin); - - // register commands & permissions - CommandDispatcher<CommandSourceStack> dispatcher = event.getDispatcher(); - registerCommands(dispatcher, plugin, plugin, "spark"); } - private final Supplier<MinecraftServer> server; + private final MinecraftServer server; private Map<String, PermissionNode<Boolean>> registeredPermissions = Collections.emptyMap(); - public ForgeServerSparkPlugin(ForgeSparkMod mod, Supplier<MinecraftServer> server) { + public ForgeServerSparkPlugin(ForgeSparkMod mod, MinecraftServer server) { super(mod); this.server = server; } + @Override + public void enable() { + super.enable(); + + // register commands + registerCommands(this.server.getCommands().getDispatcher()); + + // register listeners + MinecraftForge.EVENT_BUS.register(this); + } + + @Override + public void disable() { + super.disable(); + + // unregister listeners + MinecraftForge.EVENT_BUS.unregister(this); + } + @SubscribeEvent public void onDisable(ServerStoppingEvent event) { - disable(); + if (event.getServer() == this.server) { + disable(); + } } @SubscribeEvent @@ -112,6 +125,15 @@ public class ForgeServerSparkPlugin extends ForgeSparkPlugin implements Command< this.registeredPermissions = builder.build(); } + @SubscribeEvent + public void onCommandRegister(RegisterCommandsEvent e) { + registerCommands(e.getDispatcher()); + } + + private void registerCommands(CommandDispatcher<CommandSourceStack> dispatcher) { + registerCommands(dispatcher, this, this, "spark"); + } + @Override public int run(CommandContext<CommandSourceStack> context) throws CommandSyntaxException { String[] args = processArgs(context, false); @@ -163,8 +185,8 @@ public class ForgeServerSparkPlugin extends ForgeSparkPlugin implements Command< @Override public Stream<ForgeCommandSender> getCommandSenders() { return Stream.concat( - this.server.get().getPlayerList().getPlayers().stream(), - Stream.of(this.server.get()) + this.server.getPlayerList().getPlayers().stream(), + Stream.of(this.server) ).map(sender -> new ForgeCommandSender(sender, this)); } |