13
0
geforkt von Mirrors/Paper

cleanup player death event adventure logic (#10095)

There was a TODO left there regarding the translated death
message being used by plugins to identify the cause of death. This
should be mitigated now because the LegacyComponentSerializer default
implemenation uses our custom flattener which renders vanilla
translatable components to their English representation.
Dieser Commit ist enthalten in:
Jake Potrebic 2023-12-29 12:28:58 -08:00
Ursprung e1faa8e814
Commit ee6a9c50d8
7 geänderte Dateien mit 76 neuen und 78 gelöschten Zeilen

Datei anzeigen

@ -3065,33 +3065,36 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
--- a/src/main/java/org/bukkit/event/entity/PlayerDeathEvent.java
+++ b/src/main/java/org/bukkit/event/entity/PlayerDeathEvent.java
@@ -0,0 +0,0 @@ import org.jetbrains.annotations.Nullable;
*/
public class PlayerDeathEvent extends EntityDeathEvent {
private int newExp = 0;
private String deathMessage = "";
+ private net.kyori.adventure.text.Component adventure$deathMessage; // Paper
- private String deathMessage = "";
+ private net.kyori.adventure.text.Component deathMessage; // Paper - adventure
private int newLevel = 0;
private int newTotalExp = 0;
private boolean keepLevel = false;
private boolean keepInventory = false;
+ // Paper start
+ public PlayerDeathEvent(@NotNull final Player player, @NotNull final List<ItemStack> drops, final int droppedExp, @Nullable final net.kyori.adventure.text.Component adventure$deathMessage) {
+ this(player, drops, droppedExp, 0, adventure$deathMessage, null);
+ // Paper start - adventure
+ @org.jetbrains.annotations.ApiStatus.Internal
+ public PlayerDeathEvent(final @NotNull Player player, final @NotNull List<ItemStack> drops, final int droppedExp, final @Nullable net.kyori.adventure.text.Component deathMessage) {
+ this(player, drops, droppedExp, 0, deathMessage);
+ }
+
+ public PlayerDeathEvent(@NotNull final Player player, @NotNull final List<ItemStack> drops, final int droppedExp, final int newExp, @Nullable final net.kyori.adventure.text.Component adventure$deathMessage, @Nullable String deathMessage) {
+ this(player, drops, droppedExp, newExp, 0, 0, adventure$deathMessage, deathMessage);
+ @org.jetbrains.annotations.ApiStatus.Internal
+ public PlayerDeathEvent(final @NotNull Player player, final @NotNull List<ItemStack> drops, final int droppedExp, final int newExp, final @Nullable net.kyori.adventure.text.Component deathMessage) {
+ this(player, drops, droppedExp, newExp, 0, 0, deathMessage);
+ }
+ public PlayerDeathEvent(@NotNull final Player player, @NotNull final List<ItemStack> drops, final int droppedExp, final int newExp, final int newTotalExp, final int newLevel, @Nullable final net.kyori.adventure.text.Component adventure$deathMessage, @Nullable String deathMessage) {
+
+ @org.jetbrains.annotations.ApiStatus.Internal
+ public PlayerDeathEvent(final @NotNull Player player, final @NotNull List<ItemStack> drops, final int droppedExp, final int newExp, final int newTotalExp, final int newLevel, final @Nullable net.kyori.adventure.text.Component deathMessage) {
+ super(player, drops, droppedExp);
+ this.newExp = newExp;
+ this.newTotalExp = newTotalExp;
+ this.newLevel = newLevel;
+ this.deathMessage = deathMessage;
+ this.adventure$deathMessage = adventure$deathMessage;
+ }
+ // Paper end
+
+ // Paper end - adventure
+ @Deprecated // Paper
public PlayerDeathEvent(@NotNull final Player player, @NotNull final List<ItemStack> drops, final int droppedExp, @Nullable final String deathMessage) {
this(player, drops, droppedExp, 0, deathMessage);
@ -3108,8 +3111,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
this.newExp = newExp;
this.newTotalExp = newTotalExp;
this.newLevel = newLevel;
this.deathMessage = deathMessage;
+ this.adventure$deathMessage = deathMessage != null ? net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().deserialize(deathMessage) : null; // Paper
- this.deathMessage = deathMessage;
+ this.deathMessage = net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().deserializeOrNull(deathMessage); // Paper
}
@NotNull
@ -3117,26 +3120,25 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
return (Player) entity;
}
+ // Paper start
+ // Paper start - adventure
+ /**
+ * Set the death message that will appear to everyone on the server.
+ *
+ * @param deathMessage Message to appear to other players on the server.
+ * @param deathMessage Component message to appear to other players on the server.
+ */
+ public void deathMessage(net.kyori.adventure.text.@Nullable Component deathMessage) {
+ this.deathMessage = null;
+ this.adventure$deathMessage = deathMessage;
+ public void deathMessage(final net.kyori.adventure.text.@Nullable Component deathMessage) {
+ this.deathMessage = deathMessage;
+ }
+
+ /**
+ * Get the death message that will appear to everyone on the server.
+ *
+ * @return Message to appear to other players on the server.
+ * @return Component message to appear to other players on the server.
+ */
+ public net.kyori.adventure.text.@Nullable Component deathMessage() {
+ return this.adventure$deathMessage;
+ return this.deathMessage;
+ }
+ // Paper end
+ // Paper end - adventure
+
/**
* Set the death message that will appear to everyone on the server.
@ -3146,8 +3148,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
*/
+ @Deprecated // Paper
public void setDeathMessage(@Nullable String deathMessage) {
this.deathMessage = deathMessage;
+ this.adventure$deathMessage = deathMessage != null ? net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().deserialize(deathMessage) : null; // Paper
- this.deathMessage = deathMessage;
+ this.deathMessage = net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().deserializeOrNull(deathMessage); // Paper
}
/**
@ -3160,13 +3162,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ @Deprecated // Paper
public String getDeathMessage() {
- return deathMessage;
+ return this.deathMessage != null ? this.deathMessage : (this.adventure$deathMessage != null ? getDeathMessageString(this.adventure$deathMessage) : null); // Paper
+ return net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().serializeOrNull(this.deathMessage); // Paper
}
-
+ // Paper start //TODO: add translation API to drop String deathMessage in favor of just Adventure
+ private static String getDeathMessageString(net.kyori.adventure.text.Component component) {
+ return net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().serialize(component);
+ }
+ // Paper end
/**
* Gets how much EXP the Player should have at respawn.

Datei anzeigen

@ -183,20 +183,22 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
--- a/src/main/java/org/bukkit/event/entity/PlayerDeathEvent.java
+++ b/src/main/java/org/bukkit/event/entity/PlayerDeathEvent.java
@@ -0,0 +0,0 @@ public class PlayerDeathEvent extends EntityDeathEvent {
return (Player) entity;
}
// Paper start
+ // Paper start - improve death events
+ /**
+ * Clarity method for getting the player. Not really needed except
+ * for reasons of clarity.
+ *
+ *
+ * @return Player who is involved in this event
+ */
+ @NotNull
+ public Player getPlayer() {
+ return getEntity();
+ public @NotNull Player getPlayer() {
+ return this.getEntity();
+ }
+
+ // Paper end - improve death events
+
// Paper start - adventure
/**
* Set the death message that will appear to everyone on the server.
*

Datei anzeigen

@ -13,14 +13,14 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+++ b/src/main/java/org/bukkit/event/entity/PlayerDeathEvent.java
@@ -0,0 +0,0 @@ public class PlayerDeathEvent extends EntityDeathEvent {
}
// Paper end
// Paper end - adventure
- @Deprecated // Paper
public PlayerDeathEvent(@NotNull final Player player, @NotNull final List<ItemStack> drops, final int droppedExp, @Nullable final String deathMessage) {
this(player, drops, droppedExp, 0, deathMessage);
}
@@ -0,0 +0,0 @@ public class PlayerDeathEvent extends EntityDeathEvent {
this.adventure$deathMessage = deathMessage != null ? net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().deserialize(deathMessage) : null; // Paper
this.deathMessage = net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().deserializeOrNull(deathMessage); // Paper
}
+ @Deprecated // Paper

Datei anzeigen

@ -8,59 +8,58 @@ diff --git a/src/main/java/org/bukkit/event/entity/PlayerDeathEvent.java b/src/m
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/event/entity/PlayerDeathEvent.java
+++ b/src/main/java/org/bukkit/event/entity/PlayerDeathEvent.java
@@ -0,0 +0,0 @@
package org.bukkit.event.entity;
import java.util.List;
+
+import org.bukkit.GameMode;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
@@ -0,0 +0,0 @@ public class PlayerDeathEvent extends EntityDeathEvent {
private int newTotalExp = 0;
private boolean keepLevel = false;
private boolean keepInventory = false;
// Paper start
+ private boolean doExpDrop;
+
public PlayerDeathEvent(@NotNull final Player player, @NotNull final List<ItemStack> drops, final int droppedExp, @Nullable final net.kyori.adventure.text.Component adventure$deathMessage) {
this(player, drops, droppedExp, 0, adventure$deathMessage, null);
}
+ private boolean doExpDrop; // Paper - shouldDropExperience API
// Paper start - adventure
@org.jetbrains.annotations.ApiStatus.Internal
public PlayerDeathEvent(final @NotNull Player player, final @NotNull List<ItemStack> drops, final int droppedExp, final @Nullable net.kyori.adventure.text.Component deathMessage) {
@@ -0,0 +0,0 @@ public class PlayerDeathEvent extends EntityDeathEvent {
}
public PlayerDeathEvent(@NotNull final Player player, @NotNull final List<ItemStack> drops, final int droppedExp, final int newExp, final int newTotalExp, final int newLevel, @Nullable final net.kyori.adventure.text.Component adventure$deathMessage, @Nullable String deathMessage) {
+ this(player, drops, droppedExp, newExp, newTotalExp, newLevel, adventure$deathMessage, deathMessage, true);
@org.jetbrains.annotations.ApiStatus.Internal
public PlayerDeathEvent(final @NotNull Player player, final @NotNull List<ItemStack> drops, final int droppedExp, final int newExp, final int newTotalExp, final int newLevel, final @Nullable net.kyori.adventure.text.Component deathMessage) {
+ // Paper start - shouldDropExperience API
+ this(player, drops, droppedExp, newExp, newTotalExp, newLevel, deathMessage, true);
+ }
+
+ public PlayerDeathEvent(@NotNull final Player player, @NotNull final List<ItemStack> drops, final int droppedExp, final int newExp, final int newTotalExp, final int newLevel, @Nullable final net.kyori.adventure.text.Component adventure$deathMessage, @Nullable String deathMessage, boolean doExpDrop) {
+ @org.jetbrains.annotations.ApiStatus.Internal
+ public PlayerDeathEvent(final @NotNull Player player, final @NotNull List<ItemStack> drops, final int droppedExp, final int newExp, final int newTotalExp, final int newLevel, final @Nullable net.kyori.adventure.text.Component deathMessage, final boolean doExpDrop) {
+ // Paper end - shouldDropExperience API
super(player, drops, droppedExp);
this.newExp = newExp;
this.newTotalExp = newTotalExp;
this.newLevel = newLevel;
this.deathMessage = deathMessage;
this.adventure$deathMessage = adventure$deathMessage;
+ this.doExpDrop = doExpDrop;
+ this.doExpDrop = doExpDrop; // Paper - shouldDropExperience API
}
// Paper end
// Paper end - adventure
@@ -0,0 +0,0 @@ public class PlayerDeathEvent extends EntityDeathEvent {
@Deprecated // Paper
public PlayerDeathEvent(@NotNull final Player player, @NotNull final List<ItemStack> drops, final int droppedExp, final int newExp, final int newTotalExp, final int newLevel, @Nullable final String deathMessage) {
+ // Paper start - shouldDropExperience API
+ this(player, drops, droppedExp, newExp, newTotalExp, newLevel, deathMessage, true);
+ }
+
+ @Deprecated // Paper
+ public PlayerDeathEvent(@NotNull final Player player, @NotNull final List<ItemStack> drops, final int droppedExp, final int newExp, final int newTotalExp, final int newLevel, @Nullable final String deathMessage, boolean doExpDrop) {
+ // Paper end - shouldDropExperience API
super(player, drops, droppedExp);
this.newExp = newExp;
this.newTotalExp = newTotalExp;
@@ -0,0 +0,0 @@ public class PlayerDeathEvent extends EntityDeathEvent {
public List<ItemStack> getItemsToKeep() {
return itemsToKeep;
this.newLevel = newLevel;
this.deathMessage = net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().deserializeOrNull(deathMessage); // Paper
+ this.doExpDrop = doExpDrop; // Paper - shouldDropExperience API
}
+
@Deprecated // Paper
@@ -0,0 +0,0 @@ public class PlayerDeathEvent extends EntityDeathEvent {
}
// Paper end
+ // Paper start - shouldDropExperience API
+ /**
+ * @return should experience be dropped from this death
+ */
@ -74,6 +73,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ public void setShouldDropExperience(boolean doExpDrop) {
+ this.doExpDrop = doExpDrop;
+ }
// Paper end
+ // Paper end - shouldDropExperience API
+
@NotNull
@Override
public Player getEntity() {

Datei anzeigen

@ -2785,7 +2785,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
String deathmessage = defaultMessage.getString();
this.keepLevel = keepInventory; // SPIGOT-2222: pre-set keepLevel
- org.bukkit.event.entity.PlayerDeathEvent event = CraftEventFactory.callPlayerDeathEvent(this, loot, deathmessage, keepInventory);
+ org.bukkit.event.entity.PlayerDeathEvent event = CraftEventFactory.callPlayerDeathEvent(this, loot, PaperAdventure.asAdventure(defaultMessage), defaultMessage.getString(), keepInventory); // Paper - Adventure
+ org.bukkit.event.entity.PlayerDeathEvent event = CraftEventFactory.callPlayerDeathEvent(this, loot, PaperAdventure.asAdventure(defaultMessage), keepInventory); // Paper - Adventure
// SPIGOT-943 - only call if they have an inventory open
if (this.containerMenu != this.inventoryMenu) {
@ -4678,13 +4678,10 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
}
- public static PlayerDeathEvent callPlayerDeathEvent(ServerPlayer victim, List<org.bukkit.inventory.ItemStack> drops, String deathMessage, boolean keepInventory) {
+ public static PlayerDeathEvent callPlayerDeathEvent(ServerPlayer victim, List<org.bukkit.inventory.ItemStack> drops, net.kyori.adventure.text.Component deathMessage, String stringDeathMessage, boolean keepInventory) { // Paper - Adventure
+ public static PlayerDeathEvent callPlayerDeathEvent(ServerPlayer victim, List<org.bukkit.inventory.ItemStack> drops, net.kyori.adventure.text.Component deathMessage, boolean keepInventory) { // Paper - Adventure
CraftPlayer entity = victim.getBukkitEntity();
- PlayerDeathEvent event = new PlayerDeathEvent(entity, drops, victim.getExpReward(), 0, deathMessage);
+ PlayerDeathEvent event = new PlayerDeathEvent(entity, drops, victim.getExpReward(), 0, deathMessage, stringDeathMessage); // Paper - Adventure
PlayerDeathEvent event = new PlayerDeathEvent(entity, drops, victim.getExpReward(), 0, deathMessage);
event.setKeepInventory(keepInventory);
event.setKeepLevel(victim.keepLevel); // SPIGOT-2222: pre-set keepLevel
org.bukkit.World world = entity.getWorld();
@@ -0,0 +0,0 @@ public class CraftEventFactory {
* Server methods
*/

Datei anzeigen

@ -45,7 +45,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
@@ -0,0 +0,0 @@ public class ServerPlayer extends Player {
String deathmessage = defaultMessage.getString();
this.keepLevel = keepInventory; // SPIGOT-2222: pre-set keepLevel
org.bukkit.event.entity.PlayerDeathEvent event = CraftEventFactory.callPlayerDeathEvent(this, loot, PaperAdventure.asAdventure(defaultMessage), defaultMessage.getString(), keepInventory); // Paper - Adventure
org.bukkit.event.entity.PlayerDeathEvent event = CraftEventFactory.callPlayerDeathEvent(this, loot, PaperAdventure.asAdventure(defaultMessage), keepInventory); // Paper - Adventure
+ // Paper start - cancellable death event
+ if (event.isCancelled()) {
+ // make compatible with plugins that might have already set the health in an event listener
@ -443,7 +443,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
for (org.bukkit.inventory.ItemStack stack : event.getDrops()) {
@@ -0,0 +0,0 @@ public class CraftEventFactory {
PlayerDeathEvent event = new PlayerDeathEvent(entity, drops, victim.getExpReward(), 0, deathMessage, stringDeathMessage); // Paper - Adventure
PlayerDeathEvent event = new PlayerDeathEvent(entity, drops, victim.getExpReward(), 0, deathMessage);
event.setKeepInventory(keepInventory);
event.setKeepLevel(victim.keepLevel); // SPIGOT-2222: pre-set keepLevel
+ populateFields(victim, event); // Paper - make cancellable

Datei anzeigen

@ -214,11 +214,11 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
return event;
}
- public static PlayerDeathEvent callPlayerDeathEvent(ServerPlayer victim, List<org.bukkit.inventory.ItemStack> drops, net.kyori.adventure.text.Component deathMessage, String stringDeathMessage, boolean keepInventory) { // Paper - Adventure
+ public static PlayerDeathEvent callPlayerDeathEvent(ServerPlayer victim, List<Entity.DefaultDrop> drops, net.kyori.adventure.text.Component deathMessage, String stringDeathMessage, boolean keepInventory) { // Paper - Adventure & improve drops
- public static PlayerDeathEvent callPlayerDeathEvent(ServerPlayer victim, List<org.bukkit.inventory.ItemStack> drops, net.kyori.adventure.text.Component deathMessage, boolean keepInventory) { // Paper - Adventure
+ public static PlayerDeathEvent callPlayerDeathEvent(ServerPlayer victim, List<Entity.DefaultDrop> drops, net.kyori.adventure.text.Component deathMessage, boolean keepInventory) { // Paper - Adventure & improve drops
CraftPlayer entity = victim.getBukkitEntity();
- PlayerDeathEvent event = new PlayerDeathEvent(entity, drops, victim.getExpReward(), 0, deathMessage, stringDeathMessage); // Paper - Adventure
+ PlayerDeathEvent event = new PlayerDeathEvent(entity, new io.papermc.paper.util.TransformingRandomAccessList<>(drops, Entity.DefaultDrop::stack, FROM_FUNCTION), victim.getExpReward(), 0, deathMessage, stringDeathMessage); // Paper - Adventure & improve drops
- PlayerDeathEvent event = new PlayerDeathEvent(entity, drops, victim.getExpReward(), 0, deathMessage);
+ PlayerDeathEvent event = new PlayerDeathEvent(entity, new io.papermc.paper.util.TransformingRandomAccessList<>(drops, Entity.DefaultDrop::stack, FROM_FUNCTION), victim.getExpReward(), 0, deathMessage); // Paper - improve drops
event.setKeepInventory(keepInventory);
event.setKeepLevel(victim.keepLevel); // SPIGOT-2222: pre-set keepLevel
populateFields(victim, event); // Paper - make cancellable