Merge branch 'respect-death-messages-gamerule' of https://github.com/OptimisticDeving/extras

This commit is contained in:
Chipmunk 2024-06-24 22:02:49 -04:00
commit 86e6f8b57e

View file

@ -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;
@ -9,6 +12,7 @@ import org.bukkit.entity.ExperienceOrb;
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;
@ -19,7 +23,13 @@ import org.bukkit.inventory.ItemStack;
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())) {
@ -48,12 +58,28 @@ public final class PlayerDamage implements Listener {
}
}
@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 {