Package org.bukkit.event.entity

# Class PlayerDeathEvent

java.lang.Object
org.bukkit.event.Event
org.bukkit.event.entity.EntityEvent
org.bukkit.event.entity.EntityDeathEvent
org.bukkit.event.entity.PlayerDeathEvent

All Implemented Interfaces:
  `Cancellable`

public class PlayerDeathEvent extends EntityDeathEvent
Thrown whenever a `Player` dies

## Nested Class Summary

## Nested classes/interfaces inherited from class org.bukkit.event.Event

`Event.Result`

## Field Summary

### Fields inherited from class org.bukkit.event.entity.EntityEvent

`entity`

## Constructor Summary

Constructors
Constructor
Description
`PlayerDeathEvent(@NotNull Player player, @NotNull List<ItemStack> drops, int droppedExp, int newExp, int newTotalExp, int newLevel, @Nullable String deathMessage)`
Deprecated.
`PlayerDeathEvent(@NotNull Player player, @NotNull List<ItemStack> drops, int droppedExp, int newExp, int newTotalExp, int newLevel, @Nullable String deathMessage, boolean doExpDrop)`
Deprecated.
`PlayerDeathEvent(@NotNull Player player, @NotNull List<ItemStack> drops, int droppedExp, int newExp, int newTotalExp, int newLevel, Component adventure$deathMessage, @Nullable String deathMessage)`
`PlayerDeathEvent(@NotNull Player player, @NotNull List<ItemStack> drops, int droppedExp, int newExp, int newTotalExp, int newLevel, Component adventure$deathMessage, @Nullable String deathMessage, boolean doExpDrop)`
`PlayerDeathEvent(@NotNull Player player, @NotNull List<ItemStack> drops, int droppedExp, int newExp, @Nullable String deathMessage)`
Deprecated.
`PlayerDeathEvent(@NotNull Player player, @NotNull List<ItemStack> drops, int droppedExp, int newExp, Component adventure$deathMessage, @Nullable String deathMessage)`
`PlayerDeathEvent(@NotNull Player player, @NotNull List<ItemStack> drops, int droppedExp, @Nullable String deathMessage)`
`PlayerDeathEvent(@NotNull Player player, @NotNull List<ItemStack> drops, int droppedExp, Component adventure$deathMessage)`

## Method Summary

Modifier and Type
Method
Description
`Component`
`deathMessage()`
Get the death message that will appear to everyone on the server.
`void`
`deathMessage(Component deathMessage)`
Set the death message that will appear to everyone on the server.
`@Nullable String`
`getDeathMessage()`
Deprecated.
in favour of `deathMessage()`
`@NotNull Player`
`getEntity()`
Returns the Entity involved in this event
`@NotNull List<ItemStack>`
`getItemsToKeep()`
A mutable collection to add items that the player should retain in their inventory on death (Similar to KeepInventory game rule) You MUST remove the item from the .getDrops() collection too or it will duplicate!
`boolean`
`getKeepInventory()`
Gets if the Player keeps inventory on death.
`boolean`
`getKeepLevel()`
Gets if the Player should keep all EXP at respawn.
`int`
`getNewExp()`
Gets how much EXP the Player should have at respawn.
`int`
`getNewLevel()`
Gets the Level the Player should have at respawn.
`int`
`getNewTotalExp()`
Gets the Total EXP the Player should have at respawn.
`@NotNull Player`
`getPlayer()`
Clarity method for getting the player.
`void`
`setDeathMessage(@Nullable String deathMessage)`
Deprecated.
in favour of `deathMessage(net.kyori.adventure.text.Component)`
`void`
`setKeepInventory(boolean keepInventory)`
Sets if the Player keeps inventory on death.
`void`
`setKeepLevel(boolean keepLevel)`
Sets if the Player should keep all EXP at respawn.
`void`
`setNewExp(int exp)`
Sets how much EXP the Player should have at respawn.
`void`
`setNewLevel(int level)`
Sets the Level the Player should have at respawn.
`void`
`setNewTotalExp(int totalExp)`
Sets the Total EXP the Player should have at respawn.
`void`
`setShouldDropExperience(boolean doExpDrop)`
`boolean`
`shouldDropExperience()`

