Third pass: packet magic to pseudo-respawn NPCs

also cache the NPC position to slightly improve efficiency. Micro-effic
matters a lot here.
This commit is contained in:
mcmonkey4eva 2014-12-05 22:06:03 -08:00
parent 306138165f
commit 1c6a1cc719

View File

@ -322,14 +322,30 @@ public class EventListen implements Listener {
} }
public void showNPCReset(final Player player, final NPC npc) { public void showNPCReset(final Player player, final NPC npc) {
// TODO: Replicate this with packets! ((CraftPlayer)player).getHandle().playerConnection.sendPacket
npc.despawn(); (new PacketPlayOutEntityDestroy(npc.getEntity().getEntityId()));
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() { Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override @Override
public void run() { public void run() {
npc.spawn(npc.getStoredLocation()); if (player.isOnline() && player.isValid()
&& npc.isSpawned() && npc.getEntity().getType() == EntityType.PLAYER) {
((CraftPlayer) player).getHandle().playerConnection.sendPacket(new PacketPlayOutPlayerInfo
(EnumPlayerInfoAction.ADD_PLAYER, ((CraftPlayer) npc.getEntity()).getHandle()));
((CraftPlayer) player).getHandle().playerConnection.sendPacket(new PacketPlayOutNamedEntitySpawn
(((CraftPlayer) npc.getEntity()).getHandle()));
}
} }
}, 1); }, 1);
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
if (player.isOnline() && player.isValid()
&& npc.isSpawned() && npc.getEntity().getType() == EntityType.PLAYER) {
((CraftPlayer) player).getHandle().playerConnection.sendPacket(new PacketPlayOutPlayerInfo
(EnumPlayerInfoAction.REMOVE_PLAYER, ((CraftPlayer) npc.getEntity()).getHandle()));
}
}
}, 61);
} }
@EventHandler @EventHandler
@ -347,11 +363,12 @@ public class EventListen implements Listener {
for (final NPC npc: getAllNPCs()) { for (final NPC npc: getAllNPCs()) {
if (npc.isSpawned() && npc.getEntity().getType() == EntityType.PLAYER) { if (npc.isSpawned() && npc.getEntity().getType() == EntityType.PLAYER) {
// if to.world=npc.world and in-range, and if (from.world = npc.world and out-of-range, or from a different world) // if to.world=npc.world and in-range, and if (from.world = npc.world and out-of-range, or from a different world)
if ((to.getWorld().getName().equalsIgnoreCase(npc.getEntity().getLocation().getWorld().getName()) Location npcPos = npc.getEntity().getLocation();
&& npc.getEntity().getLocation().distanceSquared(to) < maxRad) if ((to.getWorld() == npcPos.getWorld()
&& ((from.getWorld().getName().equalsIgnoreCase(npc.getEntity().getLocation().getWorld().getName()) && npcPos.distanceSquared(to) < maxRad)
&& npc.getEntity().getLocation().distanceSquared(from) > maxRad) && ((from.getWorld() == npcPos.getWorld()
|| !from.getWorld().getName().equalsIgnoreCase(to.getWorld().getName()))) { && npcPos.distanceSquared(from) > maxRad)
|| from.getWorld() != to.getWorld())) {
showNPCReset(event.getPlayer(), npc); showNPCReset(event.getPlayer(), npc);
} }
} }