Solved Check if there is enough space in inventory

Discussion in 'Plugin Development' started by gidonyou, Feb 13, 2014.

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

    gidonyou

    I currently used Barinade 's code. However, i have empty space, but giving me the error message i don't..

    This is what i Got
    Code:java
    1. public static void inventoryEmpty(Player player, Material item, int num) {
    2. ItemStack itemToAdd = new ItemStack(item, num);
    3. int freeSpace = 0;
    4. for (ItemStack i : player.getInventory()) {
    5. if (i == null) {
    6. freeSpace += itemToAdd.getType().getMaxStackSize();
    7. } else if (i.getType() == itemToAdd.getType()) {
    8. freeSpace += i.getType().getMaxStackSize() - i.getAmount();
    9. }
    10. }
    11. if (itemToAdd.getAmount() >= freeSpace) {
    12. // add items
    13. player.getInventory().addItem(new ItemStack(item, num));
    14. } else {
    15. // not enough space, tell the player and abort mission
    16. player.sendMessage(Lang.TITLE.toString() + Lang.noSpaceinv);
    17. }
    18. }


    Code:java
    1. if(clicked.getType() == Material.LOG){
    2. int pri = 50;
    3. if(pri <= currentPoint){
    4. Points.removePoint(player, pri);
    5. emptySpaceChecker.inventoryEmpty(player, Material.LOG, 64);
    6. }else{
    7. player.sendMessage(Lang.TITLE.toString() + Lang.noPoint);
    8. }
    9. }


    My inventory is almost empty, but it still gives me the Error
    Code:java
    1. player.sendMessage(Lang.TITLE.toString() + Lang.noSpaceinv);


    And I found the bug that it remove the point first.. So is there any way that i can return the method boolean?
     
  2. Offline

    xTigerRebornx

  3. Offline

    leimekiller

    Code:java
    1. if( player.getInventory.firstEmpty() != -1)
    2. {
    3. //inv is not full
    4. }
    5. else
    6. {
    7. //inv is full
    8. }
     
  4. Offline

    WarmakerT

    Try this.
    Code:
        @EventHandler
        public void onBlockBreak(BlockBreakEvent event) {
            event.getBlock().getDrops().clear();
            for(ItemStack is : event.getBlock().getDrops()) {
                HashMap<Integer, ItemStack> rest = event.getPlayer().getInventory().addItem(is);
                if(!rest.isEmpty()) {
                    for(int i = 0; i <= rest.size(); i++) {
                        event.getBlock().getDrops().add(rest.get(i));
                    }
                }
            }
        }
    This was actually for another thread, but the basis is the following: event.getPLayer().getInventory().addItem(is) will try to add the ItemStack wherever it can on the inventory, finding every single space available, and will return what it doesn't find space to add.
     
    Last edited: Dec 28, 2014
Thread Status:
Not open for further replies.

Share This Page