Skip to content

Commit

Permalink
v3.3.2 b595
Browse files Browse the repository at this point in the history
* fix rare occurrence of the player death message not showing the player's name on Paper servers
  • Loading branch information
stumper66 committed Dec 27, 2021
1 parent 446cc40 commit 403284e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>me.lokka30</groupId>
<artifactId>LevelledMobs</artifactId>
<version>3.3.2 b594</version>
<version>3.3.2 b595</version>
<packaging>jar</packaging>

<name>LevelledMobs</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
}

0 comments on commit 403284e

Please sign in to comment.