From f9497b31c2358d7d0b9aef549e9530c072035c56 Mon Sep 17 00:00:00 2001 From: fullwall Date: Mon, 18 Nov 2013 15:18:18 +0800 Subject: [PATCH] Move some methods into AbstractNPC, implement sorted() --- .../citizensnpcs/commands/NPCCommands.java | 12 +++-- .../net/citizensnpcs/npc/CitizensNPC.java | 52 +------------------ .../citizensnpcs/npc/CitizensNPCRegistry.java | 32 ++++++++++++ src/main/java/net/citizensnpcs/util/NMS.java | 8 --- 4 files changed, 41 insertions(+), 63 deletions(-) diff --git a/src/main/java/net/citizensnpcs/commands/NPCCommands.java b/src/main/java/net/citizensnpcs/commands/NPCCommands.java index 048ca54de..4ecef7c3e 100644 --- a/src/main/java/net/citizensnpcs/commands/NPCCommands.java +++ b/src/main/java/net/citizensnpcs/commands/NPCCommands.java @@ -595,20 +595,22 @@ public class NPCCommands { List npcs = new ArrayList(); 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); + } } } diff --git a/src/main/java/net/citizensnpcs/npc/CitizensNPC.java b/src/main/java/net/citizensnpcs/npc/CitizensNPC.java index 41408ec27..2f25a2563 100644 --- a/src/main/java/net/citizensnpcs/npc/CitizensNPC.java +++ b/src/main/java/net/citizensnpcs/npc/CitizensNPC.java @@ -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 { diff --git a/src/main/java/net/citizensnpcs/npc/CitizensNPCRegistry.java b/src/main/java/net/citizensnpcs/npc/CitizensNPCRegistry.java index 4034f5bbf..da6d38a04 100644 --- a/src/main/java/net/citizensnpcs/npc/CitizensNPCRegistry.java +++ b/src/main/java/net/citizensnpcs/npc/CitizensNPCRegistry.java @@ -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 sorted() { + return npcs.sorted(); + } + public static class MapNPCCollection implements NPCCollection { private final Map npcs = Maps.newHashMap(); @@ -131,6 +140,13 @@ public class CitizensNPCRegistry implements NPCRegistry { public void remove(int id) { npcs.remove(id); } + + @Override + public Iterable sorted() { + List vals = new ArrayList(npcs.values()); + Collections.sort(vals, NPC_COMPARATOR); + return vals; + } } public static interface NPCCollection extends Iterable { @@ -139,6 +155,8 @@ public class CitizensNPCRegistry implements NPCRegistry { public void put(int id, NPC npc); public void remove(int id); + + public Iterable 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 sorted() { + List vals = new ArrayList(npcs.valueCollection()); + Collections.sort(vals, NPC_COMPARATOR); + return vals; + } } + private static final Comparator NPC_COMPARATOR = new Comparator() { + @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 diff --git a/src/main/java/net/citizensnpcs/util/NMS.java b/src/main/java/net/citizensnpcs/util/NMS.java index 098f0d15e..7493af86e 100644 --- a/src/main/java/net/citizensnpcs/util/NMS.java +++ b/src/main/java/net/citizensnpcs/util/NMS.java @@ -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;