Paper/src/main/java/net/minecraft/server/EntityTracker.java

188 Zeilen
7.3 KiB
Java

package net.minecraft.server;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class EntityTracker {
2012-07-29 09:33:13 +02:00
private final WorldServer world;
private Set b = new HashSet();
public IntHashMap trackedEntities = new IntHashMap(); // CraftBukkit - private -> public
private int d;
2012-07-29 09:33:13 +02:00
public EntityTracker(WorldServer worldserver) {
this.world = worldserver;
this.d = worldserver.getMinecraftServer().getServerConfigurationManager().a();
}
2011-05-14 16:29:42 +02:00
// CraftBukkit - synchronized
public synchronized void track(Entity entity) {
if (entity instanceof EntityPlayer) {
this.addEntity(entity, 512, 2);
EntityPlayer entityplayer = (EntityPlayer) entity;
2012-07-29 09:33:13 +02:00
Iterator iterator = this.b.iterator();
while (iterator.hasNext()) {
EntityTrackerEntry entitytrackerentry = (EntityTrackerEntry) iterator.next();
if (entitytrackerentry.tracker != entityplayer) {
2012-02-29 22:31:04 +01:00
entitytrackerentry.updatePlayer(entityplayer);
}
}
} else if (entity instanceof EntityFishingHook) {
this.addEntity(entity, 64, 5, true);
} else if (entity instanceof EntityArrow) {
this.addEntity(entity, 64, 20, false);
2011-11-20 09:01:14 +01:00
} else if (entity instanceof EntitySmallFireball) {
this.addEntity(entity, 64, 10, false);
2011-05-26 14:48:22 +02:00
} else if (entity instanceof EntityFireball) {
this.addEntity(entity, 64, 10, false);
} else if (entity instanceof EntitySnowball) {
this.addEntity(entity, 64, 10, true);
2011-11-20 09:01:14 +01:00
} else if (entity instanceof EntityEnderPearl) {
this.addEntity(entity, 64, 10, true);
} else if (entity instanceof EntityEnderSignal) {
2012-07-29 09:33:13 +02:00
this.addEntity(entity, 64, 4, true);
} else if (entity instanceof EntityEgg) {
this.addEntity(entity, 64, 10, true);
2011-11-20 09:01:14 +01:00
} else if (entity instanceof EntityPotion) {
this.addEntity(entity, 64, 10, true);
2012-03-01 11:49:23 +01:00
} else if (entity instanceof EntityThrownExpBottle) {
this.addEntity(entity, 64, 10, true);
} else if (entity instanceof EntityItem) {
this.addEntity(entity, 64, 20, true);
} else if (entity instanceof EntityMinecart) {
2011-11-20 09:01:14 +01:00
this.addEntity(entity, 80, 3, true);
} else if (entity instanceof EntityBoat) {
2011-11-20 09:01:14 +01:00
this.addEntity(entity, 80, 3, true);
} else if (entity instanceof EntitySquid) {
2011-11-20 09:01:14 +01:00
this.addEntity(entity, 64, 3, true);
} else if (entity instanceof EntityWither) {
this.addEntity(entity, 80, 3, false);
} else if (entity instanceof EntityBat) {
this.addEntity(entity, 80, 3, false);
} else if (entity instanceof IAnimal) {
2011-11-20 09:01:14 +01:00
this.addEntity(entity, 80, 3, true);
} else if (entity instanceof EntityEnderDragon) {
this.addEntity(entity, 160, 3, true);
} else if (entity instanceof EntityTNTPrimed) {
this.addEntity(entity, 160, 10, true);
} else if (entity instanceof EntityFallingBlock) {
this.addEntity(entity, 160, 20, true);
} else if (entity instanceof EntityPainting) {
this.addEntity(entity, 160, Integer.MAX_VALUE, false);
2011-09-15 02:23:52 +02:00
} else if (entity instanceof EntityExperienceOrb) {
this.addEntity(entity, 160, 20, true);
2011-11-20 09:01:14 +01:00
} else if (entity instanceof EntityEnderCrystal) {
this.addEntity(entity, 256, Integer.MAX_VALUE, false);
} else if (entity instanceof EntityItemFrame) {
this.addEntity(entity, 160, Integer.MAX_VALUE, false);
}
}
public void addEntity(Entity entity, int i, int j) {
this.addEntity(entity, i, j, false);
}
2011-05-14 16:29:42 +02:00
// CraftBukkit - synchronized
public synchronized void addEntity(Entity entity, int i, int j, boolean flag) {
if (i > this.d) {
i = this.d;
}
if (this.trackedEntities.b(entity.id)) {
// CraftBukkit - removed exception throw as tracking an already tracked entity theoretically shouldn't cause any issues.
2011-05-14 16:29:42 +02:00
// throw new IllegalStateException("Entity is already tracked!");
} else {
EntityTrackerEntry entitytrackerentry = new EntityTrackerEntry(entity, i, j, flag);
2012-07-29 09:33:13 +02:00
this.b.add(entitytrackerentry);
this.trackedEntities.a(entity.id, entitytrackerentry);
2012-07-29 09:33:13 +02:00
entitytrackerentry.scanPlayers(this.world.players);
}
}
2011-05-14 16:29:42 +02:00
// CraftBukkit - synchronized
public synchronized void untrackEntity(Entity entity) {
if (entity instanceof EntityPlayer) {
EntityPlayer entityplayer = (EntityPlayer) entity;
2012-07-29 09:33:13 +02:00
Iterator iterator = this.b.iterator();
while (iterator.hasNext()) {
EntityTrackerEntry entitytrackerentry = (EntityTrackerEntry) iterator.next();
entitytrackerentry.a(entityplayer);
}
}
EntityTrackerEntry entitytrackerentry1 = (EntityTrackerEntry) this.trackedEntities.d(entity.id);
if (entitytrackerentry1 != null) {
2012-07-29 09:33:13 +02:00
this.b.remove(entitytrackerentry1);
entitytrackerentry1.a();
}
}
2011-05-14 16:29:42 +02:00
// CraftBukkit - synchronized
public synchronized void updatePlayers() {
ArrayList arraylist = new ArrayList();
2012-07-29 09:33:13 +02:00
Iterator iterator = this.b.iterator();
while (iterator.hasNext()) {
EntityTrackerEntry entitytrackerentry = (EntityTrackerEntry) iterator.next();
2012-07-29 09:33:13 +02:00
entitytrackerentry.track(this.world.players);
2012-03-01 11:49:23 +01:00
if (entitytrackerentry.n && entitytrackerentry.tracker instanceof EntityPlayer) {
arraylist.add((EntityPlayer) entitytrackerentry.tracker);
}
}
2012-07-29 09:33:13 +02:00
iterator = arraylist.iterator();
while (iterator.hasNext()) {
EntityPlayer entityplayer = (EntityPlayer) iterator.next();
EntityPlayer entityplayer1 = entityplayer;
Iterator iterator1 = this.b.iterator();
while (iterator1.hasNext()) {
EntityTrackerEntry entitytrackerentry1 = (EntityTrackerEntry) iterator1.next();
2012-07-29 09:33:13 +02:00
if (entitytrackerentry1.tracker != entityplayer1) {
entitytrackerentry1.updatePlayer(entityplayer1);
}
}
}
}
2011-05-14 16:29:42 +02:00
// CraftBukkit - synchronized
2011-03-02 20:00:16 +01:00
public synchronized void a(Entity entity, Packet packet) {
2012-02-29 22:31:04 +01:00
EntityTrackerEntry entitytrackerentry = (EntityTrackerEntry) this.trackedEntities.get(entity.id);
if (entitytrackerentry != null) {
entitytrackerentry.broadcast(packet);
}
}
2011-05-14 16:29:42 +02:00
// CraftBukkit - synchronized
public synchronized void sendPacketToEntity(Entity entity, Packet packet) {
2012-02-29 22:31:04 +01:00
EntityTrackerEntry entitytrackerentry = (EntityTrackerEntry) this.trackedEntities.get(entity.id);
if (entitytrackerentry != null) {
entitytrackerentry.broadcastIncludingSelf(packet);
}
}
2011-05-14 16:29:42 +02:00
// CraftBukkit - synchronized
public synchronized void untrackPlayer(EntityPlayer entityplayer) {
2012-07-29 09:33:13 +02:00
Iterator iterator = this.b.iterator();
while (iterator.hasNext()) {
EntityTrackerEntry entitytrackerentry = (EntityTrackerEntry) iterator.next();
2012-02-29 22:31:04 +01:00
entitytrackerentry.clear(entityplayer);
}
}
}