[SOLVED] Inventory

Discussion in 'Spigot Plugin Development' started by Just_Jitse, Jul 5, 2015.

  1. Hello people,

    I am trying to make it like this:
    - in gm0 and no OP, people can't change the place or drop the item
    - in OP mode, everything is just possible like always

    but I am getting a nullpointerexeption when clicking outside the inventory

    Could someone help me?

    Thanks!

    Class:
    Code (Text):
        @EventHandler
        public void onItemClick(InventoryClickEvent e){
           //I think the problem is here
            if(e.getCursor().getType() != null){
                if(e.getWhoClicked().isOp()){
                    return;}
                else if(e.getClickedInventory().equals(InventoryType.PLAYER)){
                    return;
                }
                else{
                    e.setCancelled(true);
                }
            }
            else{
                return;
            }
        }
     
        @EventHandler
        public void onItemMove(InventoryMoveItemEvent e){
            if(((ServerOperator) e.getSource().getHolder()).isOp()){
                return;
                } else {
                    e.setCancelled(true);
                }
        }
     
        @EventHandler
        public void onDrop(PlayerDropItemEvent e){
            if(e.getPlayer().isOp()){
                return;
                } else {
                    e.setCancelled(true);
                }
        }
     
  2. You have in the inventoryClickevent the getAction() method, and you just check if the action is the action you want to check, and what also could be good, is to check if e.getCursor() isn't null using an if statement
     
  3. It isn't what also could be good, it is what should be done. If the getCursor() item is null, that means you can't do getCursor().getType(), therefore throwing an error. So you must put getCursor() != null instead of getCursor().getType() != null. Sorry if this sounded like I offended you, but just clarifying some things.
     
  4. I know it should be done, but if you check if the action is a curtain action, you would know that getCursor() is always not null, and then you don't need to do it, but it then would still be a good idea.
     
  5. @BlazingBroGamer Okay, changed it, but didn't made a difference at all. Still giving the nullpointerexeption :(
    Checking the action won't help @bram0101 because the exeption is given no matter which action I do outside the inventory.

    ps. Didn't felt like you were offfending me, keep going mate ;)
     
  6. Can you give us the stack trace?

    ps. I thought I was offending @bram0101 :p
     
  7. Could you give the full exception you get, and the source so we can see how you changed the code?
     
  8. Code (Text):
        @EventHandler
        public void onItemClick(InventoryClickEvent e){
            if(e.getCursor() != null){
                if(e.getWhoClicked().isOp()){
                    return;}
                else if(e.getClickedInventory().equals(InventoryType.PLAYER)){
                    return;
                }
                else{
                    e.setCancelled(true);
                }
            }
            else{
                return;
            }
        }
       
        @EventHandler
        public void onItemMove(InventoryMoveItemEvent e){
            if(((ServerOperator) e.getSource().getHolder()).isOp()){
                return;
                } else {
                    e.setCancelled(true);
                }
        }
       
        @EventHandler
        public void onDrop(PlayerDropItemEvent e){
            if(e.getPlayer().isOp()){
                return;
                } else {
                    e.setCancelled(true);
                }
        }
       
    @bram0101 , ps it's placed in an implement listener
     
  9. what do you have on line 16?
     
  10. yes, what's on line 29?
     
  11. Found the problem, just had the remove
    Code (Text):
                else if(e.getClickedInventory().equals(InventoryType.PLAYER)){
                    return;
                }
    now it works fine, thanks guys ;)
     
  12. @bram0101
    Code (Text):
    else if(e.getClickedInventory().equals(InventoryType.PLAYER)){
    is line 29
     
  13. to check the inventory type, you first check if the clicked inventory isn't null, and then you check if the type of the clicked inventory is equal to InventoryType.PLAYER
     
  14. So, it should be like this? @bram0101

    Code (Text):
        @EventHandler
        public void onItemClick(InventoryClickEvent e){
            if(e.getCursor() != null || e.getWhoClicked().getGameMode() != GameMode.CREATIVE){
                if(e.getClickedInventory().equals(InventoryType.PLAYER)){
                    e.setCancelled(true);
                    } else{
                return;
                }
            }
        }
     
  15. if(e.getClickedInventory().equals(InventoryType.PLAYER)){

    that's wrong, you check if an inventory is equal to an inventory type, so it would be checking if the clicked inventory ISN'T null and checking if the type (you can find the get method for the type inside the inventory class) of the clicked inventory is equal to the PLAYER value in the InventoryType class.
     
  16. And how am I supposed to do that, if I may ask? xD @bram0101