Small optimisations

This commit is contained in:
fullwall 2016-08-25 19:40:58 +08:00
parent 2dab3d338d
commit 7cb434db21
3 changed files with 26 additions and 28 deletions

View File

@ -333,7 +333,7 @@ public class CitizensNPC extends AbstractNPC {
} }
private void updateFlyableState() { private void updateFlyableState() {
EntityType type = getTrait(MobType.class).getType(); EntityType type = isSpawned() ? getEntity().getType() : getTrait(MobType.class).getType();
if (type == null) if (type == null)
return; return;
if (Util.isAlwaysFlyable(type)) { if (Util.isAlwaysFlyable(type)) {

View File

@ -46,26 +46,21 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable
private void findNewTarget() { private void findNewTarget() {
List<Entity> nearby = npc.getEntity().getNearbyEntities(range, range, range); List<Entity> nearby = npc.getEntity().getNearbyEntities(range, range, range);
final Location npcLocation = npc.getEntity().getLocation(NPC_LOCATION),
cacheLocation1 = new Location(null, 0, 0, 0), cacheLocation2 = new Location(null, 0, 0, 0);
Collections.sort(nearby, new Comparator<Entity>() { Collections.sort(nearby, new Comparator<Entity>() {
@Override @Override
public int compare(Entity o1, Entity o2) { public int compare(Entity o1, Entity o2) {
Location l1 = o1.getLocation(cacheLocation1); Location l1 = o1.getLocation(CACHE_LOCATION);
Location l2 = o2.getLocation(cacheLocation2); Location l2 = o2.getLocation(CACHE_LOCATION2);
if (!npcLocation.getWorld().equals(l1.getWorld()) || !npcLocation.getWorld().equals(l2.getWorld())) { if (!NPC_LOCATION.getWorld().equals(l1.getWorld()) || !NPC_LOCATION.getWorld().equals(l2.getWorld())) {
return -1; return -1;
} }
double d1 = l1.distanceSquared(npcLocation); return Double.compare(l1.distanceSquared(NPC_LOCATION), l2.distanceSquared(NPC_LOCATION));
double d2 = l2.distanceSquared(npcLocation);
return Double.compare(d1, d2);
} }
}); });
for (Entity entity : nearby) { for (Entity entity : nearby) {
if (entity.getType() != EntityType.PLAYER if (entity.getType() != EntityType.PLAYER || ((Player) entity).getGameMode() == GameMode.SPECTATOR
|| entity.getLocation(cacheLocation1).getWorld() != npcLocation.getWorld() || entity.getLocation(CACHE_LOCATION).getWorld() != NPC_LOCATION.getWorld()
|| CitizensAPI.getNPCRegistry().getNPC(entity) != null || CitizensAPI.getNPCRegistry().getNPC(entity) != null)
|| ((Player) entity).getGameMode() == GameMode.SPECTATOR)
continue; continue;
lookingAt = (Player) entity; lookingAt = (Player) entity;
return; return;
@ -76,7 +71,7 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable
if (lookingAt == null) if (lookingAt == null)
return true; return true;
if (!lookingAt.isOnline() || lookingAt.getWorld() != npc.getEntity().getWorld() if (!lookingAt.isOnline() || lookingAt.getWorld() != npc.getEntity().getWorld()
|| lookingAt.getLocation().distanceSquared(npc.getEntity().getLocation(NPC_LOCATION)) > range) { || lookingAt.getLocation(PLAYER_LOCATION).distanceSquared(NPC_LOCATION) > range) {
lookingAt = null; lookingAt = null;
} }
return lookingAt == null; return lookingAt == null;
@ -102,6 +97,7 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable
public void run() { public void run() {
if (!enabled || !npc.isSpawned() || npc.getNavigator().isNavigating()) if (!enabled || !npc.isSpawned() || npc.getNavigator().isNavigating())
return; return;
npc.getEntity().getLocation(NPC_LOCATION);
if (hasInvalidTarget()) { if (hasInvalidTarget()) {
findNewTarget(); findNewTarget();
} }
@ -136,5 +132,8 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable
return "LookClose{" + enabled + "}"; return "LookClose{" + enabled + "}";
} }
private static final Location CACHE_LOCATION = new Location(null, 0, 0, 0);
private static final Location CACHE_LOCATION2 = new Location(null, 0, 0, 0);
private static final Location NPC_LOCATION = new Location(null, 0, 0, 0); private static final Location NPC_LOCATION = new Location(null, 0, 0, 0);
private static final Location PLAYER_LOCATION = new Location(null, 0, 0, 0);
} }

View File

@ -376,11 +376,11 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
} }
private void updatePackets(boolean navigating) { private void updatePackets(boolean navigating) {
if (updateCounter++ > Setting.PACKET_UPDATE_DELAY.asInt()) { if (updateCounter++ <= Setting.PACKET_UPDATE_DELAY.asInt())
return;
updateCounter = 0; updateCounter = 0;
Location current = getBukkitEntity().getLocation(packetLocationCache); Location current = getBukkitEntity().getLocation(packetLocationCache);
Packet<?>[] packets = new Packet[navigating ? EnumItemSlot.values().length Packet<?>[] packets = new Packet[navigating ? EnumItemSlot.values().length : EnumItemSlot.values().length + 1];
: EnumItemSlot.values().length + 1];
if (!navigating) { if (!navigating) {
packets[5] = new PacketPlayOutEntityHeadRotation(this, packets[5] = new PacketPlayOutEntityHeadRotation(this,
(byte) MathHelper.d(NMSImpl.getHeadYaw(this) * 256.0F / 360.0F)); (byte) MathHelper.d(NMSImpl.getHeadYaw(this) * 256.0F / 360.0F));
@ -391,7 +391,6 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
} }
NMSImpl.sendPacketsNearby(getBukkitEntity(), current, packets); NMSImpl.sendPacketsNearby(getBukkitEntity(), current, packets);
} }
}
public void updatePathfindingRange(float pathfindingRange) { public void updatePathfindingRange(float pathfindingRange) {
this.navigation.setRange(pathfindingRange); this.navigation.setRange(pathfindingRange);