[Q] How do I get inventory of player?

Discussion in 'Plugin Development' started by ColdIce, Jan 18, 2011.

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

    ColdIce

    Hello!

    What is the code for returning i list of all items in inventory of a player to the chat?

    Thanks!
     
  2. Offline

    Taco

    Try this:

    player.getInventory().getContents().toString();
     
  3. Offline

    ColdIce

    That didn't work.
     
  4. Offline

    DerpinLlama

    PHP:
                    String player "";
                    
    ItemStack[] items plugin.getServer().getPlayer(player).getInventory().getContents();
                    for(
    int i 0<= items.lengthi++){
                        
    plugin.getServer().getPlayer(player).sendMessage("inventory_item player=" split[1] + " slot=" " type=" items[i].getTypeId() + " amount=" items[i].getAmount());
                    }
     
  5. Offline

    ColdIce

    Thanks!

    I got it not to list up air. I also want this to list everything in one sendMessage, not spam the player with many lines.

    Also, how can I group a item? I tried with three slots of dirt, and it listed three dirt where amount = 1
     
  6. Offline

    DerpinLlama

    PHP:
                    String player "";
                    
    String buff "";
                    
    ItemStack[] items plugin.getServer().getPlayer(player).getInventory().getContents();
                    for(
    int i 0<= items.lengthi++){
                        
    buff += items[i].getType().toString() + " (" items[i].getAmount() + "), ";
                    }
                    
    plugin.getServer().getPlayer(player).sendMessage(buff.substring(0buff.length() - 2));
    Don't know how to group the item, sorry.
     
  7. If you want to copy their inventory to yours, while in game, type: /preview [player]
    [player] being the name of the player, who's inventory you want. Be warned, this will replace your inventory.
     
  8. Offline

    ColdIce

    When I run the code, it outputs "ArrayOutBounds 36" or something
     
  9. Code:
     String player = "";
                    String buff = "";
                    ItemStack[] items = plugin.getServer().getPlayer(player).getInventory().getContents();
                    for(int i = 0; i < items.length; i++){
                        buff += items[i].getType().toString() + " (" + items[i].getAmount() + "), ";
                    }
                   plugin.getServer().getPlayer(player).sendMessage(buff.substring(0, buff.length() - 2));
    
    try :)
     
  10. Offline

    ColdIce

    PHP:
        if(items[i].getType().toString().equalsIgnoreCase("AIR"))
                        {
                            
                        }else
                        {
                              
    buff += items[i].getType().toString() + " (" items[i].getAmount() + "), ";
                        }
    That gives an error with array.
     
  11. ah i overlooked something
    Code:
    String player = event.getPlayer().getName();
    i got to have the players name to get it so either try to get the name trough an event.
    or test it with your own Playername
     
  12. Offline

    ColdIce

    I'm using this under and OnCommand event (or something).
    So when and admin is using: /inventory [player_name_here], the code is getting the [player_name] and uses it.

    But the array error is when I'm skipping AIR-block
     
  13. me confused, code plx :)
     
  14. Offline

    ColdIce

    PHP:
    if (split[0].equalsIgnoreCase("/inventory")) {        
                    
    String buff "";
                    
    ItemStack[] items this.plugin.getServer().getPlayer(split[1]).getInventory().getContents();
                    for(
    int i 0<= items.lengthi++){
                        if(
    items[i].getType().toString().equalsIgnoreCase("AIR"))
                        {
                            
                        }else
                        {
                              
    buff += items[i].getType().toString() + " (" items[i].getAmount() + "), ";
                        }
                    }
                }
    When I'm doing this, an error is triggered about "ArrayOutOfBounds"
     
  15. Code:
    for(int i = 0; i <= items.length; i++){

    i <= items.length should be i < items.length because the return of items.lenght is the number of items.
    but in java you start at 0 so if you get to for example 7 which is also number items in the array the index is out of bounce
    my previous post with example was to fix that but it's still there :)



     
  16. Offline

    ColdIce

    I didn't saw that!

    Thank you so much for your help! :)
     
  17. Offline

    Chuck Lieb

    Shouldn't items[i].getType().toString() return an ID, and not a string?
     
  18. Offline

    Archelaus

    getType returns a Material, from the Enum.
     
Thread Status:
Not open for further replies.

Share This Page