diff --git a/src/main/java/cr0s/warpdrive/CloakChunkWatcher.java b/src/main/java/cr0s/warpdrive/CloakChunkWatcher.java index 3aae3a0a..d807f5c9 100644 --- a/src/main/java/cr0s/warpdrive/CloakChunkWatcher.java +++ b/src/main/java/cr0s/warpdrive/CloakChunkWatcher.java @@ -1,6 +1,9 @@ package cr0s.warpdrive; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.world.ChunkCoordIntPair; +import net.minecraftforge.event.entity.EntityJoinWorldEvent; import net.minecraftforge.event.world.ChunkWatchEvent; public class CloakChunkWatcher { @@ -10,8 +13,8 @@ public class CloakChunkWatcher { ChunkCoordIntPair chunk = event.chunk; // Check chunk for locating in cloaked areas - WarpDrive.cloaks.onChunkLoaded(event.player, chunk.chunkXPos, chunk.chunkZPos); WarpDrive.logger.info("onChunkLoaded " + chunk.chunkXPos + " " + chunk.chunkZPos); + WarpDrive.cloaks.onChunkLoaded(event.player, chunk.chunkXPos, chunk.chunkZPos); /* List list = new ArrayList(); @@ -22,4 +25,14 @@ public class CloakChunkWatcher { ((EntityPlayerMP)p).playerNetServerHandler.sendPacketToPlayer(new Packet56MapChunks(list)); */ } + + @SubscribeEvent + public void onEntityJoinWorld(EntityJoinWorldEvent event){ + if (!event.world.isRemote) { + if (event.entity instanceof EntityPlayerMP) { + WarpDrive.logger.info("onEntityJoinWorld " + event.entity); + WarpDrive.cloaks.onEntityJoinWorld((EntityPlayerMP)event.entity); + } + } + } } diff --git a/src/main/java/cr0s/warpdrive/data/CloakManager.java b/src/main/java/cr0s/warpdrive/data/CloakManager.java index 2a48a3d1..937a6371 100644 --- a/src/main/java/cr0s/warpdrive/data/CloakManager.java +++ b/src/main/java/cr0s/warpdrive/data/CloakManager.java @@ -16,50 +16,70 @@ import cr0s.warpdrive.network.PacketHandler; */ public class CloakManager { - + private LinkedList cloaks; - + public CloakManager() { this.cloaks = new LinkedList(); } - + public boolean isCloaked(int dimensionID, int x, int y, int z) { for (CloakedArea area : this.cloaks) { if (area.dimensionId != dimensionID) { continue; } - + if (area.aabb.minX <= x && area.aabb.maxX >= x && area.aabb.minY <= y && area.aabb.maxY >= y && area.aabb.minZ <= z && area.aabb.maxZ >= z) { return true; } } - + return false; } - + public boolean onChunkLoaded(EntityPlayerMP player, int chunkPosX, int chunkPosZ) { for (CloakedArea area : this.cloaks) { + // skip other dimensions if (area.dimensionId != player.worldObj.provider.dimensionId) { continue; } - - if (area.aabb.minX <= (chunkPosX << 4 + 15) && area.aabb.maxX >= (chunkPosX << 4) && area.aabb.minZ <= (chunkPosZ << 4 + 15) - && area.aabb.maxZ >= (chunkPosZ << 4)) { + + // force refresh if the chunk overlap the cloak + if ( area.aabb.minX <= (chunkPosX << 4 + 15) && area.aabb.maxX >= (chunkPosX << 4) + && area.aabb.minZ <= (chunkPosZ << 4 + 15) && area.aabb.maxZ >= (chunkPosZ << 4) ) { PacketHandler.sendCloakPacket(player, area.aabb, area.tier, false); } } - + return false; } - + + public boolean onEntityJoinWorld(EntityPlayerMP player) { + for (CloakedArea area : this.cloaks) { + // skip other dimensions + if (area.dimensionId != player.worldObj.provider.dimensionId) { + continue; + } + + // force refresh if player is outside the cloak + if ( area.aabb.minX > player.posX || area.aabb.maxX < player.posX + || area.aabb.minY > player.posY || area.aabb.maxY < player.posY + || area.aabb.minZ > player.posZ || area.aabb.maxZ < player.posZ ) { + PacketHandler.sendCloakPacket(player, area.aabb, area.tier, false); + } + } + + return false; + } + public boolean isAreaExists(World worldObj, int x, int y, int z) { return (getCloakedArea(worldObj, x, y, z) != null); } - + public void addCloakedAreaWorld(World worldObj, int minX, int minY, int minZ, int maxX, int maxY, int maxZ, int x, int y, int z, byte tier) { cloaks.add(new CloakedArea(worldObj, x, y, z, AxisAlignedBB.getBoundingBox(minX, minY, minZ, maxX, maxY, maxZ), tier)); } - + public void removeCloakedArea(World worldObj, int x, int y, int z) { int index = 0; for (int i = 0; i < cloaks.size(); i++) { @@ -72,19 +92,19 @@ public class CloakManager { break; } } - + cloaks.remove(index); } - + public CloakedArea getCloakedArea(World worldObj, int x, int y, int z) { for (CloakedArea area : this.cloaks) { if (area.coreX == x && area.coreY == y && area.coreZ == z && area.dimensionId == worldObj.provider.dimensionId) return area; } - + return null; } - + public void updatePlayer(EntityPlayer player) { for (CloakedArea area : this.cloaks) { area.updatePlayer(player);