From 69fc32e3f6502b19b6c50670a63c1280abb34162 Mon Sep 17 00:00:00 2001 From: Layue13 Date: Sat, 7 Nov 2026 14:11:48 +0800 Subject: [PATCH] perf: tanks and tainted crops stop re-querying the same positions each tick Closes TDLM/thaumicadditions#20, closes TDLM/thaumicadditions#22 Co-Authored-By: Claude Sonnet 5.5 --- dependencies.gradle | 1 + .../old/tile/TileOldReinforcedTank.java | 19 +++-------- .../tileentity/TileTaintedCrops.java | 32 ++++++++++--------- .../additions/tileentity/TileTankBasic.java | 24 ++++++-------- .../tileentity/TankAndCropLogicTest.java | 32 +++++++++++++++++++ 5 files changed, 65 insertions(+), 43 deletions(-) create mode 100644 src/test/java/com/pengu/thaumcraft/additions/tileentity/TankAndCropLogicTest.java diff --git a/dependencies.gradle b/dependencies.gradle index a9b6350..08a0fcb 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -4,6 +4,7 @@ repositories { dependencies { // Thaumcraft 4 full dev jar (api + common impl; the sources import thaumcraft.common.* and thaumcraft.client.*) + testImplementation("junit:junit:4.13.2") api("uk.potatolab:Thaumcraft4:4.2.3.5-20261028.155248-g1a600e9") // cofh.api.energy.IEnergyReceiver is used by TileThaumicGenerator (the CoFH API lives in CoFHLib, not CoFHCore) compileOnly("uk.potatolab:CoFHLib-1.7.10-Legacy:1.7.10R3.0.3-20261028.145717-gbf0ac89") diff --git a/src/main/java/com/pengu/thaumcraft/additions/piping/old/tile/TileOldReinforcedTank.java b/src/main/java/com/pengu/thaumcraft/additions/piping/old/tile/TileOldReinforcedTank.java index 5a72f85..810db0c 100644 --- a/src/main/java/com/pengu/thaumcraft/additions/piping/old/tile/TileOldReinforcedTank.java +++ b/src/main/java/com/pengu/thaumcraft/additions/piping/old/tile/TileOldReinforcedTank.java @@ -14,20 +14,11 @@ public class TileOldReinforcedTank extends TileTankBasic { } public void updateEntity() { - if(!(this.worldObj.getBlock(this.xCoord, this.yCoord + 1, this.zCoord) instanceof BlockTankBasic) && !(this.worldObj.getBlock(this.xCoord, this.yCoord - 1, this.zCoord) instanceof BlockTankBasic) && this.worldObj.getBlockMetadata(this.xCoord, this.yCoord, this.zCoord) != 0) { - Util.setBlockMetadata(this.worldObj, this.xCoord, this.yCoord, this.zCoord, 0); - } - - if(!(this.worldObj.getBlock(this.xCoord, this.yCoord + 1, this.zCoord) instanceof BlockTankBasic) && this.worldObj.getBlock(this.xCoord, this.yCoord - 1, this.zCoord) instanceof BlockTankBasic && this.worldObj.getBlockMetadata(this.xCoord, this.yCoord, this.zCoord) != 1) { - Util.setBlockMetadata(this.worldObj, this.xCoord, this.yCoord, this.zCoord, 1); - } - - if(this.worldObj.getBlock(this.xCoord, this.yCoord + 1, this.zCoord) instanceof BlockTankBasic && !(this.worldObj.getBlock(this.xCoord, this.yCoord - 1, this.zCoord) instanceof BlockTankBasic) && this.worldObj.getBlockMetadata(this.xCoord, this.yCoord, this.zCoord) != 2) { - Util.setBlockMetadata(this.worldObj, this.xCoord, this.yCoord, this.zCoord, 2); - } - - if(this.worldObj.getBlock(this.xCoord, this.yCoord + 1, this.zCoord) instanceof BlockTankBasic && this.worldObj.getBlock(this.xCoord, this.yCoord - 1, this.zCoord) instanceof BlockTankBasic && this.worldObj.getBlockMetadata(this.xCoord, this.yCoord, this.zCoord) != 3) { - Util.setBlockMetadata(this.worldObj, this.xCoord, this.yCoord, this.zCoord, 3); + boolean above = this.worldObj.getBlock(this.xCoord, this.yCoord + 1, this.zCoord) instanceof BlockTankBasic; + boolean below = this.worldObj.getBlock(this.xCoord, this.yCoord - 1, this.zCoord) instanceof BlockTankBasic; + int wantedMeta = TileTankBasic.connectionMeta(above, below); + if(this.worldObj.getBlockMetadata(this.xCoord, this.yCoord, this.zCoord) != wantedMeta) { + Util.setBlockMetadata(this.worldObj, this.xCoord, this.yCoord, this.zCoord, wantedMeta); } boolean ext = this.worldObj.getTileEntity(this.xCoord, this.yCoord + 1, this.zCoord) instanceof TileOldReinforcedTank && this.vis + this.taint < this.capacity; diff --git a/src/main/java/com/pengu/thaumcraft/additions/tileentity/TileTaintedCrops.java b/src/main/java/com/pengu/thaumcraft/additions/tileentity/TileTaintedCrops.java index e018690..ca1d0fa 100644 --- a/src/main/java/com/pengu/thaumcraft/additions/tileentity/TileTaintedCrops.java +++ b/src/main/java/com/pengu/thaumcraft/additions/tileentity/TileTaintedCrops.java @@ -2,11 +2,8 @@ package com.pengu.thaumcraft.additions.tileentity; import com.pengu.thaumcraft.additions.init.All; import com.pengu.thaumcraft.additions.utils.Util; -import java.lang.reflect.Method; import net.minecraft.init.Blocks; -import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.world.World; import net.minecraftforge.common.IPlantable; import net.minecraftforge.common.util.ForgeDirection; import thaumcraft.api.TileThaumcraft; @@ -17,24 +14,29 @@ public class TileTaintedCrops extends TileThaumcraft { public int tick = 0; + /** Ticks of growth needed per stage; the taint biome grows faster. */ + public static int growthThreshold(boolean taintBiome) { + return 4000 - (taintBiome?450:0); + } + + /** Growth only counts while the crop is not yet fully grown. */ + public static boolean countsGrowth(int meta) { + return meta < 4; + } + public void updateEntity() { - if(!this.worldObj.getBlock(this.xCoord, this.yCoord - 1, this.zCoord).canSustainPlant(this.worldObj, this.xCoord, this.yCoord - 1, this.zCoord, ForgeDirection.UP, (IPlantable)All.B_taint_crops) && this.worldObj.getBlock(this.xCoord, this.yCoord - 1, this.zCoord) != Blocks.grass && this.worldObj.getBlock(this.xCoord, this.yCoord - 1, this.zCoord) != Blocks.dirt) { + net.minecraft.block.Block soil = this.worldObj.getBlock(this.xCoord, this.yCoord - 1, this.zCoord); + if(!soil.canSustainPlant(this.worldObj, this.xCoord, this.yCoord - 1, this.zCoord, ForgeDirection.UP, (IPlantable)All.B_taint_crops) && soil != Blocks.grass && soil != Blocks.dirt) { + // the crop block is gone after this, so there is nothing left to drop this.worldObj.setBlockToAir(this.xCoord, this.yCoord, this.zCoord); - - try { - Method m = this.worldObj.getBlock(this.xCoord, this.yCoord, this.zCoord).getClass().getDeclaredMethod("dropBlockAsItem", new Class[]{World.class, Integer.class, Integer.class, Integer.class, ItemStack.class}); - m.setAccessible(true); - m.invoke(this.worldObj.getBlock(this.xCoord, this.yCoord, this.zCoord), new Object[]{this.worldObj, Integer.valueOf(this.xCoord), Integer.valueOf(this.yCoord), Integer.valueOf(this.zCoord), null}); - } catch (Throwable var2) { - ; - } - } else { - if(this.worldObj.getBlockMetadata(this.xCoord, this.yCoord, this.zCoord) < 4) { + int meta = this.worldObj.getBlockMetadata(this.xCoord, this.yCoord, this.zCoord); + if(countsGrowth(meta)) { ++this.tick; } - if(this.tick >= 4000 - (this.worldObj.getBiomeGenForCoords(this.xCoord, this.zCoord).biomeID == Config.biomeTaintID?450:0)) { + // the lowest possible threshold is 3550, so the biome is only looked up once it matters + if(this.tick >= growthThreshold(true) && this.tick >= growthThreshold(this.worldObj.getBiomeGenForCoords(this.xCoord, this.zCoord).biomeID == Config.biomeTaintID)) { Util.setBlockMetadata(this.worldObj, this.xCoord, this.yCoord, this.zCoord, this.worldObj.getBlockMetadata(this.xCoord, this.yCoord, this.zCoord) + 1); } diff --git a/src/main/java/com/pengu/thaumcraft/additions/tileentity/TileTankBasic.java b/src/main/java/com/pengu/thaumcraft/additions/tileentity/TileTankBasic.java index 28d3238..6855701 100644 --- a/src/main/java/com/pengu/thaumcraft/additions/tileentity/TileTankBasic.java +++ b/src/main/java/com/pengu/thaumcraft/additions/tileentity/TileTankBasic.java @@ -23,21 +23,17 @@ public class TileTankBasic extends TileThaumcraft implements ITubable { this.capacity = 1005; } + /** Block metadata for a tank by which neighbours are tanks: 0 none, 1 only below, 2 only above, 3 both. */ + public static int connectionMeta(boolean above, boolean below) { + return (above?2:0) | (below?1:0); + } + public void updateEntity() { - if(!(this.worldObj.getBlock(this.xCoord, this.yCoord + 1, this.zCoord) instanceof BlockTankBasic) && !(this.worldObj.getBlock(this.xCoord, this.yCoord - 1, this.zCoord) instanceof BlockTankBasic) && this.worldObj.getBlockMetadata(this.xCoord, this.yCoord, this.zCoord) != 0) { - Util.setBlockMetadata(this.worldObj, this.xCoord, this.yCoord, this.zCoord, 0); - } - - if(!(this.worldObj.getBlock(this.xCoord, this.yCoord + 1, this.zCoord) instanceof BlockTankBasic) && this.worldObj.getBlock(this.xCoord, this.yCoord - 1, this.zCoord) instanceof BlockTankBasic && this.worldObj.getBlockMetadata(this.xCoord, this.yCoord, this.zCoord) != 1) { - Util.setBlockMetadata(this.worldObj, this.xCoord, this.yCoord, this.zCoord, 1); - } - - if(this.worldObj.getBlock(this.xCoord, this.yCoord + 1, this.zCoord) instanceof BlockTankBasic && !(this.worldObj.getBlock(this.xCoord, this.yCoord - 1, this.zCoord) instanceof BlockTankBasic) && this.worldObj.getBlockMetadata(this.xCoord, this.yCoord, this.zCoord) != 2) { - Util.setBlockMetadata(this.worldObj, this.xCoord, this.yCoord, this.zCoord, 2); - } - - if(this.worldObj.getBlock(this.xCoord, this.yCoord + 1, this.zCoord) instanceof BlockTankBasic && this.worldObj.getBlock(this.xCoord, this.yCoord - 1, this.zCoord) instanceof BlockTankBasic && this.worldObj.getBlockMetadata(this.xCoord, this.yCoord, this.zCoord) != 3) { - Util.setBlockMetadata(this.worldObj, this.xCoord, this.yCoord, this.zCoord, 3); + boolean above = this.worldObj.getBlock(this.xCoord, this.yCoord + 1, this.zCoord) instanceof BlockTankBasic; + boolean below = this.worldObj.getBlock(this.xCoord, this.yCoord - 1, this.zCoord) instanceof BlockTankBasic; + int wantedMeta = TileTankBasic.connectionMeta(above, below); + if(this.worldObj.getBlockMetadata(this.xCoord, this.yCoord, this.zCoord) != wantedMeta) { + Util.setBlockMetadata(this.worldObj, this.xCoord, this.yCoord, this.zCoord, wantedMeta); } if(!this.worldObj.isRemote) { diff --git a/src/test/java/com/pengu/thaumcraft/additions/tileentity/TankAndCropLogicTest.java b/src/test/java/com/pengu/thaumcraft/additions/tileentity/TankAndCropLogicTest.java new file mode 100644 index 0000000..d692bc0 --- /dev/null +++ b/src/test/java/com/pengu/thaumcraft/additions/tileentity/TankAndCropLogicTest.java @@ -0,0 +1,32 @@ +package com.pengu.thaumcraft.additions.tileentity; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +/** TDLM/thaumicadditions#20 and #22: the lookups were hoisted without changing what the tiles decide. */ +public class TankAndCropLogicTest { + + @Test + public void tankMetadataMatchesTheOriginalFourBranches() { + assertEquals(0, TileTankBasic.connectionMeta(false, false)); + assertEquals(1, TileTankBasic.connectionMeta(false, true)); + assertEquals(2, TileTankBasic.connectionMeta(true, false)); + assertEquals(3, TileTankBasic.connectionMeta(true, true)); + } + + @Test + public void cropThresholdIsLowerInTheTaintBiome() { + assertEquals(4000, TileTaintedCrops.growthThreshold(false)); + assertEquals(3550, TileTaintedCrops.growthThreshold(true)); + } + + @Test + public void cropOnlyCountsGrowthBelowStageFour() { + assertTrue(TileTaintedCrops.countsGrowth(0)); + assertTrue(TileTaintedCrops.countsGrowth(3)); + assertFalse(TileTaintedCrops.countsGrowth(4)); + } +}