@Override @SuppressWarnings( "deprecation") // Player.updateInventory has been deprecated for a while with no alternative // available yet public void onClick(final InventoryClickEvent click) { final int last = click.getView().getTopInventory().getSize() - 1; // cancel shift clicks on items outside of delivery inventory to prevent indirect moving of // items into delivery inventory if (click.isShiftClick() && (click.getCurrentItem().getTypeId() != Material.AIR.getId()) && (click.getRawSlot() > last)) click.setCancelled(true); // cancel clicks with item on cursor in delivery inventory to prevent direct placement of items // into delivery inventory if ((click.getCursor().getTypeId() != Material.AIR.getId()) && (click.getRawSlot() >= 0) && (click.getRawSlot() <= last)) click.setCancelled(true); if (click.isCancelled()) { click.setCursor(click.getCursor()); ((Player) click.getWhoClicked()).updateInventory(); this.courier.send((Player) click.getWhoClicked(), "withdraw-only"); } }
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) public void onInventoryClick(InventoryClickEvent e) { if (useLocaleInv && e.getView() instanceof LocaleInventory) { LocaleInventory inv = (LocaleInventory) e.getView(); InventoryClickEvent clickEvent = new InventoryClickEvent( inv.getView(), e.getSlotType(), e.getRawSlot(), e.isRightClick(), e.isShiftClick()); Bukkit.getServer().getPluginManager().callEvent(clickEvent); if (clickEvent.isCancelled()) { e.setCancelled(true); } else { switch (clickEvent.getResult()) { case DEFAULT: // Can't really do this with current events case ALLOW: System.out.println("ok..."); System.out.println(inv.getView().getItem(e.getRawSlot())); inv.getView().setItem(e.getRawSlot(), clickEvent.getCursor()); System.out.println(inv.getView().getItem(e.getRawSlot())); break; case DENY: break; } } for (LocaleInventory localeInv : localeInventories) { if (localeInv != inv) localeInv.reload(); } } }
@Override protected void onPurchaseClick( InventoryClickEvent event, Player player, ItemStack[] usedRecipe, ItemStack offered1, ItemStack offered2) { super.onPurchaseClick(event, player, usedRecipe, offered1, offered2); if (event.isCancelled()) return; ItemStack book = usedRecipe[2]; String title = getTitleOfBook(book); if (title == null) { // this should not happen.. because the recipes were created based on the shopkeeper's // offers event.setCancelled(true); return; } // get chest: Block chest = ((WrittenBookPlayerShopkeeper) shopkeeper).getChest(); if (!Utils.isChest(chest.getType())) { event.setCancelled(true); return; } // remove blank book from chest: boolean removed = false; Inventory inv = ((Chest) chest.getState()).getInventory(); ItemStack[] contents = inv.getContents(); for (int i = 0; i < contents.length; i++) { if (contents[i] != null && contents[i].getType() == Material.BOOK_AND_QUILL) { if (contents[i].getAmount() == 1) { contents[i] = null; } else { contents[i].setAmount(contents[i].getAmount() - 1); } removed = true; break; } } if (!removed) { event.setCancelled(true); return; } // get price: Integer priceInt = ((WrittenBookPlayerShopkeeper) shopkeeper).offers.get(title); if (priceInt == null) { event.setCancelled(true); return; } int price = this.getAmountAfterTaxes(priceInt.intValue()); // add earnings to chest: if (price > 0) { int highCost = price / Settings.highCurrencyValue; int lowCost = price % Settings.highCurrencyValue; if (highCost > 0) { if (Utils.addItems(contents, createHighCurrencyItem(highCost)) != 0) { event.setCancelled(true); return; } } if (lowCost > 0) { if (Utils.addItems(contents, createCurrencyItem(lowCost)) != 0) { event.setCancelled(true); return; } } } // set chest contents: inv.setContents(contents); }
@EventHandler public void onInventoryClick(InventoryClickEvent event) { if (event.isCancelled()) { return; } String name = ChatColor.stripColor(event.getInventory().getName()); String category = ""; boolean isCategory = name.equalsIgnoreCase("Townships Categories"); String[] names = name.split(" "); if (!isCategory) { if (names.length != 2 || !names[1].equals("Regions")) { return; } else { category = names[0].toLowerCase(); } } Player player = (Player) event.getWhoClicked(); event.setCancelled(true); if (event.getCurrentItem() == null || event.getCurrentItem().getType() == Material.AIR || !event.getCurrentItem().hasItemMeta()) { // player.closeInventory(); return; } if (isCategory) { String categoryOpen = ChatColor.stripColor(event.getCurrentItem().getItemMeta().getDisplayName()).toLowerCase(); player.closeInventory(); player.performCommand("to list " + categoryOpen); return; } if (event.getCurrentItem().hasItemMeta() && ChatColor.stripColor(event.getCurrentItem().getItemMeta().getDisplayName()) .equals("Back to Categories")) { player.closeInventory(); player.performCommand("to list"); return; } // player.performCommand("to info " + // ChatColor.stripColor(event.getCurrentItem().getItemMeta().getDisplayName())); String regionName = ChatColor.stripColor(event.getCurrentItem().getItemMeta().getDisplayName()).toLowerCase(); RegionType rt = rm.getRegionType(regionName); SuperRegionType srt = null; if (rt == null) { srt = rm.getSuperRegionType(regionName); if (srt == null) { player.closeInventory(); return; } } String backButton = ChatColor.stripColor( event .getInventory() .getItem(event.getInventory().getSize() - 1) .getItemMeta() .getLore() .get(0)); player.closeInventory(); if (rt != null) { InfoGUIListener.openInfoInventory(rt, player, backButton); } else { InfoGUIListener.openInfoInventory(srt, player, backButton); } }
// Handles player interaction with anvil while in or out of creative. @SuppressWarnings("deprecation") @EventHandler(priority = EventPriority.NORMAL) public void anvilFix(final InventoryClickEvent e) { if (!e.isCancelled()) { HumanEntity ent = e.getWhoClicked(); // Anvil used with survival if (ent instanceof Player) { final Player player = (Player) ent; Inventory inv = e.getInventory(); // Anvil used with creative: if (player.getGameMode() == GameMode.CREATIVE) { if (inv instanceof AnvilInventory) { AnvilInventory anvil = (AnvilInventory) inv; InventoryView view = e.getView(); int rawSlot = e.getRawSlot(); if (rawSlot == view.convertSlot(rawSlot)) { if (rawSlot == 2) { ItemStack[] items = anvil.getContents(); ItemStack item1 = items[0]; ItemStack item2 = items[1]; if (item1 != null && item2 != null) { int id1 = item1.getTypeId(); int id2 = item2.getTypeId(); if (id1 != 0 && id2 != 0) { ItemStack item3 = e.getCurrentItem(); if (item3 != null) { ItemMeta meta = item3.getItemMeta(); if (meta != null) { // Player has to have 60 or more levels to use the creative combine. if (player.getLevel() >= 60) { player.setLevel(player.getLevel() - 60); player.sendMessage(ChatColor.GREEN + "Repair/Combine Successful!"); Bukkit.getServer() .getScheduler() .scheduleSyncDelayedTask( mobfighter, new Runnable() { public void run() { // Bypass being set to survival after using anvil if the // player has creative immunity. if (mobfighter .getConfig() .getList("Creative Immunity") .contains(player.getDisplayName())) return; // Sets the player back to survival once the anvil is used. e.getWhoClicked().getOpenInventory().close(); player.setGameMode(GameMode.SURVIVAL); } }, 20 * 2); } // End of creative combine. } // Didn't have enough levels to combine/finishing. } // Item 3 was/wasn't null. } // Item 1 & 2 id's were/weren't null. } // Item 1 & 2 was/wasn't null. } // End of (rawSlot == 2) } // End of convert view } // End of anvil inventory else { // Bypass having the player's inventory closed if they have creative immunity. if (mobfighter .getConfig() .getList("Creative Immunity") .contains(player.getDisplayName())) e.setCancelled(false); // Closes the player's inventory after the anvil is used. else { e.setCancelled(true); player.closeInventory(); } } } // End of anvil with creative } // End of anvil with survival } // End of !isCancelled } // End of anvilFix
@Override protected void onPurchaseClick(InventoryClickEvent event, Player player) { super.onPurchaseClick(event, player); if (event.isCancelled()) return; // get type and cost ItemStack item = event.getCurrentItem(); ItemStack type = item.clone(); type.setAmount(1); Cost cost = ((TradingPlayerShopkeeper) shopkeeper).costs.get(type); if (cost == null) { event.setCancelled(true); return; } if (cost.amount != item.getAmount()) { event.setCancelled(true); return; } // get chest Block chest = ((TradingPlayerShopkeeper) shopkeeper).getChest(); if (!Utils.isChest(chest.getType())) { event.setCancelled(true); return; } // remove item from chest Inventory inventory = ((Chest) chest.getState()).getInventory(); ItemStack[] contents = inventory.getContents(); boolean removed = this.removeFromInventory(item, contents); if (!removed) { event.setCancelled(true); return; } // add traded items to chest if (cost.item1 == null) { event.setCancelled(true); return; } else { ItemStack c = cost.item1.clone(); c.setAmount(this.getAmountAfterTaxes(c.getAmount())); if (c.getAmount() > 0) { boolean added = this.addToInventory(c, contents); if (!added) { event.setCancelled(true); return; } } } if (cost.item2 != null) { ItemStack c = cost.item2.clone(); c.setAmount(this.getAmountAfterTaxes(c.getAmount())); if (c.getAmount() > 0) { boolean added = this.addToInventory(c, contents); if (!added) { event.setCancelled(true); return; } } } // save chest contents inventory.setContents(contents); }
@SuppressWarnings("deprecation") @EventHandler public void onInventoryClick(InventoryClickEvent event) { // controller.getLogger().info("CLICK: " + event.getAction() + ", " + event.getClick() + " on " // + event.getSlotType() + " in "+ event.getInventory().getType() + " slots: " + event.getSlot() // + ":" + event.getRawSlot()); if (event.isCancelled()) return; if (!(event.getWhoClicked() instanceof Player)) return; Player player = (Player) event.getWhoClicked(); Mage apiMage = controller.getMage(player); if (!(apiMage instanceof com.elmakers.mine.bukkit.magic.Mage)) return; final com.elmakers.mine.bukkit.magic.Mage mage = (com.elmakers.mine.bukkit.magic.Mage) apiMage; GUIAction gui = mage.getActiveGUI(); if (gui != null) { gui.clicked(event); return; } // Check for temporary items and skill items InventoryAction action = event.getAction(); InventoryType inventoryType = event.getInventory().getType(); ItemStack clickedItem = event.getCurrentItem(); boolean isDrop = event.getClick() == ClickType.DROP || event.getClick() == ClickType.CONTROL_DROP; boolean isSkill = clickedItem != null && Wand.isSkill(clickedItem); // Preventing putting skills in containers if (isSkill && inventoryType != InventoryType.CRAFTING) { if (!isDrop) { event.setCancelled(true); } return; } // Check for right-click-to-use if (isSkill && action == InventoryAction.PICKUP_HALF) { Spell spell = mage.getSpell(Wand.getSpell(clickedItem)); if (spell != null) { spell.cast(); } player.closeInventory(); event.setCancelled(true); return; } if (clickedItem != null && NMSUtils.isTemporary(clickedItem)) { String message = NMSUtils.getTemporaryMessage(clickedItem); if (message != null && message.length() > 1) { mage.sendMessage(message); } ItemStack replacement = NMSUtils.getReplacement(clickedItem); event.setCurrentItem(replacement); event.setCancelled(true); return; } // Check for wearing spells ItemStack heldItem = event.getCursor(); if (heldItem != null && event.getSlotType() == InventoryType.SlotType.ARMOR) { if (Wand.isSpell(heldItem)) { event.setCancelled(true); return; } if (Wand.isWand(clickedItem) || Wand.isWand(heldItem)) { controller.onArmorUpdated(mage); } } boolean isHotbar = event.getAction() == InventoryAction.HOTBAR_SWAP || event.getAction() == InventoryAction.HOTBAR_MOVE_AND_READD; if (isHotbar && event.getSlotType() == InventoryType.SlotType.ARMOR) { int slot = event.getHotbarButton(); ItemStack item = mage.getPlayer().getInventory().getItem(slot); if (item != null && Wand.isSpell(item)) { event.setCancelled(true); return; } controller.onArmorUpdated(mage); } Wand activeWand = mage.getActiveWand(); boolean isChest = inventoryType == InventoryType.CHEST || inventoryType == InventoryType.HOPPER || inventoryType == InventoryType.DISPENSER || inventoryType == InventoryType.DROPPER; boolean clickedWand = Wand.isWand(clickedItem); boolean isContainerSlot = event.getSlot() == event.getRawSlot(); if (activeWand != null && activeWand.isInventoryOpen()) { // Kind of a hack to avoid hotbar items with 0 amounts disappearing :( Inventory inventory = player.getInventory(); for (int i = 0; i < 8; i++) { ItemStack item = inventory.getItem(i); if (item != null && item.getAmount() == 0) { item.setAmount(1); } } // Don't allow the offhand slot to be messed with while the spell inventory is open if (event.getRawSlot() == 45) { event.setCancelled(true); return; } if (Wand.isSpell(clickedItem) && clickedItem.getAmount() != 1) { clickedItem.setAmount(1); } if (clickedWand) { event.setCancelled(true); if (dropChangesPages) { activeWand.cycleInventory(); } else { activeWand.cycleHotbar(1); } return; } // So many ways to try and move the wand around, that we have to watch for! if (isHotbar && Wand.isWand(player.getInventory().getItem(event.getHotbarButton()))) { event.setCancelled(true); return; } // Can't wear spells if (event.getAction() == InventoryAction.MOVE_TO_OTHER_INVENTORY && clickedItem != null) { if (controller.isWearable(clickedItem)) { event.setCancelled(true); return; } } // Safety check for something that ought not to be possible // but perhaps happens with lag? if (Wand.isWand(event.getCursor())) { activeWand.closeInventory(); event.setCursor(null); event.setCancelled(true); return; } } else if (activeWand != null) { // Check for changes that could have been made to the active wand Integer activeSlot = player.getInventory().getHeldItemSlot(); if (activeSlot != null && (event.getSlot() == activeSlot || (event.getAction() == InventoryAction.HOTBAR_SWAP && event.getHotbarButton() == activeSlot))) { mage.checkWand(); activeWand = mage.getActiveWand(); } } else if (clickedWand && Wand.Undroppable && !player.hasPermission("Magic.wand.override_drop") && isChest && !isContainerSlot) { Wand wand = new Wand(controller, clickedItem); if (wand.isUndroppable()) { event.setCancelled(true); return; } } // Check for armor changing if (event.getAction() == InventoryAction.MOVE_TO_OTHER_INVENTORY && clickedItem != null) { if (controller.isWearable(clickedItem)) { controller.onArmorUpdated(mage); } } // Check for dropping items out of a wand's inventory // or dropping undroppable wands if (isDrop) { if (clickedWand) { Wand wand = new Wand(controller, clickedItem); if (wand.isUndroppable()) { event.setCancelled(true); if (activeWand != null) { if (activeWand.getHotbarCount() > 1) { activeWand.cycleHotbar(1); } else { activeWand.closeInventory(); } } return; } } if (activeWand != null && activeWand.isInventoryOpen()) { ItemStack droppedItem = clickedItem; if (!Wand.isSpell(droppedItem)) { mage.giveItem(droppedItem); event.setCurrentItem(null); event.setCancelled(true); return; } // This is a hack to deal with spells on cooldown disappearing, // Since the event handler doesn't match the zero-count itemstacks Integer slot = event.getSlot(); int heldSlot = player.getInventory().getHeldItemSlot(); Inventory hotbar = activeWand.getHotbar(); if (hotbar != null && slot >= 0 && slot <= hotbar.getSize() && slot != heldSlot && activeWand.getMode() == WandMode.INVENTORY) { if (slot > heldSlot) slot--; if (slot < hotbar.getSize()) { droppedItem = hotbar.getItem(slot); } else { slot = null; } } else { slot = null; } if (!controller.isSpellDroppingEnabled()) { player.closeInventory(); String spellName = Wand.getSpell(droppedItem); if (spellName != null && !activeWand.isManualQuickCastDisabled()) { Spell spell = mage.getSpell(spellName); if (spell != null) { activeWand.cast(spell); // Just in case a spell has levelled up... jeez! if (hotbar != null && slot != null) { droppedItem = hotbar.getItem(slot); } } } event.setCancelled(true); // This is needed to avoid spells on cooldown disappearing from the hotbar if (hotbar != null && slot != null && mage.getActiveGUI() == null) { player.getInventory().setItem(event.getSlot(), droppedItem); player.updateInventory(); } return; } ItemStack newDrop = controller.removeItemFromWand(activeWand, droppedItem); if (newDrop != null) { Location location = player.getLocation(); Item item = location.getWorld().dropItem(location, newDrop); item.setVelocity(location.getDirection().normalize()); } else { event.setCancelled(true); } } return; } // Check for wand cycling with active inventory if (activeWand != null) { WandMode wandMode = activeWand.getMode(); if ((wandMode == WandMode.INVENTORY && inventoryType == InventoryType.CRAFTING) || (wandMode == WandMode.CHEST && inventoryType == InventoryType.CHEST)) { if (activeWand.isInventoryOpen()) { if (event.getAction() == InventoryAction.NOTHING) { int direction = event.getClick() == ClickType.LEFT ? 1 : -1; activeWand.cycleInventory(direction); event.setCancelled(true); return; } if (event.getSlotType() == InventoryType.SlotType.ARMOR) { event.setCancelled(true); return; } // Chest mode falls back to selection from here. if (event.getAction() == InventoryAction.PICKUP_HALF || wandMode == WandMode.CHEST) { controller.onPlayerActivateIcon(mage, activeWand, clickedItem); player.closeInventory(); event.setCancelled(true); } } } } }