diff options
Diffstat (limited to 'src/main/java/com/leonardobishop')
6 files changed, 687 insertions, 55 deletions
diff --git a/src/main/java/com/leonardobishop/quests/itemgetter/ItemGetter.java b/src/main/java/com/leonardobishop/quests/itemgetter/ItemGetter.java new file mode 100644 index 00000000..661d7302 --- /dev/null +++ b/src/main/java/com/leonardobishop/quests/itemgetter/ItemGetter.java @@ -0,0 +1,41 @@ +package com.leonardobishop.quests.itemgetter; + +import com.leonardobishop.quests.Quests; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.inventory.ItemStack; +import org.bukkit.plugin.java.JavaPlugin; + +public interface ItemGetter { + + /** + * Gets an ItemStack from a configuration. + * Implementations should specific to the server version. + * + * @param path the path to where the item is defined in the config (null if item is defined in second param) + * @param config the configuration file + * @param plugin Quests plugin instance + * @param excludes exclude certain fields in the configuration + * @return {@link org.bukkit.inventory.ItemStack} + */ + ItemStack getItem(String path, ConfigurationSection config, Quests plugin, Filter... excludes); + + /** + * Gets an ItemStack from a given string (which represents a material). + * For pre-1.13 server implementations, the string may use a data code. + * + * @param material the string + * @return {@link org.bukkit.inventory.ItemStack} + */ + ItemStack getItemStack(String material, Quests plugin); + + enum Filter { + DISPLAY_NAME, + LORE, + ENCHANTMENTS, + ITEM_FLAGS, + UNBREAKABLE, + ATTRIBUTE_MODIFIER, + CUSTOM_MODEL_DATA; + } +}
\ No newline at end of file diff --git a/src/main/java/com/leonardobishop/quests/itemgetter/ItemGetterLatest.java b/src/main/java/com/leonardobishop/quests/itemgetter/ItemGetterLatest.java new file mode 100644 index 00000000..4015385a --- /dev/null +++ b/src/main/java/com/leonardobishop/quests/itemgetter/ItemGetterLatest.java @@ -0,0 +1,231 @@ +package com.leonardobishop.quests.itemgetter; + +import com.leonardobishop.quests.Quests; +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.bukkit.attribute.Attribute; +import org.bukkit.attribute.AttributeModifier; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.EquipmentSlot; +import org.bukkit.inventory.ItemFlag; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.plugin.java.JavaPlugin; + +import java.util.*; + +public class ItemGetterLatest implements ItemGetter { + + /* + supporting: + - name + - material + - lore + - enchantments (NamespacedKey) + - itemflags + - unbreakable + - attribute modifier + - custom model data + + requires at least API version 1.14 + */ + @Override + public ItemStack getItem(String path, ConfigurationSection config, Quests plugin, ItemGetter.Filter... excludes) { + if (path != null && !path.equals("")) { + path = path + "."; + } + List<Filter> filters = Arrays.asList(excludes); + + String cName = config.getString(path + "name", path + "name"); + String cType = config.getString(path + "item", config.getString(path + "type", path + "item")); + boolean hasCustomModelData = config.contains(path + "custommodeldata"); + int customModelData = config.getInt(path + "custommodeldata", 0); + boolean unbreakable = config.getBoolean(path + "unbreakable", false); + List<String> cLore = config.getStringList(path + "lore"); + List<String> cItemFlags = config.getStringList(path + "itemflags"); + boolean hasAttributeModifiers = config.contains(path + "attributemodifiers"); + List<Map<?, ?>> cAttributeModifiers = config.getMapList(path + "attributemodifiers"); + + String name; + Material type = null; + int data = 0; + + // material + ItemStack is = getItemStack(cType, plugin); + ItemMeta ism = is.getItemMeta(); + + // name + if (!filters.contains(Filter.DISPLAY_NAME)) { + name = ChatColor.translateAlternateColorCodes('&', cName); + ism.setDisplayName(name); + } + + // lore + if (!filters.contains(Filter.LORE)) { + List<String> lore = new ArrayList<>(); + if (cLore != null) { + for (String s : cLore) { + lore.add(ChatColor.translateAlternateColorCodes('&', s)); + } + } + ism.setLore(lore); + } + + // attribute modifiers + if (!filters.contains(Filter.ATTRIBUTE_MODIFIER)) { + if (hasAttributeModifiers) { + for (Map<?, ?> attr : cAttributeModifiers) { + String cAttribute = (String) attr.get("attribute"); + Attribute attribute = null; + for (Attribute enumattr : Attribute.values()) { + if (enumattr.toString().equals(cAttribute)) { + attribute = enumattr; + break; + } + } + + if (attribute == null) continue; + + Map<?, ?> configurationSection = (Map<?, ?>) attr.get("modifier"); + + String cUUID = (String) configurationSection.get("uuid"); + String cModifierName = (String) configurationSection.get("name"); + String cModifierOperation = (String) configurationSection.get("operation"); + double cAmount; + try { + Object cAmountObj = configurationSection.get("amount"); + if (cAmountObj instanceof Integer) { + cAmount = ((Integer) cAmountObj).doubleValue(); + } else { + cAmount = (Double) cAmountObj; + } + } catch (Exception e) { + cAmount = 1; + } + String cEquipmentSlot = (String) configurationSection.get("equipmentslot"); + + UUID uuid = null; + if (cUUID != null) { + try { + uuid = UUID.fromString(cUUID); + } catch (Exception ignored) { + // ignored + } + } + EquipmentSlot equipmentSlot = null; + if (cEquipmentSlot != null) { + try { + equipmentSlot = EquipmentSlot.valueOf(cEquipmentSlot); + } catch (Exception ignored) { + // ignored + } + } + AttributeModifier.Operation operation = AttributeModifier.Operation.ADD_NUMBER; + try { + operation = AttributeModifier.Operation.valueOf(cModifierOperation); + } catch (Exception ignored) { + // ignored + } + + AttributeModifier modifier; + if (uuid == null) { + modifier = new AttributeModifier(cModifierName, cAmount, operation); + } else if (equipmentSlot == null) { + modifier = new AttributeModifier(uuid, cModifierName, cAmount, operation); + } else { + modifier = new AttributeModifier(uuid, cModifierName, cAmount, operation, equipmentSlot); + } + + ism.addAttributeModifier(attribute, modifier); + } + } + } + + // item flags + if (!filters.contains(Filter.ITEM_FLAGS)) { + if (config.isSet(path + "itemflags")) { + for (String flag : cItemFlags) { + for (ItemFlag iflag : ItemFlag.values()) { + if (iflag.toString().equals(flag)) { + ism.addItemFlags(iflag); + break; + } + } + } + } + } + + // custom model data + if (!filters.contains(Filter.CUSTOM_MODEL_DATA)) { + if (hasCustomModelData) { + ism.setCustomModelData(customModelData); + } + } + + // unbreakable + if (!filters.contains(Filter.UNBREAKABLE)) { + ism.setUnbreakable(unbreakable); + } + + // enchantments + if (!filters.contains(Filter.ENCHANTMENTS)) { + if (config.isSet(path + "enchantments")) { + for (String key : config.getStringList(path + "enchantments")) { + String[] split = key.split(":"); + if (split.length < 2) { + plugin.getQuestsLogger().debug("Enchantment does not follow format {namespace}:{name}:{level} : " + key); + continue; + } + String namespace = split[0]; + String ench = split[1]; + String levelName; + if (split.length >= 3) { + levelName = split[2]; + } else { + levelName = "1"; + } + + NamespacedKey namespacedKey; + try { + namespacedKey = new NamespacedKey(namespace, ench); + } catch (Exception e) { + plugin.getQuestsLogger().debug("Unrecognised namespace: " + namespace); + continue; + } + Enchantment enchantment; + if ((enchantment = Enchantment.getByKey(namespacedKey)) == null) { + plugin.getQuestsLogger().debug("Unrecognised enchantment: " + namespacedKey); + continue; + } + + int level; + try { + level = Integer.parseInt(levelName); + } catch (NumberFormatException e) { + level = 1; + } + + is.addUnsafeEnchantment(enchantment, level); + } + } + } + + is.setItemMeta(ism); + return is; + } + + @Override + public ItemStack getItemStack(String material, Quests plugin) { + Material type; + try { + type = Material.valueOf(material); + } catch (Exception e) { + plugin.getQuestsLogger().debug("Unrecognised material: " + material); + type = Material.STONE; + } + return new ItemStack(type, 1); + } +} diff --git a/src/main/java/com/leonardobishop/quests/itemgetter/ItemGetter_1_13.java b/src/main/java/com/leonardobishop/quests/itemgetter/ItemGetter_1_13.java new file mode 100644 index 00000000..b356f696 --- /dev/null +++ b/src/main/java/com/leonardobishop/quests/itemgetter/ItemGetter_1_13.java @@ -0,0 +1,221 @@ +package com.leonardobishop.quests.itemgetter; + +import com.leonardobishop.quests.Quests; +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.bukkit.attribute.Attribute; +import org.bukkit.attribute.AttributeModifier; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.EquipmentSlot; +import org.bukkit.inventory.ItemFlag; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.plugin.java.JavaPlugin; + +import java.util.*; + +public class ItemGetter_1_13 implements ItemGetter { + /* + reads the following: + - name + - material + - lore + - enchantments (NamespacedKey) + - itemflags + - unbreakable + - attribute modifier + + requires at least API version 1.13 + */ + @Override + public ItemStack getItem(String path, ConfigurationSection config, Quests plugin, ItemGetter.Filter... excludes) { + if (path != null && !path.equals("")) { + path = path + "."; + } + List<Filter> filters = Arrays.asList(excludes); + + String cName = config.getString(path + "name", path + "name"); + String cType = config.getString(path + "item", config.getString(path + "type", path + "item")); + boolean unbreakable = config.getBoolean(path + "unbreakable", false); + List<String> cLore = config.getStringList(path + "lore"); + List<String> cItemFlags = config.getStringList(path + "itemflags"); + boolean hasAttributeModifiers = config.contains(path + "attributemodifiers"); + List<Map<?, ?>> cAttributeModifiers = config.getMapList(path + "attributemodifiers"); + + String name; + Material type = null; + int data = 0; + + // material + ItemStack is = getItemStack(cType, plugin); + ItemMeta ism = is.getItemMeta(); + + // name + if (!filters.contains(Filter.DISPLAY_NAME)) { + name = ChatColor.translateAlternateColorCodes('&', cName); + ism.setDisplayName(name); + } + + // lore + if (!filters.contains(Filter.LORE)) { + List<String> lore = new ArrayList<>(); + if (cLore != null) { + for (String s : cLore) { + lore.add(ChatColor.translateAlternateColorCodes('&', s)); + } + } + ism.setLore(lore); + } + + // attribute modifiers + if (!filters.contains(Filter.ATTRIBUTE_MODIFIER)) { + if (hasAttributeModifiers) { + for (Map<?, ?> attr : cAttributeModifiers) { + String cAttribute = (String) attr.get("attribute"); + Attribute attribute = null; + for (Attribute enumattr : Attribute.values()) { + if (enumattr.toString().equals(cAttribute)) { + attribute = enumattr; + break; + } + } + + if (attribute == null) continue; + + Map<?, ?> configurationSection = (Map<?, ?>) attr.get("modifier"); + + String cUUID = (String) configurationSection.get("uuid"); + String cModifierName = (String) configurationSection.get("name"); + String cModifierOperation = (String) configurationSection.get("operation"); + double cAmount; + try { + Object cAmountObj = configurationSection.get("amount"); + if (cAmountObj instanceof Integer) { + cAmount = ((Integer) cAmountObj).doubleValue(); + } else { + cAmount = (Double) cAmountObj; + } + } catch (Exception e) { + cAmount = 1; + } + String cEquipmentSlot = (String) configurationSection.get("equipmentslot"); + + UUID uuid = null; + if (cUUID != null) { + try { + uuid = UUID.fromString(cUUID); + } catch (Exception ignored) { + // ignored + } + } + EquipmentSlot equipmentSlot = null; + if (cEquipmentSlot != null) { + try { + equipmentSlot = EquipmentSlot.valueOf(cEquipmentSlot); + } catch (Exception ignored) { + // ignored + } + } + AttributeModifier.Operation operation = AttributeModifier.Operation.ADD_NUMBER; + try { + operation = AttributeModifier.Operation.valueOf(cModifierOperation); + } catch (Exception ignored) { + // ignored + } + + AttributeModifier modifier; + if (uuid == null) { + modifier = new AttributeModifier(cModifierName, cAmount, operation); + } else if (equipmentSlot == null) { + modifier = new AttributeModifier(uuid, cModifierName, cAmount, operation); + } else { + modifier = new AttributeModifier(uuid, cModifierName, cAmount, operation, equipmentSlot); + } + + ism.addAttributeModifier(attribute, modifier); + } + } + } + + // item flags + if (!filters.contains(Filter.ITEM_FLAGS)) { + if (config.isSet(path + "itemflags")) { + for (String flag : cItemFlags) { + for (ItemFlag iflag : ItemFlag.values()) { + if (iflag.toString().equals(flag)) { + ism.addItemFlags(iflag); + break; + } + } + } + } + } + + + // unbreakable + if (!filters.contains(Filter.UNBREAKABLE)) { + ism.setUnbreakable(unbreakable); + } + + // enchantments + if (!filters.contains(Filter.ENCHANTMENTS)) { + if (config.isSet(path + "enchantments")) { + for (String key : config.getStringList(path + "enchantments")) { + String[] split = key.split(":"); + if (split.length < 2) { + plugin.getQuestsLogger().debug("Enchantment does not follow format {namespace}:{name}:{level} : " + key); + continue; + } + String namespace = split[0]; + String ench = split[1]; + String levelName; + if (split.length >= 3) { + levelName = split[2]; + } else { + levelName = "1"; + } + + NamespacedKey namespacedKey; + try { + namespacedKey = new NamespacedKey(namespace, ench); + } catch (Exception e) { + plugin.getQuestsLogger().debug("Unrecognised namespace: " + namespace); + continue; + } + Enchantment enchantment; + if ((enchantment = Enchantment.getByKey(namespacedKey)) == null) { + plugin.getQuestsLogger().debug("Unrecognised enchantment: " + namespacedKey); + continue; + } + + int level; + try { + level = Integer.parseInt(levelName); + } catch (NumberFormatException e) { + level = 1; + } + + is.addUnsafeEnchantment(enchantment, level); + } + } + } + + is.setItemMeta(ism); + return is; + } + + @Override + public ItemStack getItemStack(String material, Quests plugin) { + Material type; + try { + type = Material.valueOf(material); + } catch (Exception e) { + plugin.getQuestsLogger().debug("Unrecognised material: " + material); + type = Material.STONE; + } + return new ItemStack(type, 1); + } +} diff --git a/src/main/java/com/leonardobishop/quests/itemgetter/ItemGetter_Late_1_8.java b/src/main/java/com/leonardobishop/quests/itemgetter/ItemGetter_Late_1_8.java new file mode 100644 index 00000000..696560b1 --- /dev/null +++ b/src/main/java/com/leonardobishop/quests/itemgetter/ItemGetter_Late_1_8.java @@ -0,0 +1,145 @@ +package com.leonardobishop.quests.itemgetter; + +import com.leonardobishop.quests.Quests; +import org.apache.commons.lang.StringUtils; +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemFlag; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.plugin.java.JavaPlugin; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.regex.Pattern; + +public class ItemGetter_Late_1_8 implements ItemGetter { + /* + reads the following: + - name + - material (+ DATA) + - lore + - enchantments (NOT NamespacedKey) + - itemflags + + requires at least API version 1.8 (?) + */ + @Override + public ItemStack getItem(String path, ConfigurationSection config, Quests plugin, Filter... excludes) { + if (path != null && !path.equals("")) { + path = path + "."; + } + List<Filter> filters = Arrays.asList(excludes); + + + String cName = config.getString(path + "name", path + "name"); + String cType = config.getString(path + "item", config.getString(path + "type", path + "item")); + List<String> cLore = config.getStringList(path + "lore"); + List<String> cItemFlags = config.getStringList(path + "itemflags"); + + String name; + Material type = null; + int data = 0; + + // material + ItemStack is = getItemStack(cType, plugin); + ItemMeta ism = is.getItemMeta(); + + // lore + if (!filters.contains(Filter.LORE)) { + List<String> lore = new ArrayList<>(); + if (cLore != null) { + for (String s : cLore) { + lore.add(ChatColor.translateAlternateColorCodes('&', s)); + } + } + ism.setLore(lore); + } + + // name + if (!filters.contains(Filter.DISPLAY_NAME)) { + name = ChatColor.translateAlternateColorCodes('&', cName); + ism.setDisplayName(name); + } + + + // item flags + if (!filters.contains(Filter.ITEM_FLAGS)) { + if (config.isSet(path + "itemflags")) { + for (String flag : cItemFlags) { + for (ItemFlag iflag : ItemFlag.values()) { + if (iflag.toString().equals(flag)) { + ism.addItemFlags(iflag); + break; + } + } + } + } + } + + // enchantments + if (!filters.contains(Filter.ENCHANTMENTS)) { + if (config.isSet(path + "enchantments")) { + for (String key : config.getStringList(path + "enchantments")) { + String[] split = key.split(":"); + String ench = split[0]; + String levelName; + if (split.length >= 2) { + levelName = split[1]; + } else { + levelName = "1"; + } + + Enchantment enchantment; + if ((enchantment = Enchantment.getByName(ench)) == null) { + plugin.getQuestsLogger().debug("Unrecognised enchantment: " + ench); + continue; + } + + int level; + try { + level = Integer.parseInt(levelName); + } catch (NumberFormatException e) { + level = 1; + } + + is.addUnsafeEnchantment(enchantment, level); + } + } + } + + is.setItemMeta(ism); + return is; + } + + + @Override + public ItemStack getItemStack(String material, Quests plugin) { + Material type = null; + int data = 0; + + if (Material.getMaterial(material) != null) { + type = Material.getMaterial(material); + } else if (material.contains(":")) { + String[] parts = material.split(Pattern.quote(":")); + if (parts.length > 1) { + if (Material.getMaterial(parts[0]) != null) { + type = Material.getMaterial(parts[0]); + } + if (StringUtils.isNumeric(parts[1])) { + data = Integer.parseInt(parts[1]); + } + } + } + + if (type == null) { + plugin.getQuestsLogger().debug("Unrecognised material: " + material); + type = Material.STONE; + } + return new ItemStack(type, 1, (short) data); + } +} diff --git a/src/main/java/com/leonardobishop/quests/obj/misc/QItemStack.java b/src/main/java/com/leonardobishop/quests/obj/misc/QItemStack.java index f97b94fe..f7eca148 100644 --- a/src/main/java/com/leonardobishop/quests/obj/misc/QItemStack.java +++ b/src/main/java/com/leonardobishop/quests/obj/misc/QItemStack.java @@ -1,10 +1,8 @@ package com.leonardobishop.quests.obj.misc; -import com.leonardobishop.quests.Quests; import com.leonardobishop.quests.player.questprogressfile.QuestProgress; import com.leonardobishop.quests.player.questprogressfile.QuestProgressFile; import com.leonardobishop.quests.quests.Quest; -import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; @@ -20,15 +18,13 @@ public class QItemStack { private String name; private List<String> loreNormal; private List<String> loreStarted; - private Material type; - private int data; + private ItemStack startingItemStack; - public QItemStack(String name, List<String> loreNormal, List<String> loreStarted, Material type, int data) { + public QItemStack(String name, List<String> loreNormal, List<String> loreStarted, ItemStack startingItemStack) { this.name = name; this.loreNormal = loreNormal; this.loreStarted = loreStarted; - this.type = type; - this.data = data; + this.startingItemStack = startingItemStack; } public String getName() { @@ -55,25 +51,17 @@ public class QItemStack { this.loreStarted = loreStarted; } - public Material getType() { - return type; + public ItemStack getStartingItemStack() { + return startingItemStack; } - public void setType(Material type) { - this.type = type; - } - - public int getData() { - return data; - } - - public void setData(int data) { - this.data = data; + public void setStartingItemStack(ItemStack startingItemStack) { + this.startingItemStack = startingItemStack; } @SuppressWarnings("deprecation") public ItemStack toItemStack(Quest quest, QuestProgressFile questProgressFile, QuestProgress questProgress) { - ItemStack is = new ItemStack(type, 1, (short) data); + ItemStack is = new ItemStack(startingItemStack); ItemMeta ism = is.getItemMeta(); ism.setDisplayName(name); List<String> formattedLore = new ArrayList<>(); diff --git a/src/main/java/com/leonardobishop/quests/obj/misc/creator/QMenuCreator.java b/src/main/java/com/leonardobishop/quests/obj/misc/creator/QMenuCreator.java index eb5300e7..a8800d54 100644 --- a/src/main/java/com/leonardobishop/quests/obj/misc/creator/QMenuCreator.java +++ b/src/main/java/com/leonardobishop/quests/obj/misc/creator/QMenuCreator.java @@ -1,33 +1,39 @@ -package com.leonardobishop.quests.obj.misc.creator; - -import com.leonardobishop.quests.obj.misc.QMenu; -import com.leonardobishop.quests.player.QPlayer; -import org.bukkit.Bukkit; -import org.bukkit.inventory.Inventory; - -import java.util.HashMap; - -public class QMenuCreator implements QMenu { - - private final QPlayer owner; - - public QMenuCreator(QPlayer owner) { - this.owner = owner; - } - - @Override - public HashMap<Integer, String> getSlotsToMenu() { - return null; - } - - @Override - public QPlayer getOwner() { - return owner; - } - - public Inventory toInventory(int page) { - String title = "Quest Creator"; - +//package com.leonardobishop.quests.obj.misc.creator; +// +//import com.leonardobishop.quests.obj.misc.QMenu; +//import com.leonardobishop.quests.player.QPlayer; +//import org.bukkit.Bukkit; +//import org.bukkit.ChatColor; +//import org.bukkit.Material; +//import org.bukkit.inventory.Inventory; +//import org.bukkit.inventory.ItemStack; +//import org.bukkit.inventory.meta.ItemMeta; +// +//import java.util.ArrayList; +//import java.util.HashMap; +//import java.util.List; +// +//public class QMenuCreator implements QMenu { +// +// private final QPlayer owner; +// +// public QMenuCreator(QPlayer owner) { +// this.owner = owner; +// } +// +// @Override +// public HashMap<Integer, String> getSlotsToMenu() { +// return null; +// } +// +// @Override +// public QPlayer getOwner() { +// return owner; +// } +// +// public Inventory toInventory(int page) { +// String title = "Quest Creator"; +// // Inventory inventory = Bukkit.createInventory(null, 9, title); // // ItemStack newQuest = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 5); @@ -58,8 +64,8 @@ public class QMenuCreator implements QMenu { // inventory.setItem(4, editQuest); // inventory.setItem(6, removeQuest); // return inventory; - - return Bukkit.createInventory(null, 9, title); - } - -} +// +// return Bukkit.createInventory(null, 9, title); +// } +// +//} |
