Inventory Click Event Help

Discussion in 'Plugin Development' started by hellobrad100, Apr 6, 2014.

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

    hellobrad100

    Hello everyone :)

    I have been using InventoryClickEvent, cancelling it and it works, however players can bypass this by hoovering over the slot and pressing the number on the hotbar.

    Is there anyway to cancel that?

    Thanks :)
     
  2. Offline

    xAstraah

    Code:java
    1. Inventory myInventory = Bukkit.createInventory(null, 9, "myInventory");
    2.  
    3. @EventHandler
    4. public void click(InventoryClickEvent event)
    5. {
    6.  
    7. Inventory inventory = event.getInventory();
    8.  
    9. if(inventory.getName().equalsIgnoreCase(myInventory.getName()))
    10. {
    11. event.setCancelled(true);
    12. }
    13.  
    14. }


    hellobrad100 This should fix that for you. If i helped please like my post!
     
  3. Offline

    hellobrad100

    Hi

    Thanks for the help, but that didnt fix the issue.

    I know how to cancel the click event, my problem is the number on the hotbar feature.

    So I hover on the place I want the thing in my hotbar to go like so:
    [​IMG]

    Then I press "1" on my keyboard and then the item will go into the place I hover on, like so:
    [​IMG]

    My question is, is there a way to cancel that along with all clicking?
    Thanks :)
     
  4. Offline

    xAstraah

    hellobrad100, Make another event handler with InventoryInteractEvent, They way i showed you, But with that listener instead.
     
  5. Offline

    xAstraah

    Hartorn, InventoryInteractEvent would be more efficient just because it'll stop all interact events like taking items etc. Just for that inventory.
     
    Hartorn likes this.
  6. xAstraah :From the doc
    I thought when seeing that in the doc that you could not use it, but instead other events extending this one. ;)

    But if working, it is a better solution ;)
     
  7. Offline

    xAstraah

    Hartorn, Yeah i always use it for when i code. That's why i said it ;)
     
  8. xAstraah : Never had to use event on Inventory. Good to know :)
     
  9. Offline

    hellobrad100

    I tried this but it dont work at all. Any ideas?

    Code:java
    1. @EventHandler
    2. public static void onMove(InventoryMoveItemEvent event)
    3. {
    4. ArrayList<String> lore;
    5. ItemStack is = new ItemStack(Material.DIAMOND_SWORD);
    6. ItemMeta iss = is.getItemMeta();
    7. lore = new ArrayList<String>();
    8. lore.add(ChatColor.RED + "You can not move this!");
    9. iss.setLore(lore);
    10. is.setItemMeta(iss);
    11.  
    12. if(event.getItem().getItemMeta().getLore() == lore)
    13. {
    14. event.setCancelled(true);
    15. }
    16. }
     
  10. Offline

    xAstraah

    hellobrad100, Mate you've done it wrong. Don't use lore and stuff to set events cancelled.
    Do the way i cancelled my event in my code i sent you using a Inventory and a event.getInventory();

    hellobrad100, Try this

    Code:java
    1. Inventory myInventory = Bukkit.createInventory(null, 9, "myInventory");
    2.  
    3. @EventHandler
    4. public void click(InventoryInteractEvent event)
    5. {
    6.  
    7. Inventory inventory = event.getInventory();
    8.  
    9. if(inventory.getName().equalsIgnoreCase(myInventory.getName()))
    10. {
    11. event.setCancelled(true);
    12. }
    13.  
    14. }
    15.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  11. Offline

    hellobrad100

    xAstraah
    I am not using the code above to cancel the event.
    I only want that item not to move, everything else is fine.

    So the item with the lore doesn't move, but everything else does.

    I know about that method, it didn't work as I wanted it to. That is why I am here.
     
  12. Offline

    xAstraah

    hellobrad100, Right, See what you have '== lore' change that to get the Inventory and to get that item. Because at the momment your trying to just find a lore.
     
  13. Offline

    alecito1000

    When Event is called using Hotbar buttons, the clicked item not equals to the moved item.
    You need check if event is not cancelled.
    If the event is not cancelled check if HotbarButton not equals to -1 (this means that the player used the Hotbar Buttons, because when user do a normal click, HotbarButton = -1)
    Code:java
    1. if (event.getHotbarButton() != -1){ }


    Later, check the Item of the Player Inventory in the hotbar Button:
    Code:java
    1. ItemStack itemMoved = evt.getWhoClicked().getInventory().getItem(evt.getHotbarButton());

    And check this item again for Cancel if is necessary.

    Full code:
    Code:java
    1. if (event.getHotbarButton() != -1){
    2. ItemStack itemMoved = evt.getWhoClicked().getInventory().getItem(evt.getHotbarButton());
    3. //Check if cant move this and cancel.
    4. }
     
Thread Status:
Not open for further replies.

Share This Page