Add UUID to NPC

This commit is contained in:
fullwall 2014-01-11 23:36:46 +08:00
parent ecf5f145c6
commit 8ff4d336ff
3 changed files with 48 additions and 19 deletions

View File

@ -170,6 +170,37 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
return storedRegistries.get(name);
}
@Override
public Iterable<NPCRegistry> getNPCRegistries() {
return new Iterable<NPCRegistry>() {
@Override
public Iterator<NPCRegistry> iterator() {
return new Iterator<NPCRegistry>() {
Iterator<NPCRegistry> stored;
@Override
public boolean hasNext() {
return stored == null ? true : stored.hasNext();
}
@Override
public NPCRegistry next() {
if (stored == null) {
stored = storedRegistries.values().iterator();
return npcRegistry;
}
return stored.next();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
}
@Override
public NPCRegistry getNPCRegistry() {
return npcRegistry;

View File

@ -1,6 +1,7 @@
package net.citizensnpcs.npc;
import java.util.Collection;
import java.util.UUID;
import net.citizensnpcs.NPCNeedsRespawnEvent;
import net.citizensnpcs.Settings.Setting;
@ -39,10 +40,9 @@ import com.google.common.base.Throwables;
public class CitizensNPC extends AbstractNPC {
private EntityController entityController;
private final CitizensNavigator navigator = new CitizensNavigator(this);
private int packetUpdateCount;
public CitizensNPC(int id, String name, EntityController entityController, NPCRegistry registry) {
super(id, name, registry);
public CitizensNPC(UUID uuid, int id, String name, EntityController entityController, NPCRegistry registry) {
super(uuid, id, name, registry);
Preconditions.checkNotNull(entityController);
this.entityController = entityController;
}
@ -215,13 +215,11 @@ public class CitizensNPC extends AbstractNPC {
NMS.trySwim(getEntity());
}
navigator.run();
if (++packetUpdateCount > 30) {
if (!getNavigator().isNavigating()) {
if (!getNavigator().isNavigating() && getEntity().getWorld().getTime() % 30 == 0) {
NMS.sendPacketNearby(getStoredLocation(),
new PacketPlayOutEntityTeleport(NMS.getHandle(getEntity())));
}
packetUpdateCount = 0;
}
}
} catch (Exception ex) {
Throwable error = Throwables.getRootCause(ex);

View File

@ -8,6 +8,7 @@ import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import net.citizensnpcs.api.event.DespawnReason;
import net.citizensnpcs.api.event.NPCCreateEvent;
@ -35,10 +36,15 @@ public class CitizensNPCRegistry implements NPCRegistry {
}
@Override
public NPC createNPC(EntityType type, int id, String name) {
public NPC createNPC(EntityType type, String name) {
return createNPC(type, UUID.randomUUID(), generateUniqueId(), name);
}
@Override
public NPC createNPC(EntityType type, UUID uuid, int id, String name) {
Preconditions.checkNotNull(name, "name cannot be null");
Preconditions.checkNotNull(type, "type cannot be null");
CitizensNPC npc = getByType(type, id, name);
CitizensNPC npc = getByType(type, uuid, id, name);
if (npc == null)
throw new IllegalStateException("Could not create NPC.");
npcs.put(npc.getId(), npc);
@ -46,11 +52,6 @@ public class CitizensNPCRegistry implements NPCRegistry {
return npc;
}
@Override
public NPC createNPC(EntityType type, String name) {
return createNPC(type, generateUniqueId(), name);
}
@Override
public void deregister(NPC npc) {
npcs.remove(npc.getId());
@ -87,8 +88,8 @@ public class CitizensNPCRegistry implements NPCRegistry {
return npcs.get(id);
}
private CitizensNPC getByType(EntityType type, int id, String name) {
return new CitizensNPC(id, name, EntityControllers.createForType(type), this);
private CitizensNPC getByType(EntityType type, UUID uuid, int id, String name) {
return new CitizensNPC(uuid, id, name, EntityControllers.createForType(type), this);
}
@Override
@ -196,7 +197,6 @@ public class CitizensNPCRegistry implements NPCRegistry {
return o1.getId() - o2.getId();
}
};
private static boolean TROVE_EXISTS = false;
static {
// allow trove dependency to be optional for debugging purposes