Apply int to item

Discussion in 'Spigot Plugin Development' started by Zarlen, May 2, 2017.

  1. Hello,
    I have a problem, and it's kinda hard to explain. So instead of the issue I'll just give an example of the function I want.

    Player has 5000 gold (int 5000 stored with it) - DONE
    Player opens inventory and gold nuggets appears in the amount of the int (in parts because of stack size) - DONE
    But now I want: When the int is something like 7500, but there's not enough space in the inventory (27 slots x 64 per slot = ? max).

    So I just want: When the inventory is full (27 x 64 = ?) That it starts to convert it to Gold bars
    Each Gold Bar is worth 100 coins

    So if I have for example 8000 gold.
    I would see 80 gold bars (80 x 100 = 8000).

    If I have the int 8010, I should see 80 Gold Bars and 10 Gold Coins (the remaining)!

    Code (Text):
    public void createGoldVault(Player player) {
    String dwarfgold = (String) this.getConfig().get("userdata." + player.getName() + ".goldvault");
    Inventory goldvault = Bukkit.createInventory(null, 27, ChatColor.DARK_BLUE + "Dwarven Vault " + ChatColor.DARK_GREEN + dwarfgold + ChatColor.GOLD + "G");
    ItemStack gold = new ItemStack(Material.GOLD_NUGGET, 1, (short) 0);
    ItemMeta goldmeta = gold.getItemMeta();
    goldmeta.setDisplayName(ChatColor.GOLD + "Gold Coin");
    goldmeta.setLore(Arrays.asList(ChatColor.YELLOW + "One Gold Coin is worth:", ChatColor.YELLOW  + "Just 1 Gold Coin."));
    int goldstored = Integer.valueOf((String) this.getConfig().get("userdata." + player.getName() + ".goldvault"));
    for (int i = goldstored; i > gold.getMaxStackSize(); i = i - gold.getMaxStackSize()) {
    gold.setAmount(goldstored % gold.getMaxStackSize());
  2. Are you aware of the % (modulo) operator and what it does?
  3. Yep.
  4. I guess you could do sth like this:
    Code (Text):
    if gold > 64 * 54 // Full inventory
      barCount = gold / 100
      gold -= barCount * 100
      ... Fill inventory
      ... Fill inventory with just the nuggets
  5. When I implent it in my method it fails because of the confusion of maths
  6. It was pseudocode, I hope you realize. What is your current code?
  7. I'd rather go with a long or BigInteger for your gold.
    • Agree Agree x 1
  8. Take out
    Code (Text):
    of the for loop or the player will just keep opening
  9. You can just show the amount in the item name or divide the coin amount just as @megamichiel suggested.
  10. If only I had over 2 billion gold pieces... I'm not sure with what quantities OP will be working, but I'd use a long at most for this use-case.
  11. Don't use the type int, use the type long. Long has a max value of 2^63 - 1 (Way bigger than 8 billion)
  12. Like I said, don't use too big numbers if you don't need them. It's also not 8 billion. Unsigned ints (not java) could reach about 4.3 billion and signed ints (java) only 2.1 billion.