Hey, you there! I have a simple (noob) question. I'm creating a inventory and I want to fill the inventory to a specific amount of ItemStack. So for example: int is 5000 Then I want to fill the inventory like this: 64 64 64 64 64 64 64 64 And goes on until it's filled with 5000 items (max stacked) Anyone knows how to do this?
If you're creating an inanimate object inventory, like Bukkit#createInventory(); Then use an for loop, something like this Code (Text): for(int i = 0; i >= 5000; i++) { inventory.addItem(Material) If this wasn't what you we're wanting, please elaborate on you're question.
Doesnt work. I'm using: Code (Text): public void testInventory(Player player) { String test = (String) this.getConfig().get("userdata." + player.getName() + ".test"); Inventory testinv = Bukkit.createInventory(null, 27, ChatColor.DARK_BLUE + "Test" + test); player.openInventory(testinv); } (im using a method because I have to get the playername)
Code (Java): ItemStack key = <your item>; int amount = 5000; Inventory inv = <your GUI>; // first add and give all completely filled stacks key.setAmount(key.getMaxStackSize()); for (int i = amount; i > key.getMaxStackSize(); i = i - key.getMaxStackSize()) { inv.addItem(key.clone()); } // then add the rest not-completely filled stack key.setAmount(amount % key.getMaxStackSize()); inv.addItem(key.clone()); // untested... You didn't understand what i showed you here, don't you? EDIT: fixed typo
Too many comments? Delete the comments, maybe it looks nicer then? Code (Java): public void giveItems(Inventory inv, ItemStack key, int amount) { key.setAmount(key.getMaxStackSize()); for (int i = amount; i > key.getMaxStackSize(); i =- key.getMaxStackSize()) inv.addItem(key.clone()); key.setAmount(amount % key.getMaxStackSize()); inv.addItem(key.clone()); } Better?
I already figured it out before you posted this one (and thanks so much for your effort <3) It works, but when the inventory is full I want to switch to goldbars and one goldbar stands for 500 gold and then fill the others will gold coins again: Example: int is 2300 4 gold bars 300 gold nuggets
This may help you to solve the max stack size of items and to check if the inventory is full just use the returned map from addItems here . When the Map isn't empty, then it contains every item the player could hold in his inventory.
Based on my #giveItems above: Code (Java): public void giveStuff(Inventory inv, int amount) { int bars = ((Long) Math.round(Math.floor(amount / 500.0d))).intValue(); // that seems quite confusing, but i don't trust compilers, so i convert by myself int nuggets = amount % 500; this.giveItems(inv, new ItemStack(Material.GOLD_INGOT, 1), bars); this.giveItems(inv, new ItemStack(Material.GOLD_NUGGET, 1), nuggets); // untested... } But now i ran out of spoons.
I'm just a bit confused where to put where because his method is just a function method. My method is to open an inventory. I want it like (for those dont understand me): Each gold nugget is worth (int) 1. So when I have the int 4000, you will have 4000 gold coins (all in parts because of stacksize). But I want it when the inventory (27 slots x 64 = ? max int) is full that it starts like this: Each gold bar is worth (int) 100. So when I have the int 7000 you will have gold bars until the int gets under 100 and then add the coins from the int..
It's wrong to start a second thread about the same problem. New day, new spoon: Code (Java): public class MyMainThingy extends JavaPlugin { @Override public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { if (sender instanceof Player) ((Player) sender).openInventory(this.createGoldVault((Player) sender)); return true; } private static ItemStack getGoldIngot(String name, String[] lore) { ItemStack gold = new ItemStack(Material.GOLD_NUGGET, 1); ItemMeta goldmeta = gold.getItemMeta(); goldmeta.setDisplayName(name); goldmeta.setLore(Arrays.asList(lore)); gold.setItemMeta(goldmeta); return gold; } private static ItemStack getGoldNugget(String name, String[] lore) { ItemStack gold = new ItemStack(Material.GOLD_NUGGET, 1); ItemMeta goldmeta = gold.getItemMeta(); goldmeta.setDisplayName(name); goldmeta.setLore(Arrays.asList(lore)); gold.setItemMeta(goldmeta); return gold; } private static Inventory getGoldInv(String title, int size) { return Bukkit.createInventory(null, size, title); } private void giveItems(Inventory inv, ItemStack key, int amount) { key.setAmount(key.getMaxStackSize()); for (int i = amount; i > key.getMaxStackSize(); i =- key.getMaxStackSize()) inv.addItem(key.clone()); key.setAmount(amount % key.getMaxStackSize()); inv.addItem(key.clone()); } private void fillVaultGui(Inventory inv, int amount, int ingotWorth) { int bars = ((Long) Math.round(Math.floor(amount / ((Integer) ingotWorth).doubleValue()))).intValue(); // that seems quite confusing, but i don't trust compilers, so i convert by myself int nuggets = amount % ingotWorth; this.giveItems(inv, MyMainThingy.getGoldIngot(ChatColor.GOLD + "Gold ingot", new String[] { ChatColor.YELLOW + "One Gold ingot is worth:", ChatColor.YELLOW + Integer.toString(ingotWorth) + " gold coins." }), bars); this.giveItems(inv, MyMainThingy.getGoldNugget(ChatColor.GOLD + "Gold nugget", new String[] { ChatColor.YELLOW + "One Gold nugget is worth:", ChatColor.YELLOW + "1 gold coin." }), nuggets); } public Inventory createGoldVault(Player player) { int balance = this.getConfig().getInt("userdata." + player.getName() + ".goldvault"); Inventory gui = MyMainThingy.getGoldInv(ChatColor.DARK_BLUE + "Dwarven Vault " + ChatColor.DARK_GREEN + Integer.toString(balance) + ChatColor.GOLD + " coins", 27); this.fillVaultGui(gui, balance, 500); return gui; } } Untested. Good luck. Try to understand it before going on with something else. If you don't understand, ask.