From 3d23a7351e2c1d613b9d0f943aeee9a337756062 Mon Sep 17 00:00:00 2001 From: chickeneer Date: Mon, 22 Feb 2021 20:42:58 -0600 Subject: [PATCH] PlayerDeathEvent#getDeathMessage should provide translation for legacy reasons #5227 --- Spigot-API-Patches/Adventure.patch | 81 ++++++++++--------- .../PlayerDeathEvent-getItemsToKeep.patch | 2 +- ...layerDeathEvent-shouldDropExperience.patch | 11 +-- Spigot-Server-Patches/Adventure.patch | 9 ++- .../Improve-death-events.patch | 4 +- 5 files changed, 60 insertions(+), 47 deletions(-) diff --git a/Spigot-API-Patches/Adventure.patch b/Spigot-API-Patches/Adventure.patch index 0de2a6865..3292a5504 100644 --- a/Spigot-API-Patches/Adventure.patch +++ b/Spigot-API-Patches/Adventure.patch @@ -1319,30 +1319,30 @@ 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 deathMessage; // Paper + private String deathMessage = ""; ++ private net.kyori.adventure.text.Component adventure$deathMessage; // Paper 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 deathMessage) { -+ this(player, drops, droppedExp, 0, deathMessage); ++ 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); + } + -+ public PlayerDeathEvent(@NotNull final Player player, @NotNull final List drops, final int droppedExp, final int newExp, @Nullable final 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, @Nullable final net.kyori.adventure.text.Component adventure$deathMessage, @Nullable String deathMessage) { ++ this(player, drops, droppedExp, newExp, 0, 0, adventure$deathMessage, 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 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) { + super(player, drops, droppedExp); + this.newExp = newExp; + this.newTotalExp = newTotalExp; + this.newLevel = newLevel; + this.deathMessage = deathMessage; ++ this.adventure$deathMessage = adventure$deathMessage; + } + // Paper end + @@ -1362,8 +1362,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 this.newExp = newExp; this.newTotalExp = newTotalExp; this.newLevel = newLevel; -- this.deathMessage = deathMessage; -+ this.deathMessage = deathMessage != null ? net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().deserialize(deathMessage) : net.kyori.adventure.text.Component.empty(); // Paper + this.deathMessage = deathMessage; ++ this.adventure$deathMessage = deathMessage != null ? net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().deserialize(deathMessage) : net.kyori.adventure.text.Component.empty(); // Paper } @NotNull @@ -1372,50 +1372,59 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 } + // Paper start - /** - * Set the death message that will appear to everyone on the server. - * - * @param deathMessage Message to appear to other players on the server. - */ -- public void setDeathMessage(@Nullable String deathMessage) { -+ public void deathMessage(@Nullable net.kyori.adventure.text.Component deathMessage) { - this.deathMessage = deathMessage; - } - -@@ -0,0 +0,0 @@ public class PlayerDeathEvent extends EntityDeathEvent { - * - * @return Message to appear to other players on the server. - */ -+ public @Nullable net.kyori.adventure.text.Component deathMessage() { -+ return this.deathMessage; -+ } -+ // Paper end -+ + /** + * Set the death message that will appear to everyone on the server. + * + * @param deathMessage Message to appear to other players on the server. -+ * @deprecated in favour of {@link #deathMessage(net.kyori.adventure.text.Component)} + */ -+ @Deprecated // Paper -+ public void setDeathMessage(@Nullable String deathMessage) { -+ this.deathMessage = deathMessage != null ? net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().deserialize(deathMessage) : net.kyori.adventure.text.Component.empty(); // Paper ++ public void deathMessage(@Nullable net.kyori.adventure.text.Component deathMessage) { ++ this.deathMessage = null; ++ this.adventure$deathMessage = deathMessage; + } + + /** + * Get the death message that will appear to everyone on the server. + * + * @return Message to appear to other players on the server. -+ * @deprecated in favour of {@link #deathMessage()} + */ ++ public @Nullable net.kyori.adventure.text.Component deathMessage() { ++ return this.adventure$deathMessage; ++ } ++ // Paper end ++ + /** + * Set the death message that will appear to everyone on the server. + * + * @param deathMessage Message to appear to other players on the server. ++ * @deprecated in favour of {@link #deathMessage(net.kyori.adventure.text.Component)} + */ ++ @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) : net.kyori.adventure.text.Component.empty(); // Paper + } + + /** + * Get the death message that will appear to everyone on the server. + * + * @return Message to appear to other players on the server. ++ * @deprecated in favour of {@link #deathMessage()} + */ @Nullable + @Deprecated // Paper public String getDeathMessage() { - return deathMessage; -+ return this.deathMessage == null ? null : net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().serialize(this.deathMessage); // Paper ++ return this.deathMessage != null ? this.deathMessage : (this.adventure$deathMessage != null ? getDeathMessageString(this.adventure$deathMessage) : null); // 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/src/main/java/org/bukkit/event/inventory/InventoryType.java b/src/main/java/org/bukkit/event/inventory/InventoryType.java index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/org/bukkit/event/inventory/InventoryType.java diff --git a/Spigot-API-Patches/PlayerDeathEvent-getItemsToKeep.patch b/Spigot-API-Patches/PlayerDeathEvent-getItemsToKeep.patch index 37b5e25e0..4d387d1ce 100644 --- a/Spigot-API-Patches/PlayerDeathEvent-getItemsToKeep.patch +++ b/Spigot-API-Patches/PlayerDeathEvent-getItemsToKeep.patch @@ -20,7 +20,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 this(player, drops, droppedExp, 0, deathMessage); } @@ -0,0 +0,0 @@ public class PlayerDeathEvent extends EntityDeathEvent { - this.deathMessage = deathMessage != null ? net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().deserialize(deathMessage) : net.kyori.adventure.text.Component.empty(); // Paper + this.adventure$deathMessage = deathMessage != null ? net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().deserialize(deathMessage) : net.kyori.adventure.text.Component.empty(); // Paper } + @Deprecated // Paper diff --git a/Spigot-API-Patches/PlayerDeathEvent-shouldDropExperience.patch b/Spigot-API-Patches/PlayerDeathEvent-shouldDropExperience.patch index f6fe19a66..e51d8f51c 100644 --- a/Spigot-API-Patches/PlayerDeathEvent-shouldDropExperience.patch +++ b/Spigot-API-Patches/PlayerDeathEvent-shouldDropExperience.patch @@ -23,22 +23,23 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 // 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 deathMessage) { - this(player, drops, droppedExp, 0, deathMessage); + 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); } @@ -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 deathMessage) { -+ 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) { ++ this(player, drops, droppedExp, newExp, newTotalExp, newLevel, adventure$deathMessage, 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 deathMessage, boolean doExpDrop) { ++ 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) { super(player, drops, droppedExp); this.newExp = newExp; this.newTotalExp = newTotalExp; this.newLevel = newLevel; this.deathMessage = deathMessage; + this.adventure$deathMessage = adventure$deathMessage; + this.doExpDrop = doExpDrop; } // Paper end diff --git a/Spigot-Server-Patches/Adventure.patch b/Spigot-Server-Patches/Adventure.patch index 03084b478..69af039a4 100644 --- a/Spigot-Server-Patches/Adventure.patch +++ b/Spigot-Server-Patches/Adventure.patch @@ -782,7 +782,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 - String deathmessage = defaultMessage.getString(); - 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), keepInventory); // Paper - Adventure ++ org.bukkit.event.entity.PlayerDeathEvent event = CraftEventFactory.callPlayerDeathEvent(this, loot, PaperAdventure.asAdventure(defaultMessage), defaultMessage.getString(), keepInventory); // Paper - Adventure // SPIGOT-943 - only call if they have an inventory open if (this.activeContainer != this.defaultContainer) { @@ -2162,10 +2162,13 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 } - public static PlayerDeathEvent callPlayerDeathEvent(EntityPlayer victim, List drops, String deathMessage, boolean keepInventory) { -+ public static PlayerDeathEvent callPlayerDeathEvent(EntityPlayer victim, List drops, net.kyori.adventure.text.Component deathMessage, boolean keepInventory) { // Paper - Adventure ++ public static PlayerDeathEvent callPlayerDeathEvent(EntityPlayer victim, List drops, net.kyori.adventure.text.Component deathMessage, String stringDeathMessage, 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); ++ PlayerDeathEvent event = new PlayerDeathEvent(entity, drops, victim.getExpReward(), 0, deathMessage, stringDeathMessage); // Paper - Adventure event.setKeepInventory(keepInventory); + org.bukkit.World world = entity.getWorld(); + Bukkit.getServer().getPluginManager().callEvent(event); @@ -0,0 +0,0 @@ public class CraftEventFactory { * Server methods */ diff --git a/Spigot-Server-Patches/Improve-death-events.patch b/Spigot-Server-Patches/Improve-death-events.patch index 94ca0c95d..dce167cb2 100644 --- a/Spigot-Server-Patches/Improve-death-events.patch +++ b/Spigot-Server-Patches/Improve-death-events.patch @@ -291,7 +291,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 @@ -0,0 +0,0 @@ public class EntityPlayer extends EntityHuman implements ICrafting { IChatBaseComponent defaultMessage = this.getCombatTracker().getDeathMessage(); - org.bukkit.event.entity.PlayerDeathEvent event = CraftEventFactory.callPlayerDeathEvent(this, loot, PaperAdventure.asAdventure(defaultMessage), keepInventory); // Paper - Adventure + org.bukkit.event.entity.PlayerDeathEvent event = CraftEventFactory.callPlayerDeathEvent(this, loot, PaperAdventure.asAdventure(defaultMessage), defaultMessage.getString(), 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 @@ -368,7 +368,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 for (org.bukkit.inventory.ItemStack stack : event.getDrops()) { @@ -0,0 +0,0 @@ public class CraftEventFactory { 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 event.setKeepInventory(keepInventory); + populateFields(victim, event); // Paper - make cancellable org.bukkit.World world = entity.getWorld();