diff --git a/pom.xml b/pom.xml index c39914e44..7659f854e 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ me.lokka30 LevelledMobs - 3.3.2 b594 + 3.3.2 b595 jar LevelledMobs diff --git a/src/main/java/me/lokka30/levelledmobs/listeners/paper/PlayerDeathListener.java b/src/main/java/me/lokka30/levelledmobs/listeners/paper/PlayerDeathListener.java index 34788b7bf..3ea3a005c 100644 --- a/src/main/java/me/lokka30/levelledmobs/listeners/paper/PlayerDeathListener.java +++ b/src/main/java/me/lokka30/levelledmobs/listeners/paper/PlayerDeathListener.java @@ -6,6 +6,7 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.TranslatableComponent; +import net.kyori.adventure.text.event.HoverEvent; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; @@ -78,13 +79,8 @@ else if (damager instanceof LivingEntity) private void updateDeathMessage(@NotNull final PlayerDeathEvent event, final String mobName){ final TranslatableComponent tc = (TranslatableComponent) event.deathMessage(); if (tc == null) return; - String playerKilled = null; - - for (final net.kyori.adventure.text.Component com : tc.args()){ - if (com instanceof TextComponent) - playerKilled = ((TextComponent) com).content(); - } + final String playerKilled = extractPlayerName(tc); if (playerKilled == null) return; final TextComponent tcMobName = LegacyComponentSerializer.legacySection().deserialize(mobName); @@ -94,4 +90,33 @@ private void updateDeathMessage(@NotNull final PlayerDeathEvent event, final Str event.deathMessage(newCom); } + + @Nullable + private String extractPlayerName(final @NotNull TranslatableComponent tc){ + String playerKilled = null; + + for (final net.kyori.adventure.text.Component com : tc.args()){ + if (com instanceof TextComponent) { + final TextComponent tc2 = (TextComponent) com; + playerKilled = tc2.content(); + + if (playerKilled.isEmpty() && tc2.hoverEvent() != null) { + // in rare cases the above method returns a empty string + // we'll extract the player name from the hover event + final HoverEvent he = tc2.hoverEvent(); + if (he == null || !(he.value() instanceof HoverEvent.ShowEntity)) return null; + + final HoverEvent.ShowEntity se = (HoverEvent.ShowEntity) he.value(); + + if (se.name() instanceof TextComponent){ + final TextComponent tc3 = (TextComponent) se.name(); + playerKilled = tc3.content(); + } + } + } + } + + return playerKilled != null && playerKilled.isEmpty() ? + null : playerKilled; + } }