From 9d432a42b6b7e502f8593b437fb2c110f5de8082 Mon Sep 17 00:00:00 2001 From: Glease <4586901+Glease@users.noreply.github.com> Date: Sun, 28 Nov 2021 21:52:26 +0800 Subject: Fix pollution ticking and reading Signed-off-by: Glease <4586901+Glease@users.noreply.github.com> --- src/main/java/gregtech/api/util/GT_ChunkAssociatedData.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main/java/gregtech/api/util/GT_ChunkAssociatedData.java') diff --git a/src/main/java/gregtech/api/util/GT_ChunkAssociatedData.java b/src/main/java/gregtech/api/util/GT_ChunkAssociatedData.java index bd9148b516..aa779b0359 100644 --- a/src/main/java/gregtech/api/util/GT_ChunkAssociatedData.java +++ b/src/main/java/gregtech/api/util/GT_ChunkAssociatedData.java @@ -296,7 +296,7 @@ public abstract class GT_ChunkAssociatedData Date: Tue, 30 Nov 2021 03:07:30 +0800 Subject: Bug fix pass in UO and Pollution Signed-off-by: Glease <4586901+Glease@users.noreply.github.com> --- .../gregtech/api/util/GT_ChunkAssociatedData.java | 69 ++++++++++++++-------- src/main/java/gregtech/common/GT_Pollution.java | 59 ++++++++++++++---- .../java/gregtech/common/GT_UndergroundOil.java | 32 ++++++++-- .../common/render/GT_PollutionRenderer.java | 4 ++ 4 files changed, 124 insertions(+), 40 deletions(-) (limited to 'src/main/java/gregtech/api/util/GT_ChunkAssociatedData.java') 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 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 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 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 mutator, @Nullable Set 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) -- cgit