Search in sources :

Example 1 with PrepareItemEnchantEvent

use of org.bukkit.event.enchantment.PrepareItemEnchantEvent in project Glowstone by GlowstoneMC.

the class EnchantmentManager method calculateNewEnchantsAndLevels.

private void calculateNewEnchantsAndLevels() {
    random.setSeed(xpSeed);
    int realBookshelfs = inventory.getBookshelfCount();
    int countBookshelf = Math.min(15, realBookshelfs);
    for (int i = 0; i < enchLevelCosts.length; i++) {
        enchLevelCosts[i] = calculateLevelCost(i, countBookshelf);
        enchId[i] = -1;
        enchLevel[i] = -1;
    }
    ItemStack item = inventory.getItem();
    List<LeveledEnchant> enchants = null;
    for (int i = 0; i < enchLevelCosts.length; i++) {
        if (enchLevelCosts[i] == 0)
            continue;
        enchants = calculateCurrentEnchants(item, i, enchLevelCosts[i]);
        if (enchants != null && !enchants.isEmpty()) {
            LeveledEnchant chosen = WeightedRandom.getRandom(random, enchants);
            enchId[i] = chosen.getEnchantment().getId();
            enchLevel[i] = chosen.getEnchantmentLevel();
        }
    }
    EnchantmentOffer[] offers = null;
    if (enchants != null) {
        offers = new EnchantmentOffer[enchants.size()];
        enchants.toArray(offers);
    }
    PrepareItemEnchantEvent event = new PrepareItemEnchantEvent(player, player.getOpenInventory(), inventory.getLocation().getBlock(), item, offers, realBookshelfs);
    event.setCancelled(!canEnchant(item));
    EventFactory.callEvent(event);
    if (event.isCancelled()) {
        for (int i = 0; i < enchLevelCosts.length; i++) enchLevelCosts[i] = 0;
    }
    update();
}
Also used : EnchantmentOffer(org.bukkit.enchantments.EnchantmentOffer) PrepareItemEnchantEvent(org.bukkit.event.enchantment.PrepareItemEnchantEvent) ItemStack(org.bukkit.inventory.ItemStack)

Aggregations

EnchantmentOffer (org.bukkit.enchantments.EnchantmentOffer)1 PrepareItemEnchantEvent (org.bukkit.event.enchantment.PrepareItemEnchantEvent)1 ItemStack (org.bukkit.inventory.ItemStack)1