Search code examples
javaminecraftbukkit

Minecraft Fortune Blocks


I'm working on a plugin that when you break a block such as stone depending on what level fortune you have on your pickaxe depends on how much it will put in your inventory.

Plugin Details:

  • It automatically puts blocks and ores in your inventory

  • It automatically smelts ores and blocks

I'm not exactly sure how to make it when a player breaks a block such as stone it will put more then 1 block in their inventory. And what I'm trying to do exactly is if they have lets say fortune 6 on their pickaxe it will put maybe like 5 - 7 blocks in their inventory but if they have like fortune 30 it will put like 10 - 15 blocks in their inventory. I just tried messing around with options and seeing if I could figure it out but I can't figure it out. I don't have any experience with hashmaps either so I apologize if I'm doing them wrong as well.

Code:

@EventHandler
public void fortuneBlock(BlockBreakEvent e) {
    Player p = (Player) e.getPlayer();
    Block b = (Block) e.getBlock();

    ItemStack DP = new ItemStack(Material.DIAMOND_PICKAXE);

    if (p.getInventory().contains(DP)) {
        if (DP.containsEnchantment(Enchantment.LOOT_BONUS_BLOCKS)) {
            HashMap<Enchantment, Integer> pickaxe = p.getInventory().getItem(Material.DIAMOND_PICKAXE);
        }
    }
}

If you need any other information let me know.


Solution

  • getEnchantments()

    //Map containing all enchantments of the ItemStack
    //The Map looks like: [KEY,VALUE]
    //  [Enchantment.DURABILITY,2],
    //  [Enchantment.LOOT_BONUS_BLOCKS,4]
    HashMap<Enchantment, Integer> enchantmentMap = YourItemStack.getEnchantments();
    //Check if this map contains the wanted Enchantment
    if(enchantmentMap.containsKey(Enchantment.LOOT_BONUS_BLOCKS))
    //Get VALUE of KEY which is the enchantLevel "4"
    Integer enchantmentLevel = enchantmentMap.get(Enchantment.LOOT_BONUS_BLOCKS);
    

    You can imagine a Map like a table, every KEY has its own value. Wherefore the KEY is unique.