From 336236240cdf709724519d0026ded20384697194 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Wed, 26 Jan 2022 19:58:07 +0000 Subject: [PATCH] Fixed loot populator --- .../support/obtaining/LootPopulator.java | 204 +++++++++--------- 1 file changed, 104 insertions(+), 100 deletions(-) diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/obtaining/LootPopulator.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/obtaining/LootPopulator.java index 55840b20..5eef073b 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/obtaining/LootPopulator.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/obtaining/LootPopulator.java @@ -14,7 +14,6 @@ import org.bukkit.block.BlockState; import org.bukkit.block.Chest; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Entity; -import org.bukkit.entity.EntityType; import org.bukkit.entity.minecart.StorageMinecart; import org.bukkit.generator.BlockPopulator; import org.bukkit.inventory.Inventory; @@ -22,6 +21,7 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.EnchantmentStorageMeta; import org.bukkit.inventory.meta.ItemMeta; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.Collections; @@ -60,7 +60,7 @@ public class LootPopulator extends BlockPopulator { return; } - for (Entity entity: chunk.getEntities()) { + for (Entity entity : chunk.getEntities()) { if (!(entity instanceof StorageMinecart minecart)) { continue; } @@ -77,117 +77,121 @@ public class LootPopulator extends BlockPopulator { } } - /** - * Modify given inventory with EcoEnchants enchantments - * - * @param inventory The target inventory - */ - public void modifyInventory(Inventory inventory) { - for (ItemStack item : inventory) { - if (item == null) { - continue; - } - if (!EnchantmentTarget.ALL.getMaterials().contains(item.getType())) { - continue; - } - if (item.getType().equals(Material.BOOK)) { + private void modifyItem(@Nullable final ItemStack item) { + if (item == null) { + return; + } + if (!EnchantmentTarget.ALL.getMaterials().contains(item.getType())) { + return; + } + if (item.getType().equals(Material.BOOK)) { + return; + } + + Map toAdd = new HashMap<>(); + + ArrayList enchantments = new ArrayList<>(EcoEnchants.values()); + Collections.shuffle(enchantments); // Prevent list bias towards early enchantments like telekinesis + + double multiplier = 0.01; + if (item.getType().equals(Material.BOOK) || item.getType().equals(Material.ENCHANTED_BOOK)) { + multiplier /= plugin.getConfigYml().getInt("loot.book-times-less-likely"); + } + + if (plugin.getConfigYml().getBool("loot.reduce-probability.enabled")) { + multiplier /= plugin.getConfigYml().getDouble("loot.reduce-probability.factor"); + } + + int cap = 0; + + for (EcoEnchant enchantment : enchantments) { + if (enchantment == null || enchantment.getEnchantmentRarity() == null) { continue; } - Map toAdd = new HashMap<>(); - - ArrayList enchantments = new ArrayList<>(EcoEnchants.values()); - Collections.shuffle(enchantments); // Prevent list bias towards early enchantments like telekinesis - - double multiplier = 0.01; - if (item.getType().equals(Material.BOOK) || item.getType().equals(Material.ENCHANTED_BOOK)) { - multiplier /= plugin.getConfigYml().getInt("loot.book-times-less-likely"); + if (NumberUtils.randFloat(0, 1) > enchantment.getEnchantmentRarity().getLootProbability() * multiplier) { + continue; } + if (!enchantment.isAvailableFromLoot()) { + continue; + } + + if (!enchantment.canEnchantItem(item)) { + continue; + } + + if (!enchantment.isEnabled()) { + continue; + } + + AtomicBoolean anyConflicts = new AtomicBoolean(false); + toAdd.forEach((enchant, integer) -> { + if (enchantment.conflictsWithAny(toAdd.keySet())) { + anyConflicts.set(true); + } + if (enchant.conflictsWith(enchantment)) { + anyConflicts.set(true); + } + + EcoEnchant ecoEnchant = (EcoEnchant) enchant; + if (enchantment.getType().equals(ecoEnchant.getType()) && ecoEnchant.getType().isSingular()) { + anyConflicts.set(true); + } + }); + if (anyConflicts.get()) { + continue; + } + + int level; + + if (enchantment.getType().equals(EnchantmentType.SPECIAL)) { + double enchantlevel1 = NumberUtils.randFloat(0, 1); + double enchantlevel2 = NumberUtils.bias(enchantlevel1, plugin.getConfigYml().getDouble("enchanting-table.special-bias")); + double enchantlevel3 = 1 / (double) enchantment.getMaxLevel(); + level = (int) Math.ceil(enchantlevel2 / enchantlevel3); + } else { + double enchantlevel2 = NumberUtils.triangularDistribution(0, 1, 1); + double enchantlevel3 = 1 / (double) enchantment.getMaxLevel(); + level = (int) Math.ceil(enchantlevel2 / enchantlevel3); + } + + toAdd.put(enchantment, level); + if (plugin.getConfigYml().getBool("loot.reduce-probability.enabled")) { multiplier /= plugin.getConfigYml().getDouble("loot.reduce-probability.factor"); } - int cap = 0; - - for (EcoEnchant enchantment : enchantments) { - if (enchantment == null || enchantment.getEnchantmentRarity() == null) { - continue; - } - - if (NumberUtils.randFloat(0, 1) > enchantment.getEnchantmentRarity().getLootProbability() * multiplier) { - continue; - } - - if (!enchantment.isAvailableFromLoot()) { - continue; - } - - if (!enchantment.canEnchantItem(item)) { - continue; - } - - if (!enchantment.isEnabled()) { - continue; - } - - AtomicBoolean anyConflicts = new AtomicBoolean(false); - toAdd.forEach((enchant, integer) -> { - if (enchantment.conflictsWithAny(toAdd.keySet())) { - anyConflicts.set(true); - } - if (enchant.conflictsWith(enchantment)) { - anyConflicts.set(true); - } - - EcoEnchant ecoEnchant = (EcoEnchant) enchant; - if (enchantment.getType().equals(ecoEnchant.getType()) && ecoEnchant.getType().isSingular()) { - anyConflicts.set(true); - } - }); - if (anyConflicts.get()) { - continue; - } - - int level; - - if (enchantment.getType().equals(EnchantmentType.SPECIAL)) { - double enchantlevel1 = NumberUtils.randFloat(0, 1); - double enchantlevel2 = NumberUtils.bias(enchantlevel1, plugin.getConfigYml().getDouble("enchanting-table.special-bias")); - double enchantlevel3 = 1 / (double) enchantment.getMaxLevel(); - level = (int) Math.ceil(enchantlevel2 / enchantlevel3); - } else { - double enchantlevel2 = NumberUtils.triangularDistribution(0, 1, 1); - double enchantlevel3 = 1 / (double) enchantment.getMaxLevel(); - level = (int) Math.ceil(enchantlevel2 / enchantlevel3); - } - - toAdd.put(enchantment, level); - - if (plugin.getConfigYml().getBool("loot.reduce-probability.enabled")) { - multiplier /= plugin.getConfigYml().getDouble("loot.reduce-probability.factor"); - } - - if (!enchantment.hasFlag("hard-cap-ignore")) { - cap++; - } - - if (plugin.getConfigYml().getBool("anvil.hard-cap.enabled")) { - if (cap >= plugin.getConfigYml().getInt("anvil.hard-cap.cap")) { - break; - } - } + if (!enchantment.hasFlag("hard-cap-ignore")) { + cap++; } - if (item.getItemMeta() instanceof EnchantmentStorageMeta meta) { - toAdd.forEach(((enchantment, integer) -> meta.addStoredEnchant(enchantment, integer, false))); - item.setItemMeta(meta); - } else { - ItemMeta meta = item.getItemMeta(); - toAdd.forEach(((enchantment, integer) -> meta.addEnchant(enchantment, integer, false))); - item.setItemMeta(meta); + if (plugin.getConfigYml().getBool("anvil.hard-cap.enabled")) { + if (cap >= plugin.getConfigYml().getInt("anvil.hard-cap.cap")) { + break; + } } } + + if (item.getItemMeta() instanceof EnchantmentStorageMeta meta) { + toAdd.forEach(((enchantment, integer) -> meta.addStoredEnchant(enchantment, integer, false))); + item.setItemMeta(meta); + } else { + ItemMeta meta = item.getItemMeta(); + toAdd.forEach(((enchantment, integer) -> meta.addEnchant(enchantment, integer, false))); + item.setItemMeta(meta); + } + } + + /** + * Modify given inventory with EcoEnchants enchantments + * + * @param inventory The target inventory + */ + public void modifyInventory(@NotNull final Inventory inventory) { + for (ItemStack item : inventory) { + this.plugin.getScheduler().runLater(1, () -> modifyItem(item)); + } } }