    001    package net.minecraft.src;
    002
    003    import java.util.ArrayList;
    004    import java.util.HashSet;
    005    import java.util.Iterator;
    006    import java.util.Set;
    007
    008    import cpw.mods.fml.common.registry.EntityRegistry;
    009
    010    public class EntityTracker
    011    {
    012        private final WorldServer theWorld;
    013        private Set trackedEntities = new HashSet();
    014        private IntHashMap trackedEntityIDs = new IntHashMap();
    015        private int entityViewDistance;
    016
    017        public EntityTracker(WorldServer par1WorldServer)
    018        {
    019            this.theWorld = par1WorldServer;
    020            this.entityViewDistance = par1WorldServer.getMinecraftServer().getConfigurationManager().getEntityViewDistance();
    021        }
    022
    023        /**
    024         * if entity is a player sends all tracked events to the player, otherwise, adds with a visibility and update arate
    025         * based on the class type
    026         */
    027        public void addEntityToTracker(Entity par1Entity)
    028        {
    029            if (EntityRegistry.instance().tryTrackingEntity(this, par1Entity))
    030            {
    031                return;
    032            }
    033            if (par1Entity instanceof EntityPlayerMP)
    034            {
    035                this.addEntityToTracker(par1Entity, 512, 2);
    036                EntityPlayerMP var2 = (EntityPlayerMP)par1Entity;
    037                Iterator var3 = this.trackedEntities.iterator();
    038
    039                while (var3.hasNext())
    040                {
    041                    EntityTrackerEntry var4 = (EntityTrackerEntry)var3.next();
    042
    043                    if (var4.myEntity != var2)
    044                    {
    045                        var4.tryStartWachingThis(var2);
    046                    }
    047                }
    048            }
    049            else if (par1Entity instanceof EntityFishHook)
    050            {
    051                this.addEntityToTracker(par1Entity, 64, 5, true);
    052            }
    053            else if (par1Entity instanceof EntityArrow)
    054            {
    055                this.addEntityToTracker(par1Entity, 64, 20, false);
    056            }
    057            else if (par1Entity instanceof EntitySmallFireball)
    058            {
    059                this.addEntityToTracker(par1Entity, 64, 10, false);
    060            }
    061            else if (par1Entity instanceof EntityFireball)
    062            {
    063                this.addEntityToTracker(par1Entity, 64, 10, false);
    064            }
    065            else if (par1Entity instanceof EntitySnowball)
    066            {
    067                this.addEntityToTracker(par1Entity, 64, 10, true);
    068            }
    069            else if (par1Entity instanceof EntityEnderPearl)
    070            {
    071                this.addEntityToTracker(par1Entity, 64, 10, true);
    072            }
    073            else if (par1Entity instanceof EntityEnderEye)
    074            {
    075                this.addEntityToTracker(par1Entity, 64, 4, true);
    076            }
    077            else if (par1Entity instanceof EntityEgg)
    078            {
    079                this.addEntityToTracker(par1Entity, 64, 10, true);
    080            }
    081            else if (par1Entity instanceof EntityPotion)
    082            {
    083                this.addEntityToTracker(par1Entity, 64, 10, true);
    084            }
    085            else if (par1Entity instanceof EntityExpBottle)
    086            {
    087                this.addEntityToTracker(par1Entity, 64, 10, true);
    088            }
    089            else if (par1Entity instanceof EntityItem)
    090            {
    091                this.addEntityToTracker(par1Entity, 64, 20, true);
    092            }
    093            else if (par1Entity instanceof EntityMinecart)
    094            {
    095                this.addEntityToTracker(par1Entity, 80, 3, true);
    096            }
    097            else if (par1Entity instanceof EntityBoat)
    098            {
    099                this.addEntityToTracker(par1Entity, 80, 3, true);
    100            }
    101            else if (par1Entity instanceof EntitySquid)
    102            {
    103                this.addEntityToTracker(par1Entity, 64, 3, true);
    104            }
    105            else if (par1Entity instanceof EntityWither)
    106            {
    107                this.addEntityToTracker(par1Entity, 80, 3, false);
    108            }
    109            else if (par1Entity instanceof EntityBat)
    110            {
    111                this.addEntityToTracker(par1Entity, 80, 3, false);
    112            }
    113            else if (par1Entity instanceof IAnimals)
    114            {
    115                this.addEntityToTracker(par1Entity, 80, 3, true);
    116            }
    117            else if (par1Entity instanceof EntityDragon)
    118            {
    119                this.addEntityToTracker(par1Entity, 160, 3, true);
    120            }
    121            else if (par1Entity instanceof EntityTNTPrimed)
    122            {
    123                this.addEntityToTracker(par1Entity, 160, 10, true);
    124            }
    125            else if (par1Entity instanceof EntityFallingSand)
    126            {
    127                this.addEntityToTracker(par1Entity, 160, 20, true);
    128            }
    129            else if (par1Entity instanceof EntityPainting)
    130            {
    131                this.addEntityToTracker(par1Entity, 160, Integer.MAX_VALUE, false);
    132            }
    133            else if (par1Entity instanceof EntityXPOrb)
    134            {
    135                this.addEntityToTracker(par1Entity, 160, 20, true);
    136            }
    137            else if (par1Entity instanceof EntityEnderCrystal)
    138            {
    139                this.addEntityToTracker(par1Entity, 256, Integer.MAX_VALUE, false);
    140            }
    141            else if (par1Entity instanceof EntityItemFrame)
    142            {
    143                this.addEntityToTracker(par1Entity, 160, Integer.MAX_VALUE, false);
    144            }
    145        }
    146
    147        public void addEntityToTracker(Entity par1Entity, int par2, int par3)
    148        {
    149            this.addEntityToTracker(par1Entity, par2, par3, false);
    150        }
    151
    152        public void addEntityToTracker(Entity par1Entity, int par2, int par3, boolean par4)
    153        {
    154            if (par2 > this.entityViewDistance)
    155            {
    156                par2 = this.entityViewDistance;
    157            }
    158
    159            if (this.trackedEntityIDs.containsItem(par1Entity.entityId))
    160            {
    161                throw new IllegalStateException("Entity is already tracked!");
    162            }
    163            else
    164            {
    165                EntityTrackerEntry var5 = new EntityTrackerEntry(par1Entity, par2, par3, par4);
    166                this.trackedEntities.add(var5);
    167                this.trackedEntityIDs.addKey(par1Entity.entityId, var5);
    168                var5.sendEventsToPlayers(this.theWorld.playerEntities);
    169            }
    170        }
    171
    172        public void removeEntityFromAllTrackingPlayers(Entity par1Entity)
    173        {
    174            if (par1Entity instanceof EntityPlayerMP)
    175            {
    176                EntityPlayerMP var2 = (EntityPlayerMP)par1Entity;
    177                Iterator var3 = this.trackedEntities.iterator();
    178
    179                while (var3.hasNext())
    180                {
    181                    EntityTrackerEntry var4 = (EntityTrackerEntry)var3.next();
    182                    var4.removeFromWatchingList(var2);
    183                }
    184            }
    185
    186            EntityTrackerEntry var5 = (EntityTrackerEntry)this.trackedEntityIDs.removeObject(par1Entity.entityId);
    187
    188            if (var5 != null)
    189            {
    190                this.trackedEntities.remove(var5);
    191                var5.informAllAssociatedPlayersOfItemDestruction();
    192            }
    193        }
    194
    195        public void updateTrackedEntities()
    196        {
    197            ArrayList var1 = new ArrayList();
    198            Iterator var2 = this.trackedEntities.iterator();
    199
    200            while (var2.hasNext())
    201            {
    202                EntityTrackerEntry var3 = (EntityTrackerEntry)var2.next();
    203                var3.sendLocationToAllClients(this.theWorld.playerEntities);
    204
    205                if (var3.playerEntitiesUpdated && var3.myEntity instanceof EntityPlayerMP)
    206                {
    207                    var1.add((EntityPlayerMP)var3.myEntity);
    208                }
    209            }
    210
    211            for (int var6 = 0; var6 < var1.size(); ++var6)
    212            {
    213                EntityPlayerMP var7 = (EntityPlayerMP)var1.get(var6);
    214                Iterator var4 = this.trackedEntities.iterator();
    215
    216                while (var4.hasNext())
    217                {
    218                    EntityTrackerEntry var5 = (EntityTrackerEntry)var4.next();
    219
    220                    if (var5.myEntity != var7)
    221                    {
    222                        var5.tryStartWachingThis(var7);
    223                    }
    224                }
    225            }
    226        }
    227
    228        /**
    229         * does not send the packet to the entity if the entity is a player
    230         */
    231        public void sendPacketToAllPlayersTrackingEntity(Entity par1Entity, Packet par2Packet)
    232        {
    233            EntityTrackerEntry var3 = (EntityTrackerEntry)this.trackedEntityIDs.lookup(par1Entity.entityId);
    234
    235            if (var3 != null)
    236            {
    237                var3.sendPacketToAllTrackingPlayers(par2Packet);
    238            }
    239        }
    240
    241        /**
    242         * sends to the entity if the entity is a player
    243         */
    244        public void sendPacketToAllAssociatedPlayers(Entity par1Entity, Packet par2Packet)
    245        {
    246            EntityTrackerEntry var3 = (EntityTrackerEntry)this.trackedEntityIDs.lookup(par1Entity.entityId);
    247
    248            if (var3 != null)
    249            {
    250                var3.sendPacketToAllAssociatedPlayers(par2Packet);
    251            }
    252        }
    253
    254        public void removeAllTrackingPlayers(EntityPlayerMP par1EntityPlayerMP)
    255        {
    256            Iterator var2 = this.trackedEntities.iterator();
    257
    258            while (var2.hasNext())
    259            {
    260                EntityTrackerEntry var3 = (EntityTrackerEntry)var2.next();
    261                var3.removePlayerFromTracker(par1EntityPlayerMP);
    262            }
    263        }
    264
    265        public void func_85172_a(EntityPlayerMP par1EntityPlayerMP, Chunk par2Chunk)
    266        {
    267            Iterator var3 = this.trackedEntities.iterator();
    268
    269            while (var3.hasNext())
    270            {
    271                EntityTrackerEntry var4 = (EntityTrackerEntry)var3.next();
    272
    273                if (var4.myEntity != par1EntityPlayerMP && var4.myEntity.chunkCoordX == par2Chunk.xPosition && var4.myEntity.chunkCoordZ == par2Chunk.zPosition)
    274                {
    275                    var4.tryStartWachingThis(par1EntityPlayerMP);
    276                }
    277            }
    278        }
    279    }