From 5b2bd495da17ee34978246a2aa5ac5fceb7be3ae Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 22 May 2018 09:04:25 -0400 Subject: [PATCH] Fixed entities not fully decloaking --- .../java/cr0s/warpdrive/data/CloakedArea.java | 57 ++++--------- .../cr0s/warpdrive/network/PacketHandler.java | 83 +++++++++++++++++++ 2 files changed, 101 insertions(+), 39 deletions(-) diff --git a/src/main/java/cr0s/warpdrive/data/CloakedArea.java b/src/main/java/cr0s/warpdrive/data/CloakedArea.java index e8c133ba..fb5f0a75 100644 --- a/src/main/java/cr0s/warpdrive/data/CloakedArea.java +++ b/src/main/java/cr0s/warpdrive/data/CloakedArea.java @@ -5,8 +5,9 @@ import cr0s.warpdrive.config.WarpDriveConfig; import cr0s.warpdrive.network.PacketHandler; import cr0s.warpdrive.render.EntityFXBeam; -import java.util.LinkedList; import java.util.List; +import java.util.UUID; +import java.util.concurrent.CopyOnWriteArraySet; import net.minecraft.block.Block; import net.minecraft.client.Minecraft; @@ -17,7 +18,6 @@ import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.init.Blocks; -import net.minecraft.network.Packet; import net.minecraft.server.MinecraftServer; import net.minecraft.util.AxisAlignedBB; import net.minecraft.world.World; @@ -32,7 +32,7 @@ public class CloakedArea { public int coreX, coreY, coreZ; public int minX, minY, minZ; public int maxX, maxY, maxZ; - private LinkedList playersInArea; + private CopyOnWriteArraySet playersInArea; public byte tier; public Block fogBlock; public int fogMetadata; @@ -54,14 +54,14 @@ public class CloakedArea { this.maxY = maxY; this.maxZ = maxZ; - this.playersInArea = new LinkedList<>(); + this.playersInArea = new CopyOnWriteArraySet<>(); if (worldObj != null) { try { // Add all players currently inside the field final List list = worldObj.getEntitiesWithinAABB(EntityPlayerMP.class, AxisAlignedBB.getBoundingBox(minX, minY, minZ, maxX, maxY, maxZ)); for (final EntityPlayer player : list) { - addPlayer(player.getCommandSenderName()); + addPlayer(player.getUniqueID()); } } catch (final Exception exception) { exception.printStackTrace(); @@ -77,29 +77,16 @@ public class CloakedArea { } } - public boolean isPlayerListedInArea(final String username) { - for (final String playerInArea : playersInArea) { - if (playerInArea.equals(username)) { - return true; - } - } - - return false; + public boolean isPlayerListedInArea(final UUID uuidPlayer) { + return playersInArea.contains(uuidPlayer); } - private void removePlayer(final String username) { - for (int i = 0; i < playersInArea.size(); i++) { - if (playersInArea.get(i).equals(username)) { - playersInArea.remove(i); - return; - } - } + private void removePlayer(final UUID uuidPlayer) { + playersInArea.remove(uuidPlayer); } - private void addPlayer(final String username) { - if (!isPlayerListedInArea(username)) { - playersInArea.add(username); - } + private void addPlayer(final UUID uuidPlayer) { + playersInArea.add(uuidPlayer); } public boolean isEntityWithinArea(final EntityLivingBase entity) { @@ -148,21 +135,21 @@ public class CloakedArea { public void updatePlayer(final EntityPlayerMP EntityPlayerMP) { if (isEntityWithinArea(EntityPlayerMP)) { - if (!isPlayerListedInArea(EntityPlayerMP.getCommandSenderName())) { + if (!isPlayerListedInArea(EntityPlayerMP.getUniqueID())) { if (WarpDriveConfig.LOGGING_CLOAKING) { WarpDrive.logger.info(this + " Player " + EntityPlayerMP.getCommandSenderName() + " has entered"); } - addPlayer(EntityPlayerMP.getCommandSenderName()); + addPlayer(EntityPlayerMP.getUniqueID()); revealChunksToPlayer(EntityPlayerMP); revealEntitiesToPlayer(EntityPlayerMP); PacketHandler.sendCloakPacket(EntityPlayerMP, this, false); } } else { - if (isPlayerListedInArea(EntityPlayerMP.getCommandSenderName())) { + if (isPlayerListedInArea(EntityPlayerMP.getUniqueID())) { if (WarpDriveConfig.LOGGING_CLOAKING) { WarpDrive.logger.info(this + " Player " + EntityPlayerMP.getCommandSenderName() + " has left"); } - removePlayer(EntityPlayerMP.getCommandSenderName()); + removePlayer(EntityPlayerMP.getUniqueID()); MinecraftServer .getServer() .getConfigurationManager() @@ -210,19 +197,11 @@ public class CloakedArea { /**/ } - public void revealEntitiesToPlayer(final EntityPlayer player) { - final List list = player.worldObj.getEntitiesWithinAABBExcludingEntity(player, AxisAlignedBB.getBoundingBox(minX, minY, minZ, maxX, maxY, maxZ)); + public void revealEntitiesToPlayer(final EntityPlayerMP entityPlayerMP) { + final List list = entityPlayerMP.worldObj.getEntitiesWithinAABBExcludingEntity(entityPlayerMP, AxisAlignedBB.getBoundingBox(minX, minY, minZ, maxX, maxY, maxZ)); for (final Entity entity : list) { - final Packet packet = PacketHandler.getPacketForThisEntity(entity); - if (packet != null) { - if (WarpDriveConfig.LOGGING_CLOAKING) { - WarpDrive.logger.warn("Revealing entity " + entity + " with packet " + packet); - } - ((EntityPlayerMP) player).playerNetServerHandler.sendPacket(packet); - } else if (WarpDriveConfig.LOGGING_CLOAKING) { - WarpDrive.logger.warn("Revealing entity " + entity + " fails: null packet"); - } + PacketHandler.revealEntityToPlayer(entity, entityPlayerMP); } } diff --git a/src/main/java/cr0s/warpdrive/network/PacketHandler.java b/src/main/java/cr0s/warpdrive/network/PacketHandler.java index 470917e3..5f4acff6 100644 --- a/src/main/java/cr0s/warpdrive/network/PacketHandler.java +++ b/src/main/java/cr0s/warpdrive/network/PacketHandler.java @@ -13,11 +13,24 @@ import java.util.Collection; import java.util.List; import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityLiving; +import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.EntityTrackerEntry; +import net.minecraft.entity.ai.attributes.ServersideAttributeMap; +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.item.ItemStack; import net.minecraft.network.Packet; +import net.minecraft.network.play.server.S04PacketEntityEquipment; +import net.minecraft.network.play.server.S0APacketUseBed; +import net.minecraft.network.play.server.S1BPacketEntityAttach; +import net.minecraft.network.play.server.S1CPacketEntityMetadata; +import net.minecraft.network.play.server.S1DPacketEntityEffect; +import net.minecraft.network.play.server.S20PacketEntityProperties; +import net.minecraft.potion.PotionEffect; import net.minecraft.server.MinecraftServer; import net.minecraft.util.AxisAlignedBB; +import net.minecraft.util.MathHelper; import net.minecraft.world.World; import cpw.mods.fml.common.network.NetworkRegistry; @@ -188,6 +201,76 @@ public class PacketHandler { } catch (final Exception exception) { exception.printStackTrace(); } + WarpDrive.logger.error(String.format("Unable to get packet for entity %s", + entity)); return null; } + + public static void revealEntityToPlayer(final Entity entity, final EntityPlayerMP entityPlayerMP) { + try { + final Packet packet = getPacketForThisEntity(entity); + if (packet == null) { + WarpDrive.logger.error(String.format("Unable to reveal entity %s to player %s: null packet", + entity, entityPlayerMP)); + return; + } + if (WarpDriveConfig.LOGGING_CLOAKING) { + WarpDrive.logger.info("Revealing entity " + entity + " with packet " + packet); + } + entityPlayerMP.playerNetServerHandler.sendPacket(packet); + + if (!entity.getDataWatcher().getIsBlank()) { + entityPlayerMP.playerNetServerHandler.sendPacket(new S1CPacketEntityMetadata(entity.getEntityId(), entity.getDataWatcher(), true)); + } + + if (entity instanceof EntityLivingBase) { + final ServersideAttributeMap serversideattributemap = (ServersideAttributeMap)((EntityLivingBase) entity).getAttributeMap(); + final Collection collection = serversideattributemap.getWatchedAttributes(); + + if (!collection.isEmpty()) { + entityPlayerMP.playerNetServerHandler.sendPacket(new S20PacketEntityProperties(entity.getEntityId(), collection)); + } + } + + if (entity.ridingEntity != null) { + entityPlayerMP.playerNetServerHandler.sendPacket(new S1BPacketEntityAttach(0, entity, entity.ridingEntity)); + } + + if (entity instanceof EntityLiving && ((EntityLiving) entity).getLeashedToEntity() != null) { + entityPlayerMP.playerNetServerHandler.sendPacket(new S1BPacketEntityAttach(1, entity, ((EntityLiving) entity).getLeashedToEntity())); + } + + if (entity instanceof EntityLivingBase) { + for (int i = 0; i < 5; ++i) { + final ItemStack itemstack = ((EntityLivingBase) entity).getEquipmentInSlot(i); + + if (itemstack != null) { + entityPlayerMP.playerNetServerHandler.sendPacket(new S04PacketEntityEquipment(entity.getEntityId(), i, itemstack)); + } + } + } + + if (entity instanceof EntityPlayer) { + final EntityPlayer entityplayer = (EntityPlayer) entity; + + if (entityplayer.isPlayerSleeping()) { + entityPlayerMP.playerNetServerHandler.sendPacket(new S0APacketUseBed(entityplayer, MathHelper.floor_double(entity.posX), MathHelper.floor_double(entity.posY), MathHelper.floor_double(entity.posZ))); + } + } + + if (entity instanceof EntityLivingBase) { + final EntityLivingBase entitylivingbase = (EntityLivingBase) entity; + + for (final Object object : entitylivingbase.getActivePotionEffects()) { + if (!(object instanceof PotionEffect)) { + continue; + } + final PotionEffect potioneffect = (PotionEffect) object; + entityPlayerMP.playerNetServerHandler.sendPacket(new S1DPacketEntityEffect(entity.getEntityId(), potioneffect)); + } + } + } catch (final Exception exception) { + exception.printStackTrace(); + } + } } \ No newline at end of file