Solved Check if player inventory is full

Discussion in 'Plugin Development' started by LordManegane, Apr 21, 2013.

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

    LordManegane

    Well, im here to ask how i can check if a player inventory is full or not.
    I use a code like that (the mine is the same but with some extras, but its the same).

    ItemStack cobblestone = new ItemStack(Material.COBBLESTONE, 10);

    if((player.getInventory().contains(Material.COBBLESTONE, 10))){

    player.getInventory().addItem(<ITEM>);
    player.getInventory().removeItem(cobblestone);

    }

    Whell, if you don´t have enought space on the inventory the code takes the cobblestone and doesn´t give you the Item/Block.

    How i can check if the inventory is full? please help.
     
  2. Offline

    Wolftic

    player.getCanPickupItems() ?
     
  3. Offline

    desht

    No. That's a method to check if a LivingEntity is able to pick things up at all, not if a player has space in their inventory.

    LordManegane The method you need is player.getInventory.firstEmpty() - it returns -1 if the inventory is full. You should also be aware that addItem() returns a map (index -> item stack) if what it couldn't add. If you're just adding one item and it fails, the map will contain a single mapping: (1 -> item).
     
  4. Offline

    Fr0z3nCoders

    Code:
                    for(ItemStack item : player.getInventory().getContents()){
                        if(item != null){
                            p.sendMessage("Please clear your inventory first.");
                        }else{
                            //Do something
                        }
                    }
    ;)
     
  5. Offline

    desht

    And again, no. You've just checked if the inventory contains any item, not if it's full (not to mention that you're sending a separate message to the player for every single item stack in their inventory).

    player.getInventory().firstEmpty() is the correct way to do this.
     
  6. Offline

    joehot2000

    Do a for loop going through each inventory slot. Check if there is something in the inventory slot. If there isnt, move on, just keep doing that, and if you find no slots that are clear, you will know it is full
     
  7. Offline

    desht

    Or save yourself the trouble, and just use player.getInventory().getFirstEmpty(). Seriously.
     
  8. Offline

    joehot2000

    [​IMG]
     
  9. desht
    Yeah that would be the easy way to find an empty slot, but items stack too so it would be a shame not to use partial stacked slots.

    It would be more accurate to get the available slots, create a number then to loop through items and if it's null then add the max stack of the added type (itemToAdd.getType().getMaxStackSize()) to the slots, if it's not null and item.isSimilar(itemToAdd) then get the max stack of the item type and subtract item.getAmount() from it and Math.max(diff, 0) it (so it doesn't go negative due to other plugins) and add it to the slots.

    After the loop just check the available slots against your added item's amount and do stuff accordingly.

    Or, you could just do it how Essentials does it (slower but more reliable): https://github.com/essentials/Essen...ess3/craftbukkit/InventoryWorkaround.java#L38
     
  10. Offline

    LordManegane

    Hello, i try all the ways but i haven´t got a result.

    So, @Digi , @joehot2000 , @desht , @Fr0z3nCoders or @Wolftic Can anyone give me the code to do these alredy putted on my code?
    I don´t like to say that but.
    In need it so much.

    Thanks
     
  11. Offline

    TomTheDeveloper

    LordManegane Okey, Digi explained it how to do this, why are you asking someone to do this for you, this is a help forum, not a gift forum. And if you don't understand it very well, just ask for some more explanation (which is nearly impossible because Digi just gave you everything of what you should do). But if you just ask the code, that is a sign that there is a lack of motivation in you, coding needs a lot of motivation, you learn new things every day and if you don't have that motivation, you should quit programming. A lot of people on this forum are asking stuff, then you give them the solution, but they aren't happy, because they want that you make almost the whole plugin for them. It is not only you. Sometimes I see people who are asking cool stuff, but when they have the solution that needs a lot of code, the motivation dissappears, because they thought coding is easy and short, but that isn't. The answer is that you can't learn coding on one day, and that you need to learn every day, ....

    (This is not only targeted on you)
     
  12. Offline

    LordManegane

    TomTheDeveloper Thanks, the only thing i can say is just Thanks.
    I doesn´t going to quit programming because i really love this.

    I was desesperate.
    This doesn´t going to happend anymore.
     
  13. LordManegane
    Or you could search, I belive I've wrote that code in this forum somewhere before, but you're bound to find some kind of code that will get you started, be sure to post it here if you have problems or if you're unsure if it's correct.
     
  14. Offline

    LordManegane

    Well guys, thank´s for the help.
    I can done it.
    So many thanks.
     
  15. Offline

    Bernabeast

    All you bukkit coders are so full of crap it's unreal.
    Take a logic course first.
     
  16. Offline

    Doger

    Many many thanks guys!
    I had the same question, but its solved now.
     
  17. Offline

    milkymilkway

    I created this class named "InventoryWorkAround.class". I hope it helps! Only method that should be used is the canMerge.

    Code:
    import java.util.ArrayList;
    import java.util.List;
     
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
     
    public final class InventoryWorkAround {
     
    public static boolean canMerge(final Inventory inventory1, final Inventory inventory2) {
    // Check if all slots are full
    if ((inventory1.firstEmpty() < 0)) {
    // Check if inventory1 contains all the items inventory2 does
    if (!hasContents(inventory1, inventory2)) {
    return false;
    }
     
    // Check if an item from inventory2 + an item from inventory1 is
    // over maxStackSize
    for (ItemStack stack1 : getItems(inventory1)) {
    for (ItemStack stack2 : getItems(inventory2)) {
    if ((stack1.isSimilar(stack2)) && (stack1.getAmount() + stack2.getAmount() > stack1.getMaxStackSize())) {
    return false;
    }
    }
    }
    }
     
    // Check if there are enough slots to save time
    if (getEmptySlots(inventory1) < getFilledSlots(inventory2)) {
    // Check if an item from inventory2 + an item from inventory1 is
    // over maxStackSize
    for (ItemStack stack1 : getItems(inventory1)) {
    for (ItemStack stack2 : getItems(inventory2)) {
    if ((stack1.isSimilar(stack2)) && (stack1.getAmount() + stack2.getAmount() > stack1.getMaxStackSize())) {
    return false;
    }
    }
    }
    }
     
    return true;
    }
     
    public static int getEmptySlots(final Inventory inventory) {
    int amount = 0;
    for (ItemStack stack : inventory.getContents()) {
    if (stack == null) {
    amount++;
    }
    }
    return amount;
    }
     
    public static int getFilledSlots(final Inventory inventory) {
    int amount = 0;
    for (ItemStack stack : inventory.getContents()) {
    if (stack != null) {
    amount++;
    }
    }
    return amount;
    }
     
    public static boolean hasContents(final Inventory inventory1, final Inventory inventory2) {
    for (ItemStack stack : getItems(inventory2)) {
    if(!inventory1.contains(stack.getType())) {
    return false;
    }
    }
    return true;
    }
     
    public static List<ItemStack> getItems(final Inventory inventory) {
    List<ItemStack> contents = new ArrayList<ItemStack>();
    for (ItemStack stack : inventory.getContents()) {
    if (stack != null) {
    contents.add(stack);
    }
    }
     
    return contents;
    }
     
    }
     
  18. Offline

    PatoTheBest

    Code:
    Code:java
    1. public static void check(Player player) {
    2. boolean isEmpty = true;
    3. for (ItemStack item : player.getInventory().getContents()) {
    4. if(item != null) {
    5. isEmpty = false;
    6. break;
    7. }
    8. }
    9. if(isEmpty) {
    10. //code
    11. } else {
    12. sender.sendMessage("Please clear your inventory first.");
    13. }
    14. }
     
    Jezzerdo4 likes this.
Thread Status:
Not open for further replies.

Share This Page