Disable player clicking on items in inventory

Discussion in 'Plugin Development' started by RuthlessRage, Sep 29, 2014.

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

    RuthlessRage

    if a player opens their inventory and has lets say stone in their inventory. if they were to click on the stone and try to drop it or move its slot place id like for it to be cancelled. heres what ive tried

    public void InventoryClickEvent(InventoryClickEvent event) {
    Player p = (Player) event.getWhoClicked();

    ItemStack itemclicked = event.getCurrentItem();
    if (!(p instanceof Player)) return if clicker is not a player
    return;
    if (event.getInventory().getType() == InventoryType.PLAYER) { //make sure its a players inventory
    if ((itemclicked.getType() == Material.STONE)){//check is the item clicked is stone
    event.setCancelled(true);//cancel the click
    p.sendMessage("You arent allowed to change this slot");//send message to player upon cancel
    }
    }
    return;
    }
    }
     
  2. Offline

    97WaterPolo

    RuthlessRage
    So I have had this issue with previous versions, check to see what the event.getInventory().getType() returns. When I debugged it (you probably should debug it), it returned InventoryType.CRAFTING, which is NOT the workbench but rather the 4 slots within the players inventory. Just try it and humor me ;)

    EDIT: Either way, Bukkit.broadcastMessage(event.getInventory().getType()); will tell you if your if statement is actually true.
     
  3. Offline

    Unica

    97WaterPolo
    Close ;) Although your answer is not wrong, I don't think the OP wants to do the thing you want to do with it

    RuthlessRage
    Hello sir,

    I've experience the same problem, and I know a fix.

    • If you want to stop moving a specific item inside the players inventory, you cannot just use inventorytype.Player, although u get the player inventory, you forget the crafting inventory (2x2 slots) in the top corner, which is in the players inventory too. Therefor, you need to do
      Code:java
      1. if(e.getInventory().getType().equals(InventoryType.PLAYER)) || e.getInventory().getType().equals(InventoryType.CRAFTING))){ e.setCancelled(true);}
    • For checking certain specific items, be sure to check if the players clicks outside of the inventory, by doing
      Code:java
      1. Itemstack clicked = e.getCurrentItem();
      2. if(clicked == null || clicked.getType().equals(Material.AIR)){ }
    • e.getWhoClicked() already returns a player object, therefor you don't need to check it.
      Code:java
      1. Player p = e.getWhoClicked(); //fine :)
    • 'returns' are considered bad practice if you use it everywhere, not saying you do here, but for the future
    EDIT: fixed typo
     
    97WaterPolo likes this.
  4. Offline

    RuthlessRage

    lol so lost

    public void InventoryClickEvent(InventoryClickEvent event) {
    Player p = (Player) event.getWhoClicked();

    ItemStack itemclicked = event.getCurrentItem();
    if (!(p instanceof Player)) return if clicker is not a player
    return;
    if(e.getInventory().getType().equals(InventoryType.PLAYER)) || e.getInventory().getType().equals(InventoryType.CRAFTING))){ //make sure its a players inventory
    if ((itemclicked.getType() == Material.STONE)){//check is the item clicked is stone
    event.setCancelled(true);//cancel the click
    p.sendMessage("You arent allowed to change this slot");//send message to player upon cancel
    }
    }
    return;
    }
    }

    look i dont want to disable the whole inventory just when a player clicks on stone lock id like for the event to be canceled so i dont see the purpose in
    1. if(clicked == null || clicked.getType().equals(Material.AIR)){ }

    also i dont mind if player uses the 2x2 craftiing the stone will only be in the inventory so id like to keep it simply coexided to that connotative
     
  5. Offline

    Unica

    RuthlessRage
    U will generate an error when u misclick >.<.
     
  6. Offline

    RuthlessRage

    i insterted your method and its not working ahhhhh
     
  7. Offline

    Unica

    Zombieghost_391 likes this.
  8. Offline

    Zombieghost_391

    If what you'r trying to do is to stop player from moving anything in there Inventory then use this :D

    Code:java
    1. @EventHandler
    2. public void InventoryClickEvent(InventoryClickEvent event){
    3. Player p = (Player) event.getWhoClicked(); //WHO CLICK THE ITEM
    4. Material t = event.getCurrentItem().getType(); //GETTING WHAT WAS CLICKED
    5. if (t != null || t != Material.AIR){ //NO NEED TO STOP THEM FROM REMOVEING AIR LOLz
    6. event.setCancelled(true); //CANCELLING THIS EVENT (STOPPING THE PLAYER FROM CLICKING)
    7. p.sendMessage(ChatColor.RED + "You can not remove items from you Inventory."); //INFORMING THE PLAYER THAT HE/SHE CAN'T DO THIS
    8. }
    9. }
     
    PDKnight likes this.
Thread Status:
Not open for further replies.

Share This Page