Skip to content

Commit

Permalink
Fix compatibility with vanilla GT underground fluids
Browse files Browse the repository at this point in the history
  • Loading branch information
Johann Bernhardt committed Dec 11, 2021
1 parent c3c6973 commit 3c27f1d
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.sinthoras.visualprospecting.Utils;
import com.sinthoras.visualprospecting.database.veintypes.VeinType;
import com.sinthoras.visualprospecting.database.veintypes.VeinTypeCaching;
import com.sinthoras.visualprospecting.integration.gregtech.UndergroundFluidsWrapper;
import net.minecraft.world.World;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
Expand All @@ -13,8 +14,6 @@
import java.util.ArrayList;
import java.util.List;

import static gregtech.common.GT_UndergroundOil.undergroundOil;

public class ServerCache extends WorldCache {

public static final ServerCache instance = new ServerCache();
Expand Down Expand Up @@ -80,7 +79,7 @@ public List<UndergroundFluidPosition> prospectUndergroundFluidBlockRadius(World
Fluid fluid = null;
for (int offsetChunkX = 0; offsetChunkX < VP.undergroundFluidSizeChunkX; offsetChunkX++) {
for (int offsetChunkZ = 0; offsetChunkZ < VP.undergroundFluidSizeChunkZ; offsetChunkZ++) {
final FluidStack prospectedFluid = undergroundOil(world, chunkX + offsetChunkX, chunkZ + offsetChunkZ, -1);
final FluidStack prospectedFluid = UndergroundFluidsWrapper.prospectFluid(world, chunkX + offsetChunkX, chunkZ + offsetChunkZ);
if (prospectedFluid != null) {
fluid = prospectedFluid.getFluid();
chunks[offsetChunkX][offsetChunkZ] = prospectedFluid.amount;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.sinthoras.visualprospecting.integration.gregtech;

import gregtech.GT_Mod;
import gregtech.api.objects.GT_UO_Dimension;
import gregtech.api.objects.GT_UO_Fluid;
import gregtech.api.objects.XSTR;
import gregtech.common.GT_UndergroundOil;
import net.minecraft.world.ChunkCoordIntPair;
import net.minecraft.world.World;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;

import java.util.HashMap;
import java.util.Map;

import static gregtech.api.objects.XSTR.XSTR_INSTANCE;
import static gregtech.common.GT_Proxy.*;
import static gregtech.common.GT_Proxy.GTOIL;
import static gregtech.common.GT_UndergroundOil.undergroundOil;

public class UndergroundFluidsWrapper {

private final static boolean isGTNHGregTechUndergroundFluids;
private final static float MODE_READ_ONLY = -1;

static {
boolean foundMethod;
try {
GT_UndergroundOil.class.getDeclaredMethod("undergroundOil", World.class, int.class, int.class, float.class);
foundMethod = true;
}
catch (NoSuchMethodException e) {
foundMethod = false;
}
isGTNHGregTechUndergroundFluids = foundMethod;
}

// GTNH's GregTech5-Unofficial was rewritten and requires different access
public static FluidStack prospectFluid(World world, int chunkX, int chunkZ) {
if(isGTNHGregTechUndergroundFluids) {
return undergroundOil(world, chunkX, chunkZ, MODE_READ_ONLY);
}
else {
return vanillaProspectFluid(world, chunkX, chunkZ);
}
}

// Rewrite from GT_UndergroundOil.undergroundOil(Chunk chunk, float readOrDrainCoefficient),
// because there is no reason to require a chunk to be loaded
private static FluidStack vanillaProspectFluid(World world, int chunkX, int chunkZ) {
final ChunkCoordIntPair chunkCoordinate = new ChunkCoordIntPair(chunkX, chunkZ);
int dimensionId = world.provider.dimensionId;
GT_UO_Dimension dimension = GT_Mod.gregtechproxy.mUndergroundOil.GetDimension(dimensionId);
if (dimension == null) {
return null;
}

Map<ChunkCoordIntPair, int[]> chunkData = dimensionWiseChunkData.computeIfAbsent(dimensionId, k -> new HashMap<>(1024));

int[] tInts = chunkData.get(chunkCoordinate);

if (tInts == null) {
tInts = getDefaultChunkDataOnCreation();
}
else if (tInts[GTOIL] == 0) {
return new FluidStack(FluidRegistry.getFluid(tInts[GTOILFLUID]), 0);
}

final XSTR tRandom = new XSTR(world.getSeed() + dimensionId * 2L + (chunkX >> 3) + 8267L * (chunkZ >> 3));

GT_UO_Fluid uoFluid = dimension.getRandomFluid(tRandom);

FluidStack fluidInChunk;

if (uoFluid == null || uoFluid.getFluid() == null) {
tInts[GTOILFLUID] = Integer.MAX_VALUE;//null fluid pointer... kind of
tInts[GTOIL] = 0;
chunkData.put(chunkCoordinate, tInts);//update hash map
return null;
}
else {
if (tInts[GTOILFLUID] == uoFluid.getFluid().getID()) {//if stored fluid matches uoFluid
fluidInChunk = new FluidStack(uoFluid.getFluid(), tInts[GTOIL]);
}
else {
fluidInChunk = new FluidStack(uoFluid.getFluid(), uoFluid.getRandomAmount(tRandom));
fluidInChunk.amount = (int) ((float) fluidInChunk.amount * (0.75f + (XSTR_INSTANCE.nextFloat() / 2f)));//Randomly change amounts by +/- 25%
}
tInts[GTOIL] = fluidInChunk.amount;
tInts[GTOILFLUID] = fluidInChunk.getFluidID();
}

if (fluidInChunk.amount <= GT_UndergroundOil.DIVIDER) {
fluidInChunk.amount = 0;//return informative stack
tInts[GTOIL] = 0;//so in next access it will stop way above
}
else {
fluidInChunk.amount = fluidInChunk.amount / GT_UndergroundOil.DIVIDER;//give moderate extraction speed
}

chunkData.put(chunkCoordinate, tInts);//update hash map
return fluidInChunk;
}
}

0 comments on commit 3c27f1d

Please sign in to comment.