Class AbstractNPC

java.lang.Object
net.citizensnpcs.api.npc.AbstractNPC
All Implemented Interfaces:
Cloneable, Agent, NPC
Direct Known Subclasses:
CitizensNPC

public abstract class AbstractNPC extends Object implements NPC
  • Field Details

    • coloredNameComponentCache

      protected Object coloredNameComponentCache
    • coloredNameStringCache

      protected String coloredNameStringCache
    • traits

      protected final Map<Class<? extends Trait>,Trait> traits
  • Constructor Details

  • Method Details

    • addRunnable

      public void addRunnable(Runnable runnable)
      Description copied from interface: NPC
      Adds a Runnable that will run every tick. Note that removal is not yet supported.
      Specified by:
      addRunnable in interface NPC
      Parameters:
      runnable - Runnable to be added
    • addTrait

      public void addTrait(Class<? extends Trait> clazz)
      Description copied from interface: NPC
      Adds a trait to this NPC. This will use the TraitFactory defined for this NPC to construct and attach a trait using NPC.addTrait(Trait).
      Specified by:
      addTrait in interface NPC
      Parameters:
      clazz - The class of the trait to add
    • addTrait

      public void addTrait(Trait trait)
      Description copied from interface: NPC
      Adds a trait to this NPC.
      Specified by:
      addTrait in interface NPC
      Parameters:
      trait - Trait to add
    • clone

      public NPC clone()
      Specified by:
      clone in interface NPC
      Overrides:
      clone in class Object
      Returns:
      A clone of the NPC. May not be an exact copy depending on the Traits installed.
    • copy

      public NPC copy()
      Specified by:
      copy in interface NPC
      Returns:
      A clone of the NPC. May not be an exact copy depending on the Traits installed.
    • data

      public MetadataStore data()
      Specified by:
      data in interface NPC
      Returns:
      The metadata store of this NPC.
    • despawn

      public boolean despawn()
      Description copied from interface: NPC
      Despawns this NPC. This is equivalent to calling NPC.despawn(DespawnReason) with DespawnReason.PLUGIN.
      Specified by:
      despawn in interface NPC
      Returns:
      Whether this NPC was able to despawn
    • destroy

      public void destroy()
      Description copied from interface: NPC
      Permanently removes this NPC and all data about it from the registry it's attached to.
      Specified by:
      destroy in interface NPC
    • destroy

      public void destroy(CommandSender source)
      Description copied from interface: NPC
      Permanently removes this NPC and all data about it from the registry it's attached to.
      Specified by:
      destroy in interface NPC
      Parameters:
      source - The source of the removal
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • getDefaultGoalController

      public GoalController getDefaultGoalController()
      Description copied from interface: NPC
      Gets the default GoalController of this NPC.
      Specified by:
      getDefaultGoalController in interface NPC
      Returns:
      Default goal controller
    • getDefaultSpeechController

      public SpeechController getDefaultSpeechController()
      Description copied from interface: NPC
      Gets the default SpeechController of this NPC.
      Specified by:
      getDefaultSpeechController in interface NPC
      Returns:
      Default speech controller
    • getEntityType

      protected EntityType getEntityType()
    • getFullName

      public String getFullName()
      Description copied from interface: NPC
      Gets the full name of this NPC.
      Specified by:
      getFullName in interface NPC
      Returns:
      Full name of this NPC
    • getId

      public int getId()
      Description copied from interface: NPC
      Gets the unique ID of this NPC. This is not guaranteed to be globally unique across server sessions.
      Specified by:
      getId in interface NPC
      Returns:
      ID of this NPC
    • getItemProvider

      public Supplier<ItemStack> getItemProvider()
      Specified by:
      getItemProvider in interface NPC
      See Also:
    • getMinecraftUniqueId

      public UUID getMinecraftUniqueId()
      Description copied from interface: NPC
      For certain mob types (currently, Players) it is beneficial to change the UUID slightly to signal to the client that the mob is an NPC not a real mob. This will return NPC.getUniqueId() with the necessary changes for the current mob type.
      Specified by:
      getMinecraftUniqueId in interface NPC
      Returns:
      The client unique ID.
    • getName

      public String getName()
      Description copied from interface: NPC
      Gets the name of this NPC with color codes stripped.
      Specified by:
      getName in interface NPC
      Returns:
      Stripped name of this NPC
    • getOrAddTrait

      public <T extends Trait> T getOrAddTrait(Class<T> clazz)
      Description copied from interface: NPC
      Gets a trait from the given class. If the NPC does not currently have the trait then it will be created and attached using NPC.addTrait(Class) .
      Specified by:
      getOrAddTrait in interface NPC
      Parameters:
      clazz - Trait to get
      Returns:
      Trait with the given name
    • getOwningRegistry

      public NPCRegistry getOwningRegistry()
      Specified by:
      getOwningRegistry in interface NPC
      Returns:
      The NPCRegistry that created this NPC.
    • getRawName

      public String getRawName()
      Specified by:
      getRawName in interface NPC
    • getTrait

      public <T extends Trait> T getTrait(Class<T> trait)
      Description copied from interface: NPC
      Gets a trait from the given class. If the NPC does not currently have the trait then it will be created and attached using NPC.addTrait(Class) .
      Specified by:
      getTrait in interface NPC
      Parameters:
      trait - Trait class
      Returns:
      Trait with the given class
    • getTraitFor

      protected Trait getTraitFor(Class<? extends Trait> clazz)
    • getTraitNullable

      public <T extends Trait> T getTraitNullable(Class<T> clazz)
      Description copied from interface: NPC
      Gets the trait instance with the given class. If the NPC does not currently have the trait, null will be returned.
      Specified by:
      getTraitNullable in interface NPC
      Parameters:
      clazz - Trait class
      Returns:
      Trait with the given class
    • getTraits

      public Iterable<Trait> getTraits()
      Description copied from interface: NPC
      Returns the currently attached Traits
      Specified by:
      getTraits in interface NPC
      Returns:
      An Iterable of the current traits
    • getUniqueId

      public UUID getUniqueId()
      Description copied from interface: NPC
      Gets the unique id of this NPC. This is guaranteed to be unique for all NPCs.
      Specified by:
      getUniqueId in interface NPC
      Returns:
      The unique id
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • hasTrait

      public boolean hasTrait(Class<? extends Trait> trait)
      Description copied from interface: NPC
      Checks if this NPC has the given trait.
      Specified by:
      hasTrait in interface NPC
      Parameters:
      trait - Trait to check
      Returns:
      Whether this NPC has the given trait
    • isFlyable

      public boolean isFlyable()
      Description copied from interface: NPC
      Returns whether this NPC is flyable or not.
      Specified by:
      isFlyable in interface NPC
      Returns:
      Whether this NPC is flyable
    • isHiddenFrom

      public boolean isHiddenFrom(Player player)
      Description copied from interface: NPC
      Returns whether the given player can see the NPC (i.e. receive packets about it).
      Specified by:
      isHiddenFrom in interface NPC
      Parameters:
      player - The player to check
      Returns:
      Whether the NPC is hidden from the player
    • isProtected

      public boolean isProtected()
      Description copied from interface: NPC
      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.
      Specified by:
      isProtected in interface NPC
      Returns:
      Whether this NPC is protected
    • isPushableByFluids

      public boolean isPushableByFluids()
      Description copied from interface: NPC
      Gets whether this NPC is pushable by fluids.
      Specified by:
      isPushableByFluids in interface NPC
      Returns:
      Whether this NPC is pushable by fluids
    • load

      public void load(DataKey root)
      Description copied from interface: NPC
      Loads the NPC from the given DataKey. This reloads all traits, respawns the NPC and sets it up for execution. Should not be called often.
      Specified by:
      load in interface NPC
      Parameters:
      root - The root data key
    • removeTrait

      public void removeTrait(Class<? extends Trait> traitClass)
      Description copied from interface: NPC
      Removes a trait from this NPC.
      Specified by:
      removeTrait in interface NPC
      Parameters:
      traitClass - Trait to remove
    • requiresNameHologram

      public boolean requiresNameHologram()
      Specified by:
      requiresNameHologram in interface NPC
    • save

      public void save(DataKey root)
      Description copied from interface: NPC
      Saves the NPC to the given DataKey. This includes all metadata, traits, and spawn information that will allow it to respawn at a later time via NPC.load(DataKey).
      Specified by:
      save in interface NPC
      Parameters:
      root - The root data key
    • setAlwaysUseNameHologram

      public void setAlwaysUseNameHologram(boolean use)
      Description copied from interface: NPC
      Sets whether to always use a name hologram instead of the in-built Minecraft name.
      Specified by:
      setAlwaysUseNameHologram in interface NPC
      Parameters:
      use - Whether to use a hologram
    • setFlyable

      public void setFlyable(boolean flyable)
      Description copied from interface: NPC
      Sets whether this NPC is flyable or not. Note that this is intended for normally ground-based entities only - it will generally have no effect on mob types that were originally flyable.
      Specified by:
      setFlyable in interface NPC
      Parameters:
      flyable -
    • setItemProvider

      public void setItemProvider(Supplier<ItemStack> provider)
      Description copied from interface: NPC
      For item-type NPCs, set a Supplier of the ItemStack to use when spawning the NPC.
      Specified by:
      setItemProvider in interface NPC
      Parameters:
      provider - The supplier
    • setName

      public void setName(String name)
      Description copied from interface: NPC
      Sets the name of this NPC.
      Specified by:
      setName in interface NPC
      Parameters:
      name - Name to give this NPC
    • setNameInternal

      protected void setNameInternal(String name)
    • setProtected

      public void setProtected(boolean isProtected)
      Description copied from interface: NPC
      A helper method to set the NPC as protected or not protected from damage/entity target events. Equivalent to npc.data().set(NPC.Metadata#DEFAULT_PROTECTED_METADATA, isProtected);
      Specified by:
      setProtected in interface NPC
      Parameters:
      isProtected - Whether the NPC should be protected
    • setUseMinecraftAI

      public void setUseMinecraftAI(boolean use)
      Description copied from interface: NPC
      Set the NPC to use Minecraft AI where possible. Note that the NPC may not always behave exactly like a Minecraft mob would because of additional Citizens APIs.
      Specified by:
      setUseMinecraftAI in interface NPC
    • teleport

      public void teleport(Location location, PlayerTeleportEvent.TeleportCause cause)
      Description copied from interface: NPC
      An alternative to npc.getEntity().getLocation() that teleports passengers as well.
      Specified by:
      teleport in interface NPC
      Parameters:
      location - The destination location
      cause - The cause for teleporting
    • unloadEvents

      protected void unloadEvents()
    • update

      public void update()
    • useMinecraftAI

      public boolean useMinecraftAI()
      Description copied from interface: NPC
      Whether the NPC is currently set to use Minecraft AI. Defaults to false.
      Specified by:
      useMinecraftAI in interface NPC