Inventory Full Warning

Discussion in 'Plugin Development' started by BrayzPlayz, Jan 4, 2015.

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

    BrayzPlayz

    I have created a super pickaxe plugin for a Prison server, but I want to make it so when they are mining, if their inventory is full it will send them a message saying ("Your Inventory is Full! Please Empty it") Or something like that!
     
  2. Offline

    nverdier

    @BrayzPlayz Check when the player breaks a block, iterate through the contents of their inventory, check if each slot is full. If so, send the message.
     
  3. Offline

    nbrandwine

    Code:
        public void onPickup(PlayerPickupItemEvent e)
        {
            Player p = (Player) e.getPlayer();
            Inventory pi = p.getInventory();
            if(pi.firstEmpty() == -1)
            {
                p.sendMessage(ChatColor.RED + "Your inventory is full! Please empty it!");
            }
        }
    Not tested. caveat emptor
     
  4. Offline

    nj2miami

    int org.bukkit.inventory.Inventory.firstEmpty

    Returns The first empty Slot found, or -1 if no empty slots.

    @nbrandwine - should work fine, however my question would be does the PlayerPickupItemEvent trigger or is it cancelled before hand?
     
    mine-care likes this.
  5. Offline

    nbrandwine

    Forgot to say, if you want to stop the pickup event (which wont happen anyway because if pi returns -1 it means inv is full hence not picking up item) you can always use "e.setCanceled(true);".
     
  6. Offline

    extended_clip

    What if the drop or item broken can stack on an existing stack though?

    Code:
    for (ItemStack drop : b.getDrops(p.getItemInHand())) {
    
            for (ItemStack is : i.getContents()) {
                   
                if (is == null) {
                    //empty slot
                    return;
                }
                   
                if (is.getType().equals(drop.getType()) && is.getAmount()+drop.getAmount() <= is.getMaxStackSize()) {
                    //will stack on existing itemstack
                    return;
                }
            }
     }
     
  7. Offline

    ChipDev

    Don't spoon feed someone.
    Especially if they don't already have code, That was more like a plugin request.
     
  8. Offline

    nbrandwine

    My bad, I just thought said person would attempt to understand what I did. I had always learned through example.

    IIRC the method I had will still return what the inventory space is.

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: Jan 5, 2015
    ChipDev likes this.
  9. Offline

    ChipDev

    Ok, Yeah, I understand that. :)
     
  10. Offline

    leon3001

    Trying to recreate mrCookieSlime's method (all credits to him):
    Code:
    public static boolean fits(Inventory inv, ItemStack is){
    Inventory tmpInv = Bukkit.createInventory(null, inv.getSize()); //create temporary inventory of the same size as the tested inventory
    
    for(i=0, i<inv.getSize(), i++){
    tmpInv.addItem(inv.getItem);
    }
    
    return tmpInv.addItem(is).isEmpty(); //read the docs if you don't understand ;P
    }
    Warning: The code above untested and written without an IDE. You might have to insert null checks or something.
     
Thread Status:
Not open for further replies.

Share This Page