Skip to content

Commit

Permalink
Respect death messages gamerule (#359)
Browse files Browse the repository at this point in the history
* Use Bukkit.broadcast instead of looping through getOnlinePlayers

* Check if the death message is null before broadcasting it

* Don't broadcast death messages from worlds with showDeathMessages disabled
  • Loading branch information
OptimisticDeving authored Sep 10, 2024
1 parent 8cca595 commit 60b20ab
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/main/java/pw/kaboom/extras/modules/player/PlayerDamage.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package pw.kaboom.extras.modules.player;

import io.papermc.paper.event.world.WorldGameRuleChangeEvent;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.GameRule;
import org.bukkit.World;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
Expand All @@ -9,6 +12,7 @@
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
Expand All @@ -19,7 +23,13 @@
import org.bukkit.potion.PotionEffect;
import pw.kaboom.extras.util.Utility;

import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Objects;

public final class PlayerDamage implements Listener {
private final Map<World, Boolean> deathMessageToggles = new IdentityHashMap<>();

@EventHandler
void onEntityDamage(final EntityDamageEvent event) {
if (EntityType.PLAYER.equals(event.getEntityType())) {
Expand Down Expand Up @@ -48,12 +58,28 @@ void onFoodLevelChange(final FoodLevelChangeEvent event) {
}
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
void onGameRuleChange(final WorldGameRuleChangeEvent event) {
if (event.getGameRule() != GameRule.SHOW_DEATH_MESSAGES) {
return;
}

this.deathMessageToggles.put(event.getWorld(), Boolean.parseBoolean(event.getValue()));
}

@EventHandler
void onPlayerDeath(final PlayerDeathEvent event) {
final Player player = event.getEntity();
final Component deathMessage = event.deathMessage();

for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
onlinePlayer.sendMessage(event.deathMessage());
if (deathMessage != null && this.deathMessageToggles.computeIfAbsent(
player.getWorld(),
(key) -> Objects.requireNonNullElse(
key.getGameRuleValue(GameRule.SHOW_DEATH_MESSAGES),
key.getGameRuleDefault(GameRule.SHOW_DEATH_MESSAGES)
)
)) {
Bukkit.broadcast(deathMessage);
}

try {
Expand Down

0 comments on commit 60b20ab

Please sign in to comment.