Advertisement
mattibijnens

InventoryClickEvent

Jul 12th, 2015
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | None | 0 0
  1. @SuppressWarnings("deprecation")
  2.     @EventHandler
  3.     public void CGinvClick(InventoryClickEvent event) {
  4.         final int slot = 0; //The first slot of the chest
  5.         Player p = (Player) event.getWhoClicked();
  6.         p.sendMessage(event.getInventory().getName());
  7.         if (event.getRawSlot() < event.getInventory().getSize() && //This makes sure the player clicked in the chest, and not the player's inventory.
  8.                 event.getRawSlot() == slot) // This makes sure it only gives the player the item in his hand if he clicks the item. (Could be replaced with event.getItem() == book)
  9.             {
  10.             event.setCancelled(true); //If you don't cancel it, it will do this odd thing you showed in your video, where it creates ghost books.
  11.             ItemStack book = new ItemStack(Material.WRITTEN_BOOK);
  12.             book.setAmount(1);
  13.             event.getInventory().clear(); //Clearing it, in case the player placed the book in the chest.
  14.             event.getInventory().setItem(slot,book); //Because we cancelled the event, we have to manually give the player the item on his cursor.
  15.             p.updateInventory();
  16.             p.setItemOnCursor(book); //Can't explain, remove it to see the difference :P
  17.            
  18.            
  19.         }
  20.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement