Add support for Enchanted Books, part 1 for #520

This commit is contained in:
BuildTools 2018-11-14 23:54:57 -05:00
parent 8a3272c6db
commit 447eaff49c
3 changed files with 68 additions and 25 deletions

View File

@ -14,11 +14,12 @@ package me.blackvein.quests;
import java.text.MessageFormat;
import java.util.LinkedList;
import java.util.Map.Entry;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.conversations.Conversation;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
@ -27,15 +28,17 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import me.blackvein.quests.util.ItemUtil;
import me.blackvein.quests.util.Lang;
import me.blackvein.quests.util.MiscUtil;
import me.blackvein.quests.util.RomanNumeral;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.event.NPCDeathEvent;
import net.citizensnpcs.api.event.NPCLeftClickEvent;
import net.citizensnpcs.api.event.NPCRightClickEvent;
import net.citizensnpcs.api.npc.NPC;
import net.milkbowl.vault.item.Items;
public class NpcListener implements Listener {
@ -78,25 +81,6 @@ public class NpcListener implements Listener {
} else if (!hand.getType().equals(Material.AIR)) {
for (Integer n : quester.getCurrentStage(quest).itemDeliveryTargets) {
if (n.equals(clicked.getId())) {
/*try {
String[] lang = Lang.get(player, "questInvalidDeliveryItem").split("<item>");
String prefix = lang[0];
if (hand.hasItemMeta()) {
prefix += ChatColor.LIGHT_PURPLE + "" + ChatColor.ITALIC + (hand.getItemMeta().hasDisplayName() ? hand.getItemMeta().getDisplayName() + ChatColor.GRAY + " (" : "");
}
prefix += ChatColor.AQUA;
String suffix = (hand.getDurability() != 0 ? (":" + ChatColor.BLUE + hand.getDurability()) : "") + ChatColor.GRAY;
if (hand.hasItemMeta()) {
suffix += (hand.getItemMeta().hasDisplayName() ? ")" : "");
}
suffix += " x " + ChatColor.DARK_AQUA + hand.getAmount() + ChatColor.RESET + lang[1];
plugin.query.sendMessage(player, prefix, hand.getType(), suffix);
break;
} catch (IndexOutOfBoundsException e) {
plugin.getLogger().severe("Error splitting \"" + Lang.get(player, "questInvalidDeliveryItem") + "\". Maybe missing <item> tag?");
player.sendMessage(ChatColor.RED + "Error showing this text. Please contact an administrator.");
e.printStackTrace();
}*/
String text = "";
if (hand.hasItemMeta()) {
text += ChatColor.LIGHT_PURPLE + "" + ChatColor.ITALIC + (hand.getItemMeta().hasDisplayName() ? hand.getItemMeta().getDisplayName() + ChatColor.GRAY + " (" : "");
@ -107,7 +91,18 @@ public class NpcListener implements Listener {
}
text += " x " + ChatColor.DARK_AQUA + hand.getAmount() + ChatColor.GRAY;
plugin.query.sendMessage(player, Lang.get(player, "questInvalidDeliveryItem").replace("<item>", text), hand.getType());
//evt.getClicker().sendMessage(Lang.get(player, "questInvalidDeliveryItem").replaceAll("<item>", text));
if (hand.hasItemMeta()) {
if (hand.getType().equals(Material.ENCHANTED_BOOK)) {
EnchantmentStorageMeta esmeta = (EnchantmentStorageMeta) hand.getItemMeta();
if (esmeta.hasStoredEnchants()) {
// TODO translate enchantment names
for (Entry<Enchantment, Integer> e : esmeta.getStoredEnchants().entrySet()) {
player.sendMessage(ChatColor.GRAY + "\u2515 " + ChatColor.DARK_GREEN
+ Quester.prettyEnchantmentString(e.getKey()) + " " + RomanNumeral.toRoman(e.getValue()) + "\n");
}
}
}
}
break;
}
}

View File

@ -150,9 +150,24 @@ public class ItemUtil {
if (i > -1) {
extra.put(key, i);
} else if (value.startsWith("[") && value.endsWith("]")) {
// Map such as book pages
// List such as book pages
List<String> pages = Arrays.asList(value.split(", "));
extra.put(key, pages);
} else if (value.startsWith("{") && value.endsWith("}")) {
// Map such as stored enchants for enchanted books
String[] enchants = value.replace("{", "").replace("}", "").split(", ");
Map<String, String> stored = new HashMap<String, String>();
for (String s : enchants) {
if (s.contains("=")) {
String[] keyval = s.split("=");
stored.put(keyval[0], keyval[1]);
} else {
Bukkit.getLogger().severe("Quests does not know how to handle "
+ value + " so please contact the developer on Github");
return null;
}
}
extra.put(key, stored);
} else {
extra.put(key, value);
}
@ -232,8 +247,6 @@ public class ItemUtil {
/**
* Essentially the reverse of ItemMeta.serialize()
*
* Format is ([display]name:durability) with (enchantments:levels) x (amount)
*
* @param ItemMeta class, key/value map of metadata
* @return ItemMeta
*/

View File

@ -0,0 +1,35 @@
package me.blackvein.quests.util;
import java.util.TreeMap;
public class RomanNumeral {
private final static TreeMap<Integer, String> map = new TreeMap<Integer, String>();
static {
map.put(1000, "M");
map.put(900, "CM");
map.put(500, "D");
map.put(400, "CD");
map.put(100, "C");
map.put(90, "XC");
map.put(50, "L");
map.put(40, "XL");
map.put(10, "X");
map.put(9, "IX");
map.put(5, "V");
map.put(4, "IV");
map.put(1, "I");
}
public final static String toRoman(int number) {
int l = map.floorKey(number);
if ( number == l ) {
return map.get(number);
}
return map.get(l) + toRoman(number-l);
}
}