Skip to content

Commit

Permalink
Add NPC#isSorted() and move some CitizensNPC methods into AbstractNPC
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Nov 18, 2013
1 parent adaa1f8 commit f9708d9
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
67 changes: 66 additions & 1 deletion src/main/java/net/citizensnpcs/api/npc/AbstractNPC.java
Expand Up @@ -30,6 +30,7 @@
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.metadata.FixedMetadataValue;

import com.google.common.base.Function;
Expand Down Expand Up @@ -177,6 +178,16 @@ public boolean equals(Object obj) {
return true;
}

@Override
@Deprecated
public LivingEntity getBukkitEntity() {
Entity entity = getEntity();
if (entity == null || entity instanceof LivingEntity) {
return (LivingEntity) entity;
}
throw new IllegalStateException("getBukkitEntity() called on a non-living NPC");
}

@Override
public GoalController getDefaultGoalController() {
return goalController;
Expand All @@ -187,8 +198,9 @@ public SpeechController getDefaultSpeechController() {
// TODO: Remove in future versions.
// This is here to add the Speech trait to any existing NPCs
// that were created pre-SpeechController, if invoked.
if (!hasTrait(Speech.class))
if (!hasTrait(Speech.class)) {
addTrait(Speech.class);
}
return speechController;
}

Expand All @@ -211,6 +223,11 @@ public String getName() {
return parsed;
}

@Override
public NPCRegistry getOwningRegistry() {
return registry;
}

@Override
public <T extends Trait> T getTrait(Class<T> clazz) {
Trait trait = traits.get(clazz);
Expand Down Expand Up @@ -241,11 +258,21 @@ public boolean hasTrait(Class<? extends Trait> trait) {
return traits.containsKey(trait);
}

@Override
public boolean isFlyable() {
return data().get(NPC.FLYABLE_METADATA, false);
}

@Override
public boolean isProtected() {
return data().get(NPC.DEFAULT_PROTECTED_METADATA, true);
}

@Override
public boolean isSpawned() {
return getEntity() != null;
}

@Override
public void load(final DataKey root) {
metadata.loadFrom(root.getRelative("metadata"));
Expand Down Expand Up @@ -328,6 +355,11 @@ public void save(DataKey root) {
removedTraits.clear();
}

@Override
public void setFlyable(boolean flyable) {
data().setPersistent(NPC.FLYABLE_METADATA, flyable);
}

@Override
public void setName(String name) {
this.name = name;
Expand All @@ -349,6 +381,39 @@ public void setProtected(boolean isProtected) {
data().setPersistent(NPC.DEFAULT_PROTECTED_METADATA, isProtected);
}

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() {
entity.setPassenger(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;
Entity entity = getEntity();
while (entity.getVehicle() != null) {
entity = entity.getVehicle();
}
teleport(entity, location, false, 5);
}

public void update() {
for (int i = 0; i < runnables.size(); ++i) {
runnables.get(i).run();
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/citizensnpcs/api/npc/NPC.java
Expand Up @@ -138,6 +138,8 @@ public interface NPC extends Agent, Cloneable {
*/
public Navigator getNavigator();

public NPCRegistry getOwningRegistry();

/**
* If the NPC is not spawned, then this method will return the last known
* location, or null if it has never been spawned. Otherwise, it is
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/net/citizensnpcs/api/npc/NPCRegistry.java
Expand Up @@ -73,4 +73,11 @@ public interface NPCRegistry extends Iterable<NPC> {
* @return Whether the given entity is an NPC
*/
public boolean isNPC(Entity entity);

/**
* Returns a <em>sorted</em> view of this registry, sorted by NPC id.
*
* @return A sorted view of the registry
*/
Iterable<NPC> sorted();
}

0 comments on commit f9708d9

Please sign in to comment.