Solved Stop a player from picking up an Item

Discussion in 'Plugin Development' started by Matthew Broomfield, Oct 30, 2012.

Thread Status:
Not open for further replies.
  1. The goal: Player clicks on an item: place the item back in its spot, instead of picking it up.
    The following code works perfectly for left-clicks:

    private void switchItem(InventoryClickEvent event){
    ItemStack item;
    Player player = (Player) event.getWhoClicked();
    //stop the player from picking up an item
    item = event.getCursor();
    event.setCursor(event.getCurrentItem());
    event.setCurrentItem(item);
    }

    However, I cannot get right clicks to work.
    This code can help for debugging:

    private void stats(InventoryClickEvent event) {
    System.out.println("Cursor: " + event.getCursor().toString());
    System.out.println("Current Item: " + event.getCurrentItem().toString());
    }

    Any help would be amazing :)
     
  2. Offline

    ThatBox

    I don't think it can be private, and can we see the whole class to see if it isn't another problem?
     
  3. I should have clarified that my code isn't the handler, but the event is simply passed to it, so yes, it is supposed to be private.

    I've attached the whole class.
     

    Attached Files:

  4. Didn't work. Here is a Pastebin of the class

    http://pastebin.com/n2TKsaiY

    Also, this is not the only bug, but the problem I'm simply trying to solve is when right clicking occurs.

    And WOW, do things look poorly outside of Eclipse. :p
     
  5. Offline

    FTWinston

    So it looks like the InventoryClickEvent doesn't fire for right clicks? If so, not much you can do about that directly.

    I'm not at home right now: can you pick items up by right clicking on them? It might be that the client doesn't send the Packet102WindowClick for right clicks, in which case, this event would never get called.

    Edit: That it has an isRightClick() method, and its constructor takes a "button" parameter from the network packet, would suggest that it does call for right clicks.

    Try printing out whether it's a left or right click from in the event handler method itself?
     
  6. No no no, the problem is not that they do not right click,
    The problem is that they DO right-click. They pick up half of an itemstack. I do not want this to happen.
    If they click on 16 wool, I want all 16 wool to stay in the inventory, and none of it go on the mouse cursor.
     
  7. Offline

    fireblast709

    Quite easy :3

    Code:java
    1. @EventHandler(ignoreCancelled=true)
    2. public static void onInventoryClick(InventoryClickEvent event)
    3. {
    4. Player player = (Player) event.getWhoClicked();
    5. // Whatever you want to block
    6. // say i want to block myself
    7. if(player.getName().equalsIgnoreCase("Fireblast709"))
    8. {
    9. player.sendMessage("Nope, Chuck Testa");
    10. event.setResult(Event.Result.DENY); // Dunno if this was needed
    11. event.setCancelled(true);
    12. }
    13. }
     
  8. testing now.

    works :) thank you very much. Just goes to show that I should read ALL of the javadocs and try everything. I totally skipped over that method.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  9. Offline

    fireblast709

    no problem :3
     
Thread Status:
Not open for further replies.

Share This Page