Deny an item from switching between inventories

Discussion in 'Plugin Development' started by Staartvin, Aug 13, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Staartvin

    I'm working on an virtual chest plugin.

    I want to add a blacklist of items that cannot be taken or put into the virtual chest.
    This is the view of the virtual chest:

    http://pbrd.co/14uTWhF

    I want a player to not be able to take the wooden sword and put it into his/her own inventory.

    I've tried to use InventoryClickEvent, but when I cancel it, I can never put the item back again and it will be forever on my cursor, until I close the inventory and it drops on the ground. By dropping it on the ground, a player has the item now and that is just what I don't want.

    The code that I use now:

    Code:java
    1. @EventHandler
    2. public void onInventoryClick(InventoryClickEvent event) {
    3. Inventory inv = event.getInventory();
    4. Player player = (Player) event.getWhoClicked();
    5.  
    6. // Not a inventory we want to check for.
    7. if (!inv.getTitle().equalsIgnoreCase("Global Server Inventory"))
    8. return;
    9.  
    10. List<Integer> blacklist = plugin.getMainConfig().getBlackList();
    11.  
    12. if (event.getCursor() == null)
    13. return;
    14.  
    15. ItemStack item = event.getCursor().clone();
    16.  
    17. if (blacklist.contains(item.getTypeId())) {
    18.  
    19. event.setCancelled(true);
    20.  
    21. player.sendMessage(ChatColor.RED
    22. + "You cannot transfer this item because it is blacklisted on this server.");
    23. return;
    24. }
    25. }
     
  2. Offline

    valon750

    getCursor is the selected item attached to the cursor, getCurrentItem however is the item that the player has clicked.
     
  3. Offline

    Staartvin

    The problem is that when a player clicks an item, the item gets 'onto' getCursor() and getCurrentItem() returns air.
     
  4. Offline

    valon750

    Staartvin

    Exactly, so if you want to have the item be clickable, but not movable, you want to get if the currentItem is a certain type, and if so, cancel the event.
     
  5. Offline

    Staartvin

    getCurrentItem() always returns air for me.
     
  6. Offline

    valon750

    Staartvin

    ItemStack item = event.getCurrentItem().getType();

    How about that, and then check?
     
  7. Offline

    Staartvin

    valon750

    Hmm, it seems to work now. It is not returning air anymore. Thanks!
    Why isn't it returning air anymore?
     
  8. Offline

    valon750

    Staartvin

    Hmm, not too sure, as from the looks of the code you're searching for the itemID, I'll have to take a better look at that in the morning if I get chance before work.
     
Thread Status:
Not open for further replies.

Share This Page