diff --git a/src/main/java/net/citizensnpcs/npc/entity/EntityHumanNPC.java b/src/main/java/net/citizensnpcs/npc/entity/EntityHumanNPC.java index 1e601dcbb..c43663f93 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/EntityHumanNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/EntityHumanNPC.java @@ -20,6 +20,7 @@ import net.minecraft.server.v1_4_R1.MathHelper; import net.minecraft.server.v1_4_R1.MinecraftServer; import net.minecraft.server.v1_4_R1.Navigation; import net.minecraft.server.v1_4_R1.NetworkManager; +import net.minecraft.server.v1_4_R1.Packet; import net.minecraft.server.v1_4_R1.Packet35EntityHeadRotation; import net.minecraft.server.v1_4_R1.Packet5EntityEquipment; import net.minecraft.server.v1_4_R1.PlayerInteractManager; @@ -34,10 +35,10 @@ import org.bukkit.plugin.Plugin; import org.bukkit.util.Vector; public class EntityHumanNPC extends EntityPlayer implements NPCHolder { - private final Location cachedEquipmentLocation = new Location(null, 0, 0, 0); private boolean gravity = true; - private int headYawCount; private final CitizensNPC npc; + private final Location packetLocationCache = new Location(null, 0, 0, 0); + private int packetUpdateCount; public EntityHumanNPC(MinecraftServer minecraftServer, World world, String string, PlayerInteractManager playerInteractManager, NPC npc) { @@ -130,20 +131,13 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder { if (npc == null) return; boolean navigating = npc.getNavigator().isNavigating(); - if (!navigating && ++headYawCount >= 20) { - int i = MathHelper.d(az * 256.0F / 360.0F); - NMS.sendPacketNearby(getBukkitEntity().getLocation(cachedEquipmentLocation), - new Packet35EntityHeadRotation(id, (byte) i)); - headYawCount = 0; - } + updatePackets(navigating); if (gravity && !navigating && getBukkitEntity() != null && Util.isLoaded(getBukkitEntity().getLocation(LOADED_LOCATION)) && !NMS.inWater(getBukkitEntity())) { move(0, -0.2, 0); // gravity. also works around an entity.onGround not updating issue // (onGround is normally updated by the client) } - - updateEquipment(); if (!npc.data().get("removefromplayerlist", true)) g(); if (Math.abs(motX) < EPSILON && Math.abs(motY) < EPSILON && Math.abs(motZ) < EPSILON) @@ -191,10 +185,18 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder { NMS.setHeadYaw(this, yaw); } - private void updateEquipment() { - for (int i = 0; i < 5; i++) { - NMS.sendPacketNearby(getBukkitEntity().getLocation(cachedEquipmentLocation), new Packet5EntityEquipment(id, - i, getEquipment(i))); + private void updatePackets(boolean navigating) { + if (++packetUpdateCount >= 20) { + Location current = getBukkitEntity().getLocation(packetLocationCache); + Packet[] packets = new Packet[navigating ? 5 : 6]; + if (!navigating) { + packets[5] = new Packet35EntityHeadRotation(id, (byte) MathHelper.d(az * 256.0F / 360.0F)); + } + for (int i = 0; i < 5; i++) { + packets[i] = new Packet5EntityEquipment(id, i, getEquipment(i)); + } + NMS.sendPacketsNearby(current, packets); + packetUpdateCount = 0; } } diff --git a/src/main/java/net/citizensnpcs/util/NMS.java b/src/main/java/net/citizensnpcs/util/NMS.java index 040405a47..198a16f2b 100644 --- a/src/main/java/net/citizensnpcs/util/NMS.java +++ b/src/main/java/net/citizensnpcs/util/NMS.java @@ -2,6 +2,8 @@ package net.citizensnpcs.util; import java.lang.reflect.Constructor; import java.lang.reflect.Field; +import java.util.Arrays; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Random; @@ -219,11 +221,11 @@ public class NMS { ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet); } - public static void sendPacketNearby(Location location, Packet packet) { - NMS.sendPacketNearby(location, packet, 64); + public static void sendPacketsNearby(Location location, Collection packets) { + NMS.sendPacketsNearby(location, packets, 64); } - public static void sendPacketNearby(Location location, Packet packet, double radius) { + public static void sendPacketsNearby(Location location, Collection packets, double radius) { radius *= radius; final org.bukkit.World world = location.getWorld(); for (Player ply : Bukkit.getServer().getOnlinePlayers()) { @@ -233,10 +235,16 @@ public class NMS { if (location.distanceSquared(ply.getLocation()) > radius) { continue; } - sendPacket(ply, packet); + for (Packet packet : packets) { + sendPacket(ply, packet); + } } } + public static void sendPacketsNearby(Location location, Packet... packets) { + NMS.sendPacketsNearby(location, Arrays.asList(packets), 64); + } + public static void sendToOnline(Packet... packets) { Validate.notNull(packets, "packets cannot be null"); for (Player player : Bukkit.getOnlinePlayers()) { diff --git a/src/main/java/net/citizensnpcs/util/PlayerAnimation.java b/src/main/java/net/citizensnpcs/util/PlayerAnimation.java index af3b3e4a9..adf024efa 100644 --- a/src/main/java/net/citizensnpcs/util/PlayerAnimation.java +++ b/src/main/java/net/citizensnpcs/util/PlayerAnimation.java @@ -1,5 +1,7 @@ package net.citizensnpcs.util; +import java.util.Arrays; + import net.minecraft.server.v1_4_R1.EntityPlayer; import net.minecraft.server.v1_4_R1.Packet; import net.minecraft.server.v1_4_R1.Packet17EntityLocationAction; @@ -79,6 +81,6 @@ public enum PlayerAnimation { } protected void sendPacketNearby(Packet packet, EntityPlayer player, int radius) { - NMS.sendPacketNearby(player.getBukkitEntity().getLocation(), packet, radius); + NMS.sendPacketsNearby(player.getBukkitEntity().getLocation(), Arrays.asList(packet), radius); } }