Interface NPC

All Superinterfaces:
Agent, Cloneable
All Known Implementing Classes:
AbstractNPC, CitizensNPC

public interface NPC extends Agent, Cloneable
Represents an NPC with optional Traits.
  • Method Details

    • addRunnable

      void addRunnable(Runnable runnable)
      Adds a Runnable that will run every tick. Note that removal is not yet supported.
      Parameters:
      runnable - Runnable to be added
    • addTrait

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

      void addTrait(Trait trait)
      Adds a trait to this NPC.
      Parameters:
      trait - Trait to add
    • clone

      NPC clone()
      Returns:
      A clone of the NPC. May not be an exact copy depending on the Traits installed.
    • copy

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

      Returns:
      The metadata store of this NPC.
    • despawn

      boolean despawn()
      Despawns this NPC. This is equivalent to calling despawn(DespawnReason) with DespawnReason.PLUGIN.
      Returns:
      Whether this NPC was able to despawn
    • despawn

      boolean despawn(DespawnReason reason)
      Despawns this NPC.
      Parameters:
      reason - The reason for despawning, for use in NPCDespawnEvent
      Returns:
      Whether this NPC was able to despawn
    • destroy

      void destroy()
      Permanently removes this NPC and all data about it from the registry it's attached to.
    • destroy

      void destroy(CommandSender source)
      Permanently removes this NPC and all data about it from the registry it's attached to.
      Parameters:
      source - The source of the removal
    • faceLocation

      void faceLocation(Location location)
      Faces a given Location if the NPC is spawned.
    • getBlockBreaker

      BlockBreaker getBlockBreaker(Block targetBlock, BlockBreaker.BlockBreakerConfiguration config)
      Creates a BlockBreaker that allows you to break blocks using the Minecraft breaking algorithm.
    • getDefaultGoalController

      GoalController getDefaultGoalController()
      Gets the default GoalController of this NPC.
      Returns:
      Default goal controller
    • getDefaultSpeechController

      SpeechController getDefaultSpeechController()
      Gets the default SpeechController of this NPC.
      Returns:
      Default speech controller
    • getEntity

      Entity getEntity()
      Gets the Bukkit entity associated with this NPC. This may be null if isSpawned() is false.
      Returns:
      Entity associated with this NPC
    • getFullName

      String getFullName()
      Gets the full name of this NPC.
      Returns:
      Full name of this NPC
    • getId

      int getId()
      Gets the unique ID of this NPC. This is not guaranteed to be globally unique across server sessions.
      Returns:
      ID of this NPC
    • getItemProvider

      Supplier<ItemStack> getItemProvider()
      See Also:
    • getMinecraftUniqueId

      UUID getMinecraftUniqueId()
      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 getUniqueId() with the necessary changes for the current mob type.
      Returns:
      The client unique ID.
    • getName

      String getName()
      Gets the name of this NPC with color codes stripped.
      Returns:
      Stripped name of this NPC
    • getNavigator

      Navigator getNavigator()
      Returns:
      The Navigator of this NPC.
    • getOrAddTrait

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

      NPCRegistry getOwningRegistry()
      Returns:
      The NPCRegistry that created this NPC.
    • getRawName

      String getRawName()
    • getStoredLocation

      Location getStoredLocation()
      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 npc.getBukkitEntity().getLocation().
      Returns:
      The stored location, or null if none was found.
    • getTrait

      @Deprecated <T extends Trait> T getTrait(Class<T> trait)
      Deprecated.
      for intransparent naming. Use getOrAddTrait(Class) for the same behavior.
      Gets a trait from the given class. If the NPC does not currently have the trait then it will be created and attached using addTrait(Class) .
      Parameters:
      trait - Trait class
      Returns:
      Trait with the given class
    • getTraitNullable

      <T extends Trait> T getTraitNullable(Class<T> trait)
      Gets the trait instance with the given class. If the NPC does not currently have the trait, null will be returned.
      Parameters:
      trait - Trait class
      Returns:
      Trait with the given class
    • getTraitOptional

      default <T extends Trait> com.google.common.base.Optional<T> getTraitOptional(Class<T> trait)
      Gets the trait instance with the given class. If the NPC does not currently have the trait, Optional.absent() will be returned.
      Parameters:
      trait - Trait class
      Returns:
      Trait with the given class
    • getTraits

      Iterable<Trait> getTraits()
      Returns the currently attached Traits
      Returns:
      An Iterable of the current traits
    • getUniqueId

      UUID getUniqueId()
      Gets the unique id of this NPC. This is guaranteed to be unique for all NPCs.
      Returns:
      The unique id
    • hasTrait

      boolean hasTrait(Class<? extends Trait> trait)
      Checks if this NPC has the given trait.
      Parameters:
      trait - Trait to check
      Returns:
      Whether this NPC has the given trait
    • isFlyable

      boolean isFlyable()
      Returns whether this NPC is flyable or not.
      Returns:
      Whether this NPC is flyable
    • isHiddenFrom

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

      boolean isProtected()
      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.
      Returns:
      Whether this NPC is protected
    • isPushableByFluids

      boolean isPushableByFluids()
      Gets whether this NPC is pushable by fluids.
      Returns:
      Whether this NPC is pushable by fluids
    • isSpawned

      boolean isSpawned()
      Gets whether this NPC is currently spawned.
      Returns:
      Whether this NPC is spawned
    • isUpdating

      boolean isUpdating(NPC.NPCUpdate update)
    • load

      void load(DataKey key)
      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.
      Parameters:
      key - The root data key
    • removeTrait

      void removeTrait(Class<? extends Trait> trait)
      Removes a trait from this NPC.
      Parameters:
      trait - Trait to remove
    • requiresNameHologram

      boolean requiresNameHologram()
    • save

      void save(DataKey key)
      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 load(DataKey).
      Parameters:
      key - The root data key
    • scheduleUpdate

      void scheduleUpdate(NPC.NPCUpdate update)
    • setAlwaysUseNameHologram

      void setAlwaysUseNameHologram(boolean use)
      Sets whether to always use a name hologram instead of the in-built Minecraft name.
      Parameters:
      use - Whether to use a hologram
    • setBukkitEntityType

      void setBukkitEntityType(EntityType type)
      Sets the EntityType of this NPC. The NPC will respawned if currently spawned, or will remain despawned otherwise.
      Parameters:
      type - The new mob type
    • setFlyable

      void setFlyable(boolean flyable)
      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.
      Parameters:
      flyable -
    • setItemProvider

      void setItemProvider(Supplier<ItemStack> supplier)
      For item-type NPCs, set a Supplier of the ItemStack to use when spawning the NPC.
      Parameters:
      supplier - The supplier
    • setMoveDestination

      void setMoveDestination(Location destination)
      Set the target movement destination location to walk towards using Minecraft movement. Should be set every tick.
      Parameters:
      destination - The destination Location
    • setName

      void setName(String name)
      Sets the name of this NPC.
      Parameters:
      name - Name to give this NPC
    • setProtected

      void setProtected(boolean isProtected)
      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);
      Parameters:
      isProtected - Whether the NPC should be protected
    • setSneaking

      void setSneaking(boolean sneaking)
    • setUseMinecraftAI

      void setUseMinecraftAI(boolean use)
      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.
    • shouldRemoveFromPlayerList

      boolean shouldRemoveFromPlayerList()
    • shouldRemoveFromTabList

      boolean shouldRemoveFromTabList()
      Returns:
      Whether to remove the NPC from the tablist. Only applicable for Player-type NPCs.
    • spawn

      boolean spawn(Location location)
      Attempts to spawn this NPC.
      Parameters:
      location - Location to spawn this NPC
      Returns:
      Whether this NPC was able to spawn at the location
    • spawn

      boolean spawn(Location location, SpawnReason reason)
      Attempts to spawn this NPC.
      Parameters:
      location - Location to spawn this NPC
      reason - Reason for spawning
      Returns:
      Whether this NPC was able to spawn at the location
    • teleport

      void teleport(Location location, PlayerTeleportEvent.TeleportCause cause)
      An alternative to npc.getEntity().getLocation() that teleports passengers as well.
      Parameters:
      location - The destination location
      cause - The cause for teleporting
    • useMinecraftAI

      boolean useMinecraftAI()
      Whether the NPC is currently set to use Minecraft AI. Defaults to false.