Block to Inventory & Inventory Full Check

Discussion in 'Plugin Development' started by MaxNatural, Mar 28, 2015.

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

    MaxNatural

    This is really annoying. When my inventory is full of stone and I mine a block it goes to my inventory if my inventory full of the same block but not all in stacks it drops the item and not go into the inventory.

    Code:
    package me.max.prison;
    
    import me.max.MainCore;
    
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.inventory.ItemStack;
    
    public class FullInventory implements Listener {
        
        @EventHandler
        public void BlockBreak(BlockBreakEvent e) {
          
            Player p = e.getPlayer();
          
            if(e.getPlayer().getInventory().firstEmpty() == -1){
                ItemStack add = (ItemStack) e.getBlock();
              
                try{
                    for (ItemStack item : p.getInventory().getContents()) {
                        if(item.getType() == add.getType() && item.getAmount() != 64) {
                            int space = 64 - item.getAmount();
                            if(add.getAmount() > space){
                                add.setAmount(add.getAmount() - space);
                                item.setAmount(64);
                                }
                        }else{
                            item.setAmount(item.getAmount() + add.getAmount());
                            e.setCancelled(true);
                            e.getBlock().setType(Material.AIR);
                            for(ItemStack items : e.getBlock().getDrops()) {
                                e.getPlayer().getInventory().addItem(items);
                            return;
                        }
                    }
                }
                    }catch (Exception e2) {
                        p.sendMessage(MainCore.TAG + "Your inventory is full.");
                }
            }
        }
    
               
               
                @EventHandler(priority=EventPriority.MONITOR)
                public void onBreak(BlockBreakEvent e) {
                    if (e.getPlayer() == null) {
                        return;
                    }
                   
                    boolean allowed = true;
                   
                    if((!allowed) || (e.getPlayer().getInventory().firstEmpty() == -1)) {
                        return;
                    }
                    for (ItemStack i : e.getBlock().getDrops()) {
                        e.getPlayer().getInventory().addItem(new ItemStack[] { i });
                    }
                    e.setCancelled(true);
                    e.getBlock().setType(Material.AIR);
    
                    }
            }
       
           
     
  2. Offline

    mine-care

    Your signature is wrong, check mine to see the reason :p
    Why are you catching a non thrown exception?
    Lastly just to highlight the structure:
    Add items to inv
    If inv is full, check if it contains an item stack with amount less than 64 and if so add it to that stack else means it cannot fit it in and throw it down.:p
     
Thread Status:
Not open for further replies.

Share This Page