Solved Converting ingots to blocks

Discussion in 'Spigot Plugin Development' started by BersamBas, Jul 26, 2018.

  1. Code (Java):
        public static void blocks(Player player) {
            Inventory block = Bukkit.getServer().createInventory(player, 9, ChatColor.GRAY + "Block" + ChatColor.DARK_GRAY + " Converter");
            int ironIngots = 0;
            for (int x=0;x<27;x++) {
            ItemStack item = player.getInventory().getItem(x);
            if (item != null) {
            if (item.getType() == Material.IRON_INGOT) {
                ironIngots = ironIngots + item.getAmount();
                }
            }
        }
            if (ironIngots > 8) {
                ItemStack iron = new ItemStack(Material.IRON_INGOT, ((int) ironIngots/9)*9);
                player.getInventory().removeItem(iron);
                int ironBlock = (int) ironIngots/9;
                Bukkit.broadcastMessage(String.valueOf(ironIngots));
                Bukkit.broadcastMessage(String.valueOf(ironBlock));
                if (ironBlock < 65) {
                    ItemStack ib = new ItemStack(Material.IRON_BLOCK, ironBlock);
                    block.setItem(4, ib);
                    }
                else if (ironBlock < 129) {
                    ItemStack ib = new ItemStack(Material.IRON_BLOCK, ironBlock - 64);
                    block.setItem(3, new ItemStack(Material.IRON_BLOCK, 64));      
                    block.setItem(4, ib);
                    }
                else if (ironBlock < 193) {
                    ItemStack ib = new ItemStack(Material.IRON_BLOCK, ironBlock - 128);
                    block.setItem(3, new ItemStack(Material.IRON_BLOCK, 64));      
                    block.setItem(4, new ItemStack(Material.IRON_BLOCK, 64));
                    block.setItem(5, ib);
                    }
                else if (ironBlock < 257) {
                    ItemStack ib = new ItemStack(Material.IRON_BLOCK, ironBlock - 192);
                    block.setItem(3, new ItemStack(Material.IRON_BLOCK, 64));      
                    block.setItem(4, new ItemStack(Material.IRON_BLOCK, 64));
                    block.setItem(5, new ItemStack(Material.IRON_BLOCK, 64));
                    block.setItem(6, ib);
                    }
                }
            player.openInventory(block);
            }
    I'm trying to convert Ingots into blocks and move them into an inventory but for some reason its only calculating blocks in the hot bar. Anyone know any possible solutions, please let me know.
     
  2. You are already missing the top row of the inventory here.
     
    • Agree Agree x 1
  3. An inventory is 36 slots not 27.
    Count the entire amount of iron ingots a player has, then use a while loop to remove 9 each time from the inventory and add one iron block.
     
    • Agree Agree x 1
  4. I feel stupid, thank you!