diff --git a/src/main/java/com/songoda/epicenchants/listeners/item/BookListener.java b/src/main/java/com/songoda/epicenchants/listeners/item/BookListener.java index cee9b9c..a782655 100644 --- a/src/main/java/com/songoda/epicenchants/listeners/item/BookListener.java +++ b/src/main/java/com/songoda/epicenchants/listeners/item/BookListener.java @@ -9,6 +9,7 @@ import com.songoda.epicenchants.utils.Tuple; import com.songoda.epicenchants.utils.itemnbtapi.NBTItem; import com.songoda.epicenchants.utils.single.GeneralUtils; import org.bukkit.Bukkit; +import org.bukkit.entity.Player; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; @@ -37,6 +38,16 @@ public class BookListener extends ItemListener { return; } + // get total amount of enchantments on item + int currentEnchantmentTotal = instance.getEnchantUtils().getEnchants(toApply).size(); + int maxAllowedApply = instance.getEnchantUtils().getMaximumEnchantsCanApply((Player) event.getWhoClicked()); + + // item is at max enchantments + if (currentEnchantmentTotal >= maxAllowedApply) { + instance.getLocale().getMessage("enchants.maxallowed").processPlaceholder("max_enchants", maxAllowedApply).sendPrefixedMessage(event.getWhoClicked()); + return; + } + int level = cursor.getInteger("level"); int successRate = cursor.getInteger("success-rate"); int destroyRate = cursor.getInteger("destroy-rate"); diff --git a/src/main/java/com/songoda/epicenchants/utils/EnchantUtils.java b/src/main/java/com/songoda/epicenchants/utils/EnchantUtils.java index 23d1584..5128078 100644 --- a/src/main/java/com/songoda/epicenchants/utils/EnchantUtils.java +++ b/src/main/java/com/songoda/epicenchants/utils/EnchantUtils.java @@ -11,11 +11,13 @@ import com.songoda.epicenchants.utils.itemnbtapi.NBTItem; import com.songoda.epicenchants.utils.objects.ItemBuilder; import com.songoda.epicenchants.utils.settings.Settings; import com.songoda.epicenchants.utils.single.GeneralUtils; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.event.Event; import org.bukkit.inventory.ItemStack; +import org.bukkit.permissions.PermissionAttachmentInfo; import org.jetbrains.annotations.NotNull; import javax.annotation.Nullable; @@ -141,4 +143,20 @@ public class EnchantUtils { output.removeLore(TextUtils.formatText(text)); return output.build(); } + + public int getMaximumEnchantsCanApply(Player p) { + int max = 0; + if (p.isOp()) return 100; // in theory no single item will have 100 enchantments at a time. + for (PermissionAttachmentInfo effectivePermission : p.getEffectivePermissions()) { + if (!effectivePermission.getPermission().startsWith("epicenchants.maxapply.")) continue; + + String node[] = effectivePermission.getPermission().split("\\."); + + if (Methods.isInt(node[node.length - 1])) { + int num = Integer.parseInt(node[node.length - 1]); + if (num > max) max = num; + } + } + return max; + } } diff --git a/src/main/resources/en_US.lang b/src/main/resources/en_US.lang index 5781be8..c56e84c 100644 --- a/src/main/resources/en_US.lang +++ b/src/main/resources/en_US.lang @@ -63,5 +63,6 @@ enchants: alreadyapplied: '&cYou already have that enchant applied on this item.' protected: '&7This book would have broken your item, luckily it was protected!' success: '&7You have successfully applied &6{enchant}&r&7.' + maxallowed: '&cYou cannot apply more than &4{max_enchants} &cenchants!' book: discover: '&7You examine the &6{group_color}{group_name} &7Enchantment Book, and discover &6{enchant_format}&7!'