Move some methods into AbstractNPC, implement sorted()

This commit is contained in:
fullwall 2013-11-18 15:18:18 +08:00
parent 5a38194a1c
commit 666d9cb210
4 changed files with 41 additions and 63 deletions

View File

@ -595,20 +595,22 @@ public class NPCCommands {
List<NPC> npcs = new ArrayList<NPC>();
if (args.hasFlag('a')) {
for (NPC add : npcRegistry) {
for (NPC add : npcRegistry.sorted()) {
npcs.add(add);
}
} else if (args.getValueFlags().size() == 0 && sender instanceof Player) {
for (NPC add : npcRegistry) {
if (!npcs.contains(add) && add.getTrait(Owner.class).isOwnedBy(sender))
for (NPC add : npcRegistry.sorted()) {
if (!npcs.contains(add) && add.getTrait(Owner.class).isOwnedBy(sender)) {
npcs.add(add);
}
}
} else {
if (args.hasValueFlag("owner")) {
String name = args.getFlag("owner");
for (NPC add : npcRegistry) {
if (!npcs.contains(add) && add.getTrait(Owner.class).isOwnedBy(name))
for (NPC add : npcRegistry.sorted()) {
if (!npcs.contains(add) && add.getTrait(Owner.class).isOwnedBy(name)) {
npcs.add(add);
}
}
}

View File

@ -31,7 +31,6 @@ import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.metadata.FixedMetadataValue;
import com.google.common.base.Preconditions;
@ -83,19 +82,6 @@ public class CitizensNPC extends AbstractNPC {
Util.faceLocation(getEntity(), location);
}
@Override
@Deprecated
public LivingEntity getBukkitEntity() {
if (entityController == null) {
return null;
}
Entity entity = entityController.getBukkitEntity();
if (entity == null || entity instanceof LivingEntity) {
return (LivingEntity) entity;
}
throw new IllegalStateException("getBukkitEntity() called on a non-living NPC");
}
@Override
public Entity getEntity() {
return entityController == null ? null : entityController.getBukkitEntity();
@ -114,12 +100,7 @@ public class CitizensNPC extends AbstractNPC {
@Override
public boolean isFlyable() {
updateFlyableState();
return data().get(NPC.FLYABLE_METADATA, false);
}
@Override
public boolean isSpawned() {
return getEntity() != null;
return super.isFlyable();
}
@Override
@ -164,7 +145,7 @@ public class CitizensNPC extends AbstractNPC {
@Override
public void setFlyable(boolean flyable) {
data().setPersistent(NPC.FLYABLE_METADATA, flyable);
super.setFlyable(flyable);
updateFlyableState();
}
@ -225,35 +206,6 @@ public class CitizensNPC extends AbstractNPC {
return true;
}
private void teleport(final Entity entity, Location location, boolean loaded, int delay) {
if (!loaded)
location.getBlock().getChunk();
final Entity passenger = entity.getPassenger();
entity.eject();
entity.teleport(location);
if (passenger == null)
return;
teleport(passenger, location, true, delay++);
Runnable task = new Runnable() {
@Override
public void run() {
NMS.mount(entity, passenger);
}
};
if (!location.getWorld().equals(entity.getWorld())) {
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), task, delay);
} else {
task.run();
}
}
@Override
public void teleport(Location location, TeleportCause cause) {
if (!isSpawned())
return;
teleport(NMS.getRootVehicle(getEntity()), location, false, 5);
}
@Override
public void update() {
try {

View File

@ -2,7 +2,11 @@ package net.citizensnpcs.npc;
import gnu.trove.map.hash.TIntObjectHashMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.citizensnpcs.api.event.DespawnReason;
@ -109,6 +113,11 @@ public class CitizensNPCRegistry implements NPCRegistry {
return npcs.iterator();
}
@Override
public Iterable<NPC> sorted() {
return npcs.sorted();
}
public static class MapNPCCollection implements NPCCollection {
private final Map<Integer, NPC> npcs = Maps.newHashMap();
@ -131,6 +140,13 @@ public class CitizensNPCRegistry implements NPCRegistry {
public void remove(int id) {
npcs.remove(id);
}
@Override
public Iterable<NPC> sorted() {
List<NPC> vals = new ArrayList<NPC>(npcs.values());
Collections.sort(vals, NPC_COMPARATOR);
return vals;
}
}
public static interface NPCCollection extends Iterable<NPC> {
@ -139,6 +155,8 @@ public class CitizensNPCRegistry implements NPCRegistry {
public void put(int id, NPC npc);
public void remove(int id);
public Iterable<NPC> sorted();
}
public static class TroveNPCCollection implements NPCCollection {
@ -163,8 +181,22 @@ public class CitizensNPCRegistry implements NPCRegistry {
public void remove(int id) {
npcs.remove(id);
}
@Override
public Iterable<NPC> sorted() {
List<NPC> vals = new ArrayList<NPC>(npcs.valueCollection());
Collections.sort(vals, NPC_COMPARATOR);
return vals;
}
}
private static final Comparator<NPC> NPC_COMPARATOR = new Comparator<NPC>() {
@Override
public int compare(NPC o1, NPC o2) {
return o1.getId() - o2.getId();
}
};
private static boolean TROVE_EXISTS = false;
static {
// allow trove dependency to be optional for debugging purposes

View File

@ -221,14 +221,6 @@ public class NMS {
: handle instanceof EntityHumanNPC ? ((EntityHumanNPC) handle).getNavigation() : null;
}
public static org.bukkit.entity.Entity getRootVehicle(org.bukkit.entity.Entity entity) {
Entity handle = NMS.getHandle(entity);
while (handle.vehicle != null) {
handle = handle.vehicle;
}
return handle.getBukkitEntity();
}
public static float getSpeedFor(NPC npc) {
if (!npc.isSpawned())
return DEFAULT_SPEED;