### Methods inherited from class org.bukkit.event.entity.EntityDeathEvent

`getDeathSound, getDeathSoundCategory, getDeathSoundPitch, getDeathSoundVolume, getDroppedExp, getDrops, getHandlerList, getHandlers, getReviveHealth, isCancelled, setCancelled, setDeathSound, setDeathSoundCategory, setDeathSoundPitch, setDeathSoundVolume, setDroppedExp, setReviveHealth, setShouldPlayDeathSound, shouldPlayDeathSound`

### Methods inherited from class org.bukkit.event.entity.EntityEvent

`getEntityType`

### Methods inherited from class org.bukkit.event.Event

`callEvent, getEventName, isAsynchronous`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### PlayerDeathEvent

public PlayerDeathEvent(@NotNull @NotNull Player player, @NotNull @NotNull List<ItemStack> drops, int droppedExp, @Nullable Component adventure$deathMessage)

### PlayerDeathEvent

public PlayerDeathEvent(@NotNull @NotNull Player player, @NotNull @NotNull List<ItemStack> drops, int droppedExp, int newExp, @Nullable Component adventure$deathMessage, @Nullable @Nullable String deathMessage)

### PlayerDeathEvent

public PlayerDeathEvent(@NotNull @NotNull Player player, @NotNull @NotNull List<ItemStack> drops, int droppedExp, int newExp, int newTotalExp, int newLevel, @Nullable Component adventure$deathMessage, @Nullable @Nullable String deathMessage)

### PlayerDeathEvent

public PlayerDeathEvent(@NotNull @NotNull Player player, @NotNull @NotNull List<ItemStack> drops, int droppedExp, int newExp, int newTotalExp, int newLevel, @Nullable Component adventure$deathMessage, @Nullable @Nullable String deathMessage, boolean doExpDrop)

### PlayerDeathEvent

public PlayerDeathEvent(@NotNull @NotNull Player player, @NotNull @NotNull List<ItemStack> drops, int droppedExp, @Nullable @Nullable String deathMessage)

### PlayerDeathEvent

@Deprecated public PlayerDeathEvent(@NotNull @NotNull Player player, @NotNull @NotNull List<ItemStack> drops, int droppedExp, int newExp, @Nullable @Nullable String deathMessage)
Deprecated.

### PlayerDeathEvent

@Deprecated public PlayerDeathEvent(@NotNull @NotNull Player player, @NotNull @NotNull List<ItemStack> drops, int droppedExp, int newExp, int newTotalExp, int newLevel, @Nullable @Nullable String deathMessage)
Deprecated.

### PlayerDeathEvent

@Deprecated public PlayerDeathEvent(@NotNull @NotNull Player player, @NotNull @NotNull List<ItemStack> drops, int droppedExp, int newExp, int newTotalExp, int newLevel, @Nullable @Nullable String deathMessage, boolean doExpDrop)
Deprecated.

## Method Details

### getItemsToKeep

@NotNull public @NotNull List<ItemStack> getItemsToKeep()
A mutable collection to add items that the player should retain in their inventory on death (Similar to KeepInventory game rule) You MUST remove the item from the .getDrops() collection too or it will duplicate!

        {@literal @EventHandler(ignoreCancelled = true)}
         public void onPlayerDeath(PlayerDeathEvent event) {
             for (Iterator<ItemStack> iterator = event.getDrops().iterator(); iterator.hasNext(); ) {
                 ItemStack drop = iterator.next();
                 List<String> lore = drop.getLore();
                 if (lore != null && !lore.isEmpty()) {
                     if (lore.get(0).contains("(SOULBOUND)")) {
                         iterator.remove();
                         event.getItemsToKeep().add(drop);
                     }
                 }
             }
         }

