Search in sources :

Example 1 with RewardItem

use of net.glowstone.entity.FishingRewardManager.RewardItem in project Glowstone by GlowstoneMC.

the class GlowFishingHook method getRewardItem.

private ItemStack getRewardItem() {
    RewardCategory rewardCategory = getRewardCategory();
    int level = getEnchantmentLevel(Enchantment.LUCK);
    if (rewardCategory == null || world.getServer().getFishingRewardManager().getCategoryItems(rewardCategory).isEmpty()) {
        return InventoryUtil.createEmptyStack();
    }
    double rewardCategoryChance = rewardCategory.getChance() + rewardCategory.getModifier() * level;
    double random;
    // 100%. It will rarely iterate more than once.
    do {
        random = ThreadLocalRandom.current().nextDouble(100);
        for (RewardItem rewardItem : world.getServer().getFishingRewardManager().getCategoryItems(rewardCategory)) {
            random -= rewardItem.getChance() * rewardCategoryChance / 100.0;
            if (random < 0) {
                ItemStack reward = rewardItem.getItem().clone();
                int enchantLevel = rewardItem.getMinEnchantmentLevel();
                int maxEnchantLevel = rewardItem.getMaxEnchantmentLevel();
                if (maxEnchantLevel > enchantLevel) {
                    enchantLevel = ThreadLocalRandom.current().nextInt(enchantLevel, maxEnchantLevel + 1);
                }
                if (enchantLevel > 0) {
                    enchant(reward, enchantLevel);
                }
                return reward;
            }
        }
    } while (random >= 0);
    return InventoryUtil.createEmptyStack();
}
Also used : RewardItem(net.glowstone.entity.FishingRewardManager.RewardItem) RewardCategory(net.glowstone.entity.FishingRewardManager.RewardCategory) ItemStack(org.bukkit.inventory.ItemStack)

Aggregations

RewardCategory (net.glowstone.entity.FishingRewardManager.RewardCategory)1 RewardItem (net.glowstone.entity.FishingRewardManager.RewardItem)1 ItemStack (org.bukkit.inventory.ItemStack)1