From ee6a9c50d819be4c82a0849a82e95f296bf0c0b7 Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Fri, 29 Dec 2023 12:28:58 -0800 Subject: [PATCH] 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. --- patches/api/Adventure.patch | 56 +++++++++--------- patches/api/Improve-death-events.patch | 14 +++-- .../api/PlayerDeathEvent-getItemsToKeep.patch | 4 +- ...layerDeathEvent-shouldDropExperience.patch | 59 ++++++++++--------- patches/server/Adventure.patch | 9 +-- patches/server/Improve-death-events.patch | 4 +- ...estore-vanilla-entity-drops-behavior.patch | 8 +-- 7 files changed, 76 insertions(+), 78 deletions(-) diff --git a/patches/api/Adventure.patch b/patches/api/Adventure.patch index cb867214d..20532ea29 100644 --- a/patches/api/Adventure.patch +++ b/patches/api/Adventure.patch @@ -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 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 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 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 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 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 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 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. diff --git a/patches/api/Improve-death-events.patch b/patches/api/Improve-death-events.patch index 5bd25b8a6..cc78ed422 100644 --- a/patches/api/Improve-death-events.patch +++ b/patches/api/Improve-death-events.patch @@ -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. - * diff --git a/patches/api/PlayerDeathEvent-getItemsToKeep.patch b/patches/api/PlayerDeathEvent-getItemsToKeep.patch index fcb8ae679..9b2d7d1f5 100644 --- a/patches/api/PlayerDeathEvent-getItemsToKeep.patch +++ b/patches/api/PlayerDeathEvent-getItemsToKeep.patch @@ -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 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 diff --git a/patches/api/PlayerDeathEvent-shouldDropExperience.patch b/patches/api/PlayerDeathEvent-shouldDropExperience.patch index e51d8f51c..a695a178d 100644 --- a/patches/api/PlayerDeathEvent-shouldDropExperience.patch +++ b/patches/api/PlayerDeathEvent-shouldDropExperience.patch @@ -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 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 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 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 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 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 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 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 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 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() { diff --git a/patches/server/Adventure.patch b/patches/server/Adventure.patch index d4ead9c63..941f2996c 100644 --- a/patches/server/Adventure.patch +++ b/patches/server/Adventure.patch @@ -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 drops, String deathMessage, boolean keepInventory) { -+ public static PlayerDeathEvent callPlayerDeathEvent(ServerPlayer victim, List drops, net.kyori.adventure.text.Component deathMessage, String stringDeathMessage, boolean keepInventory) { // Paper - Adventure ++ public static PlayerDeathEvent callPlayerDeathEvent(ServerPlayer victim, List 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 */ diff --git a/patches/server/Improve-death-events.patch b/patches/server/Improve-death-events.patch index 037a7ae11..81a21e5e2 100644 --- a/patches/server/Improve-death-events.patch +++ b/patches/server/Improve-death-events.patch @@ -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 diff --git a/patches/server/Restore-vanilla-entity-drops-behavior.patch b/patches/server/Restore-vanilla-entity-drops-behavior.patch index 302e450dd..ae80dabb8 100644 --- a/patches/server/Restore-vanilla-entity-drops-behavior.patch +++ b/patches/server/Restore-vanilla-entity-drops-behavior.patch @@ -214,11 +214,11 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 return event; } -- public static PlayerDeathEvent callPlayerDeathEvent(ServerPlayer victim, List drops, net.kyori.adventure.text.Component deathMessage, String stringDeathMessage, boolean keepInventory) { // Paper - Adventure -+ public static PlayerDeathEvent callPlayerDeathEvent(ServerPlayer victim, List drops, net.kyori.adventure.text.Component deathMessage, String stringDeathMessage, boolean keepInventory) { // Paper - Adventure & improve drops +- public static PlayerDeathEvent callPlayerDeathEvent(ServerPlayer victim, List drops, net.kyori.adventure.text.Component deathMessage, boolean keepInventory) { // Paper - Adventure ++ public static PlayerDeathEvent callPlayerDeathEvent(ServerPlayer victim, List 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