diff options
author | miozune <miozune@gmail.com> | 2023-05-26 21:09:03 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-26 14:09:03 +0200 |
commit | d0e291fa8cb31cedcf9ef8fbafb536f19b216907 (patch) | |
tree | 2e7474220b3f7cbeeb9db8717f010786397934e9 /src/main/java/gregtech/common | |
parent | 497080d591db8af491ece783bf9a480b50a7c16b (diff) | |
download | GT5-Unofficial-d0e291fa8cb31cedcf9ef8fbafb536f19b216907.tar.gz GT5-Unofficial-d0e291fa8cb31cedcf9ef8fbafb536f19b216907.tar.bz2 GT5-Unofficial-d0e291fa8cb31cedcf9ef8fbafb536f19b216907.zip |
Add void protection mode to fluid and ore drillers (#2023)
* Refactor GT_ParallelHelper part 1
* Refactor GT_ParallelHelper part 2
* Move void protection logic to separated class
* Clean doc
* Fix inverted logic in VoidProtectionHelper
* Add void protection mode to fluid and ore drillers
Diffstat (limited to 'src/main/java/gregtech/common')
3 files changed, 115 insertions, 32 deletions
diff --git a/src/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_OilDrillBase.java b/src/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_OilDrillBase.java index 1e7f683bc5..9cab5b4ec6 100644 --- a/src/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_OilDrillBase.java +++ b/src/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_OilDrillBase.java @@ -14,6 +14,8 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import javax.annotation.Nonnegative; + import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; @@ -37,6 +39,8 @@ import gregtech.api.render.TextureFactory; import gregtech.api.util.GT_Log; import gregtech.api.util.GT_Multiblock_Tooltip_Builder; import gregtech.api.util.GT_Utility; +import gregtech.api.util.ValidationResult; +import gregtech.api.util.ValidationType; public abstract class GT_MetaTileEntity_OilDrillBase extends GT_MetaTileEntity_DrillerBase { @@ -195,7 +199,13 @@ public abstract class GT_MetaTileEntity_OilDrillBase extends GT_MetaTileEntity_D mWorkChunkNeedsReload = false; } float speed = computeSpeed(); - FluidStack tFluid = pumpOil(speed); + ValidationResult<FluidStack> pumpResult = tryPumpOil(speed); + if (pumpResult.getType() != ValidationType.VALID) { + mEUt = 0; + mMaxProgresstime = 0; + return false; + } + FluidStack tFluid = pumpResult.getResult(); if (tFluid != null && tFluid.amount > getTotalConfigValue()) { this.mOutputFluids = new FluidStack[] { tFluid }; return true; @@ -265,37 +275,69 @@ public abstract class GT_MetaTileEntity_OilDrillBase extends GT_MetaTileEntity_D return !mOilFieldChunks.isEmpty(); } - protected FluidStack pumpOil(float speed) { + /** + * Tries to pump oil, accounting for output space if void protection is enabled. + * <p> + * If pumped fluid will not fit in output hatches, it returns a result with INVALID. + * <p> + * If vein is depleted, it returns a result with VALID and null fluid. + */ + protected ValidationResult<FluidStack> tryPumpOil(float speed) { if (mOilId <= 0) return null; - FluidStack tFluid, tOil; - tOil = new FluidStack(FluidRegistry.getFluid(mOilId), 0); if (debugDriller) { GT_Log.out.println(" pump speed = " + speed); } + if (!voidExcess) { + FluidStack simulatedOil = pumpOil(speed, true); + if (!canFitOutput(new FluidStack[] { simulatedOil })) { + return ValidationResult.of(ValidationType.INVALID, null); + } + } + + FluidStack pumpedOil = pumpOil(speed, false); + mOilFlow = pumpedOil.amount; + return ValidationResult.of(ValidationType.VALID, pumpedOil.amount == 0 ? null : pumpedOil); + } + + /** + * @param speed Speed to pump oil + * @param simulate If true, it actually does not consume vein + * @return Fluid pumped + */ + protected FluidStack pumpOil(@Nonnegative float speed, boolean simulate) { + if (speed < 0) { + throw new IllegalArgumentException("Don't pass negative speed"); + } + ArrayList<Chunk> emptyChunks = new ArrayList<>(); + FluidStack returnOil = new FluidStack(FluidRegistry.getFluid(mOilId), 0); for (Chunk tChunk : mOilFieldChunks) { - tFluid = undergroundOil(tChunk, speed); + FluidStack pumped = undergroundOil(tChunk, simulate ? -speed : speed); if (debugDriller) { GT_Log.out.println( " chunkX = " + tChunk.getChunkCoordIntPair().chunkXPos + " chunkZ = " + tChunk.getChunkCoordIntPair().chunkZPos); - if (tFluid != null) { - GT_Log.out.println(" Fluid pumped = " + tFluid.amount); + if (pumped != null) { + GT_Log.out.println(" Fluid pumped = " + pumped.amount); } else { GT_Log.out.println(" No fluid pumped "); } } - if (tFluid == null || tFluid.amount < 1) emptyChunks.add(tChunk); - if (tOil.isFluidEqual(tFluid)) tOil.amount += tFluid.amount; + if (pumped == null || pumped.amount < 1) { + emptyChunks.add(tChunk); + continue; + } + if (returnOil.isFluidEqual(pumped)) { + returnOil.amount += pumped.amount; + } } for (Chunk tChunk : emptyChunks) { mOilFieldChunks.remove(tChunk); } - mOilFlow = tOil.amount; - return tOil.amount == 0 ? null : tOil; + return returnOil; } @Override @@ -328,4 +370,9 @@ public abstract class GT_MetaTileEntity_OilDrillBase extends GT_MetaTileEntity_D l.addAll(Arrays.asList(super.getInfoData())); return l.toArray(new String[0]); } + + @Override + protected boolean isVoidExcessButtonEnabled() { + return true; + } } diff --git a/src/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_OilDrillInfinite.java b/src/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_OilDrillInfinite.java index 2c86218bfb..1146c39f21 100644 --- a/src/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_OilDrillInfinite.java +++ b/src/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_OilDrillInfinite.java @@ -50,8 +50,9 @@ public class GT_MetaTileEntity_OilDrillInfinite extends GT_MetaTileEntity_OilDri } @Override - protected FluidStack pumpOil(float speed) { - return super.pumpOil(-speed); + protected FluidStack pumpOil(float speed, boolean simulate) { + // always simulate to not deplete vein + return super.pumpOil(speed, true); } @Override diff --git a/src/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_OreDrillingPlantBase.java b/src/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_OreDrillingPlantBase.java index 8e8f16d111..b6a0d92f42 100644 --- a/src/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_OreDrillingPlantBase.java +++ b/src/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_OreDrillingPlantBase.java @@ -41,7 +41,7 @@ import gregtech.common.blocks.GT_TileEntity_Ores; public abstract class GT_MetaTileEntity_OreDrillingPlantBase extends GT_MetaTileEntity_DrillerBase { - private final ArrayList<ChunkPosition> oreBlockPositions = new ArrayList<>(); + private final List<ChunkPosition> oreBlockPositions = new ArrayList<>(); protected int mTier = 1; private int chunkRadiusConfig = getRadiusInChunks(); private boolean replaceWithCobblestone = true; @@ -130,11 +130,31 @@ public abstract class GT_MetaTileEntity_OreDrillingPlantBase extends GT_MetaTile // new layer - fill again fillMineListIfEmpty(xDrill, yDrill, zDrill, xPipe, zPipe, yHead); } - return processOreList(); + return tryProcessOreList(); } - private boolean processOreList() { + private boolean tryProcessOreList() { + if (!voidExcess) { + boolean simulateResult = processOreList(true); + if (!simulateResult) { + mEUt = 0; + mMaxProgresstime = 0; + return false; + } + } + boolean result = processOreList(false); + if (!result) { + mEUt = 0; + mMaxProgresstime = 0; + return false; + } + return true; + } + + private boolean processOreList(boolean simulate) { ChunkPosition oreBlockPos = null; + List<ChunkPosition> oreBlockPositions = simulate ? copyOreBlockPositions(this.oreBlockPositions) + : this.oreBlockPositions; int x = 0, y = 0, z = 0; Block oreBlock = null; int oreBlockMetadata = 0; @@ -150,7 +170,7 @@ public abstract class GT_MetaTileEntity_OreDrillingPlantBase extends GT_MetaTile .getBlockMetadata(x, y, z); } - if (!tryConsumeDrillingFluid()) { + if (!tryConsumeDrillingFluid(simulate)) { oreBlockPositions.add(0, oreBlockPos); return false; } @@ -163,18 +183,32 @@ public abstract class GT_MetaTileEntity_OreDrillingPlantBase extends GT_MetaTile Collection<ItemStack> oreBlockDrops = getBlockDrops(oreBlock, x, y, z); ItemStack cobble = GT_Utility.getCobbleForOre(oreBlock, metaData); - if (replaceWithCobblestone) { - getBaseMetaTileEntity().getWorld() - .setBlock(x, y, z, Block.getBlockFromItem(cobble.getItem()), cobble.getItemDamage(), 3); - } else { - getBaseMetaTileEntity().getWorld() - .setBlockToAir(oreBlockPos.chunkPosX, oreBlockPos.chunkPosY, oreBlockPos.chunkPosZ); + if (!simulate) { + if (replaceWithCobblestone) { + getBaseMetaTileEntity().getWorld() + .setBlock(x, y, z, Block.getBlockFromItem(cobble.getItem()), cobble.getItemDamage(), 3); + } else { + getBaseMetaTileEntity().getWorld() + .setBlockToAir(oreBlockPos.chunkPosX, oreBlockPos.chunkPosY, oreBlockPos.chunkPosZ); + } } - mOutputItems = getOutputByDrops(oreBlockDrops); + ItemStack[] toOutput = getOutputByDrops(oreBlockDrops); + if (simulate && !canFitOutput(toOutput)) { + return false; + } + mOutputItems = toOutput; } return true; } + private static List<ChunkPosition> copyOreBlockPositions(List<ChunkPosition> oreBlockPositions) { + List<ChunkPosition> ret = new ArrayList<>(); + for (ChunkPosition chunkPosition : oreBlockPositions) { + ret.add(new ChunkPosition(chunkPosition.chunkPosX, chunkPosition.chunkPosY, chunkPosition.chunkPosZ)); + } + return ret; + } + @Override protected boolean workingAtBottom(ItemStack aStack, int xDrill, int yDrill, int zDrill, int xPipe, int zPipe, int yHead, int oldYHead) { @@ -199,7 +233,7 @@ public abstract class GT_MetaTileEntity_OreDrillingPlantBase extends GT_MetaTile return true; } } - return processOreList(); + return tryProcessOreList(); } private void createInitialWorkingChunk() { @@ -220,7 +254,7 @@ public abstract class GT_MetaTileEntity_OreDrillingPlantBase extends GT_MetaTile int yHead, int oldYHead) { if (!mChunkLoadingEnabled || oreBlockPositions.isEmpty()) return super.workingUpward(aStack, xDrill, yDrill, zDrill, xPipe, zPipe, yHead, oldYHead); - boolean result = processOreList(); + boolean result = tryProcessOreList(); if (oreBlockPositions.isEmpty()) GT_ChunkManager.releaseTicket((TileEntity) getBaseMetaTileEntity()); return result; } @@ -331,12 +365,8 @@ public abstract class GT_MetaTileEntity_OreDrillingPlantBase extends GT_MetaTile } else return oreBlock.getDrops(getBaseMetaTileEntity().getWorld(), posX, posY, posZ, blockMeta, mTier + 3); } - private boolean tryConsumeDrillingFluid() { - if (!depleteInput(new FluidStack(ItemList.sDrillingFluid, 2000))) { - mMaxProgresstime = 0; - return false; - } - return true; + private boolean tryConsumeDrillingFluid(boolean simulate) { + return depleteInput(new FluidStack(ItemList.sDrillingFluid, 2000), simulate); } private void fillChunkMineList(int yHead, int yDrill) { @@ -433,4 +463,9 @@ public abstract class GT_MetaTileEntity_OreDrillingPlantBase extends GT_MetaTile + " " + StatCollector.translateToLocal("GT5U.machines.chunks") }; } + + @Override + protected boolean isVoidExcessButtonEnabled() { + return true; + } } |