Skip to content

Commit

Permalink
yet another fix for !visualprospectingnearspawn (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyfts authored Sep 27, 2024
1 parent 4073835 commit 0cab19d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/sinthoras/visualprospecting/VP.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class VP {
public static SimpleNetworkWrapper network;
public static final Random randomGeneration = new Random();

private static Logger LOG = LogManager.getLogger(Tags.MODID);
public static final Logger LOG = LogManager.getLogger(Tags.MODID);
public static final int gregTechSmallOreMinimumMeta = 16000;
public static final int minecraftWorldHeight = 256;
public static final int chunksPerRegionFileX = 32;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.File;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
Expand All @@ -13,9 +12,12 @@
import com.sinthoras.visualprospecting.Tags;
import com.sinthoras.visualprospecting.Utils;

import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;

public abstract class WorldCache {

protected final Map<Integer, DimensionCache> dimensions = new HashMap<>();
protected final Int2ObjectMap<DimensionCache> dimensions = new Int2ObjectOpenHashMap<>();
private boolean needsSaving = false;
protected File worldCache;
private boolean isLoaded = false;
Expand Down Expand Up @@ -43,9 +45,8 @@ public boolean loadVeinCache(String worldId) {
if (dimCompound == null) continue;

final int dimensionId = dimCompound.getInteger("dim");
final DimensionCache dimension = new DimensionCache(dimensionId);
final DimensionCache dimension = dimensions.computeIfAbsent(dimensionId, DimensionCache::new);
dimension.loadFromNbt(dimCompound);
dimensions.put(dimensionId, dimension);
loadedAny = true;
}

Expand Down Expand Up @@ -93,10 +94,8 @@ private boolean loadLegacyVeinCache(File worldCacheDirectory) {
public void saveVeinCache() {
if (needsSaving) {
for (DimensionCache dimension : dimensions.values()) {
if (!dimension.isDirty()) {
continue;
}
File dimFile = new File(worldCache.toPath() + "/DIM" + dimension.dimensionId + ".dat");
if (!dimension.isDirty()) continue;
File dimFile = new File(worldCache, "DIM" + dimension.dimensionId + ".dat");
Utils.writeNBT(dimFile, dimension.saveToNbt());
}
needsSaving = false;
Expand Down Expand Up @@ -149,11 +148,7 @@ private DimensionCache.UpdateResult updateSaveFlag(DimensionCache.UpdateResult u
}

protected DimensionCache.UpdateResult putOreVein(final OreVeinPosition oreVeinPosition) {
DimensionCache dimension = dimensions.get(oreVeinPosition.dimensionId);
if (dimension == null) {
dimension = new DimensionCache(oreVeinPosition.dimensionId);
dimensions.put(oreVeinPosition.dimensionId, dimension);
}
DimensionCache dimension = dimensions.computeIfAbsent(oreVeinPosition.dimensionId, DimensionCache::new);
return updateSaveFlag(dimension.putOreVein(oreVeinPosition));
}

Expand All @@ -174,11 +169,7 @@ public OreVeinPosition getOreVein(int dimensionId, int chunkX, int chunkZ) {
}

protected DimensionCache.UpdateResult putUndergroundFluids(final UndergroundFluidPosition undergroundFluid) {
DimensionCache dimension = dimensions.get(undergroundFluid.dimensionId);
if (dimension == null) {
dimension = new DimensionCache(undergroundFluid.dimensionId);
dimensions.put(undergroundFluid.dimensionId, dimension);
}
DimensionCache dimension = dimensions.computeIfAbsent(undergroundFluid.dimensionId, DimensionCache::new);
return updateSaveFlag(dimension.putUndergroundFluid(undergroundFluid));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.sinthoras.visualprospecting.hooks;

import java.io.File;
import java.io.IOException;
import java.util.zip.DataFormatException;

import net.minecraft.server.MinecraftServer;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.common.MinecraftForge;

import com.sinthoras.visualprospecting.Config;
Expand Down Expand Up @@ -37,6 +39,8 @@

public class HooksShared {

private static boolean newWorld = false;

// preInit "Run before anything else. Read your config, create blocks, items,
// etc, and register them with the GameRegistry."
public void fmlLifeCycleEvent(FMLPreInitializationEvent event) {
Expand Down Expand Up @@ -82,28 +86,31 @@ public void fmlLifeCycleEvent(FMLPostInitializationEvent event) {
GregTechAPI.sAfterGTPostload.add(new VeinTypeCaching());
}

public void fmlLifeCycleEvent(FMLServerAboutToStartEvent event) {}
public void fmlLifeCycleEvent(FMLServerAboutToStartEvent event) {
File worldDir = DimensionManager.getCurrentSaveRootDirectory();
if (worldDir != null) {
File regionFolder = new File(worldDir, "region");
newWorld = !regionFolder.exists();
}
}

// register server commands in this event handler
public void fmlLifeCycleEvent(FMLServerStartingEvent event) {

// Get the server and load the ID handler
final MinecraftServer minecraftServer = event.getServer();
WorldIdHandler.load(minecraftServer.worldServers[0]);
WorldServer world = DimensionManager.getWorld(0);
WorldIdHandler.load(world);

// Attempt to load the vein cache. If unable or forcing a recache...
if (!ServerCache.instance.loadVeinCache(WorldIdHandler.getWorldId()) || Config.recacheVeins) {
boolean loaded = ServerCache.instance.loadVeinCache(WorldIdHandler.getWorldId());
if (!newWorld && (!loaded || Config.recacheVeins)) {

// Reanalyze the world and reload it into memory.
try {
WorldAnalysis world = new WorldAnalysis(
minecraftServer.getEntityWorld().getSaveHandler().getWorldDirectory());
world.cacheVeins();
WorldAnalysis worldAnalysis = new WorldAnalysis(world.getSaveHandler().getWorldDirectory());
worldAnalysis.cacheVeins();
} catch (IOException | DataFormatException e) {

// Oops
VP.info("Could not load world save files to build vein cache!");
e.printStackTrace();
VP.LOG.info("Could not load world save files to build vein cache!", e);
}
}

Expand Down

0 comments on commit 0cab19d

Please sign in to comment.