getInventory() help

Discussion in 'Plugin Development' started by Arrxzon, Dec 28, 2013.

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

    Arrxzon

    Hi
    im looking to check to see if there is space in a players inventory for a certain amount of items say {9}
    if there isn't 9 slots free it will cancel the command and send out a message
    if there is 9 slots available for the kit it will go ahead with the rest

    if i have to i don't mind adding so the players inventory must be empty to use a command
    i'd prefer the other way do

    thanks
    please tahg in reply
     
  2. Offline

    mrkirby153

    Arrxzon

    Loop through the player's inventory and check if the slot is equal to null. Then add 1 to a count. After going through all the slots, compare the value to the slots needed. (To loop through the inventory run a foreach on player.getImventory().getContents()
     
  3. Offline

    Arrxzon

    mrkirby153 thanks
    im trying this way right now as it's way simpler
    only problem is sends a message for every slot..?
    i know player.getInventory().firstEmpty() or player.getInventory().getFirstEmpty() (or something) but not sure on how to implement were functional

    This is the working method that spams the message
    Thanks

    for(ItemStack item : player.getInventory().getContents()){
    if(item != null){
    player.sendMessage("Please clear your inventory first.");
    }else{
    //do stuff
    }
    }
     
  4. Offline

    zzienzz

    If you're doing what I think you're doing. You might want to consider dropping the items at the players feet if they inventory is full.

    Overflow items don't get added, and then people compain for refunds. My method suggested above could also help with the problem.
     
  5. Offline

    Arrxzon

    zzienzz
    aiming to use the above "Please clear your inventory first" message at the moment to push a release to our server.
    As it's working only thing is the message spams
     
  6. Offline

    Pizza371

    Arrxzon
    for(ItemStack item : player.getInventory().getContents()){
    if(item == null){
    player.sendMessage("Please clear your inventory first.");
    break;
    }
    }
     
  7. Offline

    Arrxzon

    doesn't seem to work
     
  8. Offline

    Pizza371

    Arrxzon my approach ? Does it still send messages? Have you altered what i gave u?
    tahg me if u wan me pls :)
     
  9. Offline

    Arrxzon

    Pizza371
    ye did not send message-took away coins-
    i have it like

    if (cmd.getName().equalsIgnoreCase("rankkit")) {
    if (args.length == 1) {
    if (args[0].equalsIgnoreCase("recruit")) {
    for(ItemStack item : player.getInventory().getContents()){
    if(item == null){
    player.sendMessage("Please clear your inventory first.");
    break;
    }
    }
    the rest doing stuff that it should if inventory is empty
    }
    }
    }

    then altered it a couple of times with no luck
     
  10. Offline

    mrkirby153

    Arrxzon

    Try
    Code:java
    1. int openSlots = 0;
    2. for(ItemStack item : player.getInventory.getContents(){
    3. If(item=null)
    4. openSlots++;
    5. }
    6. If(openSlots < slotsNeeded /* The slots in the inventory that you need to fill with items */){
    7. player.sendMessage("Please remove "+(slotsNeeded - openSlots)+" items from your inventory!");
    8. return;
    9. }
    10. // Put your code here.
     
    Arrxzon likes this.
  11. Offline

    Arrxzon

    mrkirby153

    Beautiful man thanks for this

    slight modification below got it working
    thanks again :)[diamond]:)

    Code:java
    1.  
    2. private static final int slotsNeeded = 10;
    3.  
    4. //command args
    5. int openSlots = 0;
    6. for(ItemStack item : player.getInventory().getContents()){
    7. if(item == null)
    8. openSlots++;
    9. }
    10. if(openSlots < slotsNeeded){
    11. player.sendMessage("Please remove "+(slotsNeeded - openSlots)+" items from your inventory!");
    12. return false;
    13. }
    14. // code here.
     
Thread Status:
Not open for further replies.

Share This Page