package cn.citycraft.GuiShopManager; import java.util.HashMap; import java.util.List; import org.bukkit.Bukkit; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; import cn.citycraft.GuiShopManager.GuiShopManager; public class GSMItems implements Reloadable { private HashMap items = new HashMap(); private HashMap item_names = new HashMap(); private HashMap joinItems = new HashMap(); private GuiShopManager plugin; public GSMItems(GuiShopManager plugin) { this.plugin = plugin; loadItems(plugin); } public ItemStack getItemByName(String name) { ItemStack i = this.item_names.get(name); if (i != null) return i; for (String s : this.item_names.keySet()) { if (s.equalsIgnoreCase(name)) return this.item_names.get(s); } for (String s : this.item_names.keySet()) { if (s.toLowerCase().startsWith(name.toLowerCase())) return this.item_names.get(s); } return null; } public HashMap getItemNames() { return this.item_names; } public void giveJoinItems(Player p) { if (!this.joinItems.isEmpty()) { for (Integer integer : this.joinItems.keySet()) { int loc = integer.intValue(); ItemStack i = this.joinItems.get(Integer.valueOf(loc)); if ((!p.getInventory().contains(i.getType())) || (!hasItem(i, p))) { loc--; boolean b = false; if ((loc >= p.getInventory().getSize()) || (loc < 0)) { loc = 0; b = true; } if ((!b) || (p.getInventory().getItem(loc) == null)) { p.getInventory().setItem(loc, i.clone()); } else { p.getInventory().addItem(new ItemStack[] { i.clone() }); } } } } } private boolean hasItem(ItemStack i, Player p) { for (ItemStack s : p.getInventory().getContents()) { if ((s != null) && (s.hasItemMeta()) && (s.getItemMeta().hasDisplayName()) && (s.getItemMeta().getDisplayName().equalsIgnoreCase(i .getItemMeta().getDisplayName()))) return true; } return false; } public boolean isShopItem(ItemStack s) { if (s == null) return false; for (ItemStack i : this.items.keySet()) { if (i != null) { if (i.getType() == s.getType()) { if (i.getItemMeta().getDisplayName() .equalsIgnoreCase(s.getItemMeta().getDisplayName())) return true; } } } return false; } private void loadItems(GuiShopManager plugin) { ConfigurationSection c = plugin.getConfig().getConfigurationSection( "Items"); if (c == null) { Bukkit.getLogger() .severe("[GuiShopManager] No Items were found in the config :/ Delete your config and restart the server to generate a new config file."); return; } for (String path_name : c.getKeys(false)) { ConfigurationSection sec = c.getConfigurationSection(path_name); String open_shop = sec.getString("OpenShop"); if (open_shop != null) { boolean give_on_join = sec.getBoolean("GiveOnJoin"); int inventoryloc = sec.getInt("InventoryLocation"); List list = sec.getStringList("Look"); if (list != null) { ItemStack item = plugin.getBossShop().getClassManager() .getItemStackCreator().createItemStack(list); this.items.put(item, open_shop); this.item_names.put(path_name, item); if (give_on_join) { this.joinItems.put(Integer.valueOf(inventoryloc), item); } } } } } public void playerClicked(PlayerInteractEvent e) { if (((e.getAction() == Action.RIGHT_CLICK_AIR) || (e.getAction() == Action.RIGHT_CLICK_BLOCK)) && (e.getItem() != null)) { for (ItemStack i : this.items.keySet()) { if (i != null && i.hasItemMeta() && i.getItemMeta().hasDisplayName()) { if (e.getItem() != null && e.getItem().hasItemMeta() && e.getItem().getItemMeta().hasDisplayName()) { if (i.getItemMeta() .getDisplayName() .equalsIgnoreCase( e.getItem().getItemMeta() .getDisplayName())) { String n = this.items.get(i); this.plugin.getBossShop().getAPI() .openShop(e.getPlayer(), n); e.setCancelled(true); } } } } } } @Override public void reload(GuiShopManager plugin) { this.items.clear(); this.item_names.clear(); this.joinItems.clear(); loadItems(plugin); } }