package top.r3944realms.lib39.core.event;

import net.minecraft.server.level.ServerLevel;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.event.level.LevelEvent;
import top.r3944realms.lib39.Lib39;
import top.r3944realms.lib39.util.ILevelHelper;

/**
 * The type Server handler.
 */
public class ServerEventHandler {
    /**
     * The type Mod.
     */
    public static class Mod extends ServerEventHandler {}

    /**
     * The type Game.
     */
    @EventBusSubscriber(value = Dist.DEDICATED_SERVER, bus = EventBusSubscriber.Bus.GAME, modid = Lib39.MOD_ID)
    public static class Game extends ServerEventHandler {
        /**
         * Register layer definitions.
         *
         * @param event the event
         */
        @SubscribeEvent
        public static void onLoadLevel(LevelEvent.Load event) {
            if (event.getLevel() != null && event.getLevel() instanceof ServerLevel level) {
                ILevelHelper.LevelHelper.SERVER.setLevel(level);
            }
        }

        /**
         * Register layer definitions.
         *
         * @param event the event
         */
        @SubscribeEvent
        public static void onUnloadLevel(LevelEvent.Unload event) {
            if (event.getLevel() != null && event.getLevel() instanceof ServerLevel) {
                ILevelHelper.LevelHelper.SERVER.setLevel(null);
            }
        }
    }
}
