Translate items with color for older MC versions, see #575

This commit is contained in:
BuildTools 2018-12-18 00:54:50 -05:00
parent 6bb5f81e68
commit 2ff85225e2
5 changed files with 74 additions and 16 deletions

View File

@ -90,7 +90,7 @@ public class NpcListener implements Listener {
text += (hand.getItemMeta().hasDisplayName() ? ")" : ""); text += (hand.getItemMeta().hasDisplayName() ? ")" : "");
} }
text += " x " + ChatColor.DARK_AQUA + hand.getAmount() + ChatColor.GRAY; text += " x " + ChatColor.DARK_AQUA + hand.getAmount() + ChatColor.GRAY;
plugin.query.sendMessage(player, Lang.get(player, "questInvalidDeliveryItem").replace("<item>", text), hand.getType()); plugin.query.sendMessage(player, Lang.get(player, "questInvalidDeliveryItem").replace("<item>", text), hand.getType(), hand.getDurability());
if (hand.hasItemMeta()) { if (hand.hasItemMeta()) {
if (hand.getType().equals(Material.ENCHANTED_BOOK)) { if (hand.getType().equals(Material.ENCHANTED_BOOK)) {
EnchantmentStorageMeta esmeta = (EnchantmentStorageMeta) hand.getItemMeta(); EnchantmentStorageMeta esmeta = (EnchantmentStorageMeta) hand.getItemMeta();

View File

@ -1255,7 +1255,8 @@ public class Quester {
* @param co * @param co
* See CustomObjective class * See CustomObjective class
*/ */
public void finishObjective(Quest quest, String objective, ItemStack material, ItemStack delivery, Enchantment enchantment, EntityType mob, String player, NPC npc, Location location, DyeColor color, String pass, CustomObjective co) { @SuppressWarnings("deprecation")
public void finishObjective(Quest quest, String objective, ItemStack itemStack, ItemStack delivery, Enchantment enchantment, EntityType mob, String player, NPC npc, Location location, DyeColor color, String pass, CustomObjective co) {
Player p = getPlayer(); Player p = getPlayer();
if (getCurrentStage(quest).objectiveOverride != null) { if (getCurrentStage(quest).objectiveOverride != null) {
if (testComplete(quest)) { if (testComplete(quest)) {
@ -1276,7 +1277,7 @@ public class Quester {
String stack = getQuestData(quest).blocksBroken.toString(); String stack = getQuestData(quest).blocksBroken.toString();
String amount = stack.substring(stack.lastIndexOf(" x ") + 3).replace("}]", ""); String amount = stack.substring(stack.lastIndexOf(" x ") + 3).replace("}]", "");
message = message + " " + amount + "/" + amount; message = message + " " + amount + "/" + amount;
plugin.query.sendMessage(p, message, material.getType()); plugin.query.sendMessage(p, message, itemStack.getType(), itemStack.getDurability());
if (testComplete(quest)) { if (testComplete(quest)) {
quest.nextStage(this); quest.nextStage(this);
} }
@ -1285,7 +1286,7 @@ public class Quester {
String stack = getQuestData(quest).blocksDamaged.toString(); String stack = getQuestData(quest).blocksDamaged.toString();
String amount = stack.substring(stack.lastIndexOf(" x ") + 3).replace("}]", ""); String amount = stack.substring(stack.lastIndexOf(" x ") + 3).replace("}]", "");
message = message + " " + amount + "/" + amount; message = message + " " + amount + "/" + amount;
plugin.query.sendMessage(p, message, material.getType()); plugin.query.sendMessage(p, message, itemStack.getType(), itemStack.getDurability());
if (testComplete(quest)) { if (testComplete(quest)) {
quest.nextStage(this); quest.nextStage(this);
} }
@ -1294,7 +1295,7 @@ public class Quester {
String stack = getQuestData(quest).blocksPlaced.toString(); String stack = getQuestData(quest).blocksPlaced.toString();
String amount = stack.substring(stack.lastIndexOf(" x ") + 3).replace("}]", ""); String amount = stack.substring(stack.lastIndexOf(" x ") + 3).replace("}]", "");
message = message + " " + amount + "/" + amount; message = message + " " + amount + "/" + amount;
plugin.query.sendMessage(p, message, material.getType()); plugin.query.sendMessage(p, message, itemStack.getType(), itemStack.getDurability());
if (testComplete(quest)) { if (testComplete(quest)) {
quest.nextStage(this); quest.nextStage(this);
} }
@ -1303,7 +1304,7 @@ public class Quester {
String stack = getQuestData(quest).blocksUsed.toString(); String stack = getQuestData(quest).blocksUsed.toString();
String amount = stack.substring(stack.lastIndexOf(" x ") + 3).replace("}]", ""); String amount = stack.substring(stack.lastIndexOf(" x ") + 3).replace("}]", "");
message = message + " " + amount + "/" + amount; message = message + " " + amount + "/" + amount;
plugin.query.sendMessage(p, message, material.getType()); plugin.query.sendMessage(p, message, itemStack.getType(), itemStack.getDurability());
if (testComplete(quest)) { if (testComplete(quest)) {
quest.nextStage(this); quest.nextStage(this);
} }
@ -1312,7 +1313,7 @@ public class Quester {
String stack = getQuestData(quest).blocksCut.toString(); String stack = getQuestData(quest).blocksCut.toString();
String amount = stack.substring(stack.lastIndexOf(" x ") + 3).replace("}]", ""); String amount = stack.substring(stack.lastIndexOf(" x ") + 3).replace("}]", "");
message = message + " " + amount + "/" + amount; message = message + " " + amount + "/" + amount;
plugin.query.sendMessage(p, message, material.getType()); plugin.query.sendMessage(p, message, itemStack.getType(), itemStack.getDurability());
if (testComplete(quest)) { if (testComplete(quest)) {
quest.nextStage(this); quest.nextStage(this);
} }
@ -1332,7 +1333,7 @@ public class Quester {
break; break;
} }
} }
plugin.query.sendMessage(p, message, material.getType(), enchantment); plugin.query.sendMessage(p, message, itemStack.getType(), itemStack.getDurability(), enchantment);
if (testComplete(quest)) { if (testComplete(quest)) {
quest.nextStage(this); quest.nextStage(this);
} }
@ -1340,7 +1341,8 @@ public class Quester {
String obj = Lang.get(p, "deliver"); String obj = Lang.get(p, "deliver");
obj = obj.replace("<npc>", plugin.getNPCName(getCurrentStage(quest).itemDeliveryTargets.get(getCurrentStage(quest).itemsToDeliver.indexOf(delivery)))); obj = obj.replace("<npc>", plugin.getNPCName(getCurrentStage(quest).itemDeliveryTargets.get(getCurrentStage(quest).itemsToDeliver.indexOf(delivery))));
String message = ChatColor.GREEN + "(" + Lang.get(p, "completed") + ") " + obj; String message = ChatColor.GREEN + "(" + Lang.get(p, "completed") + ") " + obj;
plugin.query.sendMessage(p, message, getCurrentStage(quest).itemsToDeliver.get(getCurrentStage(quest).itemsToDeliver.indexOf(delivery)).getType()); ItemStack is = getCurrentStage(quest).itemsToDeliver.get(getCurrentStage(quest).itemsToDeliver.indexOf(delivery));
plugin.query.sendMessage(p, message, is.getType(), is.getDurability());
if (testComplete(quest)) { if (testComplete(quest)) {
quest.nextStage(this); quest.nextStage(this);
} }

View File

@ -1901,11 +1901,11 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener
if (Enchantment.getByName(Lang.getKey(enchant).replace("ENCHANTMENT_", "")) != null) { if (Enchantment.getByName(Lang.getKey(enchant).replace("ENCHANTMENT_", "")) != null) {
Material m = Material.matchMaterial(serial); Material m = Material.matchMaterial(serial);
Enchantment e = Enchantment.getByName(Lang.getKey(enchant).replace("ENCHANTMENT_", "")); Enchantment e = Enchantment.getByName(Lang.getKey(enchant).replace("ENCHANTMENT_", ""));
query.sendMessage(quester.getPlayer(), s.replace(serial, "<item>").replace(enchant, "<enchantment>"), m, e); query.sendMessage(quester.getPlayer(), s.replace(serial, "<item>").replace(enchant, "<enchantment>"), m, (short) 0, e); //TODO GET DURABILITY VALUE
continue; continue;
} else if (Material.matchMaterial(serial) != null) { } else if (Material.matchMaterial(serial) != null) {
Material m = Material.matchMaterial(serial); Material m = Material.matchMaterial(serial);
query.sendMessage(quester.getPlayer(), s.replace(serial, "<item>"), m); query.sendMessage(quester.getPlayer(), s.replace(serial, "<item>"), m, (short) 0); //TODO GET DURABILITY VALUE
continue; continue;
} else { } else {
try { try {

View File

@ -19,10 +19,13 @@ import me.blackvein.quests.Quests;
import org.apache.commons.lang3.reflect.MethodUtils; import org.apache.commons.lang3.reflect.MethodUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.DyeColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.LeatherArmorMeta;
public class LocaleQuery { public class LocaleQuery {
private static Class<?> craftMagicNumbers = null; private static Class<?> craftMagicNumbers = null;
@ -39,13 +42,23 @@ public class LocaleQuery {
oldVersion = isBelow113(bukkitVersion); oldVersion = isBelow113(bukkitVersion);
} }
public void sendMessage(Player player, String message, Material material) { /**
* Send message with item name translated to the client's locale<p>
*
* Durability arg is arbitrary for 1.13+
*
* @param player The player for whom the message is to be sent
* @param message The message to be sent to the player
* @param material The item to be translated
* @param durability Durability for the item being translated
*/
public void sendMessage(Player player, String message, Material material, short durability) {
if (plugin.translateItems) { if (plugin.translateItems) {
String key = queryByType(material); String key = queryByType(material);
if (key != null) { if (key != null) {
if (oldVersion) { if (oldVersion) {
if (key.startsWith("tile.") || key.startsWith("item.")) { if (key.startsWith("tile.") || key.startsWith("item.")) {
key = key + ".name"; key += getColorIfApplicable(material, durability) + ".name";
} }
} }
String msg = message.replace("<item>", "\",{\"translate\":\"" + key + "\"},\""); String msg = message.replace("<item>", "\",{\"translate\":\"" + key + "\"},\"");
@ -56,14 +69,25 @@ public class LocaleQuery {
player.sendMessage(message.replace("<item>", Quester.prettyItemString(material.name()))); player.sendMessage(message.replace("<item>", Quester.prettyItemString(material.name())));
} }
/**
* Send message with item name translated to the client's locale<p>
*
* Durability arg is arbitrary for 1.13+
*
* @param player The player for whom the message is to be sent
* @param message The message to be sent to the player
* @param material The item to be translated
* @param durability Durability for the item being translated
* @param enchantment The enchantment to be translated
*/
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public void sendMessage(Player player, String message, Material material, Enchantment enchantment) { public void sendMessage(Player player, String message, Material material, short durability, Enchantment enchantment) {
if (plugin.translateItems) { if (plugin.translateItems) {
String key = queryByType(material); String key = queryByType(material);
if (key != null) { if (key != null) {
if (oldVersion) { if (oldVersion) {
if (key.startsWith("tile.") || key.startsWith("item.")) { if (key.startsWith("tile.") || key.startsWith("item.")) {
key = key + ".name"; key += getColorIfApplicable(material, durability) + ".name";
} }
} }
String key2 = ""; String key2 = "";
@ -164,4 +188,36 @@ public class LocaleQuery {
Bukkit.getLogger().severe("Quests received invalid Bukkit version " + bukkitVersion); Bukkit.getLogger().severe("Quests received invalid Bukkit version " + bukkitVersion);
return false; return false;
} }
/**
* Appends a color to an item. Note that this will make item names invalid if the item is not eligible<p>
*
* Method not useful for MC 1.13+
*
* @param material The material to be processed
* @param durability The durability indicating variation
* @return color, or blank string if not applicable
*/
@SuppressWarnings("deprecation")
public String getColorIfApplicable(Material material, short durability){
String key = "";
if (material.name().equals("INK_SACK")) {
DyeColor dye = DyeColor.getByDyeData((byte)durability);
key = "." + MiscUtil.fixUnderscore(dye.name().toLowerCase());
} else if ((material.name().equals("WOOL") || material.name().equals("CARPET")) && durability == 0) {
// White wool/carpet, do nothing
} else if (material.name().equals("WOOL") || material.name().equals("CARPET") || material.name().equals("STAINED_CLAY")
|| material.name().equals("STAINED_GLASS") || material.name().equals("STAINED_GLASS_PANE")) {
DyeColor dye = DyeColor.getByWoolData((byte)durability);
key = "." + MiscUtil.fixUnderscore(dye.name().toLowerCase());
} else if (material.name().equals("LEATHER_HELMET") || material.name().equals("LEATHER_CHESTPLATE")
|| material.name().equals("LEATHER_LEGGINGS") || material.name().equals("LEATHER_BOOTS")) {
ItemStack is = new ItemStack(material, 1, durability);
LeatherArmorMeta lam = (LeatherArmorMeta) is.getItemMeta();
DyeColor dye = DyeColor.getByColor(lam.getColor());
key = "." + MiscUtil.fixUnderscore(dye.name().toLowerCase());
}
return key;
}
} }

View File

@ -49,7 +49,7 @@ public class MiscUtil {
return null; return null;
} }
private static String fixUnderscore(String s) { public static String fixUnderscore(String s) {
int index = s.indexOf('_'); int index = s.indexOf('_');
if (index == -1) { if (index == -1) {
return null; return null;