Work around entity equipping

This commit is contained in:
fullwall 2012-10-31 14:13:38 +08:00
parent dd610ccfc5
commit 40cb146757
5 changed files with 32 additions and 6 deletions

View File

@ -193,6 +193,7 @@ public abstract class CitizensNPC extends AbstractNPC {
return false;
}
NMS.setHeadYaw(mcEntity, loc.getYaw());
getBukkitEntity().setMetadata(NPC_METADATA_MARKER,
new FixedMetadataValue(CitizensAPI.getPlugin(), true));

View File

@ -2,6 +2,7 @@ package net.citizensnpcs.npc.entity;
import java.io.IOException;
import java.net.Socket;
import java.util.Arrays;
import java.util.List;
import net.citizensnpcs.api.event.NPCPushEvent;
@ -20,6 +21,7 @@ import net.minecraft.server.MinecraftServer;
import net.minecraft.server.Navigation;
import net.minecraft.server.NetHandler;
import net.minecraft.server.NetworkManager;
import net.minecraft.server.Packet5EntityEquipment;
import net.minecraft.server.World;
import org.bukkit.Bukkit;
@ -32,6 +34,8 @@ import org.bukkit.util.Vector;
public class EntityHumanNPC extends EntityPlayer implements NPCHolder {
private final CitizensNPC npc;
private net.minecraft.server.ItemStack[] previousEquipment = { null, null, null, null, null };
public EntityHumanNPC(MinecraftServer minecraftServer, World world, String string,
ItemInWorldManager itemInWorldManager, NPC npc) {
super(minecraftServer, world, string, itemInWorldManager);
@ -125,7 +129,7 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder {
super.j_();
if (npc == null)
return;
updateEquipment();
NMS.updateAI(this);
Navigation navigation = getNavigation();
if (Math.abs(motX) < EPSILON && Math.abs(motY) < EPSILON && Math.abs(motZ) < EPSILON)
@ -169,6 +173,23 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder {
NMS.setHeadYaw(this, yaw);
}
private void updateEquipment() {
int changes = 0;
for (int i = 0; i < previousEquipment.length; i++) {
net.minecraft.server.ItemStack previous = previousEquipment[i];
net.minecraft.server.ItemStack current = getEquipment(i);
if (current == null)
continue;
if (!net.minecraft.server.ItemStack.equals(previous, current)) {
Util.sendPacketNearby(getBukkitEntity().getLocation(), new Packet5EntityEquipment(id, i,
current));
++changes;
}
}
if (changes > 0)
previousEquipment = Arrays.copyOf(getEquipment(), previousEquipment.length);
}
public static class PlayerNPC extends CraftPlayer implements NPCHolder {
private final CitizensNPC npc;

View File

@ -25,16 +25,16 @@ public class DelayTrigger implements WaypointTrigger {
return String.format("Delay for %d ticks", delay);
}
public int getDelay() {
return delay;
}
@Override
public void onWaypointReached(NPC npc, Location waypoint) {
if (delay > 0)
scheduleTask(npc.getTrait(Waypoints.class).getCurrentProvider());
}
public int getDelay() {
return delay;
}
private void scheduleTask(final WaypointProvider provider) {
provider.setPaused(true);
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {

View File

@ -68,7 +68,7 @@ public class NMS {
return;
if (remove) {
handle.world.players.remove(handle);
} else {
} else if (!handle.world.players.contains(handle)) {
handle.world.players.add(handle);
}
}

View File

@ -138,6 +138,10 @@ public class Util {
return type;
}
public static void sendPacketNearby(Location location, Packet packet) {
sendPacketNearby(location, packet, 64);
}
public static void sendPacketNearby(Location location, Packet packet, double radius) {
radius *= radius;
final World world = location.getWorld();