mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2024-11-23 11:05:49 +01:00
Move some methods into AbstractNPC, implement sorted()
This commit is contained in:
parent
23704dbfc3
commit
f9497b31c2
@ -595,20 +595,22 @@ public class NPCCommands {
|
|||||||
List<NPC> npcs = new ArrayList<NPC>();
|
List<NPC> npcs = new ArrayList<NPC>();
|
||||||
|
|
||||||
if (args.hasFlag('a')) {
|
if (args.hasFlag('a')) {
|
||||||
for (NPC add : npcRegistry) {
|
for (NPC add : npcRegistry.sorted()) {
|
||||||
npcs.add(add);
|
npcs.add(add);
|
||||||
}
|
}
|
||||||
} else if (args.getValueFlags().size() == 0 && sender instanceof Player) {
|
} else if (args.getValueFlags().size() == 0 && sender instanceof Player) {
|
||||||
for (NPC add : npcRegistry) {
|
for (NPC add : npcRegistry.sorted()) {
|
||||||
if (!npcs.contains(add) && add.getTrait(Owner.class).isOwnedBy(sender))
|
if (!npcs.contains(add) && add.getTrait(Owner.class).isOwnedBy(sender)) {
|
||||||
npcs.add(add);
|
npcs.add(add);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (args.hasValueFlag("owner")) {
|
if (args.hasValueFlag("owner")) {
|
||||||
String name = args.getFlag("owner");
|
String name = args.getFlag("owner");
|
||||||
for (NPC add : npcRegistry) {
|
for (NPC add : npcRegistry.sorted()) {
|
||||||
if (!npcs.contains(add) && add.getTrait(Owner.class).isOwnedBy(name))
|
if (!npcs.contains(add) && add.getTrait(Owner.class).isOwnedBy(name)) {
|
||||||
npcs.add(add);
|
npcs.add(add);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,7 +31,6 @@ import org.bukkit.entity.Entity;
|
|||||||
import org.bukkit.entity.EntityType;
|
import org.bukkit.entity.EntityType;
|
||||||
import org.bukkit.entity.LivingEntity;
|
import org.bukkit.entity.LivingEntity;
|
||||||
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
|
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
|
||||||
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
|
|
||||||
import org.bukkit.metadata.FixedMetadataValue;
|
import org.bukkit.metadata.FixedMetadataValue;
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
@ -83,19 +82,6 @@ public class CitizensNPC extends AbstractNPC {
|
|||||||
Util.faceLocation(getEntity(), location);
|
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
|
@Override
|
||||||
public Entity getEntity() {
|
public Entity getEntity() {
|
||||||
return entityController == null ? null : entityController.getBukkitEntity();
|
return entityController == null ? null : entityController.getBukkitEntity();
|
||||||
@ -114,12 +100,7 @@ public class CitizensNPC extends AbstractNPC {
|
|||||||
@Override
|
@Override
|
||||||
public boolean isFlyable() {
|
public boolean isFlyable() {
|
||||||
updateFlyableState();
|
updateFlyableState();
|
||||||
return data().get(NPC.FLYABLE_METADATA, false);
|
return super.isFlyable();
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isSpawned() {
|
|
||||||
return getEntity() != null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -164,7 +145,7 @@ public class CitizensNPC extends AbstractNPC {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setFlyable(boolean flyable) {
|
public void setFlyable(boolean flyable) {
|
||||||
data().setPersistent(NPC.FLYABLE_METADATA, flyable);
|
super.setFlyable(flyable);
|
||||||
updateFlyableState();
|
updateFlyableState();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -225,35 +206,6 @@ public class CitizensNPC extends AbstractNPC {
|
|||||||
return true;
|
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
|
@Override
|
||||||
public void update() {
|
public void update() {
|
||||||
try {
|
try {
|
||||||
|
@ -2,7 +2,11 @@ package net.citizensnpcs.npc;
|
|||||||
|
|
||||||
import gnu.trove.map.hash.TIntObjectHashMap;
|
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.Iterator;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import net.citizensnpcs.api.event.DespawnReason;
|
import net.citizensnpcs.api.event.DespawnReason;
|
||||||
@ -109,6 +113,11 @@ public class CitizensNPCRegistry implements NPCRegistry {
|
|||||||
return npcs.iterator();
|
return npcs.iterator();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterable<NPC> sorted() {
|
||||||
|
return npcs.sorted();
|
||||||
|
}
|
||||||
|
|
||||||
public static class MapNPCCollection implements NPCCollection {
|
public static class MapNPCCollection implements NPCCollection {
|
||||||
private final Map<Integer, NPC> npcs = Maps.newHashMap();
|
private final Map<Integer, NPC> npcs = Maps.newHashMap();
|
||||||
|
|
||||||
@ -131,6 +140,13 @@ public class CitizensNPCRegistry implements NPCRegistry {
|
|||||||
public void remove(int id) {
|
public void remove(int id) {
|
||||||
npcs.remove(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> {
|
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 put(int id, NPC npc);
|
||||||
|
|
||||||
public void remove(int id);
|
public void remove(int id);
|
||||||
|
|
||||||
|
public Iterable<NPC> sorted();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class TroveNPCCollection implements NPCCollection {
|
public static class TroveNPCCollection implements NPCCollection {
|
||||||
@ -163,8 +181,22 @@ public class CitizensNPCRegistry implements NPCRegistry {
|
|||||||
public void remove(int id) {
|
public void remove(int id) {
|
||||||
npcs.remove(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;
|
private static boolean TROVE_EXISTS = false;
|
||||||
static {
|
static {
|
||||||
// allow trove dependency to be optional for debugging purposes
|
// allow trove dependency to be optional for debugging purposes
|
||||||
|
@ -221,14 +221,6 @@ public class NMS {
|
|||||||
: handle instanceof EntityHumanNPC ? ((EntityHumanNPC) handle).getNavigation() : null;
|
: 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) {
|
public static float getSpeedFor(NPC npc) {
|
||||||
if (!npc.isSpawned())
|
if (!npc.isSpawned())
|
||||||
return DEFAULT_SPEED;
|
return DEFAULT_SPEED;
|
||||||
|
Loading…
Reference in New Issue
Block a user