diff --git a/paper/src/main/java/com/badbones69/crazyenchantments/paper/api/CrazyManager.java b/paper/src/main/java/com/badbones69/crazyenchantments/paper/api/CrazyManager.java index 37b5debf..dfc269b9 100644 --- a/paper/src/main/java/com/badbones69/crazyenchantments/paper/api/CrazyManager.java +++ b/paper/src/main/java/com/badbones69/crazyenchantments/paper/api/CrazyManager.java @@ -804,6 +804,22 @@ public boolean canAddEnchantment(@NotNull Player player, @NotNull ItemStack item Math.min(getPlayerBaseEnchantments(player) - getEnchantmentLimiter(item), getPlayerMaxEnchantments(player)); } + /** + * Checks if the player can add more enchants to the current item based on set limits without the enchant limiter. + * @param player The {@link Player} that has the item. + * @param cEnchantments The amount of crazy enchants on the item. + * @param vanillaEnchantments The amount of vanilla enchantments on the item. + * @return True if they are able to add more enchants. + */ + public boolean canAddEnchantment(@NotNull Player player, int cEnchantments, int vanillaEnchantments) { + if (!this.maxEnchantmentCheck || player.hasPermission("crazyenchantments.bypass.limit")) return true; + + int enchantAmount = cEnchantments; + if (this.checkVanillaLimit) enchantAmount += vanillaEnchantments; + + return enchantAmount < getPlayerMaxEnchantments(player); + } + public int randomLevel(CEnchantment enchantment, Category category) { int enchantmentMax = enchantment.getMaxLevel(); // Max set by the enchantment. int randomLevel = 1 + new Random().nextInt(enchantmentMax); diff --git a/paper/src/main/java/com/badbones69/crazyenchantments/paper/api/objects/BlackSmithResult.java b/paper/src/main/java/com/badbones69/crazyenchantments/paper/api/objects/BlackSmithResult.java index 52c223ab..60fd6215 100644 --- a/paper/src/main/java/com/badbones69/crazyenchantments/paper/api/objects/BlackSmithResult.java +++ b/paper/src/main/java/com/badbones69/crazyenchantments/paper/api/objects/BlackSmithResult.java @@ -55,10 +55,10 @@ public BlackSmithResult(Player player, ItemStack mainItem, ItemStack subItem) { if (enchantment.canEnchantItem(subItem) && subCE.hasVanillaEnchantment(enchantment)) { if (level == subLevel && level < enchantment.getMaxLevel()) { - mainCE.setVanillaEnchantment(enchantment, level + 1); + mainCE.addVanillaEnchantment(enchantment, level + 1); this.cost += BlackSmithManager.getLevelUp(); } else if (level < subLevel) { - mainCE.setVanillaEnchantment(enchantment, subLevel); + mainCE.addVanillaEnchantment(enchantment, subLevel); this.cost += BlackSmithManager.getLevelUp(); } } @@ -72,10 +72,10 @@ public BlackSmithResult(Player player, ItemStack mainItem, ItemStack subItem) { if (enchantment.canEnchantItem(subItem) && subCE.hasCEnchantment(enchantment)) { if (level == subLevel && level < enchantment.getMaxLevel()) { - mainCE.setCEnchantment(enchantment, level + 1); + mainCE.addCEnchantment(enchantment, level + 1); this.cost += BlackSmithManager.getLevelUp(); } else if (level < subLevel) { - mainCE.setCEnchantment(enchantment, subLevel); + mainCE.addCEnchantment(enchantment, subLevel); this.cost += BlackSmithManager.getLevelUp(); } } @@ -85,8 +85,8 @@ public BlackSmithResult(Player player, ItemStack mainItem, ItemStack subItem) { for (Entry entry : compare.getNewVanillaEnchantments().entrySet()) { Enchantment enchantment = entry.getKey(); - if (enchantment.canEnchantItem(subItem) && crazyManager.canAddEnchantment(player, mainItem)) { - mainCE.setVanillaEnchantment(enchantment, entry.getValue()); + if (enchantment.canEnchantItem(subItem) && mainCE.canAddEnchantment(player)) { + mainCE.addVanillaEnchantment(enchantment, entry.getValue()); this.cost += BlackSmithManager.getAddEnchantment(); } } @@ -94,8 +94,8 @@ public BlackSmithResult(Player player, ItemStack mainItem, ItemStack subItem) { for (Entry entry : compare.getNewCEnchantments().entrySet()) { CEnchantment enchantment = entry.getKey(); - if (enchantment.canEnchantItem(subItem) && crazyManager.canAddEnchantment(player, mainItem) && crazyManager.canAddEnchantment(player, subItem)) { - mainCE.setCEnchantment(enchantment, entry.getValue()); + if (enchantment.canEnchantItem(mainItem) && mainCE.canAddEnchantment(player)) { + mainCE.addCEnchantment(enchantment, entry.getValue()); this.cost += BlackSmithManager.getAddEnchantment(); } } diff --git a/paper/src/main/java/com/badbones69/crazyenchantments/paper/api/objects/CEItem.java b/paper/src/main/java/com/badbones69/crazyenchantments/paper/api/objects/CEItem.java index ba9a7e71..2c5ad626 100644 --- a/paper/src/main/java/com/badbones69/crazyenchantments/paper/api/objects/CEItem.java +++ b/paper/src/main/java/com/badbones69/crazyenchantments/paper/api/objects/CEItem.java @@ -5,6 +5,7 @@ import com.badbones69.crazyenchantments.paper.api.CrazyManager; import com.badbones69.crazyenchantments.paper.controllers.settings.EnchantmentBookSettings; import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; @@ -59,7 +60,7 @@ public Map getVanillaEnchantments() { return this.vanillaEnchantments; } - public void setVanillaEnchantment(Enchantment enchantment, int level) { + public void addVanillaEnchantment(Enchantment enchantment, int level) { this.vanillaEnchantments.put(enchantment, level); } @@ -79,14 +80,18 @@ public Map getCEnchantments() { return this.cEnchantments; } - public void setCEnchantment(CEnchantment enchantment, int level) { + public void addCEnchantment(CEnchantment enchantment, int level) { this.cEnchantments.put(enchantment, level); } public void removeCEnchantment(CEnchantment enchantment) { this.cEnchantmentRemove.add(enchantment); } - + + public boolean canAddEnchantment(Player player) { + return crazyManager.canAddEnchantment(player, this.cEnchantments.size(), this.vanillaEnchantments.size()); + } + public ItemStack build() { this.vanillaEnchantmentRemove.forEach(this.item::removeEnchantment); this.vanillaEnchantments.keySet().forEach(enchantment -> this.item.addUnsafeEnchantment(enchantment, this.vanillaEnchantments.get(enchantment)));