diff --git a/src/main/java/com/seibel/lod/core/api/EventApi.java b/src/main/java/com/seibel/lod/core/api/EventApi.java index 064fda24a..857a639ea 100644 --- a/src/main/java/com/seibel/lod/core/api/EventApi.java +++ b/src/main/java/com/seibel/lod/core/api/EventApi.java @@ -20,8 +20,8 @@ package com.seibel.lod.core.api; import com.seibel.lod.core.api.ClientApi.LagSpikeCatcher; -import com.seibel.lod.core.builders.lodBuilding.LodBuilder; -import com.seibel.lod.core.enums.worldGeneration.BatchGenerator; +import com.seibel.lod.core.objects.opengl.builders.lodBuilding.LodBuilder; +import com.seibel.lod.core.objects.opengl.builders.worldGeneration.BatchGenerator; import com.seibel.lod.core.enums.WorldType; import com.seibel.lod.core.handlers.dependencyInjection.SingletonHandler; import com.seibel.lod.core.objects.lod.LodDimension; @@ -41,72 +41,83 @@ import com.seibel.lod.core.wrapperInterfaces.world.IWorldWrapper; /** * This holds the methods that should be called by the host mod loader (Fabric, * Forge, etc.). Specifically server and client events. - * * @author James Seibel * @version 11-12-2021 */ -public class EventApi { +public class EventApi +{ public static final boolean ENABLE_STACK_DUMP_LOGGING = false; public static final EventApi INSTANCE = new EventApi(); - + private static final IMinecraftClientWrapper MC = SingletonHandler.get(IMinecraftClientWrapper.class); private static final ILodConfigWrapperSingleton CONFIG = SingletonHandler.get(ILodConfigWrapperSingleton.class); private static final IVersionConstants VERSION_CONSTANTS = SingletonHandler.get(IVersionConstants.class); - + /** * can be set if we want to recalculate variables related to the LOD view * distance */ private boolean recalculateWidths = false; - - private EventApi() { - + + private EventApi() + { + } - + // =============// // tick events // // =============// public BatchGenerator batchGenerator = null; - - public void serverTickEvent() { + + public void serverTickEvent() + { if (!MC.playerExists() || ApiShared.lodWorld.getIsWorldNotLoaded()) return; - + LodDimension lodDim = ApiShared.lodWorld.getLodDimension(MC.getCurrentDimension()); if (lodDim == null) return; if (ApiShared.isShuttingDown) return; - - if (CONFIG.client().worldGenerator().getEnableDistantGeneration()) { - try { + + if (CONFIG.client().worldGenerator().getEnableDistantGeneration()) + { + try + { if (batchGenerator == null) batchGenerator = new BatchGenerator(ApiShared.lodBuilder, lodDim); batchGenerator.queueGenerationRequests(lodDim, ApiShared.lodBuilder); - } catch (Exception e) { + } + catch (Exception e) + { // Exception may happen if world got unloaded unorderly e.printStackTrace(); } - } else { - if (batchGenerator != null) { + } + else + { + if (batchGenerator != null) + { batchGenerator.stop(false); batchGenerator = null; } } } - + // ==============// // world events // // ==============// - - public void worldSaveEvent() { + + public void worldSaveEvent() + { ApiShared.lodWorld.saveAllDimensions(false); // Do an async save. } - + private boolean isCurrentlyOnSinglePlayerServer = false; - + /** This is also called when a new dimension loads */ - public void worldLoadEvent(IWorldWrapper world) { + public void worldLoadEvent(IWorldWrapper world) + { if (ENABLE_STACK_DUMP_LOGGING) ApiShared.LOGGER.info( "WorldLoadEvent called here for " @@ -119,21 +130,22 @@ public class EventApi { ApiShared.isShuttingDown = false; DataPointUtil.WORLD_HEIGHT = world.getHeight(); LodBuilder.MIN_WORLD_HEIGHT = world.getMinHeight(); // This updates the World height - + // LodNodeGenWorker.restartExecutorService(); // ThreadMapUtil.clearMaps(); - + // the player just loaded a new world/dimension ApiShared.lodWorld.selectWorld(LodUtil.getWorldID(world)); - + // make sure the correct LODs are being rendered // (if this isn't done the previous world's LODs may be drawn) ClientApi.renderer.regenerateLODsNextFrame(); ApiShared.previousVertQual = CONFIG.client().graphics().quality().getVerticalQuality(); } - + /** This is also called when the user disconnects from a server+ */ - public void worldUnloadEvent(IWorldWrapper world) { + public void worldUnloadEvent(IWorldWrapper world) + { if (ENABLE_STACK_DUMP_LOGGING) ApiShared.LOGGER.info( "WorldUnloadEvent called here for " @@ -144,20 +156,18 @@ public class EventApi { // AFTER setting MC to not be in a singlePlayerServer if (isCurrentlyOnSinglePlayerServer && world.getWorldType() == WorldType.ClientWorld) return; - - // TODO should "resetMod()" be called here? -James - + // if this isn't done unfinished tasks may be left in the queue // preventing new LodChunks form being generated ApiShared.isShuttingDown = true; - + // TODO Better report on when world gen is stuck and timeout if (batchGenerator != null) batchGenerator.stop(true); batchGenerator = null; - + ApiShared.lodWorld.deselectWorld(); // This force a save and shutdown lodDim properly - + // prevent issues related to the buffer builder // breaking or retaining previous data when changing worlds. ClientApi.renderer.destroyBuffers(); @@ -165,13 +175,14 @@ public class EventApi { GLProxy.ensureAllGLJobCompleted(); recalculateWidths = true; ApiShared.previousVertQual = null; - + // TODO: Check if after the refactoring, is this still needed ClientApi.renderer = new LodRenderer(ClientApi.lodBufferBuilderFactory); ClientApi.INSTANCE.rendererDisabledBecauseOfExceptions = false; } - - public void blockChangeEvent(IChunkWrapper chunk, IDimensionTypeWrapper dimType) { + + public void blockChangeEvent(IChunkWrapper chunk, IDimensionTypeWrapper dimType) + { if (dimType != MC.getCurrentDimension()) return; // recreate the LOD where the blocks were changed @@ -179,19 +190,22 @@ public class EventApi { ClientApi.INSTANCE.toBeLoaded.add(chunk.getLongChunkPos()); blockChangeUpdate.end("clientChunkLoad"); } - + // =============// // Misc Events // // =============// - + // NOTE: This is being called from Render Thread. + /** Re-centers the given LodDimension if it needs to be. */ - public void playerMoveEvent(LodDimension lodDim) { + public void playerMoveEvent(LodDimension lodDim) + { // make sure the dimension is centered RegionPos playerRegionPos = new RegionPos(MC.getPlayerBlockPos()); RegionPos center = lodDim.getCenterRegionPos(); RegionPos worldRegionOffset = new RegionPos(playerRegionPos.x - center.x, playerRegionPos.z - center.z); - if (worldRegionOffset.x != 0 || worldRegionOffset.z != 0) { + if (worldRegionOffset.x != 0 || worldRegionOffset.z != 0) + { lodDim.move(worldRegionOffset); // LOGGER.info("offset: " + worldRegionOffset.x + "," + worldRegionOffset.z + // "\t center: " + lodDim.getCenterX() + "," + lodDim.getCenterZ()); @@ -199,7 +213,8 @@ public class EventApi { } /** Re-sizes all LodDimensions if they need to be. */ - public void viewDistanceChangedEvent() { + public void viewDistanceChangedEvent() + { // calculate how wide the dimension(s) should be in regions int chunksWide; if (MC.getWrappedClientWorld().getDimensionType().hasCeiling()) @@ -207,23 +222,24 @@ public class EventApi { LodUtil.CEILED_DIMENSION_MAX_RENDER_DISTANCE) * 2 + 1; else chunksWide = CONFIG.client().graphics().quality().getLodChunkRenderDistance() * 2 + 1; - + int newWidth = (int) Math.ceil(chunksWide / (float) LodUtil.REGION_WIDTH_IN_CHUNKS); // make sure we have an odd number of regions newWidth += (newWidth & 1) == 0 ? 1 : 0; - + // do the dimensions need to change in size? - if (ApiShared.lodBuilder.defaultDimensionWidthInRegions != newWidth || recalculateWidths) { + if (ApiShared.lodBuilder.defaultDimensionWidthInRegions != newWidth || recalculateWidths) + { // update the dimensions to fit the new width ApiShared.lodWorld.resizeDimensionRegionWidth(newWidth); ApiShared.lodBuilder.defaultDimensionWidthInRegions = newWidth; ClientApi.renderer.setupBuffers(); - + recalculateWidths = false; // LOGGER.info("new dimension width in regions: " + newWidth + "\t potential: " // + newWidth ); } DetailDistanceUtil.updateSettings(); } - + }