Soup Damager - Check if Inventory is empty

Discussion in 'Plugin Development' started by VNGC, May 15, 2020.

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

    VNGC

    Hey, i am trying to make my damager.

    i want that if a player joins the area, he gets an inv full of soups and recraft. if his inventory is empty, he should get a full inv again. i think i have to check if the players inv is empty, and if it is fill his inv. but it wont work. the player does get a new inv every second. doesnt matter his inv state.

    code:

    Code:
        public void Runnable() {
            Bukkit.getScheduler().runTaskTimer(plugin, new Runnable() {
                @Override
                public void run() {
                    for (Player player : Bukkit.getOnlinePlayers()) {
                        int maxX = 21;
                        int minX = 14;
                        int y = 101;
                        int maxZ = -8;
                        int minZ = -14;
    
                        int playerX = player.getLocation().getBlockX();
                        int playerY = player.getLocation().getBlockY();
                        int playerZ = player.getLocation().getBlockZ();
                        if (y == playerY) {
                            if (playerX <= maxX && playerX >= minX) {
                                if (playerZ <= maxZ && playerZ >= minZ) {
                                    for (ItemStack item : player.getInventory().getContents()) {
                                        if (item == null) {
                                            player.getInventory().setItem(13, new ItemStack(Material.RED_MUSHROOM, 64));
                                            player.getInventory().setItem(14, new ItemStack(Material.BROWN_MUSHROOM, 64));
                                            player.getInventory().setItem(15, new ItemStack(Material.BOWL, 64));
                                            for (int i = 0; i < 33; i++) {
                                                player.getInventory().addItem(new ItemStack(Material.MUSHROOM_STEW));
                                            }
                                        }
                                    }
                                    player.damage(2.5);
                                }
                            }
                        }
                    }
                }
            }, 0, 15L);
        }
     
  2. Offline

    KarimAKL

    @VNGC You're resetting the inventory if there's a single item missing, not if the whole inventory is missing.

    Add a boolean called 'empty' that defaults to 'true', then loop the inventory, if there's an item in it, set the boolean to 'false' and break the loop.

    Then you can just check that boolean and set the inventory if it's 'true'.
     
Thread Status:
Not open for further replies.

Share This Page