aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/gregtech/api/util/GT_ChunkAssociatedData.java69
-rw-r--r--src/main/java/gregtech/common/GT_Pollution.java59
-rw-r--r--src/main/java/gregtech/common/GT_UndergroundOil.java32
-rw-r--r--src/main/java/gregtech/common/render/GT_PollutionRenderer.java4
4 files changed, 124 insertions, 40 deletions
diff --git a/src/main/java/gregtech/api/util/GT_ChunkAssociatedData.java b/src/main/java/gregtech/api/util/GT_ChunkAssociatedData.java
index aa779b0359..184f6f1011 100644
--- a/src/main/java/gregtech/api/util/GT_ChunkAssociatedData.java
+++ b/src/main/java/gregtech/api/util/GT_ChunkAssociatedData.java
@@ -104,7 +104,7 @@ public abstract class GT_ChunkAssociatedData<T extends GT_ChunkAssociatedData.ID
}
private ChunkCoordIntPair getRegionID(int aChunkX, int aChunkZ) {
- return new ChunkCoordIntPair(aChunkX / regionLength, aChunkZ / regionLength);
+ return new ChunkCoordIntPair(Math.floorDiv(aChunkX, regionLength), Math.floorDiv(aChunkZ, regionLength));
}
/**
@@ -168,7 +168,7 @@ public abstract class GT_ChunkAssociatedData<T extends GT_ChunkAssociatedData.ID
}
private void saveRegions(Stream<SuperRegion> stream) {
- stream.filter(r -> !r.isDirty())
+ stream.filter(r -> r.isDirty())
.map(c -> (Runnable) c::save)
.map(r -> CompletableFuture.runAsync(r, IO_WORKERS))
.reduce(CompletableFuture::allOf)
@@ -242,7 +242,12 @@ public abstract class GT_ChunkAssociatedData<T extends GT_ChunkAssociatedData.ID
}
protected File getSaveDirectory(World w) {
- return new File(w.getSaveHandler().getWorldDirectory(), "gregtech");
+ File base;
+ if (w.provider.getSaveFolder() == null)
+ base = w.getSaveHandler().getWorldDirectory();
+ else
+ base = new File(w.getSaveHandler().getWorldDirectory(), w.provider.getSaveFolder());
+ return new File(base, "gregtech");
}
public interface IData {
@@ -256,20 +261,23 @@ public abstract class GT_ChunkAssociatedData<T extends GT_ChunkAssociatedData.ID
private final T[] data = createData();
private final File backingStorage;
private final WeakReference<World> world;
+ /**
+ * Be aware, this means region coord, not bottom-left chunk coord
+ */
private final ChunkCoordIntPair coord;
- private SuperRegion(World world, int chunkX, int chunkZ) {
+ private SuperRegion(World world, int regionX, int regionZ) {
this.world = new WeakReference<>(world);
- this.coord = new ChunkCoordIntPair(chunkX, chunkZ);
- backingStorage = new File(getSaveDirectory(world), String.format("%s.%d.%d.dat", mId, chunkX, chunkZ));
+ this.coord = new ChunkCoordIntPair(regionX, regionZ);
+ backingStorage = new File(getSaveDirectory(world), String.format("%s.%d.%d.dat", mId, regionX, regionZ));
if (backingStorage.isFile())
load();
}
- private SuperRegion(World world, ChunkCoordIntPair coord) {
+ private SuperRegion(World world, ChunkCoordIntPair regionCoord) {
this.world = new WeakReference<>(world);
- this.coord = coord;
- backingStorage = new File(getSaveDirectory(world), String.format("%s.%d.%d.dat", mId, coord.chunkXPos, coord.chunkZPos));
+ this.coord = regionCoord;
+ backingStorage = new File(getSaveDirectory(world), String.format("%s.%d.%d.dat", mId, regionCoord.chunkXPos, regionCoord.chunkZPos));
if (backingStorage.isFile())
load();
}
@@ -308,16 +316,16 @@ public abstract class GT_ChunkAssociatedData<T extends GT_ChunkAssociatedData.ID
}
private int getChunkX(int index) {
- return index / regionLength + coord.chunkXPos;
+ return index / regionLength + coord.chunkXPos * regionLength;
}
private int getChunkZ(int index) {
- return index % regionLength + coord.chunkZPos;
+ return index % regionLength + coord.chunkZPos * regionLength;
}
public boolean isDirty() {
for (T datum : data) {
- if (datum != null && datum.isSameAsDefault())
+ if (datum != null && !datum.isSameAsDefault())
return true;
}
return false;
@@ -333,8 +341,6 @@ public abstract class GT_ChunkAssociatedData<T extends GT_ChunkAssociatedData.ID
}
private void save0() throws IOException {
- if (!isDirty())
- return;
//noinspection ResultOfMethodCallIgnored
backingStorage.getParentFile().mkdirs();
File tmpFile = getTmpFile();
@@ -392,19 +398,32 @@ public abstract class GT_ChunkAssociatedData<T extends GT_ChunkAssociatedData.ID
private void loadFromFile(File file) throws IOException {
World world = Objects.requireNonNull(this.world.get(), "Attempting to load region of another world!");
try (DataInputStream input = new DataInputStream(new FileInputStream(file))) {
- boolean nullRange = input.readBoolean();
- int ptr = 0;
- while (ptr != data.length) {
- int rangeEnd = ptr + input.readUnsignedShort();
- if (!nullRange) {
- for (; ptr < rangeEnd; ptr++) {
- data[ptr] = readElement(input, version, world, getChunkX(ptr), getChunkZ(ptr));
- }
- } else {
- Arrays.fill(data, ptr, rangeEnd, null);
- ptr = rangeEnd;
+ byte b = input.readByte();
+ switch (b) {
+ case 0:
+ loadV0(input, world);
+ break;
+ default:
+ GT_Log.err.printf("Unknown ChunkAssociatedData version %d\n", b);
+ }
+ }
+ }
+
+ private void loadV0(DataInput input, World world) throws IOException {
+ int version = input.readByte();
+ boolean nullRange = input.readBoolean();
+ int ptr = 0;
+ while (ptr != data.length) {
+ int rangeEnd = ptr + input.readUnsignedShort();
+ if (!nullRange) {
+ for (; ptr < rangeEnd; ptr++) {
+ data[ptr] = readElement(input, version, world, getChunkX(ptr), getChunkZ(ptr));
}
+ } else {
+ Arrays.fill(data, ptr, rangeEnd, null);
+ ptr = rangeEnd;
}
+ nullRange = !nullRange;
}
}
diff --git a/src/main/java/gregtech/common/GT_Pollution.java b/src/main/java/gregtech/common/GT_Pollution.java
index 8ad39306f1..afaac41650 100644
--- a/src/main/java/gregtech/common/GT_Pollution.java
+++ b/src/main/java/gregtech/common/GT_Pollution.java
@@ -9,6 +9,7 @@ import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.net.GT_Packet_Pollution;
import gregtech.api.util.GT_ChunkAssociatedData;
import gregtech.api.util.GT_Utility;
+import gregtech.common.render.GT_PollutionRenderer;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.EntityLivingBase;
@@ -101,6 +102,7 @@ public class GT_Pollution {
public static void onWorldTick(TickEvent.WorldTickEvent aEvent) {//called from proxy
//return if pollution disabled
if (!GT_Mod.gregtechproxy.mPollution) return;
+ if (aEvent.phase == TickEvent.Phase.START) return;
final GT_Pollution pollutionInstance = dimensionWisePollution.get(aEvent.world.provider.dimensionId);
if (pollutionInstance == null) return;
pollutionInstance.tickPollutionInWorld((int) (aEvent.world.getTotalWorldTime() % cycleLen));
@@ -278,16 +280,32 @@ public class GT_Pollution {
return dimensionWisePollution.computeIfAbsent(world.provider.dimensionId, i -> new GT_Pollution(world));
}
+ /** @see #addPollution(World, int, int, int) */
public static void addPollution(IGregTechTileEntity te, int aPollution) {
- if (!GT_Mod.gregtechproxy.mPollution || aPollution == 0) return;
+ if (!GT_Mod.gregtechproxy.mPollution || aPollution == 0 || te.isClientSide()) return;
mutatePollution(te.getWorld(), te.getXCoord() >> 4, te.getZCoord() >> 4, d -> d.changeAmount(aPollution), null);
}
+ /** @see #addPollution(World, int, int, int) */
public static void addPollution(Chunk ch, int aPollution) {
- if (!GT_Mod.gregtechproxy.mPollution || aPollution == 0) return;
+ if (!GT_Mod.gregtechproxy.mPollution || aPollution == 0 || ch.worldObj.isRemote) return;
mutatePollution(ch.worldObj, ch.xPosition, ch.zPosition, d -> d.changeAmount(aPollution), null);
}
+ /**
+ * Add some pollution to given chunk. Can pass in negative to remove pollution.
+ * Will clamp the final pollution number to 0 if it would be changed into negative.
+ *
+ * @param w world to modify. do nothing if it's a client world
+ * @param chunkX chunk coordinate X, i.e. blockX >> 4
+ * @param chunkZ chunk coordinate Z, i.e. blockZ >> 4
+ * @param aPollution desired delta. Positive means the pollution in chunk would go higher.
+ */
+ public static void addPollution(World w, int chunkX, int chunkZ, int aPollution) {
+ if (!GT_Mod.gregtechproxy.mPollution || aPollution == 0 || w.isRemote) return;
+ mutatePollution(w, chunkX, chunkZ, d -> d.changeAmount(aPollution), null);
+ }
+
private static void mutatePollution(World world, int x, int z, Consumer<ChunkData> mutator, @Nullable Set<ChunkCoordIntPair> chunks) {
ChunkData data = STORAGE.get(world, x, z);
boolean hadPollution = data.getAmount() > 0;
@@ -303,14 +321,36 @@ public class GT_Pollution {
}
}
+ /** @see #getPollution(World, int, int) */
public static int getPollution(IGregTechTileEntity te) {
- return getPollution(te.getWorld().getChunkFromBlockCoords(te.getXCoord(), te.getZCoord()));
+ return getPollution(te.getWorld(), te.getXCoord() >> 4, te.getZCoord() >> 4);
}
+ /** @see #getPollution(World, int, int) */
public static int getPollution(Chunk ch) {
+ return getPollution(ch.worldObj, ch.xPosition, ch.zPosition);
+ }
+
+ /**
+ * Get the pollution in specified chunk
+ * @param w world to look in. can be a client world, but that limits the knowledge to what server side send us
+ * @param chunkX chunk coordinate X, i.e. blockX >> 4
+ * @param chunkZ chunk coordinate Z, i.e. blockZ >> 4
+ * @return pollution amount. may be 0 if pollution is disabled, or if it's a client world and server did not send
+ * us info about this chunk
+ */
+ public static int getPollution(World w, int chunkX, int chunkZ) {
if (!GT_Mod.gregtechproxy.mPollution)
return 0;
- return STORAGE.get(ch).getAmount();
+ if (w.isRemote)
+ // it really should be querying the client side stuff instead
+ return GT_PollutionRenderer.getKnownPollution(chunkX << 4, chunkZ << 4);
+ return STORAGE.get(w, chunkX, chunkZ).getAmount();
+ }
+
+ @Deprecated
+ public static int getPollution(ChunkCoordIntPair aCh, int aDim) {
+ return getPollution(DimensionManager.getWorld(aDim), aCh.chunkXPos, aCh.chunkZPos);
}
public static boolean hasPollution(Chunk ch) {
@@ -319,12 +359,6 @@ public class GT_Pollution {
return STORAGE.isCreated(ch.worldObj, ch.getChunkCoordIntPair()) && STORAGE.get(ch).getAmount() > 0;
}
- public static int getPollution(ChunkCoordIntPair aCh, int aDim) {
- if (!GT_Mod.gregtechproxy.mPollution)
- return 0;
- return STORAGE.get(DimensionManager.getWorld(aDim), aCh.chunkXPos, aCh.chunkZPos).getAmount();
- }
-
//Add compatibility with old code
@Deprecated /*Don't use it... too weird way of passing position*/
public static void addPollution(World aWorld, ChunkPosition aPos, int aPollution) {
@@ -352,7 +386,8 @@ public class GT_Pollution {
@SubscribeEvent
public void onWorldLoad(WorldEvent.Load e) {
// super class loads everything lazily. We force it to load them all.
- STORAGE.loadAll(e.world);
+ if (!e.world.isRemote)
+ STORAGE.loadAll(e.world);
}
}
@@ -369,6 +404,8 @@ public class GT_Pollution {
@Override
protected ChunkData readElement(DataInput input, int version, World world, int chunkX, int chunkZ) throws IOException {
+ if (version != 0)
+ throw new IOException("Region file corrupted");
ChunkData data = new ChunkData(input.readInt());
if (data.getAmount() > 0)
getPollutionManager(world).pollutedChunks.add(new ChunkCoordIntPair(chunkX, chunkZ));
diff --git a/src/main/java/gregtech/common/GT_UndergroundOil.java b/src/main/java/gregtech/common/GT_UndergroundOil.java
index 21b681de4a..b725b4bbb6 100644
--- a/src/main/java/gregtech/common/GT_UndergroundOil.java
+++ b/src/main/java/gregtech/common/GT_UndergroundOil.java
@@ -31,22 +31,46 @@ public class GT_UndergroundOil {
private static final GT_UndergroundOilStore STORAGE = new GT_UndergroundOilStore();
private static final ChunkData NIL_FLUID_STACK = new ChunkData(-1, null, null, false);
+ /**
+ * Effectively just call {@code undergroundOil(te, -1)} for you
+ * @see #undergroundOil(World, int, int, float)
+ */
public static FluidStack undergroundOilReadInformation(IGregTechTileEntity te){
return undergroundOil(te.getWorld().getChunkFromBlockCoords(te.getXCoord(),te.getZCoord()),-1);
}
+ /**
+ * Effectively just call {@code undergroundOil(chunk, -1)} for you
+ * @see #undergroundOil(World, int, int, float)
+ */
public static FluidStack undergroundOilReadInformation(Chunk chunk) {
return undergroundOil(chunk,-1);
}
+ /** @see #undergroundOil(World, int, int, float) */
public static FluidStack undergroundOil(IGregTechTileEntity te, float readOrDrainCoefficient){
return undergroundOil(te.getWorld().getChunkFromBlockCoords(te.getXCoord(),te.getZCoord()),readOrDrainCoefficient);
}
//Returns whole content for information purposes -> when drainSpeedCoefficient < 0
//Else returns extracted fluidStack if amount > 0, or null otherwise
+ /** @see #undergroundOil(World, int, int, float) */
public static FluidStack undergroundOil(Chunk chunk, float readOrDrainCoefficient) {
- ChunkData chunkData = STORAGE.get(chunk);
+ return undergroundOil(chunk.worldObj, chunk.xPosition, chunk.zPosition, readOrDrainCoefficient);
+ }
+
+ /**
+ * Pump fluid or read info.
+ * @param w a remote World. For a WorldClient it will always tell you null
+ * @param chunkX chunk coordinate X, i.e. blockX >> 4
+ * @param chunkZ chunk coordinate Z, i.e. blockZ >> 4
+ * @param readOrDrainCoefficient how fast to pump. The higher the faster. use negative to read expected current output
+ * @return null if nothing here, or depleted already, or a client side world
+ */
+ public static FluidStack undergroundOil(World w, int chunkX, int chunkZ, float readOrDrainCoefficient) {
+ if (w.isRemote)
+ return null; // troublemakers go away
+ ChunkData chunkData = STORAGE.get(w, chunkX, chunkZ);
if (chunkData.getVein() == null || chunkData.getFluid() == null) // nothing here...
return null;
//do stuff on it if needed
@@ -102,7 +126,7 @@ public class GT_UndergroundOil {
ChunkData chunkData = STORAGE.get(e.getChunk());
Fluid fluid = chunkData.getFluid();
if (fluid != null && fluid.getID() == e.getData().getInteger("GTOILFLUID"))
- chunkData.setAmount(Math.min(0, Math.min(chunkData.getAmount(), e.getData().getInteger("GTOIL"))));
+ chunkData.setAmount(Math.min(chunkData.getAmount(), e.getData().getInteger("GTOIL")));
}
}
@@ -250,7 +274,7 @@ public class GT_UndergroundOil {
public void changeAmount(int delta) {
if (delta != 0)
dirty = true;
- this.amount = Math.max(0, amount - delta);
+ this.amount = Math.max(amount + delta, 0);
}
@Nullable
@@ -272,7 +296,7 @@ public class GT_UndergroundOil {
@Override
public boolean isSameAsDefault() {
- return dirty;
+ return !dirty;
}
}
}
diff --git a/src/main/java/gregtech/common/render/GT_PollutionRenderer.java b/src/main/java/gregtech/common/render/GT_PollutionRenderer.java
index 305eadf145..5128af1c36 100644
--- a/src/main/java/gregtech/common/render/GT_PollutionRenderer.java
+++ b/src/main/java/gregtech/common/render/GT_PollutionRenderer.java
@@ -101,6 +101,10 @@ public class GT_PollutionRenderer {
return color(oColor, pollutionMap.getPollution(x, z)/1000, 300, 500, foliageColor);
}
+ public static int getKnownPollution(int x, int z) {
+ return pollutionMap.getPollution(x, z);
+ }
+
@SubscribeEvent(priority = EventPriority.LOW)
public void manipulateColor(EntityViewRenderEvent.FogColors event) {
if (!DEBUG && Minecraft.getMinecraft().thePlayer.capabilities.isCreativeMode)