|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: CC0-1.0 |
| 3 | + * |
| 4 | + * Dedicated to the public domain under CC0 1.0 Universal. |
| 5 | + * |
| 6 | + * You can obtain a full copy of the license at: |
| 7 | + * https://creativecommons.org/publicdomain/zero/1.0/ |
| 8 | + */ |
| 9 | + |
| 10 | +package dev.triassic.template.fabric; |
| 11 | + |
| 12 | +import dev.triassic.template.TemplateImpl; |
| 13 | +import dev.triassic.template.TemplatePlugin; |
| 14 | +import dev.triassic.template.command.Commander; |
| 15 | +import dev.triassic.template.fabric.command.FabricCommander; |
| 16 | +import dev.triassic.template.util.PlatformType; |
| 17 | +import java.nio.file.Path; |
| 18 | +import net.fabricmc.api.DedicatedServerModInitializer; |
| 19 | +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; |
| 20 | +import net.fabricmc.loader.api.FabricLoader; |
| 21 | +import net.minecraft.server.MinecraftServer; |
| 22 | +import org.checkerframework.checker.nullness.qual.NonNull; |
| 23 | +import org.incendo.cloud.CommandManager; |
| 24 | +import org.incendo.cloud.SenderMapper; |
| 25 | +import org.incendo.cloud.execution.ExecutionCoordinator; |
| 26 | +import org.incendo.cloud.fabric.FabricServerCommandManager; |
| 27 | +import org.slf4j.Logger; |
| 28 | +import org.slf4j.LoggerFactory; |
| 29 | + |
| 30 | +/** |
| 31 | + * The main entry point for the plugin on the Fabric platform. |
| 32 | + * |
| 33 | + * <p>It implements {@link TemplatePlugin} |
| 34 | + * to provide necessary platform-specific functionality.</p> |
| 35 | + */ |
| 36 | +public final class FabricTemplatePlugin implements TemplatePlugin, DedicatedServerModInitializer { |
| 37 | + |
| 38 | + private Logger logger; |
| 39 | + private FabricServerCommandManager<Commander> commandManager; |
| 40 | + private TemplateImpl impl; |
| 41 | + |
| 42 | + @Override |
| 43 | + public void onInitializeServer() { |
| 44 | + this.logger = LoggerFactory.getLogger(FabricTemplatePlugin.class); |
| 45 | + this.commandManager = new FabricServerCommandManager<>( |
| 46 | + ExecutionCoordinator.simpleCoordinator(), |
| 47 | + SenderMapper.create( |
| 48 | + FabricCommander::from, |
| 49 | + commander -> ((FabricCommander) commander).source() |
| 50 | + ) |
| 51 | + ); |
| 52 | + |
| 53 | + ServerLifecycleEvents.SERVER_STARTING.register(this::onServerStarting); |
| 54 | + ServerLifecycleEvents.SERVER_STOPPING.register(this::onServerStopping); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Called when the server is starting. |
| 59 | + * |
| 60 | + * <p>Initializes the {@link TemplateImpl} instance for the Fabric platform.</p> |
| 61 | + */ |
| 62 | + private void onServerStarting(final MinecraftServer server) { |
| 63 | + this.impl = new TemplateImpl(this); |
| 64 | + impl.initialize(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Called when the server is stopping. |
| 69 | + */ |
| 70 | + private void onServerStopping(final MinecraftServer server) { |
| 71 | + if (impl != null) { |
| 72 | + impl.shutdown(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public @NonNull Logger logger() { |
| 78 | + return logger; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public @NonNull Path dataDirectory() { |
| 83 | + return FabricLoader.getInstance().getConfigDir(); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public @NonNull PlatformType platformType() { |
| 88 | + return PlatformType.FABRIC; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public @NonNull CommandManager<Commander> commandManager() { |
| 93 | + return this.commandManager; |
| 94 | + } |
| 95 | +} |
0 commit comments