diff options
author | Jason Mitchell <mitchej@gmail.com> | 2024-06-29 10:48:10 -0700 |
---|---|---|
committer | Jason Mitchell <mitchej+github@gmail.com> | 2024-07-09 21:38:05 -0700 |
commit | 59766c69f7622309075efd577c9543a8ac24c3f9 (patch) | |
tree | 1181f958421381e1a1464d1d004a9b7c849a7df3 /src/main/java/gregtech/api/multitileentity/WeakTargetRef.java | |
parent | 3724fbbcb67ee2566419654e31eb88eb5b7f88f6 (diff) | |
download | GT5-Unofficial-59766c69f7622309075efd577c9543a8ac24c3f9.tar.gz GT5-Unofficial-59766c69f7622309075efd577c9543a8ac24c3f9.tar.bz2 GT5-Unofficial-59766c69f7622309075efd577c9543a8ac24c3f9.zip |
MultiTileEntityBlock work
* Merge MultiTileEntityBlockRegistryInternal into MultiTileEntityBlock
* Add a hard dep on NEID for meta extension
* Use in world block meta for MuTE ID
* Use one block per MuTE Registry
* Add WeakTargetRef
* Migrate `controller` reference for parts over to a non non cachable WeakTargetRef
* Migrate controller WeakReference usage to WeakTargetRef
Diffstat (limited to 'src/main/java/gregtech/api/multitileentity/WeakTargetRef.java')
-rw-r--r-- | src/main/java/gregtech/api/multitileentity/WeakTargetRef.java | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/main/java/gregtech/api/multitileentity/WeakTargetRef.java b/src/main/java/gregtech/api/multitileentity/WeakTargetRef.java new file mode 100644 index 0000000000..1509384f5e --- /dev/null +++ b/src/main/java/gregtech/api/multitileentity/WeakTargetRef.java @@ -0,0 +1,88 @@ +package gregtech.api.multitileentity; + +import java.lang.ref.WeakReference; + +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.ChunkCoordinates; +import net.minecraft.world.World; + +import gregtech.api.multitileentity.interfaces.IMultiTileEntity; + +public class WeakTargetRef<T extends IMultiTileEntity> { + + protected final ChunkCoordinates position = new ChunkCoordinates(0, -1, 0); + protected final Class<?> targetClass; + protected boolean shouldCache; + protected WeakReference<T> target = new WeakReference<>(null); + protected World world = null; + + public WeakTargetRef(Class<?> targetClass, boolean shouldCache) { + this.targetClass = targetClass; + this.shouldCache = shouldCache; + } + + public WeakTargetRef(T target, boolean shouldCache) { + this(target.getClass(), shouldCache); + setTarget(target); + } + + public void setTarget(T newTarget) { + if (!targetClass.isInstance(newTarget)) { + throw new IllegalArgumentException("Target is not of the correct type"); + } + position.set(newTarget.getXCoord(), newTarget.getYCoord(), newTarget.getZCoord()); + world = newTarget.getWorld(); + } + + public void setWorld(World world) { + this.world = world; + } + + public void setPosition(ChunkCoordinates position) { + this.position.set(position.posX, position.posY, position.posZ); + } + + public void setPosition(int x, int y, int z) { + this.position.set(x, y, z); + } + + public void setShouldCache(boolean shouldCache) { + this.shouldCache = shouldCache; + } + + public T get() { + if (!shouldCache) { + return resolveTarget(); + } + T result = target.get(); + if (result == null || result.isDead()) { + result = resolveTarget(); + if (result != null) { + target = new WeakReference<>(result); + } else { + target.clear(); + } + } + return result; + } + + @SuppressWarnings("unchecked") + protected T resolveTarget() { + if (world != null && position.posX >= 0 && world.blockExists(position.posX, position.posY, position.posZ)) { + final TileEntity te = world.getTileEntity(position.posX, position.posY, position.posZ); + return this.targetClass.isInstance(te) ? (T) te : null; + } + return null; + } + + public ChunkCoordinates getPosition() { + return position; + } + + public void invalidate() { + target.clear(); + world = null; + position.set(0, -1, 0); + } + +} |