Skip to content

Commit

Permalink
Small ores generation check 2x2 chunks. TFC will not crash if ore con…
Browse files Browse the repository at this point in the history
…fig json files are broken.
  • Loading branch information
DisasterMoo committed Oct 27, 2019
1 parent 8065ba2 commit 21b8b58
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
Expand Up @@ -25,7 +25,6 @@
import net.dries007.tfc.objects.te.TEPlacedItemFlat;
import net.dries007.tfc.util.Helpers;
import net.dries007.tfc.world.classic.ChunkGenTFC;
import net.dries007.tfc.world.classic.WorldTypeTFC;
import net.dries007.tfc.world.classic.chunkdata.ChunkDataTFC;
import net.dries007.tfc.world.classic.worldgen.vein.Vein;

Expand Down Expand Up @@ -53,12 +52,19 @@ public void generate(Random random, int chunkX, int chunkZ, World world, IChunkG
if (chunkGenerator instanceof ChunkGenTFC && world.provider.getDimension() == 0)
{
final BlockPos chunkBlockPos = new BlockPos(chunkX << 4, 0, chunkZ << 4);
ChunkDataTFC chunkData = ChunkDataTFC.get(world, chunkBlockPos);
if (!chunkData.isInitialized()) return;

// Grab 2x2 area
ChunkDataTFC[] chunkData = {ChunkDataTFC.get(world, chunkBlockPos), // This chunk
ChunkDataTFC.get(world, chunkBlockPos.add(16, 0, 0)),
ChunkDataTFC.get(world, chunkBlockPos.add(0, 0, 16)),
ChunkDataTFC.get(world, chunkBlockPos.add(16, 0, 16))};
if (!chunkData[0].isInitialized()) return;

// Set constant values here
int xoff = chunkX * 16 + 8;
int zoff = chunkZ * 16 + 8;
int lowestYScan = world.getTopSolidOrLiquidBlock(chunkBlockPos).getY() - 35; // Same as in 1.7.10, 35 below the surface

// Get the proper list of veins
List<Vein> veins;
if (generateOres)
Expand All @@ -67,10 +73,16 @@ public void generate(Random random, int chunkX, int chunkZ, World world, IChunkG
if (!veins.isEmpty())
{
veins.removeIf(v -> {
if (!v.type.hasLooseRocks()) return true;
if (!chunkData.getChunkOres().contains(v.type.ore)) return true;
// Only generates small ores whose veins generated at least at half rock layer 2
return (v.getHighestY() < (WorldTypeTFC.ROCKLAYER2 + WorldTypeTFC.ROCKLAYER3) / 2);
if (!v.type.hasLooseRocks() || v.getHighestY() < lowestYScan) return true;
for (ChunkDataTFC data : chunkData)
{
// No need to check for initialized chunk data, ore hashset will be empty.
if (data.getChunkOres().contains(v.type.ore))
{
return false;
}
}
return true;
});
}
}
Expand All @@ -86,7 +98,7 @@ public void generate(Random random, int chunkX, int chunkZ, World world, IChunkG
0,
zoff + random.nextInt(16)
);
Rock rock = chunkData.getRock1(pos);
Rock rock = chunkData[0].getRock1(pos);
generateRock(world, pos.up(world.getTopSolidOrLiquidBlock(pos).getY()), getRandomVein(veins, random), rock);
}
}
Expand Down
Expand Up @@ -70,7 +70,7 @@ public void preInit(File dir)
}
catch (IOException e)
{
throw new Error("Problem reading ore vein config files.", e);
throw new Error("Problem reading ore vein config folder.", e);
}
if (oreFiles.isEmpty())
{
Expand All @@ -93,38 +93,33 @@ public void preInit(File dir)

public void postInit()
{
List<String> worldGenData = new ArrayList<>();
weightedVeinTypes.clear();
veinTypeRegistry.clear();
oreFiles.forEach(file -> {
try
{
worldGenData.add(FileUtils.readFileToString(file, Charset.defaultCharset()));
}
catch (IOException e)
{
throw new Error("Error reading " + file, e);
}
});

if (!worldGenData.isEmpty())
{
try
{
weightedVeinTypes.clear();
veinTypeRegistry.clear();
worldGenData.forEach(data -> {
Map<String, VeinType> values = GSON.fromJson(data, new TypeToken<Map<String, VeinType>>() {}.getType());
String json = FileUtils.readFileToString(file, Charset.defaultCharset());
try
{
Map<String, VeinType> values = GSON.fromJson(json, new TypeToken<Map<String, VeinType>>() {}.getType());
values.forEach((name, veinType) -> {
veinType.setRegistryName(name);
veinTypeRegistry.put(name, veinType);
weightedVeinTypes.add(veinType.weight, veinType);
});
});
}
catch (JsonParseException e)
{
// Don't crash the game if one of the files error-ed, just show it in log
TerraFirmaCraft.getLog().warn("Error parsing " + file, e);
}
}
catch (JsonParseException e)
catch (IOException e)
{
TerraFirmaCraft.getLog().warn("There was a serious issue parsing the ore generation files!! TFC will not generate any ores!", e);
// Don't crash the game if one of the files error-ed, just show it in log
TerraFirmaCraft.getLog().warn("Error reading " + file, e);
}
}
});
if (veinTypeRegistry.isEmpty())
{
TerraFirmaCraft.getLog().warn("The ore vein registry is empty!! TFC will not generate any ores!");
Expand Down

0 comments on commit 21b8b58

Please sign in to comment.