Check if the inv is full

Discussion in 'Plugin Development' started by q8minecraft, Dec 16, 2014.

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

    q8minecraft

    So I want to check if the inv is full and if it is full, I want to create a hologram that lasts up to 1second, Any help ?
    Code:
    package me.q8minecraft.fullinv;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import com.gmail.filoghost.holograms.api.HolographicDisplaysAPI;
    
    public class FullInv extends JavaPlugin implements Listener{
       
       
        String enabled = "Full inv has been enabled!";
       
        String disabled = "Full inv has been disabld!";
       
        String rights = ", Coded and devoloped by q8minecraft";
       
       
        public void onEnable() {
            Bukkit.getServer().getConsoleSender().sendMessage(enabled + rights);
        }
       
        public void onDisable() {
            Bukkit.getServer().getConsoleSender().sendMessage(disabled);
        }
       
       
        @EventHandler
        public void onBlockBreak(BlockBreakEvent e) {
            Player p = e.getPlayer();
            if (p.getInventory().firstEmpty() == -1) {
                org.bukkit.block.Block block = e.getBlock();
                HolographicDisplaysAPI.createHologram(this, block.getLocation(), ChatColor.RED + "INVENTORY IS FULL");
            }
           
        }
       
       
    
    }
    
     
  2. Offline

    teej107

    @q8minecraft What's the issue? Looks like you got it to me.

    EDIT: Apart from not registering your events.
     
  3. Offline

    q8minecraft

    @teej107 what I want to do is whenever I break a block and if my inv is full it creates a hologram that lasts for 1second, but when I tested it, it didnt work..
     
  4. Offline

    teej107

  5. Offline

    q8minecraft

    @teej107 wops, I thought I did that..
    How do I make it disappear in like 2 seconds ? Do I add a scheduler, or wait() ?
     
  6. Offline

    teej107

  7. Offline

    q8minecraft

    @teej107 thank you very much! I'm very sorry but I need one more thing, I kinda want to make it red and bold text but it wont let me for some reason.. ?
    Code:
    HolographicDisplaysAPI.createHologram(this, block.getLocation(), ChatColor.RED + ChatColor.BOLD +  "INVENTORY IS FULL");
     
  8. Offline

    extended_clip

    You can use my plugin InventoryFull. Just google
    InventoryFull by RyanM
     
  9. Offline

    q8minecraft

    @extended_clip I kinda want to make my own :p
    I need a bit of help here.. Everything is working fine but on every block break it keeps creating more and more holograms, how do I make it create only one hologram ? I crashed more than once with this..

    Nvm, I figured it out :D What I only need is to make it bold and red texts..

    Everything is working perfectly! Thank you guys so much for your help <3

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: Dec 20, 2014
  10. Offline

    leon3001

    Mind sharing your final (and working) code? It could be useful to others with the same problem. :)
     
    Last edited: Dec 17, 2014
    Rocoty likes this.
  11. Offline

    extended_clip

  12. Offline

    Webbeh

    I believe they weren't asking your code.
     
    leon3001 likes this.
  13. Offline

    extended_clip

    Do u even read bro?

     
  14. Offline

    Webbeh

    @extended_clip
    Not yours. At all. Did YOU even read who the guy quoted ?
     
  15. Offline

    q8minecraft

    Guys, Stop fighting and help each other instead..
    I need some help with the plugin, I need to check if my inv is completely full, like this, [​IMG]
    Not with some stone I can pick, Like this..
    [​IMG]
    I hope you got the point, like even if I can pick up a block from the same material, it wont show up until you have a completely FULL inventory and you cant even pick any kind blocks, even if its the same material.
     
  16. Offline

    mythbusterma

    @q8minecraft

    If you want to be certain their inventory is completely full, you can iterate over their inventory, checking the stack size of each of the stacks against the Material's stack size.
     
  17. Offline

    mrCookieSlime

    @q8minecraft @mythbusterma
    There is a way more easy way to do this. The method addItem() returns a Collection of Items which have been rejected.
    This way, we can simply create a copy of the Inventory and check whether adding an Item returns Items that have been rejected.

    PHP:
    public static boolean fits(Inventory invItemStack item) {
            
    // Now create a copy of the Inventory since we don't want to add the Item to the actual Inventory. But if you do want that. You can simply leave this out and just use inv instead of inv2. This is just here incase you really want to only check for this.
            
    Inventory inv2 Bukkit.createInventory(nullinv.getSize());
            
    // But since this new Inventory is still empty, we need to copy all Items from the old Inventory to the new one.
            
    for (int i 0inv.getContents().lengthi++) {
                
    inv2.setItem(iinv.getContents()[i]);
            }
            
    // And then return whether Items got rejected or not.
            
    return inv2.addItem(item).isEmpty();
        }
     
  18. Offline

    Webbeh

    Wow, thanks, I actually did an iterate on the players' itemstack and found that solution too buggy. Updating to that one !
     
  19. Offline

    q8minecraft

  20. Offline

    mrCookieSlime

    @q8minecraft
    You could do that.
    But you could also just copy that method and call it inside your Event.
     
  21. Offline

    q8minecraft

    @mrCookieSlime I'm kinda a beginner with Java. still learning, How do I call the method from the Event ?
     
  22. Offline

    mrCookieSlime

    @q8minecraft
    Just write fits(inventory, item) and it will return a boolean you can use.
     
  23. Offline

    q8minecraft

    I'm getting some errors on fit(inv, item)

    Code:
    package me.q8minecraft.FullInvTest;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class FullInvTest extends JavaPlugin implements Listener {
    
       
        String enabled = "FullInvTest";
        String rights = "Coded by q8minecraf";
       
       
       
        public void onEnable() {
            Bukkit.getServer().getConsoleSender().sendMessage( enabled + rights);
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
       
       
        public static boolean fits(Inventory inv, ItemStack item) {
            Inventory inv2 = Bukkit.createInventory(null, inv.getSize());
            for (int i = 0; i < inv.getContents().length; i++) {
                inv2.setItem(i, inv.getContents()[i]);
            }
            return inv2.addItem(item).isEmpty();}
       
        @EventHandler
        public void onBlockBreak(BlockBreakEvent e) {
            Player p = e.getPlayer();
            fits(inventory, item);
            p.sendMessage("inv is full");
       
            return fits(inv, item);
        }
       
    }
    
     
  24. Offline

    mrCookieSlime

    @q8minecraft
    Events do not return a boolean...
    Remove the return on your Event.
    You also never declared the Variables inventory and item, you want to replace those with the Players Inventory and the dropped Item...

    And as I said fits returns a boolean, so an if-statement would be more appropriate here.
     
  25. Offline

    pookeythekid

    @q8minecraft Firstly, a void doesn't return anything, hence the name "void." So remove your return statement in your event. Next, you have to use your fits method in an if statement if you want to make it useful. Ninja'd.
    Code:
    if (!fits(p.getInventory(), new ItemStack(e.getBlock().getDrops())) {
        p.sendMessage("inv is full.");
    }
     
  26. Offline

    q8minecraft

    @mrCookieSlime @pookeythekid According to pookythekid the code should be like this.. but for some reason, its not working..
    Code:
    public static boolean fits(Inventory inv, ItemStack item) {
            Inventory inv2 = Bukkit.createInventory(null, inv.getSize());
            for (int i = 0; i < inv.getContents().length; i++) {
                inv2.setItem(i, inv.getContents()[i]);
            }
            return inv2.addItem(item).isEmpty();}
      
        @EventHandler
        public void onBlockBreak(BlockBreakEvent e) {
            Player p = e.getPlayer();
            if(!fits(p.getInventory(), new ItemStack((ItemStack) e.getBlock().getDrops()))) {
               p.sendMessage("inv is full.");
      
        }
      
    }
    }
    
    
     
  27. Offline

    JordyPwner

    @q8minecraft

    Try this:
     
  28. Offline

    q8minecraft

    @JordyPwner Do I just replace my boolean with yours ? If so, how I'm gonna call it, I'm getting errors when I do. And what do I write inside the if(isFull) { and
    }else{
     
  29. Offline

    JordyPwner

    this is basic java. So i suggest you to learn more java.
    Also DONT USE STATIC
     
  30. Offline

    leon3001

    Did you read the replies above? That would return true when he could still pickup stone (assuming this case:
    )
     
Thread Status:
Not open for further replies.

Share This Page