Class Entity

java.lang.Object
net.minestom.server.entity.Entity
All Implemented Interfaces:
DataContainer, EventHandler, PermissionHandler, Viewable
Direct Known Subclasses:
ExperienceOrb, LivingEntity, ObjectEntity

public abstract class Entity
extends java.lang.Object
implements Viewable, EventHandler, DataContainer, PermissionHandler
Could be a player, a monster, or an object.

To create your own entity you probably want to extends ObjectEntity or EntityCreature instead.

  • Field Details

  • Constructor Details

  • Method Details

    • scheduleNextTick

      public void scheduleNextTick​(@NotNull java.util.function.Consumer<Entity> callback)
      Schedules a task to be run during the next entity tick. It ensures that the task will be executed in the same thread as the entity (depending of the ThreadProvider).
      Parameters:
      callback - the task to execute during the next entity tick
    • getEntity

      @Nullable public static Entity getEntity​(int id)
      Gets an entity based on its id (from getEntityId()).

      Entity id are unique server-wide.

      Parameters:
      id - the entity unique id
      Returns:
      the entity having the specified id, null if not found
    • update

      public abstract void update​(long time)
      Called each tick.
      Parameters:
      time - time of the update in milliseconds
    • spawn

      public abstract void spawn()
      Called when a new instance is set.
    • isOnGround

      public boolean isOnGround()
    • shouldSendVelocityUpdate

      protected boolean shouldSendVelocityUpdate​(long time)
      Checks if now is a good time to send a velocity update packet.
      Parameters:
      time - the current time in milliseconds
      Returns:
      true if the velocity update packet should be send
    • getVelocityUpdatePeriod

      public long getVelocityUpdatePeriod()
      Gets the period, in ms, between two velocity update packets.
      Returns:
      period, in ms, between two velocity update packets
    • setVelocityUpdatePeriod

      public void setVelocityUpdatePeriod​(long velocityUpdatePeriod)
      Sets the period, in ms, between two velocity update packets.
      Parameters:
      velocityUpdatePeriod - period, in ms, between two velocity update packets
    • teleport

      public void teleport​(@NotNull Position position, @Nullable java.lang.Runnable callback)
      Teleports the entity only if the chunk at position is loaded or if Instance.hasEnabledAutoChunkLoad() returns true.
      Parameters:
      position - the teleport position
      callback - the optional callback executed, even if auto chunk is not enabled
    • teleport

      public void teleport​(@NotNull Position position)
    • setView

      public void setView​(float yaw, float pitch)
      Changes the view of the entity.
      Parameters:
      yaw - the new yaw
      pitch - the new pitch
    • setView

      public void setView​(@NotNull Position position)
      Changes the view of the entity. Only the yaw and pitch are used.
      Parameters:
      position - the new view
    • isAutoViewable

      public boolean isAutoViewable()
      When set to true, the entity will automatically get new viewers when they come too close This can be use to complete control over which player can see it, without having to deal with raw packets.

      True by default for all entities. When set to false, it is important to mention that the players will not be removed automatically from its viewers list, you would have to do that manually when being too far.

      Returns:
      true if the entity is automatically viewable for close players, false otherwise
    • setAutoViewable

      public void setAutoViewable​(boolean autoViewable)
      Makes the entity auto viewable or only manually.
      Parameters:
      autoViewable - should the entity be automatically viewable for close players
      See Also:
      isAutoViewable()
    • addViewer

      public boolean addViewer​(@NotNull Player player)
      Description copied from interface: Viewable
      Adds a viewer.
      Specified by:
      addViewer in interface Viewable
      Parameters:
      player - the viewer to add
      Returns:
      true if the player has been added, false otherwise (could be because he is already a viewer)
    • removeViewer

      public boolean removeViewer​(@NotNull Player player)
      Description copied from interface: Viewable
      Removes a viewer.
      Specified by:
      removeViewer in interface Viewable
      Parameters:
      player - the viewer to remove
      Returns:
      true if the player has been removed, false otherwise (could be because he was not a viewer)
    • getViewers

      @NotNull public java.util.Set<Player> getViewers()
      Description copied from interface: Viewable
      Gets all the viewers of this viewable element.
      Specified by:
      getViewers in interface Viewable
      Returns:
      A Set containing all the element's viewers
    • getData

      public Data getData()
      Description copied from interface: DataContainer
      Gets the Data of this container.

      A DataContainer data is always optional, meaning that this will be null if no data has been defined.

      Specified by:
      getData in interface DataContainer
      Returns:
      the Data of this container, can be null
    • setData

      public void setData​(@Nullable Data data)
      Description copied from interface: DataContainer
      Sets the Data of this container.

      Default implementations are DataImpl and SerializableDataImpl depending on your use-case.

      Specified by:
      setData in interface DataContainer
      Parameters:
      data - the new Data of this container, null to remove it
    • getAllPermissions

      @NotNull public java.util.Collection<Permission> getAllPermissions()
      Description copied from interface: PermissionHandler
      Returns all permissions associated to this command sender. The returned collection should be modified only by subclasses.
      Specified by:
      getAllPermissions in interface PermissionHandler
      Returns:
      the permissions of this command sender.
    • tick

      public void tick​(long time)
      Updates the entity, called every tick.

      Ignored if getInstance() returns null.

      Parameters:
      time - the update time in milliseconds
    • sendVelocityPacket

      public void sendVelocityPacket()
      Equivalent to sendPacketsToViewers(getVelocityPacket());.
    • getAliveTicks

      public long getAliveTicks()
      Gets the number of ticks this entity has been active for.
      Returns:
      the number of ticks this entity has been active for
    • handleVoid

      protected void handleVoid()
      How does this entity handle being in the void?
    • getEventCallbacksMap

      @NotNull public java.util.Map<java.lang.Class<? extends Event>,​java.util.Collection<EventCallback>> getEventCallbacksMap()
      Description copied from interface: EventHandler
      Gets a Map containing all the listeners assigned to a specific Event type.
      Specified by:
      getEventCallbacksMap in interface EventHandler
      Returns:
      a Map with all the listeners
    • callEvent

      public <E extends Event> void callEvent​(@NotNull java.lang.Class<E> eventClass, @NotNull E event)
      Description copied from interface: EventHandler
      Calls the specified Event with all the assigned EventCallback.

      Events are always called in the current thread.

      Specified by:
      callEvent in interface EventHandler
      Type Parameters:
      E - the event type
      Parameters:
      eventClass - the event class
      event - the event object
    • getEntityId

      public int getEntityId()
      Each entity has an unique id (server-wide) which will change after a restart.
      Returns:
      the unique entity id
      See Also:
      to retrive an entity based on its id
    • getEntityType

      public EntityType getEntityType()
      Returns the entity type.
      Returns:
      the entity type
    • getUuid

      @NotNull public java.util.UUID getUuid()
      Gets the entity UUID.
      Returns:
      the entity unique id
    • setUuid

      protected void setUuid​(@NotNull java.util.UUID uuid)
      Changes the internal entity UUID, mostly unsafe.
      Parameters:
      uuid - the new entity uuid
    • isActive

      public boolean isActive()
      Returns false just after instantiation, set to true after calling setInstance(Instance).
      Returns:
      true if the entity has been linked to an instance, false otherwise
    • getBoundingBox

      @NotNull public BoundingBox getBoundingBox()
      Is used to check collision with coordinates or other blocks/entities.
      Returns:
      the entity bounding box
    • setBoundingBox

      public void setBoundingBox​(float x, float y, float z)
      Changes the internal entity bounding box.

      WARNING: this does not change the entity hit-box which is client-side.

      Parameters:
      x - the bounding box X size
      y - the bounding box Y size
      z - the bounding box Z size
    • getChunk

      @Nullable public Chunk getChunk()
      Convenient method to get the entity current chunk.
      Returns:
      the entity chunk, can be null even if unlikely
    • getInstance

      @Nullable public Instance getInstance()
      Gets the entity current instance.
      Returns:
      the entity instance, can be null if the entity doesn't have an instance yet
    • setInstance

      public void setInstance​(@NotNull Instance instance)
      Changes the entity instance.
      Parameters:
      instance - the new instance of the entity
      Throws:
      java.lang.NullPointerException - if instance is null
      java.lang.IllegalStateException - if instance has not been registered in InstanceManager
    • getVelocity

      @NotNull public Vector getVelocity()
      Gets the entity current velocity.
      Returns:
      the entity current velocity
    • setVelocity

      public void setVelocity​(@NotNull Vector velocity)
      Changes the entity velocity and calls EntityVelocityEvent.

      The final velocity can be cancelled or modified by the event.

      Parameters:
      velocity - the new entity velocity
    • hasVelocity

      public boolean hasVelocity()
      Gets if the entity currently has a velocity applied.
      Returns:
      true if velocity is not set to 0
    • setGravity

      public void setGravity​(float gravityDragPerTick)
      Changes the gravity of the entity.
      Parameters:
      gravityDragPerTick - the gravity drag per tick
    • getDistance

      public float getDistance​(@NotNull Entity entity)
      Gets the distance between two entities.
      Parameters:
      entity - the entity to get the distance from
      Returns:
      the distance between this and entity
    • getVehicle

      @Nullable public Entity getVehicle()
      Gets the entity vehicle or null.
      Returns:
      the entity vehicle, or null if there is not any
    • addPassenger

      public void addPassenger​(@NotNull Entity entity)
      Adds a new passenger to this entity.
      Parameters:
      entity - the new passenger
      Throws:
      java.lang.NullPointerException - if entity is null
      java.lang.IllegalStateException - if getInstance() returns null
    • removePassenger

      public void removePassenger​(@NotNull Entity entity)
      Removes a passenger to this entity.
      Parameters:
      entity - the passenger to remove
      Throws:
      java.lang.NullPointerException - if entity is null
      java.lang.IllegalStateException - if getInstance() returns null
    • hasPassenger

      public boolean hasPassenger()
      Gets if the entity has any passenger.
      Returns:
      true if the entity has any passenger, false otherwise
    • getPassengers

      @NotNull public java.util.Set<Entity> getPassengers()
      Gets the entity passengers.
      Returns:
      an unmodifiable list containing all the entity passengers
    • getPassengersPacket

      @NotNull protected SetPassengersPacket getPassengersPacket()
    • triggerStatus

      public void triggerStatus​(byte status)
      Entity statuses can be found here.
      Parameters:
      status - the status to trigger
    • isOnFire

      public boolean isOnFire()
      Gets if the entity is on fire.
      Returns:
      true if the entity is in fire, false otherwise
    • setOnFire

      public void setOnFire​(boolean fire)
      Sets the entity in fire visually.

      WARNING: if you want to apply damage or specify a duration, see LivingEntity.setFireForDuration(int, TimeUnit).

      Parameters:
      fire - should the entity be set in fire
    • isInvisible

      public boolean isInvisible()
      Gets if the entity is invisible or not.
      Returns:
      true if the entity is invisible, false otherwise
    • setInvisible

      public void setInvisible​(boolean invisible)
      Changes the internal invisible value and send a EntityMetaDataPacket to make visible or invisible the entity to its viewers.
      Parameters:
      invisible - true to set the entity invisible, false otherwise
    • isGlowing

      public boolean isGlowing()
      Gets if the entity is glowing or not.
      Returns:
      true if the entity is glowing, false otherwise
    • setGlowing

      public void setGlowing​(boolean glowing)
      Sets or remove the entity glowing effect.
      Parameters:
      glowing - true to make the entity glows, false otherwise
    • getCustomName

      public ColoredText getCustomName()
      Gets the entity custom name.
      Returns:
      the custom name of the entity, null if there is not
    • setCustomName

      public void setCustomName​(ColoredText customName)
      Changes the entity custom name.
      Parameters:
      customName - the custom name of the entity, null to remove it
    • isCustomNameVisible

      public boolean isCustomNameVisible()
      Gets the custom name visible metadata field.
      Returns:
      true if the custom name is visible, false otherwise
    • setCustomNameVisible

      public void setCustomNameVisible​(boolean customNameVisible)
      Changes the internal custom name visible field and send a EntityMetaDataPacket to update the entity state to its viewers.
      Parameters:
      customNameVisible - true to make the custom name visible, false otherwise
    • isSilent

      public boolean isSilent()
    • setSilent

      public void setSilent​(boolean silent)
    • setNoGravity

      public void setNoGravity​(boolean noGravity)
      Changes the noGravity metadata field and change the gravity behaviour accordingly.
      Parameters:
      noGravity - should the entity ignore gravity
    • hasNoGravity

      public boolean hasNoGravity()
      Gets the noGravity metadata field.
      Returns:
      true if the entity ignore gravity, false otherwise
    • refreshPosition

      public void refreshPosition​(float x, float y, float z)
      Used to refresh the entity and its passengers position - put the entity in the right instance chunk - update the viewable chunks (load and unload) - add/remove players from the viewers list if isAutoViewable() is enabled

      WARNING: unsafe, should only be used internally in Minestom. Use teleport(Position) instead.

      Parameters:
      x - new position X
      y - new position Y
      z - new position Z
    • refreshPosition

      public void refreshPosition​(@NotNull Position position)
      Parameters:
      position - the new position
      See Also:
      refreshPosition(float, float, float)
    • refreshView

      public void refreshView​(float yaw, float pitch)
      Updates the entity view internally.

      Warning: you probably want to use setView(float, float).

      Parameters:
      yaw - the yaw
      pitch - the pitch
    • setSneaking

      public void setSneaking​(boolean sneaking)
      Makes the entity sneak.

      WARNING: this will not work for the client itself.

      Parameters:
      sneaking - true to make the entity sneak
    • setSprinting

      public void setSprinting​(boolean sprinting)
      Makes the entity sprint.

      WARNING: this will not work on the client itself.

      Parameters:
      sprinting - true to make the entity sprint
    • getPosition

      public Position getPosition()
      Gets the entity position.
      Returns:
      the current position of the entity
    • getEyeHeight

      public float getEyeHeight()
      Gets the entity eye height.
      Returns:
      the entity eye height
    • setEyeHeight

      public void setEyeHeight​(float eyeHeight)
      Changes the entity eye height.
      Parameters:
      eyeHeight - the entity eye height
    • sameChunk

      public boolean sameChunk​(@NotNull Position position)
      Gets if this entity is in the same chunk as the specified position.
      Parameters:
      position - the checked position chunk
      Returns:
      true if the entity is in the same chunk as position
    • sameChunk

      public boolean sameChunk​(@NotNull Entity entity)
      Gets if the entity is in the same chunk as another.
      Parameters:
      entity - the entity to check
      Returns:
      true if both entities are in the same chunk, false otherwise
    • remove

      public void remove()
      Removes the entity from the server immediately.

      WARNING: this do not trigger the EntityDeathEvent event.

    • isRemoved

      public boolean isRemoved()
      Gets if this entity has been removed.
      Returns:
      true if this entity is removed
    • scheduleRemove

      public void scheduleRemove​(long delay, @NotNull TimeUnit timeUnit)
      Triggers remove() after the specified time.
      Parameters:
      delay - the time before removing the entity
      timeUnit - the unit of the delay
    • isRemoveScheduled

      public boolean isRemoveScheduled()
      Gets if the entity removal has been scheduled with scheduleRemove(long, TimeUnit).
      Returns:
      true if the entity removal has been scheduled
    • getVelocityPacket

      @NotNull protected EntityVelocityPacket getVelocityPacket()
    • getMetadataPacket

      @NotNull public EntityMetaDataPacket getMetadataPacket()
      Gets an EntityMetaDataPacket sent when adding viewers. Used for synchronization.
      Returns:
      The EntityMetaDataPacket related to this entity
    • getMetadataConsumer

      @NotNull public java.util.function.Consumer<BinaryWriter> getMetadataConsumer()
      Should be override when wanting to add a new metadata index
      Returns:
      The consumer used to write EntityMetaDataPacket in getMetadataPacket()
    • sendMetadataIndex

      protected void sendMetadataIndex​(int index)
      Sends a EntityMetaDataPacket containing only the specified index The index is wrote using fillMetadataIndex(BinaryWriter, int).
      Parameters:
      index - the metadata index
    • fillMetadataIndex

      protected void fillMetadataIndex​(@NotNull BinaryWriter packet, int index)
      Used to fill/write a specific metadata index. The proper use to add a new metadata index is to override this and add your case. Then you can also override getMetadataConsumer() and fill your newly added index.
      Parameters:
      packet - the packet writer
      index - the index to fill/write
    • sendSynchronization

      protected void sendSynchronization()
    • askSynchronization

      public void askSynchronization()
      Asks for a synchronization (position) to happen during next entity tick.
    • shouldRemove

      protected boolean shouldRemove()