package com.me.tft_02.soulbound.config; import java.util.ArrayList; import java.util.List; import java.util.Set; import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.material.MaterialData; import com.me.tft_02.soulbound.datatypes.ActionType; import com.me.tft_02.soulbound.datatypes.SoulbindItem; public class ItemsConfig extends ConfigLoader { private static ItemsConfig instance; private List soulbindOnCraft = new ArrayList(); private List soulbindOnOpenChest = new ArrayList(); private List soulbindOnPickupItem = new ArrayList(); private List soulbindOnDrop = new ArrayList(); private List soulbindOnRespawn = new ArrayList(); private List soulbindOnKit = new ArrayList(); public ItemsConfig() { super("items.yml"); loadKeys(); } public static ItemsConfig getInstance() { if (instance == null) { instance = new ItemsConfig(); } return instance; } @SuppressWarnings("deprecation") @Override protected void loadKeys() { ConfigurationSection configurationSection = config.getConfigurationSection("Items"); if (configurationSection == null) { return; } Set itemConfigSet = configurationSection.getKeys(false); for (String itemName : itemConfigSet) { String[] itemInfo = itemName.split("[|]"); Material itemMaterial = Material.matchMaterial(itemInfo[0]); if (itemMaterial == null) { plugin.getLogger().warning( "Invalid material name. This item will be skipped. - " + itemInfo[0]); continue; } byte itemData = (itemInfo.length == 2) ? Byte.valueOf(itemInfo[1]) : 0; MaterialData itemMaterialData = new MaterialData(itemMaterial, itemData); List lore = new ArrayList(); if (config.contains("Items." + itemName + ".Lore")) { for (String loreEntry : config.getStringList("Items." + itemName + ".Lore")) { lore.add(ChatColor.translateAlternateColorCodes('&', loreEntry)); } } String name = null; if (config.contains("Items." + itemName + ".Name")) { name = ChatColor.translateAlternateColorCodes('&', config.getString("Items." + itemName + ".Name")); } SoulbindItem soulbindItem = new SoulbindItem(itemMaterialData, name, lore); List actions = config.getStringList("Items." + itemName + ".Actions"); for (ActionType actionType : ActionType.values()) { if (actions.contains(actionType.toString())) { addSoulbindItem(actionType, soulbindItem); } } } } private void addSoulbindItem(ActionType actionType, SoulbindItem soulbindItem) { switch (actionType) { case CRAFT: soulbindOnCraft.add(soulbindItem); return; case OPEN_CHEST: soulbindOnOpenChest.add(soulbindItem); return; case PICKUP_ITEM: soulbindOnPickupItem.add(soulbindItem); return; case DROP_ITEM: soulbindOnDrop.add(soulbindItem); return; case RESPAWN: soulbindOnRespawn.add(soulbindItem); return; case KIT: soulbindOnKit.add(soulbindItem); return; } } public List getSoulbindItems(ActionType actionType) { switch (actionType) { case CRAFT: return soulbindOnCraft; case OPEN_CHEST: return soulbindOnOpenChest; case PICKUP_ITEM: return soulbindOnPickupItem; case DROP_ITEM: return soulbindOnDrop; case RESPAWN: return soulbindOnRespawn; case KIT: return soulbindOnKit; default: return null; } } public boolean isActionItem(ItemStack itemStack, ActionType actionType) { for (SoulbindItem soulbindItem : getSoulbindItems(actionType)) { if (itemStack.getData().equals(soulbindItem.getMaterialData())) { if (itemStack.hasItemMeta()) { ItemMeta itemMeta = itemStack.getItemMeta(); if (soulbindItem.getName() != null) if (itemMeta.getDisplayName().contains(soulbindItem.getName())) if (soulbindItem.getLore() != null && !soulbindItem.getLore().isEmpty()) if (itemMeta.hasLore() && itemMeta.getLore().containsAll(soulbindItem.getLore())) return true; } } } return false; } }