Don't use mc.thePlayer (unless you are in a Gui Screen or something) - that is client side only.
I don't think there is a vanilla method that does what you want, but you can write one yourself pretty easily:
public boolean hasNumItems(EntityPlayer p, Item req, int n) {
// loop through inventory, check if item matches, and subtract from n the size of the stack
// do this until n < 1 or the end of the inventory is reached
return n < 1;
}
Not likely - you can't compare ItemStacks by using the '==' operator, as that compares identity, not value. You can, however, use ItemStack.areItemStacksEqual or a similar method, but you need to choose carefully. Do you care about comparing stack size, NBT tag, and such things, or are you just interested in the Item that it contains?
Something like this would probably work just fine, depending on your needs:
for loop {
ItemStack stack = inventory.getStackInSlot(i);
if (stack != null && stack.getItem() == item) {
n -= stack.getStackSize();
}
}
Make sure to break out if n < 1, as at that point there's no reason to continue looping through the inventory.
I need to know if they have an item and then do they have a variable amount to that, say do they have 2 coal, or 4 iron
... that was the entire point of the parameter 'n' that I was using - that's the amount that you require the player to have, at a minimum. The logic is pretty straightforward, so if you're having trouble with it, you may need to take some time to think about exactly what is going on in the method, what each parameter is doing, and why I used 'n < 1' as the return statement.
I did some looking and i can't see a method that allows for item and quantity, anyone know how to do this?
I don't think there is a vanilla method that does what you want, but you can write one yourself pretty easily:
Something like this would probably work just fine, depending on your needs:
Make sure to break out if n < 1, as at that point there's no reason to continue looping through the inventory.
... that was the entire point of the parameter 'n' that I was using - that's the amount that you require the player to have, at a minimum. The logic is pretty straightforward, so if you're having trouble with it, you may need to take some time to think about exactly what is going on in the method, what each parameter is doing, and why I used 'n < 1' as the return statement.