Added IChunkLoadTile

This commit is contained in:
Chicken-Bomes committed 2014-12-17 23:39:43 +10:00
1 parent feb8bfd028
commit 29e4eda53b
2 files changed
+36

No files matched your search

@@ -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();
}
@@ -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<TileEntity>)event.getChunk().chunkTileEntityMap.values()))
if(t instanceof IChunkLoadTile)
((IChunkLoadTile)t).onChunkLoad();
}
}