Skip to content

Commit

Permalink
Add UUID to NPC
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Jan 11, 2014
1 parent cc71874 commit ed75edf
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 49 deletions.
10 changes: 7 additions & 3 deletions src/main/java/net/citizensnpcs/api/CitizensAPI.java
Expand Up @@ -23,9 +23,6 @@ public final class CitizensAPI {
private CitizensAPI() {
}

private static final CitizensAPI instance = new CitizensAPI();
private static ScriptCompiler scriptCompiler;

/**
* Creates a new <em>anonymous</em> {@link NPCRegistry} with its own set of
* {@link NPC}s. This is not stored by the Citizens plugin.
Expand Down Expand Up @@ -93,6 +90,10 @@ public static NPCRegistry getNamedNPCRegistry(String name) {
return getImplementation().getNamedNPCRegistry(name);
}

public static Iterable<NPCRegistry> getNPCRegistries() {
return getImplementation().getNPCRegistries();
}

/**
* Gets the current implementation's <em>default</em> {@link NPCRegistry}.
*
Expand Down Expand Up @@ -201,4 +202,7 @@ public static void shutdown() {
scriptCompiler.interrupt();
scriptCompiler = null;
}

private static final CitizensAPI instance = new CitizensAPI();
private static ScriptCompiler scriptCompiler;
}
10 changes: 6 additions & 4 deletions src/main/java/net/citizensnpcs/api/CitizensPlugin.java
Expand Up @@ -32,17 +32,19 @@ public interface CitizensPlugin extends Plugin {
public NPCSelector getDefaultNPCSelector();

/**
*
*
* @param pluginName
* The plugin name
* @return A NPCRegistry previously created via
* {@link #createNamedNPCRegistry(String)}, or null if not found
*/
public NPCRegistry getNamedNPCRegistry(String name);

public Iterable<NPCRegistry> getNPCRegistries();

/**
* Gets the <em>default</em> {@link NPCRegistry}.
*
*
* @return The NPC registry
*/
public NPCRegistry getNPCRegistry();
Expand All @@ -56,14 +58,14 @@ public interface CitizensPlugin extends Plugin {

/**
* Gets the SpeechFactory.
*
*
* @return Citizens speech factory
*/
public SpeechFactory getSpeechFactory();

/**
* Gets the TraitFactory.
*
*
* @return Citizens trait factory
*/
public TraitFactory getTraitFactory();
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/net/citizensnpcs/api/npc/AbstractNPC.java
Expand Up @@ -3,6 +3,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import javax.annotation.Nullable;

Expand Down Expand Up @@ -74,13 +75,15 @@ public void setPersistent(String key, Object data) {
private final List<Runnable> runnables = Lists.newArrayList();
private final SpeechController speechController = new SimpleSpeechController(this);
protected final Map<Class<? extends Trait>, Trait> traits = Maps.newHashMap();
private UUID uuid;

protected AbstractNPC(int id, String name, NPCRegistry registry) {
protected AbstractNPC(UUID uuid, int id, String name, NPCRegistry registry) {
if (name.length() > 16) {
Messaging.severe("ID", id, "created with name length greater than 16, truncating", name, "to",
name.substring(0, 15));
name = name.substring(0, 15);
}
this.uuid = uuid;
this.id = id;
this.registry = registry;
this.name = name;
Expand Down Expand Up @@ -247,6 +250,11 @@ public Iterable<Trait> getTraits() {
return traits.values();
}

@Override
public UUID getUniqueId() {
return uuid;
}

@Override
public int hashCode() {
final int prime = 31;
Expand Down Expand Up @@ -276,7 +284,7 @@ public boolean isSpawned() {
@Override
public void load(final DataKey root) {
metadata.loadFrom(root.getRelative("metadata"));
// Load traits
uuid = UUID.fromString(root.getString("uuid"));

String traitNames = root.getString("traitnames");
Set<DataKey> keys = Sets.newHashSet(root.getRelative("traits").getSubKeys());
Expand Down Expand Up @@ -334,6 +342,7 @@ public void removeTrait(Class<? extends Trait> traitClass) {
public void save(DataKey root) {
metadata.saveTo(root.getRelative("metadata"));
root.setString("name", getFullName());
root.setString("uuid", uuid.toString());

// Save all existing traits
StringBuilder traitNames = new StringBuilder();
Expand Down
64 changes: 37 additions & 27 deletions src/main/java/net/citizensnpcs/api/npc/NPC.java
@@ -1,5 +1,7 @@
package net.citizensnpcs.api.npc;

import java.util.UUID;

import net.citizensnpcs.api.ai.GoalController;
import net.citizensnpcs.api.ai.Navigator;
import net.citizensnpcs.api.ai.speech.SpeechController;
Expand All @@ -25,15 +27,15 @@ public interface NPC extends Agent, Cloneable {
* Adds a trait to this NPC. This will use the {@link TraitFactory} defined
* for this NPC to construct and attach a trait using
* {@link #addTrait(Trait)}.
*
*
* @param trait
* The class of the trait to add
*/
public void addTrait(Class<? extends Trait> trait);

/**
* Adds a trait to this NPC.
*
*
* @param trait
* Trait to add
*/
Expand All @@ -53,14 +55,14 @@ public interface NPC extends Agent, Cloneable {
/**
* Despawns this NPC. This is equivalent to calling
* {@link #despawn(DespawnReason)} with {@link DespawnReason#PLUGIN}.
*
*
* @return Whether this NPC was able to despawn
*/
public boolean despawn();

/**
* Despawns this NPC.
*
*
* @param reason
* The reason for despawning, for use in {@link NPCDespawnEvent}
* @return Whether this NPC was able to despawn
Expand All @@ -81,7 +83,7 @@ public interface NPC extends Agent, Cloneable {
/**
* Gets the Bukkit entity associated with this NPC. This may be
* <code>null</code> if {@link #isSpawned()} is false.
*
*
* @return Entity associated with this NPC
* @deprecated Use {@link #getEntity()} instead
* @see #getEntity()
Expand All @@ -91,44 +93,44 @@ public interface NPC extends Agent, Cloneable {

/**
* Gets the default {@link GoalController} of this NPC.
*
*
* @return Default goal controller
*/
public GoalController getDefaultGoalController();

/**
* Gets the default {@link SpeechController} of this NPC.
*
*
* @return Default speech controller
*/
public SpeechController getDefaultSpeechController();

/**
* Gets the Bukkit entity associated with this NPC. This may be
* <code>null</code> if {@link #isSpawned()} is false.
*
*
* @return Entity associated with this NPC
*/
public Entity getEntity();

/**
* Gets the full name of this NPC.
*
*
* @return Full name of this NPC
*/
public String getFullName();

/**
* Gets the unique ID of this NPC. This is not guaranteed to be globally
* unique across server sessions.
*
*
* @return ID of this NPC
*/
public int getId();

/**
* Gets the name of this NPC with color codes stripped.
*
*
* @return Stripped name of this NPC
*/
public String getName();
Expand All @@ -144,7 +146,7 @@ public interface NPC extends Agent, Cloneable {
* 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
* equivalent to calling <code>npc.getBukkitEntity().getLocation()</code>.
*
*
* @return The stored location, or <code>null</code> if none was found.
*/
public Location getStoredLocation();
Expand All @@ -153,7 +155,7 @@ public interface NPC extends Agent, Cloneable {
* Gets a trait from the given class. If the NPC does not currently have the
* trait then it will be created and attached using {@link #addTrait(Class)}
* .
*
*
* @param trait
* Trait to get
* @return Trait with the given name
Expand All @@ -162,14 +164,22 @@ public interface NPC extends Agent, Cloneable {

/**
* Returns the currently attached {@link Trait}s
*
*
* @return An Iterable of the current traits
*/
public Iterable<Trait> getTraits();

/**
* Checks if this NPC has the given trait.
* Gets the unique id of this NPC. This is guaranteed to be unique for all
* NPCs.
*
* @return The unique id
*/
public UUID getUniqueId();

/**
* Checks if this NPC has the given trait.
*
* @param trait
* Trait to check
* @return Whether this NPC has the given trait
Expand All @@ -178,22 +188,22 @@ public interface NPC extends Agent, Cloneable {

/**
* Returns whether this NPC is flyable or not.
*
*
* @return Whether this NPC is flyable
*/
public boolean isFlyable();

/**
* Gets whether this NPC is protected from damage, movement and other events
* that players and mobs use to change the entity state of the NPC.
*
*
* @return Whether this NPC is protected
*/
public boolean isProtected();

/**
* Gets whether this NPC is currently spawned.
*
*
* @return Whether this NPC is spawned
*/
public boolean isSpawned();
Expand All @@ -202,15 +212,15 @@ public interface NPC extends Agent, Cloneable {
* Loads the {@link NPC} from the given {@link DataKey}. This reloads all
* traits, respawns the NPC and sets it up for execution. Should not be
* called often.
*
*
* @param key
* The root data key
*/
public void load(DataKey key);

/**
* Removes a trait from this NPC.
*
*
* @param trait
* Trait to remove
*/
Expand All @@ -220,7 +230,7 @@ public interface NPC extends Agent, Cloneable {
* Saves the {@link NPC} to the given {@link DataKey}. This includes all
* metadata, traits, and spawn information that will allow it to respawn at
* a later time via {@link #load(DataKey)}.
*
*
* @param key
* The root data key
*/
Expand All @@ -231,7 +241,7 @@ public interface NPC extends Agent, Cloneable {
* <em>living</em> entity types, with scope for additional types in the
* future. The NPC will respawned if currently spawned, or will remain
* despawned otherwise.
*
*
* @param type
* The new mob type
* @throws IllegalArgumentException
Expand All @@ -243,14 +253,14 @@ public interface NPC extends Agent, Cloneable {
* Sets whether this NPC is <tt>flyable</tt> or not. Note that this is
* intended for normally <em>ground-based</em> entities only - it will
* generally have no effect on mob types that were originally flyable.
*
*
* @param flyable
*/
public void setFlyable(boolean flyable);

/**
* Sets the name of this NPC.
*
*
* @param name
* Name to give this NPC
*/
Expand All @@ -261,15 +271,15 @@ public interface NPC extends Agent, Cloneable {
* NPC as protected or not protected from damage/entity target events.
* Equivalent to
* <code>npc.data().set(NPC.DEFAULT_PROTECTED_METADATA, isProtected);</code>
*
*
* @param isProtected
* Whether the NPC should be protected
*/
public void setProtected(boolean isProtected);

/**
* Attempts to spawn this NPC.
*
*
* @param location
* Location to spawn this NPC
* @return Whether this NPC was able to spawn at the location
Expand All @@ -279,7 +289,7 @@ public interface NPC extends Agent, Cloneable {
/**
* An alternative to {{@link #getBukkitEntity().getLocation()} that
* teleports passengers as well.
*
*
* @param location
* The destination location
* @param cause
Expand Down

0 comments on commit ed75edf

Please sign in to comment.