Adding an item to this list that the player did not previously have will give them the item on death. An example case could be a "Note" that "You died at X/Y/Z coordinates"

Returns:
  The list to hold items to keep

### shouldDropExperience

public boolean shouldDropExperience()

Returns:
  should experience be dropped from this death

### setShouldDropExperience

public void setShouldDropExperience(boolean doExpDrop)

Parameters:
  `doExpDrop` - sets if experience should be dropped from this death

### getEntity

@NotNull public @NotNull Player getEntity()
Description copied from class: `EntityEvent`
Returns the Entity involved in this event

Overrides:
  `getEntity` in class `EntityDeathEvent`
Returns:
  Entity who is involved in this event

### getPlayer

@NotNull public @NotNull Player getPlayer()
Clarity method for getting the player. Not really needed except for reasons of clarity.

Returns:
  Player who is involved in this event

### deathMessage

public void deathMessage(@Nullable Component deathMessage)
Set the death message that will appear to everyone on the server.

Parameters:
  `deathMessage` - Message to appear to other players on the server.

### deathMessage

@Nullable public Component deathMessage()
Get the death message that will appear to everyone on the server.

Returns:
  Message to appear to other players on the server.

### setDeathMessage

@Deprecated public void setDeathMessage(@Nullable @Nullable String deathMessage)
Deprecated.
in favour of `deathMessage(net.kyori.adventure.text.Component)`
Set the death message that will appear to everyone on the server.

Parameters:
  `deathMessage` - Message to appear to other players on the server.

### getDeathMessage

@Nullable @Deprecated public @Nullable String getDeathMessage()
Deprecated.
in favour of `deathMessage()`
Get the death message that will appear to everyone on the server.

Returns:
  Message to appear to other players on the server.

### getNewExp

public int getNewExp()
Gets how much EXP the Player should have at respawn.

This does not indicate how much EXP should be dropped, please see `EntityDeathEvent.getDroppedExp()` for that.

Returns:
  New EXP of the respawned player

### setNewExp

public void setNewExp(int exp)
Sets how much EXP the Player should have at respawn.

This does not indicate how much EXP should be dropped, please see `EntityDeathEvent.setDroppedExp(int)` for that.

Parameters:
  `exp` - New EXP of the respawned player

### getNewLevel

public int getNewLevel()
Gets the Level the Player should have at respawn.

Returns:
  New Level of the respawned player

### setNewLevel

public void setNewLevel(int level)
Sets the Level the Player should have at respawn.

Parameters:
  `level` - New Level of the respawned player

### getNewTotalExp

public int getNewTotalExp()
Gets the Total EXP the Player should have at respawn.

Returns:
  New Total EXP of the respawned player

### setNewTotalExp

public void setNewTotalExp(int totalExp)
Sets the Total EXP the Player should have at respawn.

Parameters:
  `totalExp` - New Total EXP of the respawned player

### getKeepLevel

public boolean getKeepLevel()
Gets if the Player should keep all EXP at respawn.

This flag overrides other EXP settings

Returns:
  True if Player should keep all pre-death exp

### setKeepLevel

public void setKeepLevel(boolean keepLevel)
Sets if the Player should keep all EXP at respawn.

This overrides all other EXP settings

This doesn't prevent the EXP from dropping. `EntityDeathEvent.setDroppedExp(int)` should be used stop the EXP from dropping.

Parameters:
  `keepLevel` - True to keep all current value levels

### setKeepInventory

public void setKeepInventory(boolean keepInventory)
Sets if the Player keeps inventory on death.

This doesn't prevent the items from dropping. `getDrops().clear()` should be used stop the items from dropping.

Parameters:
  `keepInventory` - True to keep the inventory

### getKeepInventory

public boolean getKeepInventory()
Gets if the Player keeps inventory on death.

Returns:
  True if the player keeps inventory on death