fix ActionItem Null Error...

Signed-off-by: j502647092 <jtb1@163.com>
pull/1/MERGE
j502647092 2015-07-17 13:47:54 +08:00
parent e82831db0d
commit 6f7cd753c3
1 changed files with 111 additions and 116 deletions

View File

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