Solved Placing Items in a GUI, and multiple types of items?

Discussion in 'Plugin Development' started by YogurtSmudge, Dec 12, 2016.

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

    YogurtSmudge

    Ok so for any of you that have read any of my threads before...

    Problem 1:
    So if a player has a Diamond sword in their cursor and they try to place it, it's supposed to let them. And it does, But only sometimes. For example, If I try to pick up the sword multiple times it'll let me place it sometimes and cancel the event sometimes aswell.

    Code:
            
                if (event.getAction() == InventoryAction.PLACE_ONE ||
                        event.getAction() == InventoryAction.PLACE_SOME ||
                        event.getAction() == InventoryAction.PLACE_ALL &&
                        event.getCursor().getType() != Material.DIAMOND_SWORD) {
                    event.setCancelled(true);
                }
    
    Is there any method for getting a item that the CURSOR is currently holding?


    Problem 2:
    I currently have this method:
    Code:
                if (event.isLeftClick() && event.getCurrentItem().getType() != Material.DIAMOND_SWORD) {
                    event.setCancelled(true);
    
                }
    
    Which basically allows a player to pickup a diamond sword.
    It does work. However if I try to add more items for example like this:
    Code:
                if (event.isLeftClick() && event.getCurrentItem().getType() != Material.DIAMOND_SWORD || event.getCurrentItem().getType() != Material.BOW) {
                    event.setCancelled(true);
    
                }
    
    The player will no longer be able to pick up any of the two items.

    Please help with anything you guys can!
    Thanks again! GUI's are a pain! xD
     
    Last edited: Dec 12, 2016
  2. Offline

    Lordloss

    You allready used it, its event.getCursor().

    Carefully read what your if-statement does. It cannot work this way.
    You should read again some documentation how && and || operators work.
     
  3. Firstly, as @Lordloss said, event.getCursor() returns the item on the cursor. See here for more information. Secondly, if you look at your if statement carefully (the second one), then you will see that you say OR the item is not a bow then cancel the event. So, you will not be able to pick up anything apart from a bow.

    HOWEVER, since you check if it is not a diamond sword...

    The event is going to cancel if it is anything apart from a Bow, AND anything apart from a Diamond Sword, so in other words it's going to cancel whatever happens.

    EDIT:

    P.S: Oh, you also might want to check for a left click on any other items as well, it doesn't carry down.
     
  4. Offline

    YogurtSmudge

    Sulphate likes this.
Thread Status:
Not open for further replies.

Share This Page