Skip to content

Commit

Permalink
Added ability to use HEX color codes or normal color codes for mob na…
Browse files Browse the repository at this point in the history
…me in Messages.yml
  • Loading branch information
Dreeam-qwq committed Dec 16, 2024
1 parent b928484 commit 8edec91
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -843,8 +843,8 @@ public static Component entityDeathPlaceholders(Component msg, Player player, En
final boolean hasBiome = msg.contains(Component.text("%biome%"));
final boolean hasDistance = msg.contains(Component.text("%distance%"));

msg = msg.replaceText(Util.replace("%entity%", Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(entity))))
.replaceText(Util.replace("%entity_display%", entity.getCustomName() != null ? entity.getCustomName() : Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(entity))))
msg = msg.replaceText(Util.replace("%entity%", EntityUtil.getEntityCustomNameComponent(entity)))
.replaceText(Util.replace("%entity_display%", entity.getCustomName() != null ? entity.getCustomName() : EntityUtil.getEntityCustomName(entity)))
.replaceText(Util.replace("%killer%", Util.getPlayerName(player)))
.replaceText(Util.replace("%killer_display%", Util.getPlayerDisplayName(player)))
.replaceText(Util.replace("%world%", entity.getLocation().getWorld().getName()))
Expand Down Expand Up @@ -892,8 +892,8 @@ public static String entityDeathPlaceholders(String msg, Player player, Entity e
final boolean hasBiome = msg.contains("%biome%");
final boolean hasDistance = msg.contains("%distance%");

msg = msg.replaceAll("%entity%", Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(entity)))
.replaceAll("%entity_display%", entity.getCustomName() != null ? entity.getCustomName() : Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(entity)))
msg = msg.replaceAll("%entity%", EntityUtil.getEntityCustomName(entity))
.replaceAll("%entity_display%", entity.getCustomName() != null ? entity.getCustomName() : EntityUtil.getEntityCustomName(entity))
.replaceAll("%killer%", Util.getPlayerName(player))
.replaceAll("%killer_display%", Util.getPlayerDisplayName(player))
.replaceAll("%world%", entity.getLocation().getWorld().getName())
Expand Down Expand Up @@ -964,23 +964,24 @@ public static Component playerDeathPlaceholders(Component msg, PlayerManager pm,
}

if (mob != null) {
String mobName = mob.getName();
String mobNameStr = mob.getName();
Component mobName = Component.text(mobNameStr);
if (Settings.getInstance().getConfig().getBoolean(Config.RENAME_MOBS_ENABLED.getPath())) {
String[] chars = Settings.getInstance().getConfig().getString(Config.RENAME_MOBS_IF_CONTAINS.getPath()).split("(?!^)");
for (String ch : chars) {
if (mobName.contains(ch)) {
mobName = Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(mob));
if (mobNameStr.contains(ch)) {
mobName = EntityUtil.getEntityCustomNameComponent(mob);
break;
}
}
}

if (!(mob instanceof Player) && Settings.getInstance().getConfig().getBoolean(Config.DISABLE_NAMED_MOBS.getPath())) {
mobName = Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(mob));
mobName = EntityUtil.getEntityCustomNameComponent(mob);
}

msg = msg.replaceText(Util.replace("%killer%", mobName))
.replaceText(Util.replace("%killer_type%", Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(mob))));
.replaceText(Util.replace("%killer_type%", EntityUtil.getEntityCustomNameComponent(mob)));

if (mob instanceof Player) {
Player p = (Player) mob;
Expand Down Expand Up @@ -1037,18 +1038,18 @@ public static String playerDeathPlaceholders(String msg, PlayerManager pm, Entit
String[] chars = Settings.getInstance().getConfig().getString(Config.RENAME_MOBS_IF_CONTAINS.getPath()).split("(?!^)");
for (String ch : chars) {
if (mobName.contains(ch)) {
mobName = Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(mob));
mobName = EntityUtil.getEntityCustomName(mob);
break;
}
}
}

if (!(mob instanceof Player) && Settings.getInstance().getConfig().getBoolean(Config.DISABLE_NAMED_MOBS.getPath())) {
mobName = Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(mob));
mobName = EntityUtil.getEntityCustomName(mob);
}

msg = msg.replaceAll("%killer%", mobName)
.replaceAll("%killer_type%", Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(mob)));
.replaceAll("%killer_type%", EntityUtil.getEntityCustomName(mob));

if (mob instanceof Player) {
Player p = (Player) mob;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package dev.mrshawn.deathmessages.utils;

import dev.mrshawn.deathmessages.config.Messages;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Tameable;

Expand Down Expand Up @@ -96,6 +99,18 @@ public static String getConfigNodeByEntity(Entity e) {
return e.getType().getEntityClass().getSimpleName().toLowerCase();
}

public static Component getEntityCustomNameComponent(Entity e) {
final String rawName = Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(e));

return Util.convertFromLegacy(rawName);
}

public static String getEntityCustomName(Entity e) {
final String rawName = Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(e));

return PlainTextComponentSerializer.plainText().serialize(Util.convertFromLegacy(rawName));
}

