package dev.zontreck.libzontreck; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.sql.SQLException; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.UUID; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import dev.zontreck.libzontreck.chestgui.ChestGUIRegistry; import dev.zontreck.libzontreck.config.ServerConfig; import dev.zontreck.libzontreck.currency.Bank; import dev.zontreck.libzontreck.currency.CurrencyHelper; import dev.zontreck.libzontreck.events.BlockRestoreQueueRegistrationEvent; import dev.zontreck.libzontreck.items.ModItems; import dev.zontreck.libzontreck.memory.world.BlockRestoreQueue; import dev.zontreck.libzontreck.memory.world.BlockRestoreQueueRegistry; import dev.zontreck.libzontreck.memory.world.DatabaseMigrations; import dev.zontreck.libzontreck.memory.world.DatabaseWrapper; import dev.zontreck.libzontreck.menus.ChestGUIScreen; import dev.zontreck.libzontreck.types.ModMenuTypes; import dev.zontreck.libzontreck.networking.NetworkEvents; import net.minecraft.client.gui.screens.MenuScreens; import net.minecraft.server.level.ServerLevel; import org.slf4j.Logger; import com.mojang.logging.LogUtils; import dev.zontreck.libzontreck.commands.Commands; import dev.zontreck.libzontreck.events.ForgeEventHandlers; import dev.zontreck.libzontreck.memory.player.VolatilePlayerStorage; import dev.zontreck.libzontreck.networking.ModMessages; import dev.zontreck.libzontreck.profiles.Profile; import dev.zontreck.libzontreck.util.FileTreeDatastore; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.server.ServerStartedEvent; import net.minecraftforge.event.server.ServerStoppingEvent; import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.LogicalSide; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; @Mod(LibZontreck.MOD_ID) public class LibZontreck { public static final Logger LOGGER = LogUtils.getLogger(); public static final String MOD_ID = "libzontreck"; public static final Map PROFILES; public static VolatilePlayerStorage playerStorage; public static boolean ALIVE=true; public static final String FILESTORE = FileTreeDatastore.get(); public static final Path BASE_CONFIG; public static final String PLAYER_INFO_URL = "https://api.mojang.com/users/profiles/minecraft/"; public static final String PLAYER_SKIN_URL = "https://sessionserver.mojang.com/session/minecraft/profile/"; public static final UUID NULL_ID; public static boolean LIBZONTRECK_SERVER_AVAILABLE=false; public static ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); public static LogicalSide CURRENT_SIDE; static{ NULL_ID = new UUID(0,0); PROFILES = new HashMap<>(); BASE_CONFIG = FileTreeDatastore.of("libzontreck"); if(!BASE_CONFIG.toFile().exists()) { try { Files.createDirectory(BASE_CONFIG); } catch (IOException e) { e.printStackTrace(); } } } public LibZontreck(){ LibZontreck.playerStorage=new VolatilePlayerStorage(); IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus(); // Register the setup method for modloading bus.addListener(this::setup); ServerConfig.init(); MinecraftForge.EVENT_BUS.register(this); MinecraftForge.EVENT_BUS.register(new ForgeEventHandlers()); MinecraftForge.EVENT_BUS.register(new Commands()); MinecraftForge.EVENT_BUS.register(new NetworkEvents()); MinecraftForge.EVENT_BUS.register(ChestGUIRegistry.class); ModMenuTypes.REGISTRY.register(bus); //CreativeModeTabs.register(bus); ModItems.register(bus); MinecraftForge.EVENT_BUS.register(CurrencyHelper.class); MinecraftForge.EVENT_BUS.register(Bank.class); } private void setup(final FMLCommonSetupEvent event) { ModMessages.register(); } @SubscribeEvent public void onServerStarted(final ServerStartedEvent event) { ALIVE=true; ServerConfig.init(); try { DatabaseWrapper.start(); }catch(RuntimeException e) { LOGGER.warn("Database not configured properly, it will not be available."); DatabaseWrapper.invalidate(); } CURRENT_SIDE = LogicalSide.SERVER; MinecraftForge.EVENT_BUS.post(new BlockRestoreQueueRegistrationEvent()); for(ServerLevel level : event.getServer().getAllLevels()) { // Queues have been registered, but we now need to initialize the queue's data from saveddata BlockRestoreQueueRegistry.init(level); } if(!DatabaseWrapper.hasDB)return; try { DatabaseMigrations.initMigrations(); } catch (SQLException e) { e.printStackTrace(); } } @SubscribeEvent public void onServerStopping(final ServerStoppingEvent ev) { ALIVE=false; Iterator iProfile = PROFILES.values().iterator(); while(iProfile.hasNext()) { Profile prof = iProfile.next(); iProfile.remove(); prof=null; } } @Mod.EventBusSubscriber(modid = MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD) public static class ClientModEvents { @SubscribeEvent public static void onClientSetup(FMLClientSetupEvent ev) { LibZontreck.CURRENT_SIDE = LogicalSide.CLIENT; LibZontreck.ALIVE = false; // Prevents loops on the client that are meant for server tick processing MenuScreens.register(ModMenuTypes.CHEST_GUI_MENU.get(), ChestGUIScreen::new); } } }