Skip to content

Commit

Permalink
Merge pull request #15 from LegameMc/color-patch
Browse files Browse the repository at this point in the history
Fix translated text's color
  • Loading branch information
PikaMug authored May 11, 2021
2 parents 215d74c + 75c6a1d commit 24d4f58
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions src/main/java/me/pikamug/localelib/LocaleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.apache.commons.lang.NullArgumentException;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.EntityType;
Expand Down Expand Up @@ -82,13 +83,14 @@ public boolean sendMessage(final Player player, final String message, final Mate
}
final Collection<String> enchKeys = queryEnchantments(enchantments).values();
final Collection<String> lvlKeys = queryLevels(enchantments).values();
String msg = message.replace("<item>", "\",{\"translate\":\"" + matKey + "\"},\"");
String msg = message.replace("<item>", translate(message, matKey, "<item>"));
for (final String ek : enchKeys) {
msg = msg.replaceFirst("<enchantment>", "\",{\"translate\":\"" + ek + "\"},\"");
msg = msg.replaceFirst("<enchantment>", translate(msg, ek, "<enchantment>"));
}
for (final String lk : lvlKeys) {
msg = msg.replaceFirst("<level>", "\",{\"translate\":\"" + lk + "\"},\"");
msg = msg.replaceFirst("<level>", translate(msg, lk, "<level>"));
}
System.out.println("tellraw " + player.getName() + " [\"" + msg + "\"]");
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "tellraw " + player.getName() + " [\"" + msg + "\"]");
return true;
}
Expand Down Expand Up @@ -135,10 +137,10 @@ public boolean sendMessage(final Player player, final String message, final Map<
final String msg = message;
if (enchKeys != null && !enchKeys.isEmpty()) {
for (final String ek : enchKeys) {
msg.replaceFirst("<enchantment>", "\",{\"translate\":\"" + ek + "\"},\"");
msg.replaceFirst("<enchantment>", translate(msg, ek, "<enchantment>"));
}
for (final String lk : lvlKeys) {
msg.replaceFirst("<level>", "\",{\"translate\":\"" + lk + "\"},\"");
msg.replaceFirst("<level>", translate(msg, lk, "<level>"));
}
}
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "tellraw " + player.getName() + " [\"" + msg + "\"]");
Expand Down Expand Up @@ -184,7 +186,7 @@ public boolean sendMessage(final Player player, final String message, final Enti
key = "entity.minecraft." + type.toString().toLowerCase();
}
}
final String msg = message.replace("<mob>", "\",{\"translate\":\"" + key + "\"},\"");
final String msg = message.replace("<mob>", translate(message, key, "<mob>"));
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "tellraw " + player.getName() + " [\"" + msg + "\"]");
return true;
}
Expand Down Expand Up @@ -323,7 +325,36 @@ public String toServerLocale(final String key) throws IllegalAccessException, In

return (String) trans.invoke(localeClazz, key);
}



/**
*
* @param message The message to be sent to the player
* @param key the raw keys of the enchantments
* @param placeholder <item>, <enchantment>, <level> and <mob>
* @return the text to replace the placeholder in the message
*/
private String translate(String message, String key, String placeholder){

String replacement = "\",{\"translate\":\"" + key + "\"";
// Get the text before the placeholder
String text = message.split(placeholder)[0];
// If the text before the placeholder uses any color code
if(text.contains("§")){
// Get the color code that apply on the text and remove §, so we can get the color name later
String colorCode = ChatColor.getLastColors(text).replace("§", "");
// Get the color name
String colorName = ChatColor.getByChar(colorCode).name();

// Add the color
replacement += ", \"color\":\"" + colorName.toLowerCase() + "\"";
}

replacement += "},\"";

return replacement;
}

/**
* Checks whether the server's Bukkit version supports use of the ItemMeta#getBasePotionData method.
*
Expand Down

0 comments on commit 24d4f58

Please sign in to comment.