Skip to content

Commit

Permalink
Some update for message sending
Browse files Browse the repository at this point in the history
* Don't call internal message broadcast event if message is empty.
* Don't send mm mob related death message if there is no mm mob death message configured.
  • Loading branch information
Dreeam-qwq committed Dec 30, 2024
1 parent 432c2d2 commit 2ad8268
Show file tree
Hide file tree
Showing 14 changed files with 224 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import dev.mrshawn.deathmessages.enums.MessageType;
import dev.mrshawn.deathmessages.enums.MobType;
import dev.mrshawn.deathmessages.utils.Assets;
import dev.mrshawn.deathmessages.utils.ComponentUtil;
import dev.mrshawn.deathmessages.utils.EntityUtil;
import dev.mrshawn.deathmessages.utils.Util;
import net.kyori.adventure.text.TextComponent;
Expand Down Expand Up @@ -48,18 +49,20 @@ void onEntityDeath(EntityDeathEvent e) {

if (!(pm.getLastEntityDamager() instanceof LivingEntity) || pm.getLastEntityDamager() == e.getEntity()) {
TextComponent[] naturalDeath = Assets.playerNatureDeathMessage(pm, player);
TextComponent oldNaturalDeath = naturalDeath[0] != null ? naturalDeath[0].append(naturalDeath[1]) : 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);
TextComponent oldNaturalDeath = naturalDeath[0].append(naturalDeath[1]); // Dreeam TODO: Remove in 1.4.21

if (!ComponentUtil.isMessageEmpty(naturalDeath)) {
BroadcastDeathMessageEvent event = new BroadcastDeathMessageEvent(
player,
null,
MessageType.NATURAL,
oldNaturalDeath,
naturalDeath,
Util.getBroadcastWorlds(player),
false
);
Bukkit.getPluginManager().callEvent(event);
}
} else {
// Killed by mob
Entity ent = pm.getLastEntityDamager();
Expand Down Expand Up @@ -88,32 +91,21 @@ void onEntityDeath(EntityDeathEvent e) {
}

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

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

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

Expand All @@ -135,17 +127,19 @@ void onEntityDeath(EntityDeathEvent e) {
if (damager == null) return; // Entity killed by Entity should not include in DM

TextComponent[] entityDeath = Assets.entityDeathMessage(em, mobType);
TextComponent oldEntityDeath = entityDeath[0] != null ? entityDeath[0].append(entityDeath[1]) : 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);
TextComponent oldEntityDeath = entityDeath[0].append(entityDeath[1]); // Dreeam TODO: Remove in 1.4.21

if (!ComponentUtil.isMessageEmpty(entityDeath)) {
BroadcastEntityDeathMessageEvent event = new BroadcastEntityDeathMessageEvent(
damager,
e.getEntity(),
MessageType.ENTITY,
oldEntityDeath,
entityDeath,
Util.getBroadcastWorlds(e.getEntity())
);
Bukkit.getPluginManager().callEvent(event);
}
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import dev.mrshawn.deathmessages.enums.MessageType;
import dev.mrshawn.deathmessages.files.Config;
import dev.mrshawn.deathmessages.utils.Assets;
import dev.mrshawn.deathmessages.utils.ComponentUtil;
import dev.mrshawn.deathmessages.utils.Util;
import net.kyori.adventure.text.TextComponent;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -66,18 +67,20 @@ public void untagPlayer(PlayerUntagEvent e) {

deathMessage[1] = deathMessageBody;

TextComponent oldDeathMessage = deathMessage[0] != null ? deathMessage[0].append(deathMessage[1]) : deathMessage[1]; // Dreeam TODO: Remove in 1.4.21

BroadcastDeathMessageEvent event = new BroadcastDeathMessageEvent(
player,
(LivingEntity) e.getPreviousEnemies().get(0),
MessageType.PLAYER,
oldDeathMessage,
deathMessage,
Util.getBroadcastWorlds(player),
gangKill
);
Bukkit.getPluginManager().callEvent(event);
TextComponent oldDeathMessage = deathMessage[0].append(deathMessage[1]); // Dreeam TODO: Remove in 1.4.21

if (!ComponentUtil.isMessageEmpty(deathMessage)) {
BroadcastDeathMessageEvent event = new BroadcastDeathMessageEvent(
player,
(LivingEntity) e.getPreviousEnemies().get(0),
MessageType.PLAYER,
oldDeathMessage,
deathMessage,
Util.getBroadcastWorlds(player),
gangKill
);
Bukkit.getPluginManager().callEvent(event);
}
});

