From 726fef01f62428212f2e13b16df57d1f76b9aef2 Mon Sep 17 00:00:00 2001 From: Layue13 Date: Sat, 7 Nov 2026 14:17:27 +0800 Subject: [PATCH] perf: creative node registers its location once, server skips the collision stack walk Closes TDLM/thaumicadditions#17 Co-Authored-By: Claude Sonnet 5.5 --- .../additions/blocks/BlockCreativeNode.java | 5 ++ .../tileentity/TileCreativeNode.java | 22 ++++- .../tileentity/CreativeNodeRegistryTest.java | 80 +++++++++++++++++++ 3 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/pengu/thaumcraft/additions/tileentity/CreativeNodeRegistryTest.java diff --git a/src/main/java/com/pengu/thaumcraft/additions/blocks/BlockCreativeNode.java b/src/main/java/com/pengu/thaumcraft/additions/blocks/BlockCreativeNode.java index 54a05df..c6fb593 100644 --- a/src/main/java/com/pengu/thaumcraft/additions/blocks/BlockCreativeNode.java +++ b/src/main/java/com/pengu/thaumcraft/additions/blocks/BlockCreativeNode.java @@ -40,6 +40,11 @@ public class BlockCreativeNode extends BlockContainer { } public AxisAlignedBB getCollisionBoundingBoxFromPool(World w, int x, int y, int z) { + if(!w.isRemote) { + // rendering never happens on the server, so the stack walk below can only find nothing + return AxisAlignedBB.getBoundingBox(0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D); + } + StackTraceElement[] var5 = Thread.currentThread().getStackTrace(); int var6 = var5.length; diff --git a/src/main/java/com/pengu/thaumcraft/additions/tileentity/TileCreativeNode.java b/src/main/java/com/pengu/thaumcraft/additions/tileentity/TileCreativeNode.java index dce87e1..e07e56d 100644 --- a/src/main/java/com/pengu/thaumcraft/additions/tileentity/TileCreativeNode.java +++ b/src/main/java/com/pengu/thaumcraft/additions/tileentity/TileCreativeNode.java @@ -35,6 +35,7 @@ public class TileCreativeNode extends TileVisNode implements INode, IWandable { public Color targetColor = new Color(16777215); public Color color = new Color(16777215); public boolean hasUpdatedOnce; + private boolean locationRegistered; public void updateEntity() { @@ -44,12 +45,30 @@ public class TileCreativeNode extends TileVisNode implements INode, IWandable { if(!this.hasUpdatedOnce) { this.hasUpdatedOnce = !this.hasUpdatedOnce; - } else { + } else if(!this.locationRegistered) { this.generateId(); } } + public void invalidate() { + this.unregisterLocation(); + super.invalidate(); + } + + public void onChunkUnload() { + this.unregisterLocation(); + super.onChunkUnload(); + } + + private void unregisterLocation() { + if(this.id != null && TileNode.locations != null) { + TileNode.locations.remove(this.id); + } + + this.locationRegistered = false; + } + public AspectList getAspects() { return (new AspectList()).add(Aspect.AIR, 134217).add(Aspect.WATER, 134217).add(Aspect.FIRE, 134217).add(Aspect.ORDER, 134217).add(Aspect.ENTROPY, 134217).add(Aspect.EARTH, 134217); } @@ -232,6 +251,7 @@ public class TileCreativeNode extends TileVisNode implements INode, IWandable { t.add(Integer.valueOf(this.yCoord)); t.add(Integer.valueOf(this.zCoord)); TileNode.locations.put(this.id, t); + this.locationRegistered = true; } return this.id; diff --git a/src/test/java/com/pengu/thaumcraft/additions/tileentity/CreativeNodeRegistryTest.java b/src/test/java/com/pengu/thaumcraft/additions/tileentity/CreativeNodeRegistryTest.java new file mode 100644 index 0000000..e9c080b --- /dev/null +++ b/src/test/java/com/pengu/thaumcraft/additions/tileentity/CreativeNodeRegistryTest.java @@ -0,0 +1,80 @@ +package com.pengu.thaumcraft.additions.tileentity; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.HashMap; +import net.minecraft.world.World; +import net.minecraft.world.WorldProviderSurface; +import net.minecraft.world.WorldServer; +import org.junit.Before; +import org.junit.Test; +import thaumcraft.common.tiles.TileNode; + +/** TDLM/thaumicadditions#17: a creative node registers its location once, not every tick, and unregisters on removal. */ +public class CreativeNodeRegistryTest { + + private TileCreativeNode node; + + @SuppressWarnings("unchecked") + private static T allocate(Class type) throws Exception { + Constructor ctor = sun.reflect.ReflectionFactory.getReflectionFactory() + .newConstructorForSerialization(type, Object.class.getDeclaredConstructor()); + return (T) ctor.newInstance(); + } + + @Before + public void setUp() throws Exception { + if (TileNode.locations == null) { + Field f = TileNode.class.getDeclaredField("locations"); + f.setAccessible(true); + f.set(null, new HashMap()); + } + TileNode.locations.clear(); + World world = allocate(FixtureWorld.class); + Field provider = World.class.getDeclaredField("provider"); + provider.setAccessible(true); + provider.set(world, new WorldProviderSurface()); + this.node = allocate(TileCreativeNode.class); + this.node.setWorldObj(world); + this.node.xCoord = 3; + this.node.yCoord = 70; + this.node.zCoord = 5; + } + + @Test + public void locationIsPutOnceAcrossManyTicks() { + this.node.updateEntity(); // first tick only marks the node as updated + this.node.updateEntity(); + ArrayList first = TileNode.locations.get("0:3:70:5"); + assertNotNull(first); + for (int i = 0; i < 1000; i++) { + this.node.updateEntity(); + } + assertSame("no re-registration on later ticks", first, TileNode.locations.get("0:3:70:5")); + assertEquals(1, TileNode.locations.size()); + } + + @Test + public void unloadingRemovesTheLocationSoItCanBeRegisteredAgain() { + this.node.updateEntity(); + this.node.updateEntity(); + assertNotNull(TileNode.locations.get("0:3:70:5")); + this.node.onChunkUnload(); + assertNull(TileNode.locations.get("0:3:70:5")); + this.node.updateEntity(); + assertNotNull(TileNode.locations.get("0:3:70:5")); + } + + public static class FixtureWorld extends WorldServer { + + FixtureWorld() { + super(null, null, "node", 0, null, null); + } + } +} -- 2.47.3