From 7cb434db218862a52e0b48b781b457038eb6e073 Mon Sep 17 00:00:00 2001 From: fullwall Date: Thu, 25 Aug 2016 19:40:58 +0800 Subject: [PATCH] Small optimisations --- .../net/citizensnpcs/npc/CitizensNPC.java | 2 +- .../net/citizensnpcs/trait/LookClose.java | 25 +++++++++-------- .../nms/v1_10_R1/entity/EntityHumanNPC.java | 27 +++++++++---------- 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/main/src/main/java/net/citizensnpcs/npc/CitizensNPC.java b/main/src/main/java/net/citizensnpcs/npc/CitizensNPC.java index ffceb4394..33b9f1caf 100644 --- a/main/src/main/java/net/citizensnpcs/npc/CitizensNPC.java +++ b/main/src/main/java/net/citizensnpcs/npc/CitizensNPC.java @@ -333,7 +333,7 @@ public class CitizensNPC extends AbstractNPC { } private void updateFlyableState() { - EntityType type = getTrait(MobType.class).getType(); + EntityType type = isSpawned() ? getEntity().getType() : getTrait(MobType.class).getType(); if (type == null) return; if (Util.isAlwaysFlyable(type)) { diff --git a/main/src/main/java/net/citizensnpcs/trait/LookClose.java b/main/src/main/java/net/citizensnpcs/trait/LookClose.java index 76b933a45..36c4ee998 100644 --- a/main/src/main/java/net/citizensnpcs/trait/LookClose.java +++ b/main/src/main/java/net/citizensnpcs/trait/LookClose.java @@ -46,26 +46,21 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable private void findNewTarget() { List 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() { @Override public int compare(Entity o1, Entity o2) { - Location l1 = o1.getLocation(cacheLocation1); - Location l2 = o2.getLocation(cacheLocation2); - if (!npcLocation.getWorld().equals(l1.getWorld()) || !npcLocation.getWorld().equals(l2.getWorld())) { + Location l1 = o1.getLocation(CACHE_LOCATION); + Location l2 = o2.getLocation(CACHE_LOCATION2); + if (!NPC_LOCATION.getWorld().equals(l1.getWorld()) || !NPC_LOCATION.getWorld().equals(l2.getWorld())) { return -1; } - double d1 = l1.distanceSquared(npcLocation); - double d2 = l2.distanceSquared(npcLocation); - return Double.compare(d1, d2); + return Double.compare(l1.distanceSquared(NPC_LOCATION), l2.distanceSquared(NPC_LOCATION)); } }); for (Entity entity : nearby) { - if (entity.getType() != EntityType.PLAYER - || entity.getLocation(cacheLocation1).getWorld() != npcLocation.getWorld() - || CitizensAPI.getNPCRegistry().getNPC(entity) != null - || ((Player) entity).getGameMode() == GameMode.SPECTATOR) + if (entity.getType() != EntityType.PLAYER || ((Player) entity).getGameMode() == GameMode.SPECTATOR + || entity.getLocation(CACHE_LOCATION).getWorld() != NPC_LOCATION.getWorld() + || CitizensAPI.getNPCRegistry().getNPC(entity) != null) continue; lookingAt = (Player) entity; return; @@ -76,7 +71,7 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable if (lookingAt == null) return true; 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; } return lookingAt == null; @@ -102,6 +97,7 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable public void run() { if (!enabled || !npc.isSpawned() || npc.getNavigator().isNavigating()) return; + npc.getEntity().getLocation(NPC_LOCATION); if (hasInvalidTarget()) { findNewTarget(); } @@ -136,5 +132,8 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable 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 PLAYER_LOCATION = new Location(null, 0, 0, 0); } \ No newline at end of file diff --git a/v1_10_R1/src/main/java/net/citizensnpcs/nms/v1_10_R1/entity/EntityHumanNPC.java b/v1_10_R1/src/main/java/net/citizensnpcs/nms/v1_10_R1/entity/EntityHumanNPC.java index 3f23b8e95..3444af2e0 100644 --- a/v1_10_R1/src/main/java/net/citizensnpcs/nms/v1_10_R1/entity/EntityHumanNPC.java +++ b/v1_10_R1/src/main/java/net/citizensnpcs/nms/v1_10_R1/entity/EntityHumanNPC.java @@ -376,21 +376,20 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable } private void updatePackets(boolean navigating) { - if (updateCounter++ > Setting.PACKET_UPDATE_DELAY.asInt()) { - updateCounter = 0; - Location current = getBukkitEntity().getLocation(packetLocationCache); - Packet[] packets = new Packet[navigating ? EnumItemSlot.values().length - : EnumItemSlot.values().length + 1]; - if (!navigating) { - packets[5] = new PacketPlayOutEntityHeadRotation(this, - (byte) MathHelper.d(NMSImpl.getHeadYaw(this) * 256.0F / 360.0F)); - } - int i = 0; - for (EnumItemSlot slot : EnumItemSlot.values()) { - packets[i++] = new PacketPlayOutEntityEquipment(getId(), slot, getEquipment(slot)); - } - NMSImpl.sendPacketsNearby(getBukkitEntity(), current, packets); + if (updateCounter++ <= Setting.PACKET_UPDATE_DELAY.asInt()) + return; + updateCounter = 0; + Location current = getBukkitEntity().getLocation(packetLocationCache); + Packet[] packets = new Packet[navigating ? EnumItemSlot.values().length : EnumItemSlot.values().length + 1]; + if (!navigating) { + packets[5] = new PacketPlayOutEntityHeadRotation(this, + (byte) MathHelper.d(NMSImpl.getHeadYaw(this) * 256.0F / 360.0F)); } + int i = 0; + for (EnumItemSlot slot : EnumItemSlot.values()) { + packets[i++] = new PacketPlayOutEntityEquipment(getId(), slot, getEquipment(slot)); + } + NMSImpl.sendPacketsNearby(getBukkitEntity(), current, packets); } public void updatePathfindingRange(float pathfindingRange) {