Skip to content

Commit

Permalink
feat: Add option to display item name based on player client locale
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreeam-qwq committed Jan 10, 2024
1 parent e7337bb commit 8580a82
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 976 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public enum Config {
RENAME_MOBS_IF_CONTAINS("Rename-Mobs.If-Contains", "♡♥❤■"),

DISABLE_NAMED_MOBS("Disable-Named-Mobs", false),
DISPLAY_I18N_ITEM_NAME("I18N-Display.Item-Name", true),
DISPLAY_I18N_MOB_NAME("I18N-Display.Mob-Name", true),

EXPIRE_LAST_DAMAGE_EXPIRE_PLAYER("Expire-Last-Damage.Expire-Player", 7),
EXPIRE_LAST_DAMAGE_EXPIRE_ENTITY("Expire-Last-Damage.Expire-Entity", 7),
Expand Down
77 changes: 48 additions & 29 deletions Core/src/main/java/dev/mrshawn/deathmessages/utils/Assets.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.bukkit.Material;
import org.bukkit.World;
Expand Down Expand Up @@ -317,7 +317,7 @@ public static TextComponent getNaturalDeath(PlayerManager pm, String damageCause
}
}

String displayName;
Component displayName;
if (i.getItemMeta() == null || !i.getItemMeta().hasDisplayName() || i.getItemMeta().getDisplayName().isEmpty()) {
if (Settings.getInstance().getConfig().getBoolean(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_ENABLED.getPath())) {
if (!Settings.getInstance().getConfig().getBoolean(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_IGNORE_ENCHANTMENTS.getPath())) {
Expand All @@ -328,9 +328,9 @@ public static TextComponent getNaturalDeath(PlayerManager pm, String damageCause
return getNaturalDeath(pm, "Projectile-Unknown");
}
}
displayName = convertString(i.getType().name());
displayName = getI18nName(i);
} else {
displayName = i.getItemMeta().getDisplayName();
displayName = convertFromLegacy(i.getItemMeta().getDisplayName());
}

buildHover(pm.getPlayer(), msg, base, i, displayName);
Expand Down Expand Up @@ -387,7 +387,7 @@ public static TextComponent getWeapon(boolean gang, PlayerManager pm, LivingEnti

if (msg.contains("%weapon%")) {
ItemStack i = mob.getEquipment().getItemInMainHand();
String displayName;
Component displayName;
if (i.getItemMeta() == null || !i.getItemMeta().hasDisplayName() || i.getItemMeta().getDisplayName().isEmpty()) {
if (FileStore.INSTANCE.getCONFIG().getBoolean(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_ENABLED)) {
if (!FileStore.INSTANCE.getCONFIG().getBoolean(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_IGNORE_ENCHANTMENTS)) {
Expand All @@ -400,9 +400,9 @@ public static TextComponent getWeapon(boolean gang, PlayerManager pm, LivingEnti
.getString(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_SOURCE_WEAPON_DEFAULT_TO));
}
}
displayName = convertString(i.getType().name());
displayName = getI18nName(i);
} else {
displayName = i.getItemMeta().getDisplayName();
displayName = convertFromLegacy(i.getItemMeta().getDisplayName());
}

buildHover(pm.getPlayer(), msg, base, i, displayName);
Expand Down Expand Up @@ -461,7 +461,7 @@ public static TextComponent getEntityDeathWeapon(Player p, Entity e, MobType mob

if (msg.contains("%weapon%")) {
ItemStack i = p.getEquipment().getItemInMainHand();
String displayName;
Component displayName;
if (i.getItemMeta() == null || !i.getItemMeta().hasDisplayName() || i.getItemMeta().getDisplayName().isEmpty()) {
if (Settings.getInstance().getConfig().getBoolean(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_ENABLED.getPath())) {
if (!Settings.getInstance().getConfig().getBoolean(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_IGNORE_ENCHANTMENTS.getPath())) {
Expand All @@ -474,9 +474,9 @@ public static TextComponent getEntityDeathWeapon(Player p, Entity e, MobType mob
Settings.getInstance().getConfig().getString(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_SOURCE_WEAPON_DEFAULT_TO.getPath()), mobType);
}
}
displayName = convertString(i.getType().name());
displayName = getI18nName(i);
} else {
displayName = i.getItemMeta().getDisplayName();
displayName = convertFromLegacy(i.getItemMeta().getDisplayName());
}

buildHover(p, msg, base, i, displayName);
Expand Down Expand Up @@ -587,17 +587,17 @@ public static TextComponent getProjectile(boolean gang, PlayerManager pm, Living

if (msg.contains("%weapon%") && pm.getLastDamage().equals(EntityDamageEvent.DamageCause.PROJECTILE)) {
ItemStack i = mob.getEquipment().getItemInMainHand();
String displayName;
Component displayName;
if (i.getItemMeta() == null || !i.getItemMeta().hasDisplayName() || i.getItemMeta().getDisplayName().isEmpty()) {
if (Settings.getInstance().getConfig().getBoolean(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_ENABLED.getPath())) {
if (!Settings.getInstance().getConfig().getString(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_SOURCE_PROJECTILE_DEFAULT_TO.getPath())
.equals(projectileDamage)) {
return getProjectile(gang, pm, mob, Settings.getInstance().getConfig().getString(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_SOURCE_PROJECTILE_DEFAULT_TO.getPath()));
}
}
displayName = convertString(i.getType().name());
displayName = getI18nName(i);
} else {
displayName = i.getItemMeta().getDisplayName();
displayName = convertFromLegacy(i.getItemMeta().getDisplayName());
}

buildHover(pm.getPlayer(), msg, base, i, displayName);
Expand Down Expand Up @@ -657,7 +657,7 @@ public static TextComponent getEntityDeathProjectile(Player p, EntityManager em,

if (msg.contains("%weapon%") && em.getLastProjectileEntity() instanceof Arrow) {
ItemStack i = p.getEquipment().getItemInMainHand();
String displayName;
Component displayName;
if (i.getItemMeta() == null || !i.getItemMeta().hasDisplayName() || i.getItemMeta().getDisplayName().isEmpty()) {
if (Settings.getInstance().getConfig().getBoolean(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_ENABLED.getPath())) {
if (!Settings.getInstance().getConfig().getString(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_SOURCE_PROJECTILE_DEFAULT_TO.getPath())
Expand All @@ -666,9 +666,9 @@ public static TextComponent getEntityDeathProjectile(Player p, EntityManager em,
Settings.getInstance().getConfig().getString(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_SOURCE_PROJECTILE_DEFAULT_TO.getPath()), mobType);
}
}
displayName = convertString(i.getType().name());
displayName = getI18nName(i);
} else {
displayName = i.getItemMeta().getDisplayName();
displayName = convertFromLegacy(i.getItemMeta().getDisplayName());
}

buildHover(p, msg, base, i, displayName);
Expand Down Expand Up @@ -747,7 +747,7 @@ public static TextComponent getEntityDeath(Player player, Entity e, String damag
}


private static void buildHover(Player player, String msg, TextComponent.Builder base, ItemStack i, String displayName) {
private static void buildHover(Player player, String msg, TextComponent.Builder base, ItemStack i, Component displayName) {

HoverEvent<HoverEvent.ShowItem> showItem;

Expand All @@ -770,7 +770,7 @@ private static void buildHover(Player player, String msg, TextComponent.Builder
}

Component weapon = Component.text()
.append(convertFromLegacy(displayName))
.append(displayName)
.build()
.hoverEvent(showItem);

Expand Down Expand Up @@ -911,18 +911,37 @@ public static String playerDeathPlaceholders(String msg, PlayerManager pm, Livin
return msg;
}

public static String convertString(String string) {
string = string.replaceAll("_", " ").toLowerCase();
String[] spl = string.split(" ");
StringBuilder builder = new StringBuilder();
for (int i = 0; i < spl.length; i++) {
if (i == spl.length - 1) {
builder.append(StringUtils.capitalize(spl[i]));
} else {
builder.append(StringUtils.capitalize(spl[i])).append(" ");
}
/*
Use MiniMessage feature to send translatable component to player.
Thus, able to display item name for players based on player's client locale.
But I think maybe there is a better way to display localized item name to player
*/
private static Component getI18nName(ItemStack i) {
Component i18nName;
if (Settings.getInstance().getConfig().getBoolean(Config.DISPLAY_I18N_ITEM_NAME.getPath())
&& DeathMessages.majorVersion() > 12) {// Dreeam TODO - Compatible to 1.12.2 lang key
// Block block.minecraft.example
// Item item.minecraft.example
String materialType = i.getType().isBlock() ? "block" : "item";
String rawTranslatable = "<lang:" + materialType + ".minecraft." + i.getType().name().toLowerCase() + ">";
i18nName = MiniMessage.miniMessage().deserialize(rawTranslatable);
} else {
i18nName = Component.text(i.getType().name());
}
return builder.toString();

return i18nName;
}

private static Component getI18nName(LivingEntity mob) {
// Dreeam - TODO
Component i18nName;
if (Settings.getInstance().getConfig().getBoolean(Config.DISPLAY_I18N_MOB_NAME.getPath())) {
i18nName = Component.empty();
} else {
i18nName = Component.empty();
}

return i18nName;
}

public static String getEnvironment(World.Environment environment) {
Expand Down
3 changes: 2 additions & 1 deletion Core/src/main/resources/Messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ Discord:
# You may use \n to make another line.
# Example: Text: "%message%\nX:%x%\nY:%y%\nZ:%z%"
Text: ""

#Translations
# Mobs: these translations will effect the %killer_type% placeholder
# Mobs: these translations will affect the %killer_type% placeholder
Mobs:
elderguardian: "Elder Guardian"
witherskeleton: "Wither Skeleton"
Expand Down
5 changes: 5 additions & 0 deletions Core/src/main/resources/Settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ Rename-Mobs:
# If enabled, mobs will always default to their entity name. Display names will always be ignored. (Besides players)
Disable-Named-Mobs: false

# If enabled, if the item/mob has no custom name, the display of the name will base on player's client locale.
I18N-Display:
Item-Name: true
Mob-Name: true #TODO

Expire-Last-Damage:
# How long until the last mob that damaged a player expires? (Becomes a natural death cause)
# Default: 7
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ dependencies {
api("net.kyori:adventure-api:$adventureVersion")
api("net.kyori:adventure-text-serializer-legacy:$adventureVersion")
api("net.kyori:adventure-text-serializer-plain:$adventureVersion")
api("net.kyori:adventure-text-minimessage:$adventureVersion")
api("net.kyori:adventure-key:$adventureVersion")
}

Expand Down

0 comments on commit 8580a82

Please sign in to comment.