// Reduce directly detect hasOwner as few as possible just makes it looks better
public static boolean hasOwner(Entity e) {
if (e instanceof Tameable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -837,8 +837,8 @@ public static Component entityDeathPlaceholders(Component msg, Player player, En
final boolean hasBiome = msg.contains(Component.text("%biome%"));
final boolean hasDistance = msg.contains(Component.text("%distance%"));

msg = msg.replaceText(Util.replace("%entity%", Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(entity))))
.replaceText(Util.replace("%entity_display%", entity.getCustomName() != null ? entity.getCustomName() : Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(entity))))
msg = msg.replaceText(Util.replace("%entity%", EntityUtil.getEntityCustomNameComponent(entity)))
.replaceText(Util.replace("%entity_display%", entity.customName() != null ? entity.customName() : EntityUtil.getEntityCustomNameComponent(entity)))
.replaceText(Util.replace("%killer%", Util.getPlayerName(player)))
.replaceText(Util.replace("%killer_display%", Util.getPlayerDisplayNameComponent(player)))
.replaceText(Util.replace("%world%", entity.getLocation().getWorld().getName()))
Expand Down Expand Up @@ -886,8 +886,8 @@ public static String entityDeathPlaceholders(String msg, Player player, Entity e
final boolean hasBiome = msg.contains("%biome%");
final boolean hasDistance = msg.contains("%distance%");

msg = msg.replaceAll("%entity%", Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(entity)))
.replaceAll("%entity_display%", entity.getCustomName() != null ? entity.getCustomName() : Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(entity)))
msg = msg.replaceAll("%entity%", EntityUtil.getEntityCustomName(entity))
.replaceAll("%entity_display%", entity.getCustomName() != null ? entity.getCustomName() : EntityUtil.getEntityCustomName(entity))
.replaceAll("%killer%", Util.getPlayerName(player))
.replaceAll("%killer_display%", Util.getPlayerDisplayName(player))
.replaceAll("%world%", entity.getLocation().getWorld().getName())
Expand Down Expand Up @@ -958,23 +958,24 @@ public static Component playerDeathPlaceholders(Component msg, PlayerManager pm,
}

if (mob != null) {
String mobName = mob.getName();
String mobNameStr = mob.getName();
Component mobName = Component.text(mobNameStr);
if (Settings.getInstance().getConfig().getBoolean(Config.RENAME_MOBS_ENABLED.getPath())) {
String[] chars = Settings.getInstance().getConfig().getString(Config.RENAME_MOBS_IF_CONTAINS.getPath()).split("(?!^)");
for (String ch : chars) {
if (mobName.contains(ch)) {
mobName = Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(mob));
if (mobNameStr.contains(ch)) {
mobName = EntityUtil.getEntityCustomNameComponent(mob);
break;
}
}
}

if (!(mob instanceof Player) && Settings.getInstance().getConfig().getBoolean(Config.DISABLE_NAMED_MOBS.getPath())) {
mobName = Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(mob));
mobName = EntityUtil.getEntityCustomNameComponent(mob);
}

msg = msg.replaceText(Util.replace("%killer%", mobName))
.replaceText(Util.replace("%killer_type%", Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(mob))));
.replaceText(Util.replace("%killer_type%", EntityUtil.getEntityCustomNameComponent(mob)));

if (mob instanceof Player) {
Player p = (Player) mob;
Expand Down Expand Up @@ -1031,18 +1032,18 @@ public static String playerDeathPlaceholders(String msg, PlayerManager pm, Entit
String[] chars = Settings.getInstance().getConfig().getString(Config.RENAME_MOBS_IF_CONTAINS.getPath()).split("(?!^)");
for (String ch : chars) {
if (mobName.contains(ch)) {
mobName = Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(mob));
mobName = EntityUtil.getEntityCustomName(mob);
break;
}
}
}

if (!(mob instanceof Player) && Settings.getInstance().getConfig().getBoolean(Config.DISABLE_NAMED_MOBS.getPath())) {
mobName = Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(mob));
mobName = EntityUtil.getEntityCustomName(mob);
}

msg = msg.replaceAll("%killer%", mobName)
.replaceAll("%killer_type%", Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(mob)));
.replaceAll("%killer_type%", EntityUtil.getEntityCustomName(mob));

if (mob instanceof Player) {
Player p = (Player) mob;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package dev.mrshawn.deathmessages.utils;

import dev.mrshawn.deathmessages.config.Messages;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Tameable;

Expand Down Expand Up @@ -96,6 +99,18 @@ public static String getConfigNodeByEntity(Entity e) {
return e.getType().getEntityClass().getSimpleName().toLowerCase();
}

public static Component getEntityCustomNameComponent(Entity e) {
final String rawName = Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(e));

return Util.convertFromLegacy(rawName);
}

public static String getEntityCustomName(Entity e) {
final String rawName = Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(e));

return PlainTextComponentSerializer.plainText().serialize(Util.convertFromLegacy(rawName));
}

// Reduce directly detect hasOwner as few as possible just makes it looks better
public static boolean hasOwner(Entity e) {
if (e instanceof Tameable) {
Expand Down

0 comments on commit 8edec91

Please sign in to comment.