@EventHandler(priority = EventPriority.NORMAL) public void onInventoryClick(InventoryClickEvent event) { if (!isItemInfoBook(event.getCursor())) { return; } if (!isPlace(event.getAction())) { return; } if (isInventory(event.getInventory())) { return; } if ((event.getRawSlot() - event.getView().getTopInventory().getSize()) >= 0) { return; } Module.sendMessage( "bInfoBooks", (Player) event.getWhoClicked(), "You can't store InfoBooks. Please drop the InfoBook to remove it from your inventory."); Module.sendMessage( "bInfoBooks", (Player) event.getWhoClicked(), "You can get another copy of the book via the '/book' command."); event.setResult(Result.DENY); }
/** * Cancel players from removing, picking the hat in their inventory. * * @param EVENT */ @EventHandler public void cancelHatClick(final InventoryClickEvent EVENT) { if (EVENT.getCurrentItem() != null && EVENT.getCurrentItem().hasItemMeta() && EVENT.getCurrentItem().getItemMeta().hasDisplayName() && EVENT.getCurrentItem().getItemMeta().getDisplayName().equals("§8§oHat")) { EVENT.setCancelled(true); EVENT.setResult(Event.Result.DENY); EVENT.getWhoClicked().closeInventory(); Bukkit.getScheduler() .runTaskLaterAsynchronously( Core.getPlugin(), new Runnable() { @Override public void run() { for (ItemStack itemStack : EVENT.getWhoClicked().getInventory().getContents()) { if (itemStack != null && itemStack.hasItemMeta() && itemStack.getItemMeta().hasDisplayName() && itemStack.getItemMeta().getDisplayName().equals("§8§oHat") && itemStack != EVENT.getWhoClicked().getInventory().getHelmet()) EVENT.getWhoClicked().getInventory().remove(itemStack); } } }, 1); } }
@EventHandler(priority = EventPriority.HIGHEST) public void inventoryMenu(final InventoryClickEvent event) { // -------Armor wear handling-------- if (event.getSlotType() == SlotType.ARMOR && event.getClick() != ClickType.DOUBLE_CLICK) { CEventHandler.handleArmor( (Player) event.getWhoClicked(), event.getCurrentItem(), true, event); CEventHandler.handleArmor((Player) event.getWhoClicked(), event.getCursor(), false, event); if (event.getCursor() == null) CEventHandler.handleArmor( (Player) event.getWhoClicked(), event.getCurrentItem(), false, event); } else if (event.getClick() == ClickType.SHIFT_LEFT) { ItemStack current = event.getCurrentItem(); String typeS = current.getType().toString(); PlayerInventory inv = event.getWhoClicked().getInventory(); if ((typeS.endsWith("HELMET") && inv.getHelmet() == null) || (typeS.endsWith("CHESTPLATE") && inv.getChestplate() == null) || (typeS.endsWith("LEGGINGS") && inv.getLeggings() == null) || (typeS.endsWith("BOOTS") && inv.getBoots() == null)) CEventHandler.handleArmor( (Player) event.getWhoClicked(), event.getCurrentItem(), false, event); } // --------------------------------- if (event.getCurrentItem() == null || event.getCurrentItem().getType() == Material.AIR || event.getRawSlot() < -1) { return; } if (event.getView().getTopInventory().getTitle().startsWith("CE")) { Inventory topInv = event.getView().getTopInventory(); final Player p = (Player) event.getWhoClicked(); ItemStack clickedItem = event.getCurrentItem(); event.setResult(Result.ALLOW); event.setCancelled(true); // This is the back-button, located in the very last spot of each inventory if ((event.getRawSlot() == topInv.getSize() - 1) && event.getCurrentItem().getType() != Material.AIR) { p.closeInventory(); p.openInventory(Tools.getPreviousInventory(topInv.getTitle())); return; } if (event.getRawSlot() < topInv.getSize()) { // Opens the clicked Enchantments inventory and loads the permissions if needed if (topInv.getTitle().equals(Tools.prefix + "Enchantments")) { p.closeInventory(); p.openInventory(Tools.getEnchantmentMenu(p, clickedItem.getItemMeta().getDisplayName())); return; } // Opens the item inventory and loads the permissions if needed if (topInv.getTitle().equals(Tools.prefix + "Main Menu") && event.getRawSlot() == 4) { p.closeInventory(); p.openInventory(Tools.getItemMenu(p)); return; } // These are the specific menus, clicking one of them will lead to the enchanting menu, // which needs to be 'notified' of the enchantment to give and it's cost if (topInv.getTitle().equals(Tools.prefix + "Global") || topInv.getTitle().equals(Tools.prefix + "Bow") || topInv.getTitle().equals(Tools.prefix + "Armor") || topInv.getTitle().equals(Tools.prefix + "Helmet") || topInv.getTitle().equals(Tools.prefix + "Boots") || topInv.getTitle().equals(Tools.prefix + "Tool")) if (p.isOp() || Tools.checkPermission( Tools.getEnchantmentByDisplayname(clickedItem.getItemMeta().getDisplayName()), p)) { Inventory enchantingMenu = Main.CEEnchantingMenu; enchantingMenu.setItem(0, clickedItem); p.closeInventory(); p.openInventory(enchantingMenu); return; } else { p.sendMessage( ChatColor.RED + "[CE] You do not have permission to buy this Enchantment."); return; } // This opens the Item Creation Menu if (topInv.getTitle().equals(Tools.prefix + "Items")) if (p.isOp() || Tools.checkPermission( Tools.getItemByDisplayname(clickedItem.getItemMeta().getDisplayName()), p)) { Inventory approveMenu = Main.CEApproveMenu; approveMenu.setItem(0, clickedItem); p.closeInventory(); p.openInventory(approveMenu); return; } else { p.sendMessage(ChatColor.RED + "[CE] You do not have permission to buy this Item."); return; } } if (topInv.getTitle().equals(Tools.prefix + "Enchanting") || topInv.getTitle().equals(Tools.prefix + "Item Creation")) { double cost = 0; ItemStack item = clickedItem; ItemMeta im = item.getItemMeta(); String type = ""; String successString = ""; Boolean itemSet = false; // TODO: Solve this by adding item-types // Swimsuit swimsuit = (Swimsuit) Main.items.get(9); if (topInv.getTitle().equals(Tools.prefix + "Enchanting")) { if (event.getRawSlot() > topInv.getSize() && event.isLeftClick()) { CEnchantment ce = Tools.getEnchantmentByDisplayname( topInv.getContents()[0].getItemMeta().getDisplayName()); cost = ce.getCost(); type = "enchantment " + ce.getDisplayName(); if (!ce.getApplication().equals(Application.GLOBAL) && !Tools.isApplicationCorrect(ce.getApplication(), clickedItem.getType())) { String appS = ce.getApplication().toString().toLowerCase(); p.sendMessage( ChatColor.RED + "[CE] This enchantment can only be applied to " + ChatColor.GRAY + (appS.endsWith("s") ? appS : appS + "s") + ChatColor.RED + "."); return; } List<String> lore = new ArrayList<String>(); if (clickedItem.getItemMeta().hasLore()) { lore = clickedItem.getItemMeta().getLore(); int number = 0; for (String s : lore) { CEnchantment c = Tools.getEnchantmentByDisplayname(s); if (c != null && c.originalName == ce.originalName) { p.sendMessage(ChatColor.RED + "[CE] This item already has this enchantment!"); return; } if (c != null) number++; } if (Main.maxEnchants > 0 && number >= Main.maxEnchants) { p.sendMessage( ChatColor.RED + "[CE] This item has already reached the maximum of " + ChatColor.GRAY + Main.maxEnchants + ChatColor.RED + " enchantments."); return; } } lore.add(ce.getDisplayName()); im.setLore(lore); item.setItemMeta(im); } else return; } else if (topInv.getTitle().equals(Tools.prefix + "Item Creation")) { item = topInv.getContents()[0]; cost = Tools.getItemByDisplayname(item.getItemMeta().getDisplayName()).getCost(); // if(item.hasItemMeta() && item.getItemMeta().hasDisplayName() && // item.getItemMeta().getDisplayName().equals(swimsuit.getDisplayName())) { //TODO: Always // keep the position of the Swimsuit updated // // // itemSet = true; // // int count = 0; // // for(ItemStack i : event.getView().getBottomInventory()) // if(i == null || i.getType().equals(Material.AIR)) // count++; // // if(count < 4) { // p.sendMessage(ChatColor.RED + "[CE] Your Inventory is full!"); // return; // } // // // } else { if (event.getView().getBottomInventory().firstEmpty() < 0) { p.sendMessage(ChatColor.RED + "[CE] Your Inventory is full!"); return; } // } ItemMeta itemIm = item.getItemMeta(); List<String> lore = itemIm.getLore(); lore.remove(lore.size() - 1); itemIm.setLore(lore); item.setItemMeta(itemIm); type = "item " + itemIm.getDisplayName(); } // TODO: Fix the cost for items // rewrite the whole section to check which enchantment or item is selected successString = (ChatColor.GREEN + "[CE] You have created the " + type + ChatColor.GREEN + "."); if (!p.isOp() && cost != 0) { if (Main.econ.getBalance(p.getName()) >= cost) { EconomyResponse ecr = Main.econ.withdrawPlayer(p.getName(), cost); if (ecr.transactionSuccess()) successString = (ChatColor.GREEN + "[CE] Purchased " + type + ChatColor.GREEN + " for " + cost + " " + ((cost == 1) ? Main.econ.currencyNameSingular() : Main.econ.currencyNamePlural())); else { Bukkit.getConsoleSender() .sendMessage(ChatColor.RED + "[CE] An economy error has occured:"); Bukkit.getConsoleSender().sendMessage(ChatColor.RED + ecr.errorMessage); p.closeInventory(); return; } } else { p.sendMessage(ChatColor.RED + "[CE] You do not have enough money to buy this!"); p.closeInventory(); return; } } if (item != null) { if (!itemSet) if (type.startsWith("item") && event.getRawSlot() < topInv.getSize()) p.setItemInHand(item); else p.getInventory().setItem(event.getSlot(), item); // else { // ItemStack cp = item.clone(); // ItemStack le = item.clone(); // ItemStack bo = item.clone(); // // String[] parts = swimsuit.parts; // // cp.setType(Material.IRON_CHESTPLATE); // le.setType(Material.IRON_LEGGINGS); // bo.setType(Material.IRON_BOOTS); // // im.setDisplayName(parts[1]); // cp.setItemMeta(im); // im.setDisplayName(parts[2]); // le.setItemMeta(im); // im.setDisplayName(parts[3]); // bo.setItemMeta(im); // // p.getInventory().addItem(item); // p.getInventory().addItem(cp); // p.getInventory().addItem(le); // p.getInventory().addItem(bo); // // } } p.closeInventory(); p.sendMessage(successString); return; } if (event.getRawSlot() < topInv.getSize()) { p.closeInventory(); try { p.openInventory(Tools.getNextInventory(clickedItem.getItemMeta().getDisplayName())); } catch (Exception e) { p.sendMessage(ChatColor.RED + "[CE] This feature is disabled."); } } } }