Skip to content

Commit

Permalink
cleanup player death event adventure logic (#10095)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Machine-Maker authored Dec 29, 2023
1 parent 3484ae9 commit 0ae58c0
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 95 deletions.
64 changes: 31 additions & 33 deletions patches/api/0006-Adventure.patch
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ index 0000000000000000000000000000000000000000..667bfa6afc35f8a8f475431171ee474a
+}
diff --git a/src/main/java/io/papermc/paper/event/player/AsyncChatEvent.java b/src/main/java/io/papermc/paper/event/player/AsyncChatEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..4eada40b8abb1833ce623ccee0789555e370d024
index 0000000000000000000000000000000000000000..4d2eddba7dc88ba23b241ac7849114a30af968d3
--- /dev/null
+++ b/src/main/java/io/papermc/paper/event/player/AsyncChatEvent.java
@@ -0,0 +1,47 @@
Expand Down Expand Up @@ -3061,37 +3061,40 @@ index f4ac033ff8794a61c82a49dc6c3f863f3291455d..d944d67f544494355f03c5bc9afd8ea7

/**
diff --git a/src/main/java/org/bukkit/event/entity/PlayerDeathEvent.java b/src/main/java/org/bukkit/event/entity/PlayerDeathEvent.java
index 3c2ea8fec3a748cab7f5ad9100d12bd8213ec6c9..a803bfea5400b3578bb4cf3261874e873b6467d9 100644
index 3c2ea8fec3a748cab7f5ad9100d12bd8213ec6c9..03b4e0300d228e3f3a9f4f75c96e0cf9ed1a7d2d 100644
--- a/src/main/java/org/bukkit/event/entity/PlayerDeathEvent.java
+++ b/src/main/java/org/bukkit/event/entity/PlayerDeathEvent.java
@@ -12,25 +12,48 @@ import org.jetbrains.annotations.Nullable;
@@ -11,26 +11,49 @@ 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);
Expand All @@ -3108,35 +3111,34 @@ index 3c2ea8fec3a748cab7f5ad9100d12bd8213ec6c9..a803bfea5400b3578bb4cf3261874e87
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
@@ -39,25 +62,55 @@ public class PlayerDeathEvent extends EntityDeathEvent {
@@ -39,25 +62,49 @@ public class PlayerDeathEvent extends EntityDeathEvent {
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.
Expand All @@ -3146,8 +3148,8 @@ index 3c2ea8fec3a748cab7f5ad9100d12bd8213ec6c9..a803bfea5400b3578bb4cf3261874e87
*/
+ @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
}

/**
Expand All @@ -3160,13 +3162,9 @@ index 3c2ea8fec3a748cab7f5ad9100d12bd8213ec6c9..a803bfea5400b3578bb4cf3261874e87
+ @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.
Expand Down
18 changes: 10 additions & 8 deletions patches/api/0146-Improve-death-events.patch
Original file line number Diff line number Diff line change
Expand Up @@ -179,24 +179,26 @@ index a5984ab06cce95d30e70511e125f69339b574c04..130cf9e5981f701dff4fa72e71e0b5dc
+ // Paper end
}
diff --git a/src/main/java/org/bukkit/event/entity/PlayerDeathEvent.java b/src/main/java/org/bukkit/event/entity/PlayerDeathEvent.java
index a803bfea5400b3578bb4cf3261874e873b6467d9..27909044c0c136acde18d64f14e84de932ba1045 100644
index 03b4e0300d228e3f3a9f4f75c96e0cf9ed1a7d2d..38f81772edacb178e21c3f273e8c41ed46177d6d 100644
--- a/src/main/java/org/bukkit/event/entity/PlayerDeathEvent.java
+++ b/src/main/java/org/bukkit/event/entity/PlayerDeathEvent.java
@@ -63,6 +63,17 @@ public class PlayerDeathEvent extends EntityDeathEvent {
@@ -62,6 +62,19 @@ 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.
*
8 changes: 4 additions & 4 deletions patches/api/0175-PlayerDeathEvent-getItemsToKeep.patch
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ Exposes a mutable array on items a player should keep on death
Example Usage: https://gist.github.com/aikar/5bb202de6057a051a950ce1f29feb0b4

diff --git a/src/main/java/org/bukkit/event/entity/PlayerDeathEvent.java b/src/main/java/org/bukkit/event/entity/PlayerDeathEvent.java
index 27909044c0c136acde18d64f14e84de932ba1045..ed7ede83507c052e05afffea78600b16439590dd 100644
index 59b84a0e07ddc214a419d5b59ab3a86c5ab63b78..3ce76cfb09f6f5eb55e950843bdd846b2b561116 100644
--- a/src/main/java/org/bukkit/event/entity/PlayerDeathEvent.java
+++ b/src/main/java/org/bukkit/event/entity/PlayerDeathEvent.java
@@ -36,7 +36,6 @@ public class PlayerDeathEvent extends EntityDeathEvent {
@@ -37,7 +37,6 @@ 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);
}
@@ -56,6 +55,41 @@ 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
Expand Down
67 changes: 34 additions & 33 deletions patches/api/0183-PlayerDeathEvent-shouldDropExperience.patch
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,61 @@ Subject: [PATCH] PlayerDeathEvent#shouldDropExperience


diff --git a/src/main/java/org/bukkit/event/entity/PlayerDeathEvent.java b/src/main/java/org/bukkit/event/entity/PlayerDeathEvent.java
index ed7ede83507c052e05afffea78600b16439590dd..c7138d79a572a525c70b51b7c79d2c5dfb18f25c 100644
index 3ce76cfb09f6f5eb55e950843bdd846b2b561116..dad291ad8cd16d24d587e771a8b60cfb5d0bce2a 100644
--- a/src/main/java/org/bukkit/event/entity/PlayerDeathEvent.java
+++ b/src/main/java/org/bukkit/event/entity/PlayerDeathEvent.java
@@ -1,6 +1,8 @@
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;
@@ -18,6 +20,8 @@ public class PlayerDeathEvent extends EntityDeathEvent {
@@ -16,6 +16,7 @@ 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);
}
@@ -27,12 +31,17 @@ public class PlayerDeathEvent extends EntityDeathEvent {
}
+ 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) {
@@ -29,11 +30,18 @@ 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

@@ -47,6 +56,11 @@ public class PlayerDeathEvent extends EntityDeathEvent {
@@ -48,11 +56,19 @@ 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;
@@ -88,6 +102,20 @@ 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
@@ -90,6 +106,22 @@ public class PlayerDeathEvent extends EntityDeathEvent {
}
// Paper end

+ // Paper start - shouldDropExperience API
+ /**
+ * @return should experience be dropped from this death
+ */
Expand All @@ -74,6 +73,8 @@ index ed7ede83507c052e05afffea78600b16439590dd..c7138d79a572a525c70b51b7c79d2c5d
+ public void setShouldDropExperience(boolean doExpDrop) {
+ this.doExpDrop = doExpDrop;
+ }
// Paper end
+ // Paper end - shouldDropExperience API
+
@NotNull
@Override
public Player getEntity() {
Loading

0 comments on commit 0ae58c0

Please sign in to comment.