Skip to content

Commit

Permalink
Save ore veins with their string id (#62)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Robertz <dream-master@gmx.net>
  • Loading branch information
Lyfts and Dream-Master authored Jan 11, 2025
1 parent e494973 commit b260e56
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 107 deletions.
4 changes: 2 additions & 2 deletions dependencies.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
dependencies {
api('com.github.GTNewHorizons:Navigator:1.0.15:dev')
api('com.github.GTNewHorizons:GTNHLib:0.5.12:dev')
api('com.github.GTNewHorizons:GT5-Unofficial:5.09.50.25:dev')
api('com.github.GTNewHorizons:GTNHLib:0.6.0:dev')
api('com.github.GTNewHorizons:GT5-Unofficial:5.09.51.22:dev')

runtimeOnlyNonPublishable(rfg.deobf('maven.modrinth:journeymap:5.2.6'))
}
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pluginManagement {
}

plugins {
id 'com.gtnewhorizons.gtnhsettingsconvention' version '1.0.27'
id 'com.gtnewhorizons.gtnhsettingsconvention' version '1.0.30'
}


16 changes: 6 additions & 10 deletions src/main/java/com/sinthoras/visualprospecting/VPMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLLoadCompleteEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.event.FMLServerAboutToStartEvent;
Expand All @@ -30,55 +31,50 @@ public class VPMod {
// preInit "Run before anything else. Read your config, create blocks, items,
// etc, and register them with the GameRegistry."
public void fmlLifeCycleEvent(FMLPreInitializationEvent event) {
VP.debug("Registered sided proxy for: " + (Utils.isLogicalClient() ? "Client" : "Dedicated server"));
VP.debug("preInit()" + event.getModMetadata().name);
proxy.fmlLifeCycleEvent(event);
}

@Mod.EventHandler
// load "Do your mod setup. Build whatever data structures you care about. Register recipes."
public void fmlLifeCycleEvent(FMLInitializationEvent event) {
VP.debug("init()");
proxy.fmlLifeCycleEvent(event);
}

@Mod.EventHandler
// postInit "Handle interaction with other mods, complete your setup based on this."
public void fmlLifeCycle(FMLPostInitializationEvent event) {
VP.debug("postInit()");
VP.debug("Registering with the GT5U ore vein database");
VisualProspectingDatabase.registerDatabase(new VeinDatabase());
proxy.fmlLifeCycleEvent(event);
}

@Mod.EventHandler
public void fmlLifeCycle(FMLLoadCompleteEvent event) {
proxy.fmlLifeCycleEvent(event);
}

@Mod.EventHandler
public void fmlLifeCycle(FMLServerAboutToStartEvent event) {
VP.debug("Server about to start");
proxy.fmlLifeCycleEvent(event);
}

@Mod.EventHandler
// register server commands in this event handler
public void fmlLifeCycle(FMLServerStartingEvent event) {
VP.debug("Server starting");
proxy.fmlLifeCycleEvent(event);
}

@Mod.EventHandler
public void fmlLifeCycle(FMLServerStartedEvent event) {
VP.debug("Server started");
proxy.fmlLifeCycleEvent(event);
}

@Mod.EventHandler
public void fmlLifeCycle(FMLServerStoppingEvent event) {
VP.debug("Server stopping");
proxy.fmlLifeCycleEvent(event);
}

@Mod.EventHandler
public void fmlLifeCycle(FMLServerStoppedEvent event) {
VP.debug("Server stopped");
proxy.fmlLifeCycleEvent(event);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package com.sinthoras.visualprospecting.database;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Collection;
import java.util.Map;

import javax.annotation.Nullable;

import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
Expand All @@ -11,13 +18,19 @@
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sinthoras.visualprospecting.Tags;
import com.sinthoras.visualprospecting.Utils;
import com.sinthoras.visualprospecting.VP;
import com.sinthoras.visualprospecting.database.veintypes.VeinType;
import com.sinthoras.visualprospecting.database.veintypes.VeinTypeCaching;

import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.shorts.Short2ObjectMap;
import it.unimi.dsi.fastutil.shorts.Short2ObjectMaps;
import it.unimi.dsi.fastutil.shorts.Short2ObjectOpenHashMap;

public class DimensionCache {

Expand All @@ -27,10 +40,13 @@ public enum UpdateResult {
New
}

private static final File oldIdFile = new File(Tags.VISUALPROSPECTING_DIR, "veintypesLUT");
private static Short2ObjectMap<String> idConversionMap;
private final Long2ObjectMap<OreVeinPosition> oreChunks = new Long2ObjectOpenHashMap<>();
private final Long2ObjectMap<UndergroundFluidPosition> undergroundFluids = new Long2ObjectOpenHashMap<>();
public final int dimensionId;
private boolean isDirty = false;
private boolean preventSaving = false;

public DimensionCache(int dimensionId) {
this.dimensionId = dimensionId;
Expand All @@ -51,7 +67,7 @@ private NBTTagCompound saveOres() {
NBTTagCompound veinCompound = new NBTTagCompound();
veinCompound.setInteger("chunkX", vein.chunkX);
veinCompound.setInteger("chunkZ", vein.chunkZ);
veinCompound.setShort("veinTypeId", vein.veinType.veinId);
veinCompound.setString("veinTypeName", vein.veinType.name);
veinCompound.setBoolean("depleted", vein.isDepleted());
compound.setTag(String.valueOf(getOreVeinKey(vein.chunkX, vein.chunkZ)), veinCompound);
}
Expand Down Expand Up @@ -82,9 +98,16 @@ public void loadFromNbt(NBTTagCompound compound) {
NBTTagCompound veinCompound = (NBTTagCompound) base;
int chunkX = veinCompound.getInteger("chunkX");
int chunkZ = veinCompound.getInteger("chunkZ");
short veinTypeId = veinCompound.getShort("veinTypeId");
boolean depleted = veinCompound.getBoolean("depleted");
VeinType veinType = VeinTypeCaching.getVeinType(veinTypeId);
VeinType veinType;
if (veinCompound.hasKey("veinTypeId")) {
veinType = getVeinFromId(veinCompound.getShort("veinTypeId"));
if (veinType == null) return;
markDirty();
} else {
veinType = VeinTypeCaching.getVeinType(veinCompound.getString("veinTypeName"));
}

oreChunks.put(
getOreVeinKey(chunkX, chunkZ),
new OreVeinPosition(dimensionId, chunkX, chunkZ, veinType, depleted));
Expand Down Expand Up @@ -115,7 +138,9 @@ void loadLegacy(ByteBuffer oreChunksBuffer, ByteBuffer undergroundFluidsBuffer)
final int chunkZ = oreChunksBuffer.getInt();
final short veinTypeId = oreChunksBuffer.getShort();
final boolean depleted = (veinTypeId & 0x8000) > 0;
final VeinType veinType = VeinTypeCaching.getVeinType((short) (veinTypeId & 0x7FFF));
final VeinType veinType = getVeinFromId((short) (veinTypeId & 0x7FFF));
if (veinType == null) return;

oreChunks.put(
getOreVeinKey(chunkX, chunkZ),
new OreVeinPosition(dimensionId, chunkX, chunkZ, veinType, depleted));
Expand Down Expand Up @@ -220,7 +245,7 @@ public Collection<UndergroundFluidPosition> getAllUndergroundFluids() {
}

public boolean isDirty() {
return isDirty;
return isDirty && !preventSaving;
}

public void markDirty() {
Expand All @@ -245,4 +270,43 @@ public void clearOreVeins(int startChunkX, int startChunkZ, int endChunkX, int e
return withinX && withinZ;
});
}

private @Nullable VeinType getVeinFromId(short veinTypeId) {
final String veinTypeName = getIdConversionMap().get(veinTypeId);
if (veinTypeName == null) {
preventSaving = true;
VP.LOG.warn(
"Not loading ores in dimension {}. Couldn't find vein type for id {}, file {} may be missing.",
dimensionId,
veinTypeId,
oldIdFile.getAbsolutePath());
return null;
}
return VeinTypeCaching.getVeinType(veinTypeName);
}

private static Short2ObjectMap<String> getIdConversionMap() {
if (idConversionMap != null) {
return idConversionMap;
}

if (!oldIdFile.exists()) {
return Short2ObjectMaps.emptyMap();
}
try {
final Gson gson = new Gson();
final Reader reader = Files.newBufferedReader(oldIdFile.toPath());
final Map<String, Short> map = gson.fromJson(reader, new TypeToken<Map<String, Short>>() {}.getType());
reader.close();
Short2ObjectMap<String> result = new Short2ObjectOpenHashMap<>();
result.put((short) 0, Tags.ORE_MIX_NONE_NAME);
for (Map.Entry<String, Short> entry : map.entrySet()) {
result.put(entry.getValue().shortValue(), entry.getKey());
}
return idConversionMap = result;
} catch (IOException e) {
e.printStackTrace();
return Short2ObjectMaps.emptyMap();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
public abstract class WorldCache {

protected final Int2ObjectMap<DimensionCache> dimensions = new Int2ObjectOpenHashMap<>();
private boolean needsSaving = false;
protected File worldCache;
private boolean isLoaded = false;

Expand Down Expand Up @@ -86,25 +85,20 @@ private boolean loadLegacyVeinCache(File worldCacheDirectory) {

Utils.deleteDirectoryRecursively(oreVeinCacheDirectory);
Utils.deleteDirectoryRecursively(undergroundFluidCacheDirectory);
needsSaving = true;
saveVeinCache();
return true;
}

public void saveVeinCache() {
if (needsSaving) {
for (DimensionCache dimension : dimensions.values()) {
if (!dimension.isDirty()) continue;
File dimFile = new File(worldCache, "DIM" + dimension.dimensionId + ".dat");
Utils.writeNBT(dimFile, dimension.saveToNbt());
}
needsSaving = false;
for (DimensionCache dimension : dimensions.values()) {
if (!dimension.isDirty()) continue;
File dimFile = new File(worldCache, "DIM" + dimension.dimensionId + ".dat");
Utils.writeNBT(dimFile, dimension.saveToNbt());
}
}

public void reset() {
dimensions.clear();
needsSaving = false;
isLoaded = false;
}

Expand All @@ -123,7 +117,6 @@ public void resetSome(int dimID, int startX, int startZ, int endX, int endZ) {
DimensionCache dim = dimensions.get(dimID);
if (dim != null) {
dim.clearOreVeins(startX, startZ, endX, endZ);
needsSaving = true;
isLoaded = false;
}
}
Expand All @@ -142,22 +135,16 @@ public void resetSpawnChunks(ChunkCoordinates spawn, int dimID) {
resetSome(dimID, startX, startZ, endX, endZ);
}

private DimensionCache.UpdateResult updateSaveFlag(DimensionCache.UpdateResult updateResult) {
needsSaving |= updateResult != DimensionCache.UpdateResult.AlreadyKnown;
return updateResult;
}

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

protected void toggleOreVein(int dimensionId, int chunkX, int chunkZ) {
DimensionCache dimension = dimensions.get(dimensionId);
if (dimension != null) {
dimension.toggleOreVein(chunkX, chunkZ);
}
needsSaving = true;
}

public OreVeinPosition getOreVein(int dimensionId, int chunkX, int chunkZ) {
Expand All @@ -170,7 +157,7 @@ public OreVeinPosition getOreVein(int dimensionId, int chunkX, int chunkZ) {

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

public UndergroundFluidPosition getUndergroundFluid(int dimensionId, int chunkX, int chunkZ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void processMinecraftChunk(final NBTTagList tileEntities) {
public boolean matchesSingleVein() {
if (oreCounts.isEmpty()) return true;
if (oreCounts.size() > 4) return false;
VeinTypeCaching.veinTypes.stream()
VeinTypeCaching.getVeinTypes().stream()
.filter(vein -> vein.containsAllFoundOres(oreCounts.keySet(), dimName, primaryMeta, minVeinBlockY))
.forEach(matchedVeins::add);
return matchedVeins.size() <= 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public VeinType getMatchedVein() {
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).map(Map.Entry::getKey).findFirst();
if (!dominantOre.isPresent()) return VeinType.NO_VEIN;

for (VeinType veinType : VeinTypeCaching.veinTypes) {
for (VeinType veinType : VeinTypeCaching.getVeinTypes()) {
if (veinType.matchesWithSpecificPrimaryOrSecondary(allOres.keySet(), dimName, dominantOre.get())) {
matchedVeins.add(veinType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public class VeinType {
public static final int veinHeight = 9;

public final String name;
public short veinId;
public final IOreMaterialProvider oreMaterialProvider;
public final int blockSize;
public final short primaryOreMeta;
Expand Down
Loading

0 comments on commit b260e56

Please sign in to comment.