Interface Player

All Superinterfaces:
Audience, BossBarViewer, CommandSender, CommandSource, Entity, HoverEventSource<HoverEvent.ShowEntity>, Identified, LivingEntity, ObjectContentsLike, PermissionHolder, Pointered, SchedulerSource, Sound.Emitter, Sound.Source.Provider

Represents a player currently connected to the server.

A Player reference is not stable across the server lifecycle, as entering the CONFIGURATION phase (see enterConfigurationPhase()) discards the underlying player and recreates it, which invalidates every reference held before that point. Player references should not be cached, as a reference obtained from an event or lookup is only guaranteed valid for the duration of that call. Instead, re-fetch by UUID or name via Server.player(UUID) or Server.player(String).

Since:
0.1.0
  • Method Details

    • refreshCommands

      void refreshCommands()
    • profile

      PlayerProfile profile()
    • offline

      default OfflinePlayer offline()
      Gets the offline handle for this player's identity.
      Returns:
      the handle for this player's identity
      Since:
      0.1.0
    • identity

      default Identity identity()
      Specified by:
      identity in interface Identified
    • uuid

      default UUID uuid()
      Specified by:
      uuid in interface Entity
    • name

      default String name()
      Specified by:
      name in interface CommandSender
    • address

      InetAddress address()
      Gets the address this player is connected from.
      Returns:
      the client address
      Throws:
      IllegalStateException - if the connection has no resolvable IP address
      Since:
      0.1.0
    • ping

      int ping()
      Gets the round-trip time between the server and this player's client.
      Returns:
      the round-trip time in milliseconds
      Since:
      0.1.0
    • kick

      void kick(Component reason)
    • inventory

      PlayerInventory inventory()
    • enderChest

      EnderChestInventory enderChest()
    • selectedSlot

      int selectedSlot()
      Returns:
      the hotbar slot (0-8) currently selected by this player
      Since:
      0.1.0
    • setSelectedSlot

      void setSelectedSlot(int slot)
      Parameters:
      slot - the hotbar slot to select, 0-8
      Throws:
      IllegalArgumentException - if slot is out of [0, 8]
      Since:
      0.1.0
    • heldItem

      default ItemStack heldItem()
      Returns:
      the stack in the currently selected hotbar slot, never null (ItemStack.EMPTY when nothing is held)
      Since:
      0.1.0
    • setHeldItem

      default void setHeldItem(@Nullable ItemStack stack)
      Replaces the stack in the currently selected hotbar slot.
      Parameters:
      stack - the new stack, null is normalized to ItemStack.EMPTY
      Since:
      0.1.0
    • gameMode

      GameMode gameMode()
    • setGameMode

      void setGameMode(GameMode gameMode)
    • isAwaitingRespawn

      boolean isAwaitingRespawn()
      Returns:
      true while this player is dead and still on the respawn screen
      Since:
      0.1.0
    • respawn

      boolean respawn()
      Respawns this player, exactly as if they had clicked the button on the death screen.

      The respawn is queued on the region owning the player and happens on the next tick, so it is safe to call from a PlayerDeathEvent listener to skip the death screen entirely. The player comes back at their respawn point, or at the world spawn when they have none, and a PlayerRespawnEvent is fired as usual.

      Returns:
      true if the respawn was queued, false when the player is not waiting to respawn or has left the server
      Since:
      0.1.0
    • respawnPoint

      @Nullable RespawnPoint respawnPoint()
      Returns:
      where this player respawns, or null when they use the world spawn
      Since:
      0.1.0
    • setRespawnPoint

      void setRespawnPoint(@Nullable RespawnPoint point)
      Sets where this player respawns. The point is saved with the rest of their data and restored on their next login.
      Parameters:
      point - the point to respawn at, or null to fall back to the world spawn
      Since:
      0.1.0
    • setRespawnPoint

      default void setRespawnPoint(World world, Location location)
      Parameters:
      world - the world to respawn in
      location - the position to respawn at
      Since:
      0.1.0
    • setRespawnPoint

      default void setRespawnPoint(Location location)
      Parameters:
      location - the position to respawn at, in the player's current world
      Since:
      0.1.0
    • enterConfigurationPhase

      void enterConfigurationPhase()
      Switches this player's connection status from PLAY to CONFIGURATION. Useful for resending data synced during the CONFIGURATION phase.
      Since:
      0.1.0
      API Note:
      This removes the player from the world fully and creates them anew, firing PlayerQuitEvent, PlayerJoinEvent, and saving their data to disk. Any reference to this Player held before this call should be considered stale.
    • updateInventory

      void updateInventory()
      Updates the player's inventory on the client side.

      This is useful when the server has changed the inventory contents, and the client needs to be informed of those changes. This method should be called after any modifications to the player's inventory, such as adding or removing items, to ensure that the client sees the correct state.

      Since:
      0.1.0