    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            var2 = var1.iterator();
    212
    213            while (var2.hasNext())
    214            {
    215                EntityPlayerMP var7 = (EntityPlayerMP)var2.next();
    216                EntityPlayerMP var4 = var7;
    217                Iterator var5 = this.trackedEntities.iterator();
    218
    219                while (var5.hasNext())
    220                {
    221                    EntityTrackerEntry var6 = (EntityTrackerEntry)var5.next();
    222
    223                    if (var6.myEntity != var4)
    224                    {
    225                        var6.tryStartWachingThis(var4);
    226                    }
    227                }
    228            }
    229        }
    230
    231        /**
    232         * does not send the packet to the entity if the entity is a player
    233         */
    234        public void sendPacketToAllPlayersTrackingEntity(Entity par1Entity, Packet par2Packet)
    235        {
    236            EntityTrackerEntry var3 = (EntityTrackerEntry)this.trackedEntityIDs.lookup(par1Entity.entityId);
    237
    238            if (var3 != null)
    239            {
    240                var3.sendPacketToAllTrackingPlayers(par2Packet);
    241            }
    242        }
    243
    244        /**
    245         * sends to the entity if the entity is a player
    246         */
    247        public void sendPacketToAllAssociatedPlayers(Entity par1Entity, Packet par2Packet)
    248        {
    249            EntityTrackerEntry var3 = (EntityTrackerEntry)this.trackedEntityIDs.lookup(par1Entity.entityId);
    250
    251            if (var3 != null)
    252            {
    253                var3.sendPacketToAllAssociatedPlayers(par2Packet);
    254            }
    255        }
    256
    257        public void removeAllTrackingPlayers(EntityPlayerMP par1EntityPlayerMP)
    258        {
    259            Iterator var2 = this.trackedEntities.iterator();
    260
    261            while (var2.hasNext())
    262            {
    263                EntityTrackerEntry var3 = (EntityTrackerEntry)var2.next();
    264                var3.removePlayerFromTracker(par1EntityPlayerMP);
    265            }
    266        }
    267    }