if (!getPlayer.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,13 @@ public class BroadcastEntityDeathListener implements Listener {

@EventHandler
public void broadcastListener(BroadcastEntityDeathMessageEvent e) {
if (e.getTextComponents() == ComponentUtil.EMPTY)
return; // Dreeam - in Assets: return null -> return ComponentUtil.EMPTY

final Optional<PlayerManager> pm = Optional.of(e.getPlayer());
final Entity entity = e.getEntity();
final boolean hasOwner = EntityUtil.hasOwner(entity);
final TextComponent[] components = e.getTextComponents();
final TextComponent prefix = components[0];
final TextComponent messageBody = components[1];
final TextComponent message = prefix != null ? prefix.append(messageBody) : messageBody;
final TextComponent message = prefix.append(messageBody);

if (Messages.getInstance().getConfig().getBoolean("Console.Enabled")) {
Component rawMessage = Util.convertFromLegacy(Messages.getInstance().getConfig().getString("Console.Message"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,14 @@ public class BroadcastPlayerDeathListener implements Listener {

@EventHandler
public void broadcastListener(BroadcastDeathMessageEvent e) {
if (e.getTextComponents() == ComponentUtil.EMPTY)
return; // Dreeam - in Assets: return null -> return ComponentUtil.EMPTY

Optional<PlayerManager> getPlayer = PlayerManager.getPlayer(e.getPlayer());

if (!getPlayer.isPresent()) return;

final TextComponent[] components = e.getTextComponents();
final TextComponent prefix = components[0];
final TextComponent messageBody = components[1];
final TextComponent message = prefix != null ? prefix.append(messageBody) : messageBody;
final TextComponent message = prefix.append(messageBody);

if (Messages.getInstance().getConfig().getBoolean("Console.Enabled")) {
// Dreeam TODO: maybe just use formatMessage is also ok?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import dev.mrshawn.deathmessages.enums.MessageType;
import dev.mrshawn.deathmessages.enums.MobType;
import dev.mrshawn.deathmessages.utils.Assets;
import dev.mrshawn.deathmessages.utils.ComponentUtil;
import dev.mrshawn.deathmessages.utils.Util;
import io.lumine.mythic.bukkit.events.MythicMobDeathEvent;
import net.kyori.adventure.text.TextComponent;
Expand Down Expand Up @@ -36,17 +37,19 @@ public void onMythicMobDeath(MythicMobDeathEvent e) {
getEntity.ifPresent(em -> {
PlayerManager damager = em.getLastPlayerDamager();
TextComponent[] mythicDeath = Assets.entityDeathMessage(em, MobType.MYTHIC_MOB);
TextComponent oldMythicDeath = mythicDeath[0] != null ? mythicDeath[0].append(mythicDeath[1]) : mythicDeath[1]; // Dreeam TODO: Remove in 1.4.21

BroadcastEntityDeathMessageEvent event = new BroadcastEntityDeathMessageEvent(
damager,
e.getEntity(),
MessageType.ENTITY,
oldMythicDeath,
mythicDeath,
Util.getBroadcastWorlds(e.getEntity())
);
Bukkit.getPluginManager().callEvent(event);
TextComponent oldMythicDeath = mythicDeath[0].append(mythicDeath[1]); // Dreeam TODO: Remove in 1.4.21

if (!ComponentUtil.isMessageEmpty(mythicDeath)) {
BroadcastEntityDeathMessageEvent event = new BroadcastEntityDeathMessageEvent(
damager,
e.getEntity(),
MessageType.ENTITY,
oldMythicDeath,
mythicDeath,
Util.getBroadcastWorlds(e.getEntity())
);
Bukkit.getPluginManager().callEvent(event);
}
});
}
}
Expand Down
Loading

0 comments on commit 2ad8268

Please sign in to comment.