diff --git a/src/codechicken/lib/world/IChunkLoadTile.java b/src/codechicken/lib/world/IChunkLoadTile.java new file mode 100644 index 0000000..5739102 --- /dev/null +++ b/src/codechicken/lib/world/IChunkLoadTile.java @@ -0,0 +1,12 @@ +package codechicken.lib.world; + +/** + * Provides a callback for tile entities when a chunk is loaded, as an alternative to validate when the chunk hasn't been added to the world. + * To hook all world join/seperate events. Use this, TileEntity.validate with a worldObj.blockExists check, TileEntity.onChunkUnload and TileEntity.invalidate + * Be sure to call TileChunkLoadHook.init() from your mod during initialisation + * You could easily implement this in your own mod, but providing it here reduces the number of times the chunkTileEntityMap needs to be iterated + */ +public interface IChunkLoadTile +{ + public void onChunkLoad(); +} diff --git a/src/codechicken/lib/world/TileChunkLoadHook.java b/src/codechicken/lib/world/TileChunkLoadHook.java new file mode 100644 index 0000000..718d45d --- /dev/null +++ b/src/codechicken/lib/world/TileChunkLoadHook.java @@ -0,0 +1,24 @@ +package codechicken.lib.world; + +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import net.minecraft.tileentity.TileEntity; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.world.ChunkEvent; + +public class TileChunkLoadHook +{ + private static boolean init; + public static void init() { + if(init) return; + init = true; + + MinecraftForge.EVENT_BUS.register(new TileChunkLoadHook()); + } + + @SubscribeEvent + public void onChunkLoad(ChunkEvent.Load event) { + for(TileEntity t : ((Iterable)event.getChunk().chunkTileEntityMap.values())) + if(t instanceof IChunkLoadTile) + ((IChunkLoadTile)t).onChunkLoad(); + } +}