1
\$\begingroup\$

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) {
\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

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).

\$\endgroup\$
2
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Feb 11, 2013 at 11:55
  • \$\begingroup\$ Why do you take in an itemstack? Where did you get thePlayer from \$\endgroup\$
    – Bálint
    Jul 8, 2016 at 9:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .