Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

How do I check if a player has a clear inventory? I'm making a PvP arena plugin but I need it so when someone types /pvp join, it checks if they have a clear inventory. If they do have a clear inventory, it lets them join, if they don't, it asks them to.

I tried this but it's not reliable as there are more slots:

if(p.getInventory().getArmorContents() == null
    && p.getInventory().getItemInHand() == null
    && p.getInventory().getItemInHand() == null) {

Then I tried this but it did not work:

if (p.getInventory().getContents().length == 0) {
share|improve this question
add comment

1 Answer

You could try to accomplish it by looping through player's inventor and checking if Nth slot in his inventory is empty, if it isn't -- the function returns false.

    private boolean hasItems(ItemStack item) {
        for (int i=0; i<=thePlayer.inventory.getSizeInventory()-5; i++) {
             if (thePlayer.inventory.mainInventory[i].itemId != NULL) {
                 return false;
             }
        }
        return true;
    }

You subtract 1 from the getSizeInventory() because it returns length and the arrays in Java are zero-based, and 4 because crafting slots count as well (I guess).

share|improve this answer
1  
Arnas is correct. Check through the player inventory and return false immediately if an item is found. IIRC, there is a function mentioned in the MCP reversed code that does just that. I think it is in one of the inventory classes. –  Jason Coombes Feb 11 '13 at 11:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.