forked from kaboomserver/extras
Respect death messages gamerule (#359)
* 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
This commit is contained in:
parent
8cca595937
commit
60b20ab4bf
1 changed files with 28 additions and 2 deletions
|
@ -1,6 +1,9 @@
|
||||||
package pw.kaboom.extras.modules.player;
|
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.Bukkit;
|
||||||
|
import org.bukkit.GameRule;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.attribute.Attribute;
|
import org.bukkit.attribute.Attribute;
|
||||||
import org.bukkit.attribute.AttributeInstance;
|
import org.bukkit.attribute.AttributeInstance;
|
||||||
|
@ -9,6 +12,7 @@ import org.bukkit.entity.ExperienceOrb;
|
||||||
import org.bukkit.entity.HumanEntity;
|
import org.bukkit.entity.HumanEntity;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.EventPriority;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.entity.EntityDamageEvent;
|
import org.bukkit.event.entity.EntityDamageEvent;
|
||||||
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
|
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
|
||||||
|
@ -19,7 +23,13 @@ import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.potion.PotionEffect;
|
import org.bukkit.potion.PotionEffect;
|
||||||
import pw.kaboom.extras.util.Utility;
|
import pw.kaboom.extras.util.Utility;
|
||||||
|
|
||||||
|
import java.util.IdentityHashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
public final class PlayerDamage implements Listener {
|
public final class PlayerDamage implements Listener {
|
||||||
|
private final Map<World, Boolean> deathMessageToggles = new IdentityHashMap<>();
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
void onEntityDamage(final EntityDamageEvent event) {
|
void onEntityDamage(final EntityDamageEvent event) {
|
||||||
if (EntityType.PLAYER.equals(event.getEntityType())) {
|
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
|
@EventHandler
|
||||||
void onPlayerDeath(final PlayerDeathEvent event) {
|
void onPlayerDeath(final PlayerDeathEvent event) {
|
||||||
final Player player = event.getEntity();
|
final Player player = event.getEntity();
|
||||||
|
final Component deathMessage = event.deathMessage();
|
||||||
|
|
||||||
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
|
if (deathMessage != null && this.deathMessageToggles.computeIfAbsent(
|
||||||
onlinePlayer.sendMessage(event.deathMessage());
|
player.getWorld(),
|
||||||
|
(key) -> Objects.requireNonNullElse(
|
||||||
|
key.getGameRuleValue(GameRule.SHOW_DEATH_MESSAGES),
|
||||||
|
key.getGameRuleDefault(GameRule.SHOW_DEATH_MESSAGES)
|
||||||
|
)
|
||||||
|
)) {
|
||||||
|
Bukkit.broadcast(deathMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Reference in a new issue