    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 IAnimals)
    106            {
    107                this.addEntityToTracker(par1Entity, 80, 3, true);
    108            }
    109            else if (par1Entity instanceof EntityDragon)
    110            {
    111                this.addEntityToTracker(par1Entity, 160, 3, true);
    112            }
    113            else if (par1Entity instanceof EntityTNTPrimed)
    114            {
    115                this.addEntityToTracker(par1Entity, 160, 10, true);
    116            }
    117            else if (par1Entity instanceof EntityFallingSand)
    118            {
    119                this.addEntityToTracker(par1Entity, 160, 20, true);
    120            }
    121            else if (par1Entity instanceof EntityPainting)
    122            {
    123                this.addEntityToTracker(par1Entity, 160, Integer.MAX_VALUE, false);
    124            }
    125            else if (par1Entity instanceof EntityXPOrb)
    126            {
    127                this.addEntityToTracker(par1Entity, 160, 20, true);
    128            }
    129            else if (par1Entity instanceof EntityEnderCrystal)
    130            {
    131                this.addEntityToTracker(par1Entity, 256, Integer.MAX_VALUE, false);
    132            }
    133        }
    134
    135        public void addEntityToTracker(Entity par1Entity, int par2, int par3)
    136        {
    137            this.addEntityToTracker(par1Entity, par2, par3, false);
    138        }
    139
    140        public void addEntityToTracker(Entity par1Entity, int par2, int par3, boolean par4)
    141        {
    142            if (par2 > this.entityViewDistance)
    143            {
    144                par2 = this.entityViewDistance;
    145            }
    146
    147            if (this.trackedEntityIDs.containsItem(par1Entity.entityId))
    148            {
    149                throw new IllegalStateException("Entity is already tracked!");
    150            }
    151            else
    152            {
    153                EntityTrackerEntry var5 = new EntityTrackerEntry(par1Entity, par2, par3, par4);
    154                this.trackedEntities.add(var5);
    155                this.trackedEntityIDs.addKey(par1Entity.entityId, var5);
    156                var5.sendEventsToPlayers(this.theWorld.playerEntities);
    157            }
    158        }
    159
    160        public void removeEntityFromAllTrackingPlayers(Entity par1Entity)
    161        {
    162            if (par1Entity instanceof EntityPlayerMP)
    163            {
    164                EntityPlayerMP var2 = (EntityPlayerMP)par1Entity;
    165                Iterator var3 = this.trackedEntities.iterator();
    166
    167                while (var3.hasNext())
    168                {
    169                    EntityTrackerEntry var4 = (EntityTrackerEntry)var3.next();
    170                    var4.removeFromWatchingList(var2);
    171                }
    172            }
    173
    174            EntityTrackerEntry var5 = (EntityTrackerEntry)this.trackedEntityIDs.removeObject(par1Entity.entityId);
    175
    176            if (var5 != null)
    177            {
    178                this.trackedEntities.remove(var5);
    179                var5.informAllAssociatedPlayersOfItemDestruction();
    180            }
    181        }
    182
    183        public void updateTrackedEntities()
    184        {
    185            ArrayList var1 = new ArrayList();
    186            Iterator var2 = this.trackedEntities.iterator();
    187
    188            while (var2.hasNext())
    189            {
    190                EntityTrackerEntry var3 = (EntityTrackerEntry)var2.next();
    191                var3.sendLocationToAllClients(this.theWorld.playerEntities);
    192
    193                if (var3.playerEntitiesUpdated && var3.myEntity instanceof EntityPlayerMP)
    194                {
    195                    var1.add((EntityPlayerMP)var3.myEntity);
    196                }
    197            }
    198
    199            var2 = var1.iterator();
    200
    201            while (var2.hasNext())
    202            {
    203                EntityPlayerMP var7 = (EntityPlayerMP)var2.next();
    204                EntityPlayerMP var4 = var7;
    205                Iterator var5 = this.trackedEntities.iterator();
    206
    207                while (var5.hasNext())
    208                {
    209                    EntityTrackerEntry var6 = (EntityTrackerEntry)var5.next();
    210
    211                    if (var6.myEntity != var4)
    212                    {
    213                        var6.tryStartWachingThis(var4);
    214                    }
    215                }
    216            }
    217        }
    218
    219        /**
    220         * does not send the packet to the entity if the entity is a player
    221         */
    222        public void sendPacketToAllPlayersTrackingEntity(Entity par1Entity, Packet par2Packet)
    223        {
    224            EntityTrackerEntry var3 = (EntityTrackerEntry)this.trackedEntityIDs.lookup(par1Entity.entityId);
    225
    226            if (var3 != null)
    227            {
    228                var3.sendPacketToAllTrackingPlayers(par2Packet);
    229            }
    230        }
    231
    232        /**
    233         * sends to the entity if the entity is a player
    234         */
    235        public void sendPacketToAllAssociatedPlayers(Entity par1Entity, Packet par2Packet)
    236        {
    237            EntityTrackerEntry var3 = (EntityTrackerEntry)this.trackedEntityIDs.lookup(par1Entity.entityId);
    238
    239            if (var3 != null)
    240            {
    241                var3.sendPacketToAllAssociatedPlayers(par2Packet);
    242            }
    243        }
    244
    245        public void removeAllTrackingPlayers(EntityPlayerMP par1EntityPlayerMP)
    246        {
    247            Iterator var2 = this.trackedEntities.iterator();
    248
    249            while (var2.hasNext())
    250            {
    251                EntityTrackerEntry var3 = (EntityTrackerEntry)var2.next();
    252                var3.removePlayerFromTracker(par1EntityPlayerMP);
    253            }
    254        }
    255    }