Skip to content

Commit

Permalink
Refactor death message build & event params pass
Browse files Browse the repository at this point in the history
Split the prefix and message body from the completed built death message.
In the past, the death message component pass as one singe TextComponent.
And now, it is changed to TextComponent[], the first element is prefix component,
and the second the death message body.

By separate passing to event, it will build / append during broadcast stage (In BroadcastXXXDeathListener)
and Discord hook message send stage (In each Discord extension hook). Build twice or more for now,
instead of appending them during death message build.

Build twice: or more
1. In broadcastListener: console message + broadcast message to players
2. In DiscordSRV hook: discord format death message sent to discord
3. In other Discord integration hooks will add in the future

There are also duplicate / similar code, but is not a big deal.
  • Loading branch information
Dreeam-qwq committed Dec 16, 2024
1 parent d2c963c commit fa9998f
Show file tree
Hide file tree
Showing 21 changed files with 736 additions and 413 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@ public class BroadcastDeathMessageEvent extends Event implements Cancellable {
private final LivingEntity livingEntity;
private final MessageType messageType;
private final TextComponent textComponent;
private final TextComponent[] textComponents; // 0: Prefix, 1: Message body
private final boolean isGangDeath;
private final List<World> broadcastedWorlds;
private boolean isCancelled;

private static final HandlerList HANDLERS = new HandlerList();

public BroadcastDeathMessageEvent(Player player, LivingEntity livingEntity, MessageType messageType, TextComponent textComponent,
public BroadcastDeathMessageEvent(Player player, LivingEntity livingEntity, MessageType messageType, TextComponent textComponent, TextComponent[] textComponents,
List<World> broadcastedWorlds, boolean isGangDeath) {
this.player = player;
this.livingEntity = livingEntity;
this.messageType = messageType;
this.textComponent = textComponent;
this.textComponents = textComponents;
this.broadcastedWorlds = broadcastedWorlds;
this.isGangDeath = isGangDeath;
this.isCancelled = false;
Expand Down Expand Up @@ -67,10 +69,16 @@ public MessageType getMessageType() {
return this.messageType;
}

// forRemoval = true, since = "1.4.21"
@Deprecated
public TextComponent getTextComponent() {
return this.textComponent;
}

public TextComponent[] getTextComponents() {
return this.textComponents;
}

public boolean isGangDeath() {
return this.isGangDeath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@ public class BroadcastEntityDeathMessageEvent extends Event implements Cancellab
private final Entity entity;
private final MessageType messageType;
private final TextComponent textComponent;
private final TextComponent[] textComponents; // 0: Prefix, 1: Message body
private final List<World> broadcastedWorlds;
private boolean isCancelled;

private static final HandlerList HANDLERS = new HandlerList();

public BroadcastEntityDeathMessageEvent(PlayerManager pm, Entity entity, MessageType messageType, TextComponent textComponent,
public BroadcastEntityDeathMessageEvent(PlayerManager pm, Entity entity, MessageType messageType, TextComponent textComponent, TextComponent[] textComponents,
List<World> broadcastedWorlds) {
this.player = pm;
this.entity = entity;
this.messageType = messageType;
this.textComponent = textComponent;
this.textComponents = textComponents;
this.broadcastedWorlds = broadcastedWorlds;
this.isCancelled = false;
}
Expand Down Expand Up @@ -67,10 +69,16 @@ public MessageType getMessageType() {
return this.messageType;
}

// forRemoval = true, since = "1.4.21"
@Deprecated
public TextComponent getTextComponent() {
return this.textComponent;
}

public TextComponent[] getTextComponents() {
return this.textComponents;
}

public List<World> getBroadcastedWorlds() {
return this.broadcastedWorlds;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import github.scarsz.discordsrv.dependencies.jda.api.entities.MessageEmbed;
import github.scarsz.discordsrv.dependencies.jda.api.entities.TextChannel;
import github.scarsz.discordsrv.util.DiscordUtil;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.configuration.file.FileConfiguration;
Expand All @@ -32,8 +33,8 @@ public class DiscordSRVExtension {
public DiscordSRVExtension() {
}

public void sendDiscordMessage(PlayerManager pm, MessageType messageType, String message) {
List<String> channels = DiscordAssets.getInstance().getIDs(messageType);
public void sendDiscordMessage(TextComponent[] components, MessageType messageType, PlayerManager pm) {
final List<String> channels = DiscordAssets.getInstance().getIDs(messageType);

for (String groups : channels) {
if (!groups.contains(":")) {
Expand All @@ -57,18 +58,22 @@ public void sendDiscordMessage(PlayerManager pm, MessageType messageType, String
}

TextChannel textChannel = g.getTextChannelById(channelID);
String prefix = components[0] != null ? PlainTextComponentSerializer.plainText().serialize(components[0]) : "";
String messageBody = PlainTextComponentSerializer.plainText().serialize(components[1]);
String message;

if (getMessages().getBoolean("Discord.DeathMessage.Remove-Plugin-Prefix")
&& FileStore.CONFIG.getBoolean(Config.ADD_PREFIX_TO_ALL_MESSAGES)) {
message = messageBody;
} else {
message = prefix + messageBody;
}

// Try to strip Minecraft format code to plain text
if (message.contains("§")) {
message = STRIP_COLOR_PATTERN.matcher(message).replaceAll("");
}

if (getMessages().getBoolean("Discord.DeathMessage.Remove-Plugin-Prefix")
&& FileStore.CONFIG.getBoolean(Config.ADD_PREFIX_TO_ALL_MESSAGES)) {
String prefix = PlainTextComponentSerializer.plainText().serialize(Util.convertFromLegacy(getMessages().getString("Prefix")));
message = message.substring(prefix.length());
}

if (getMessages().getString("Discord.DeathMessage.Text").isEmpty()) {
textChannel.sendMessage(buildMessage(pm, message)).queue();
} else {
Expand All @@ -90,8 +95,8 @@ public void sendDiscordMessage(PlayerManager pm, MessageType messageType, String
}
}

public void sendEntityDiscordMessage(String rawMessage, PlayerManager pm, Entity entity, boolean hasOwner, MessageType messageType) {
List<String> channels = DiscordAssets.getInstance().getIDs(messageType);
public void sendEntityDiscordMessage(TextComponent[] components, MessageType messageType, PlayerManager pm, Entity entity, boolean hasOwner) {
final List<String> channels = DiscordAssets.getInstance().getIDs(messageType);

for (String groups : channels) {
if (!groups.contains(":")) {
Expand All @@ -110,9 +115,24 @@ public void sendEntityDiscordMessage(String rawMessage, PlayerManager pm, Entity
}

TextChannel textChannel = g.getTextChannelById(channelID);
String prefix = components[0] != null ? PlainTextComponentSerializer.plainText().serialize(components[0]) : "";
String messageBody = PlainTextComponentSerializer.plainText().serialize(components[1]);
String message;

if (getMessages().getBoolean("Discord.DeathMessage.Remove-Plugin-Prefix")
&& FileStore.CONFIG.getBoolean(Config.ADD_PREFIX_TO_ALL_MESSAGES)) {
message = messageBody;
} else {
message = prefix + messageBody;
}

// Try to strip Minecraft format code to plain text
if (message.contains("§")) {
message = STRIP_COLOR_PATTERN.matcher(message).replaceAll("");
}

if (getMessages().getString("Discord.DeathMessage.Text").isEmpty()) {
textChannel.sendMessage(buildMessage(rawMessage, pm.getPlayer(), entity, hasOwner))
textChannel.sendMessage(buildMessage(message, pm.getPlayer(), entity, hasOwner))
.queue();
} else {
String[] spl = getMessages().getString("Discord.DeathMessage.Text").split("\\\\n");
Expand All @@ -123,11 +143,11 @@ public void sendEntityDiscordMessage(String rawMessage, PlayerManager pm, Entity

if (pm.getLastEntityDamager() instanceof FallingBlock) {
textChannel.sendMessage(Util.convertToLegacy(Assets.playerDeathPlaceholders(Util.convertFromLegacy(sb.toString()), pm, null)
.replaceText(TextReplacementConfig.builder().matchLiteral("%message%").replacement(rawMessage).build())))
.replaceText(TextReplacementConfig.builder().matchLiteral("%message%").replacement(message).build())))
.queue();
} else {
textChannel.sendMessage(Util.convertToLegacy(Assets.playerDeathPlaceholders(Util.convertFromLegacy(sb.toString()), pm, pm.getLastEntityDamager())
.replaceText(TextReplacementConfig.builder().matchLiteral("%message%").replacement(rawMessage).build())))
.replaceText(TextReplacementConfig.builder().matchLiteral("%message%").replacement(message).build())))
.queue();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,20 @@

import dev.mrshawn.deathmessages.DeathMessages;
import dev.mrshawn.deathmessages.api.EntityManager;
import dev.mrshawn.deathmessages.api.ExplosionManager;
import dev.mrshawn.deathmessages.api.PlayerManager;
import dev.mrshawn.deathmessages.api.events.BroadcastDeathMessageEvent;
import dev.mrshawn.deathmessages.api.events.BroadcastEntityDeathMessageEvent;
import dev.mrshawn.deathmessages.config.Gangs;
import dev.mrshawn.deathmessages.config.Settings;
import dev.mrshawn.deathmessages.enums.MessageType;
import dev.mrshawn.deathmessages.enums.MobType;
import dev.mrshawn.deathmessages.files.Config;
import dev.mrshawn.deathmessages.utils.Assets;
import dev.mrshawn.deathmessages.utils.EntityUtil;
import dev.mrshawn.deathmessages.utils.Util;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.AreaEffectCloud;
import org.bukkit.entity.EnderCrystal;
import org.bukkit.entity.EnderDragon;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Firework;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
Expand Down Expand Up @@ -59,44 +49,18 @@ void onEntityDeath(EntityDeathEvent e) {
if (pm.isBlacklisted()) return;

if (!(pm.getLastEntityDamager() instanceof LivingEntity) || pm.getLastEntityDamager() == e.getEntity()) {
// Natural Death
TextComponent naturalDeath = Component.empty();
if (pm.getLastExplosiveEntity() instanceof EnderCrystal) {
naturalDeath = Assets.getNaturalDeath(pm, "End-Crystal");
} else if (pm.getLastExplosiveEntity() instanceof TNTPrimed) {
naturalDeath = Assets.getNaturalDeath(pm, "TNT");
} else if (pm.getLastExplosiveEntity() instanceof Firework) {
naturalDeath = Assets.getNaturalDeath(pm, "Firework");
} else if (pm.getLastDamage().equals(EntityDamageEvent.DamageCause.FALL)) {
naturalDeath = Assets.getNaturalDeath(pm, "Climbable");
} else if (pm.getLastDamage().equals(EntityDamageEvent.DamageCause.BLOCK_EXPLOSION)) {
Optional<ExplosionManager> explosion = ExplosionManager.getManagerIfEffected(player.getUniqueId());
if (explosion.isPresent()) {
if (explosion.get().getMaterial().name().contains("BED")) {
naturalDeath = Assets.getNaturalDeath(pm, "Bed");
}
if (Util.isNewerAndEqual(16, 0)) {
if (explosion.get().getMaterial().equals(Material.RESPAWN_ANCHOR)) {
naturalDeath = Assets.getNaturalDeath(pm, "Respawn-Anchor");
}
}
// Dreeam TODO: Check weather needs to handle unknow explosion to prevent potential empty death message
}
} else if (pm.getLastDamage().equals(EntityDamageEvent.DamageCause.PROJECTILE)) {
naturalDeath = Assets.getNaturalDeath(pm, Assets.getSimpleProjectile(pm.getLastProjectileEntity()));
} else if (Util.isNewerAndEqual(9, 0) && Util.isOlderAndEqual(999, 999) && pm.getLastEntityDamager() instanceof AreaEffectCloud) { // Fix MC-84595 - Killed by Dragon's Breath
AreaEffectCloud cloud = (AreaEffectCloud) pm.getLastEntityDamager();
if (cloud.getSource() instanceof EnderDragon) {
pm.setLastDamageCause(
Settings.getInstance().getConfig().getBoolean(Config.FIX_MC_84595.getPath())
? EntityDamageEvent.DamageCause.DRAGON_BREATH : EntityDamageEvent.DamageCause.ENTITY_ATTACK
);
}
naturalDeath = Assets.getNaturalDeath(pm, Assets.getSimpleCause(pm.getLastDamage()));
} else {
naturalDeath = Assets.getNaturalDeath(pm, Assets.getSimpleCause(pm.getLastDamage()));
}
BroadcastDeathMessageEvent event = new BroadcastDeathMessageEvent(player, null, MessageType.NATURAL, naturalDeath, Util.getBroadcastWorlds(player), false);
TextComponent[] naturalDeath = Assets.playerNatureDeathMessage(pm, player);
TextComponent oldNaturalDeath = naturalDeath[0].append(naturalDeath[1]); // Dreeam TODO: Remove in 1.4.21

BroadcastDeathMessageEvent event = new BroadcastDeathMessageEvent(
player,
null,
MessageType.NATURAL,
oldNaturalDeath,
naturalDeath,
Util.getBroadcastWorlds(player),
false
);
Bukkit.getPluginManager().callEvent(event);
} else {
// Killed by mob
Expand All @@ -119,22 +83,42 @@ void onEntityDeath(EntityDeathEvent e) {
totalMobEntities++;
}
}

if (totalMobEntities >= amount) {
gangKill = true;
}
}
TextComponent playerDeath = Assets.playerDeathMessage(pm, gangKill);

TextComponent[] playerDeath = Assets.playerDeathMessage(pm, gangKill);
TextComponent oldPlayerDeath = playerDeath[0].append(playerDeath[1]); // Dreeam TODO: Remove in 1.4.21

if (ent instanceof Player) {
BroadcastDeathMessageEvent event = new BroadcastDeathMessageEvent(player,
(LivingEntity) pm.getLastEntityDamager(), MessageType.PLAYER, playerDeath, Util.getBroadcastWorlds(player), gangKill);
BroadcastDeathMessageEvent event = new BroadcastDeathMessageEvent(
player,
(LivingEntity) pm.getLastEntityDamager(),
MessageType.PLAYER,
oldPlayerDeath,
playerDeath,
Util.getBroadcastWorlds(player),
gangKill
);
Bukkit.getPluginManager().callEvent(event);
return;
}
BroadcastDeathMessageEvent event = new BroadcastDeathMessageEvent(player,
(LivingEntity) pm.getLastEntityDamager(), MessageType.MOB, playerDeath, Util.getBroadcastWorlds(player), gangKill);

BroadcastDeathMessageEvent event = new BroadcastDeathMessageEvent(
player,
(LivingEntity) pm.getLastEntityDamager(),
MessageType.MOB,
oldPlayerDeath,
playerDeath,
Util.getBroadcastWorlds(player),
gangKill
);
Bukkit.getPluginManager().callEvent(event);
}
});

if (!getPlayer.isPresent()) {
new PlayerManager(player);
}
Expand All @@ -152,8 +136,17 @@ void onEntityDeath(EntityDeathEvent e) {
PlayerManager damager = em.getLastPlayerDamager();
if (damager == null) return; // Entity killed by Entity should not include in DM

TextComponent entityDeath = Assets.entityDeathMessage(em, mobType);
BroadcastEntityDeathMessageEvent event = new BroadcastEntityDeathMessageEvent(damager, e.getEntity(), MessageType.ENTITY, entityDeath, Util.getBroadcastWorlds(e.getEntity()));
TextComponent[] entityDeath = Assets.entityDeathMessage(em, mobType);
TextComponent oldEntityDeath = entityDeath[0].append(entityDeath[1]); // Dreeam TODO: Remove in 1.4.21

BroadcastEntityDeathMessageEvent event = new BroadcastEntityDeathMessageEvent(
damager,
e.getEntity(),
MessageType.ENTITY,
oldEntityDeath,
entityDeath,
Util.getBroadcastWorlds(e.getEntity())
);
Bukkit.getPluginManager().callEvent(event);
});
}
Expand Down
Loading

0 comments on commit fa9998f

Please sign in to comment.