Class Npc

java.lang.Object
net.risingworld.api.objects.Npc

public final class Npc extends Object
Represents an npc, i.e animals, humans, monsters etc.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Deletes the npc.
    void
    Removes an attribute.
    int
    Gets the "age" of the npc in seconds, i.e how many seconds passed since the npc was spawned.
    Gets the value to which the specified key is mapped, or null if no attribute is stored for that key.
    Gets a Clothes object which contains the clothes the npc is currently wearing.
    Gets the definition for this npc.
    long
    Gets the unique global ID of the npc.
    int
    Gets the optional "group ID" of the npc.
    int
    Gets the current health of the npc.
    Gets the "hostile player".
    int
    Gets the current hunger level of the npc.
    Gets the optional custom name of the npc.
    Gets the nearest player, or more precisely, the player which is considered the "nearest player" by the npc.
    Gets a Vector3f containing the current global x, y and z coordinate of the npc.
    Gets the rider of the npc, if present.
     
    Gets a Skin object which represents the appearance of the npc, i.e gender, haircut, skin color, hair color etc.
    int
    Gets the current thirst level of the npc.
    short
    Gets the type ID of the npc, i.e the internal ID of the npc in the definitions database.
    int
    Gets the variant of the npc.
    Gets a normalized vector from the npc position to the position of the nearest player.
    Gets the current view direction of the npc as a normalized vector.
    boolean
    Gets whether or not the provided attribute exists.
    boolean
    Gets whether or not the provided attribute exists (also checks if it has a specific type).
    boolean
    Gets whether or not the npc is currently alerted/alarmed.
    boolean
    Gets whether or not the npc is dead.
    boolean
    Gets whether or not the npc is invincible, this means it cannot be killed by any players or other npcs.
    boolean
    Gets whether the npc is currently in water or not.
    boolean
    Gets whether or not the npc is currently "locked".
    boolean
    Gets whether or not the npc is currently sleeping.
    boolean
    Gets whether the npc is "transient" or not.
    boolean
    Gets whether the npc is currently under water or not.
    void
    Kills the npc instantly.
    void
    moveTo(float x, float y, float z)
    Forces the npc to move to the provided position.
    void
    moveTo(Vector3f position)
    Forces the npc to move to the provided position.
    void
    setAge(int age)
    Sets the "age" of the npc.
    void
    setAlerted(boolean set)
    Changes the "alert" state of the npc.
    void
    setAlerted(boolean set, int duration)
    Changes the "alert" state of the npc.
    void
    setAttribute(String key, Object value)
    Stores an attribute for this npc.
    void
    setGroupID(int groupID)
    Sets the "group ID" for the npc.
    void
    setHealth(int health)
    Sets the health of the npc.
    void
    setHunger(int hunger)
    Sets the hunger level of the npc.
    void
    setInvincible(boolean set)
    Enables/disables invincibility for this npc.
    void
    setLocked(boolean set)
    Locks or unlocks the npc.
    void
    Sets the custom name of this npc.
    void
    setPosition(float x, float y, float z)
    Sets the global position of the npc (teleports the npc).
    void
    Sets the global position of the npc (teleports the npc).
    void
    setRotation(float pitch, float yaw, float roll)
     
    void
    setRotation(Quaternion quaternion)
     
    void
    setThirst(int thirst)
    Sets the thirst level of the npc.
    void
    setViewDirection(float dirx, float diry, float dirz)
    Sets the view direction of the npc.
    void
    Sets the view direction of the npc.
    boolean
    transform(short typeID, int variant)
    Turns this npc into another npc.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getGlobalID

      public long getGlobalID()
      Gets the unique global ID of the npc.
      Returns:
      the unique ID of npc.
    • getDefinition

      public Npcs.NpcDefinition getDefinition()
      Gets the definition for this npc.
      Returns:
      the npc definition.
      Example: Check if a particular npc is a human
      1Npc npc = World.getNpc(...);
      2if(npc != null) {
      3 Npcs.NpcDefinition def = npc.getDefinition();
      4 if(def.type == Npcs.Type.Human) {
      5 System.out.println("NPC is a human!");
      6 }
      7}
    • getTypeID

      public short getTypeID()
      Gets the type ID of the npc, i.e the internal ID of the npc in the definitions database.
      Returns:
      the type ID of the npc.
    • getVariant

      public int getVariant()
      Gets the variant of the npc. By default it's 0. Only certain npc can have different variations. To find out if an npc has multiple variants, check if Npcs.NpcDefinition.variations is greater than 0.
      Returns:
      the variant of the npc. Default variant is 0.
    • getGroupID

      public int getGroupID()
      Gets the optional "group ID" of the npc. A group is basically a herd or a squad. Only certain npc are member of a group/squad/herd (e.g bandits or animals which spawned in a herd).
      If a group member is alerted, all other group members will be alerted too (unless they're too far away).
      Returns:
      the group ID of the npc. -1 if no group is set.
    • setGroupID

      public void setGroupID(int groupID)
      Sets the "group ID" for the npc. A group is basically a herd or a squad.
      If a group member is alerted, all other group members will be alerted too (unless they're too far away).
      To get a new, unused group ID, you can call World.getNextNpcGroupID().
      Parameters:
      groupID - the group ID for the npc. Set -1 to set the group to "none"
      See Also:
    • isTransient

      public boolean isTransient()
      Gets whether the npc is "transient" or not. If it is transient, it will not be stored in the database (i.e it can be considered a temporary npc then which will disappear after server restart).
      Returns:
      true if the npc is transient, false if not.
    • getName

      public String getName()
      Gets the optional custom name of the npc. By default npcs don't have a custom name, unless it was set by the player.

      Note: This is not to be confused this with the internal name of the npc type (e.g "cow", "bear" etc). If you want to get that name, use the npc definition instead, e.g String name = npc.getDefinition().name;
      Returns:
      the optional custom npc name, or null if no custom name was set for this npc.
    • setName

      public void setName(String name)
      Sets the custom name of this npc. When setting a name, it will appear as nametag above the npc (visible for all players).
      Parameters:
      name - the custom name you want to set for the npc. Provide null if you want to remove the custom name (which also removes the nametag above the npc).
    • getAge

      public int getAge()
      Gets the "age" of the npc in seconds, i.e how many seconds passed since the npc was spawned. Some npcs only live for a certain amount of time.
      Returns:
      the age of the npc (in seconds).
      Example: Kill npc if it's older than 2 hours
      1//Get current age of the npc, in seconds
      2int age = npc.getAge();
      3
      4//2 hours == 120 minutes == 7200 seconds
      5if (age > (2 * 60 * 60)) {
      6 npc.kill();
      7}
    • setAge

      public void setAge(int age)
      Sets the "age" of the npc. The age describes how many seconds the npc is already alive (i.e how many seconds passed since the npc was spawned).
      Parameters:
      age - the new age, in seconds.
    • getNearestPlayer

      public Player getNearestPlayer()
      Gets the nearest player, or more precisely, the player which is considered the "nearest player" by the npc.
      Returns:
      the nearest player, or null if no player is in proximity of the npc.
    • getHostilePlayer

      public Player getHostilePlayer()
      Gets the "hostile player". For example, once a player attacks the npc, he's usually considered "hostile" by the npc, so the npc either attacks the player or tries to escape. Once the distance between the player and the npc is big enough, the "hostile player" will be reset (so the player is no longer considered hostile).
      Returns:
      the "hostile player" (see above), or null if there is currently no hostile player.
    • isLocked

      public boolean isLocked()
      Gets whether or not the npc is currently "locked". If an npc is locked, it will never move away or attack the player. Basically it stands still at the current location (although it can still fall down if spawned in the air, for example).
      Returns:
      true if the npc is "locked", false if not.
    • setLocked

      public void setLocked(boolean set)
      Locks or unlocks the npc. If an npc is locked, it will never move away or attack the player. Basically it stands still at the current location (although it can still fall down if spawned in the air, for example).
      This setting is saved to the npc database, so it's still active after a server restart.
      Parameters:
      set - true to lock the npc (so it will never move away), false to unlock it (default behavior).
    • isInvincible

      public boolean isInvincible()
      Gets whether or not the npc is invincible, this means it cannot be killed by any players or other npcs.
      Returns:
      true if the npc is invincible, false if not (default).
    • setInvincible

      public void setInvincible(boolean set)
      Enables/disables invincibility for this npc. If the npc is invincible, it cannot be killed by any players or other npcs.
      This setting is saved to the npc database, so it's still active after a server restart!
      Parameters:
      set - true to make this npc invincible, false to make it vulnerable again.
    • getRider

      public Object getRider()
      Gets the rider of the npc, if present. This only works for "Mounts", i.e if npc.getDefinition().type == Npcs.Type.Mount. If there is no rider (or if the npc isn't a mount), this function returns null.
      Returns:
      the rider of this npc (a Player, if present).
      See Also:
    • transform

      public boolean transform(short typeID, int variant)
      Turns this npc into another npc.
      Parameters:
      typeID - the target npc type ID.
      variant - the optional target variant.
      Returns:
      true if the npc type/variant could be changed, false if not (e.g if the target type ID is invalid).
    • getPosition

      public Vector3f getPosition()
      Gets a Vector3f containing the current global x, y and z coordinate of the npc.
      Returns:
      the global npc position as a Vector3f (float).
    • setPosition

      public void setPosition(Vector3f position)
      Sets the global position of the npc (teleports the npc).
      Parameters:
      position - A Vector3f (float) containing the global target position.
    • setPosition

      public void setPosition(float x, float y, float z)
      Sets the global position of the npc (teleports the npc).
      Parameters:
      x - global X coordinate to set (horizontally).
      y - global Y coordinate to set (vertically).
      z - global Z coordinate to set (horizontally).
    • moveTo

      public void moveTo(Vector3f position)
      Forces the npc to move to the provided position. The regular npc behaviour still remains active, this means it will walk to that position if possible. The npc can't reach the position if the way is obstructed, for example. If the npc is alerted, it will run to the position (instead of walking).
      Parameters:
      position - the target position. The y coordinate is usually irrelevant (unless it's a flying or swimming npc), because the npc will try to walk to that position.
    • moveTo

      public void moveTo(float x, float y, float z)
      Forces the npc to move to the provided position. The regular npc behaviour still remains active, this means it will walk to that position if possible. The npc can't reach the position if the way is obstructed, for example. If the npc is alerted, it will run to the position (instead of walking).
      Parameters:
      x - the target position X coordinate.
      y - the target position Y coordinate. Usually it's irrelevant (unless it's a flying or swimming npc), because the npc will try to walk to that position.
      z - the target position Z coordinate.
    • getRotation

      public Quaternion getRotation()
    • setRotation

      public void setRotation(Quaternion quaternion)
    • setRotation

      public void setRotation(float pitch, float yaw, float roll)
    • getViewDirection

      public Vector3f getViewDirection()
      Gets the current view direction of the npc as a normalized vector.
      Returns:
      the view direction as a Vector3f (float).
    • setViewDirection

      public void setViewDirection(Vector3f direction)
      Sets the view direction of the npc. You can use this function to force the npc to look into a certain direction.
      Parameters:
      direction - the new view direction as a Vector3f (float). Make sure to use a unit/normalized vector.
    • setViewDirection

      public void setViewDirection(float dirx, float diry, float dirz)
      Sets the view direction of the npc. You can use this function to force the npc to look into a certain direction.
      Parameters:
      dirx - X component of the new view direction vector.
      diry - Y component of the new view direction vector.
      dirz - Z component of the new view direction vector.
    • getVectorToNearestPlayer

      public Vector3f getVectorToNearestPlayer()
      Gets a normalized vector from the npc position to the position of the nearest player. If there is no player, null is returned.
      Returns:
      a vector from the npc position to the position of the nearest player (as an unit/normalized vector).
    • isInWater

      public boolean isInWater()
      Gets whether the npc is currently in water or not.
      Returns:
      true if the npc is in water, false if not.
    • isUnderwater

      public boolean isUnderwater()
      Gets whether the npc is currently under water or not.
      Returns:
      true if the npc is under water, false if not.
    • setAlerted

      public void setAlerted(boolean set)
      Changes the "alert" state of the npc. If an npc is alerted/alarmed, it starts running, and if a player is in his proximity, it will either attack the player (if it's a hostile animal) or flee from him. Setting the alert state of an npc to true, this state will last for around 20 seconds.
      Please keep in mind that the game may always override the alert state.
      Parameters:
      set - true to set the npc alerted/alarmed, false to disable the alert state.
      Example: Alert all npcs closer than 50 block to the player
      1//Get all npcs in range
      2Npc[] ncps = World.getAllNpcsInRange(player.getPosition(), 50);
      3
      4//Go through npcs and set alert state
      5for (Npc npc : npcs) {
      6 npc.setAlerted(true);
      7}
    • setAlerted

      public void setAlerted(boolean set, int duration)
      Changes the "alert" state of the npc. If an npc is alerted/alarmed, it starts running, and if a player is in his proximity, it will either attack the player (if it's a hostile animal) or flee from him. By setting the duration parameter, you can determine how long the npc will stay alerted.

      Please keep in mind that the game may always override the alert state or the alert duration.
      Parameters:
      set - true to set the npc alerted/alarmed, false to disable the alert state.
      duration - the duration (in seconds) how long the npc will stay alerted. Does nothing when setting the alert state to false.
    • isAlerted

      public boolean isAlerted()
      Gets whether or not the npc is currently alerted/alarmed.
      Returns:
      true if the npc is alerted, false if not.
    • getHealth

      public int getHealth()
      Gets the current health of the npc.
      Returns:
      the current health of the npc.
    • setHealth

      public void setHealth(int health)
      Sets the health of the npc.
      Parameters:
      health - the new npc health.
      Example: Set health back to default
      1//To find out the default health, get the npc definition
      2Npcs.NpcDefinition def = npc.getDefinition();
      3
      4//Set health
      5npc.setHealth(def.health);
    • getHunger

      public int getHunger()
      Gets the current hunger level of the npc. 100 means the npc is not hungry, 0 means the npc is starving.
      Returns:
      the current hunger level of the npc.
    • setHunger

      public void setHunger(int hunger)
      Sets the hunger level of the npc. 100 means the npc is not hungry, 0 means the npc is starving.
      Parameters:
      hunger - the new hunger level.
    • getThirst

      public int getThirst()
      Gets the current thirst level of the npc. 100 means the npc is not thirsty, 0 means the npc is dying of dehydration.
      Returns:
      the current thirst level of the npc.
    • setThirst

      public void setThirst(int thirst)
      Sets the thirst level of the npc. 100 means the npc is not thirsty, 0 means the npc is dying of dehydration.
      Parameters:
      thirst - the new thirst level.
    • isSleeping

      public boolean isSleeping()
      Gets whether or not the npc is currently sleeping.
      Returns:
      true if the npc is sleeping, false if not.
    • isDead

      public boolean isDead()
      Gets whether or not the npc is dead.
      Returns:
      true if the npc is dead, false if not.
    • kill

      public void kill()
      Kills the npc instantly. Spawns a dead body and removes the npc instance from the world
    • delete

      public void delete()
      Deletes the npc. Similar to kill(), but without spawning a dead body. NOTE: There is no need to call delete after calling kill()! Doing so does not remove the dead body of the npc (which was spawned by kill()). If you want to remove an npc without spawning a corpse, only call delete()
    • getSkin

      public Skin getSkin()
      Gets a Skin object which represents the appearance of the npc, i.e gender, haircut, skin color, hair color etc.
      Only certain npc have a skin object (e.g humans)! Other npcs - like animals - usually don't have a Skin object.
      Returns:
      a Skin object representing the visual appearance of the npc.
    • getClothes

      public Clothes getClothes()
      Gets a Clothes object which contains the clothes the npc is currently wearing.
      Only certain npc have a clothes object! Human npc can wear the same clothes as the player, while other npc can only wear certain clothes (like mounts, which can only wear saddles etc) or no clothes at all (e.g animals).
      Returns:
      a Clothes object representing the clothes of the npc.
    • setAttribute

      public void setAttribute(String key, Object value)
      Stores an attribute for this npc. You can store any type of object. Note that all attributes will be reset upon server restart.
      Parameters:
      key - the name of the attribute.
      value - the value/object you want to store.
    • getAttribute

      public Object getAttribute(String key)
      Gets the value to which the specified key is mapped, or null if no attribute is stored for that key.
      Parameters:
      key - the name of the attribute.
      Returns:
      the attribute that is stored for that key, or null of the key does not exists.
    • hasAttribute

      public boolean hasAttribute(String key)
      Gets whether or not the provided attribute exists.
      Parameters:
      key - the name of the attribute.
      Returns:
      true if the attribute exists, false if not.
    • hasAttribute

      public boolean hasAttribute(String key, Class type)
      Gets whether or not the provided attribute exists (also checks if it has a specific type).
      Parameters:
      key - the name of the attribute.
      type - the type of the attribute
      Returns:
      true if the attribute exists and if it has the desired type, false if not.
    • deleteAttribute

      public void deleteAttribute(String key)
      Removes an attribute. Does nothing if the attribute doesn't exist.
      Parameters:
      key - the name of the attribute you want to delete.