diff --git a/src/main/java/xyz/alexcrea/cuanvil/enchant/WrappedEnchantment.java b/src/main/java/xyz/alexcrea/cuanvil/enchant/WrappedEnchantment.java index 80f1de5..5d2bd54 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/enchant/WrappedEnchantment.java +++ b/src/main/java/xyz/alexcrea/cuanvil/enchant/WrappedEnchantment.java @@ -4,6 +4,7 @@ import io.delilaheve.CustomAnvil; import io.delilaheve.util.ItemUtil; import org.bukkit.NamespacedKey; import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.HumanEntity; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.EnchantmentStorageMeta; import org.bukkit.inventory.meta.ItemMeta; @@ -81,13 +82,22 @@ public abstract class WrappedEnchantment { public final int defaultMaxLevel(){return defaultMaxLevel;} /** - * If the enchantment have specialised group operation. + * Check if the enchantment have specialised group operation. * @return If the enchantment is optimised for group operation. */ protected boolean isOptimised(){ return false; } + /** + * Check if the player is allowed to use this enchantment. + * @param player The player to test. + * @return If the player is allowed to use this enchantment. + */ + public boolean isAllowed(@NotNull HumanEntity player){ + return true; + } + /** * Get current level of the enchantment. * @param item Item to search the level for. @@ -139,7 +149,6 @@ public abstract class WrappedEnchantment { public abstract void removeFrom(@NotNull ItemStack item); // Static functions - /** * Clear every enchantment from this item. * @param item Item to be cleared from enchantments. diff --git a/src/main/java/xyz/alexcrea/cuanvil/enchant/wrapped/EnchantSquaredEnchantment.java b/src/main/java/xyz/alexcrea/cuanvil/enchant/wrapped/EnchantSquaredEnchantment.java index 7abe29f..52e7171 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/enchant/wrapped/EnchantSquaredEnchantment.java +++ b/src/main/java/xyz/alexcrea/cuanvil/enchant/wrapped/EnchantSquaredEnchantment.java @@ -2,6 +2,8 @@ package xyz.alexcrea.cuanvil.enchant.wrapped; import me.athlaeos.enchantssquared.enchantments.CustomEnchant; import me.athlaeos.enchantssquared.managers.CustomEnchantManager; +import org.bukkit.entity.HumanEntity; +import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.jetbrains.annotations.NotNull; @@ -29,6 +31,18 @@ public class EnchantSquaredEnchantment extends WrappedEnchantment { return true; } + @Override + public boolean isAllowed(@NotNull HumanEntity human) { + if(human instanceof Player){ + return this.enchant.hasPermission((Player) human); + } + // Not really ideal for maintainability but will probably never be executed. (At least I hope) + boolean required = CustomEnchantManager.getInstance().isRequirePermissions(); + if (!required) return true; + + return human.hasPermission("es.enchant.*") || !human.hasPermission(this.enchant.getRequiredPermission()); + } + @Override public int getLevel(@NotNull ItemStack item, @NotNull ItemMeta meta) { return CustomEnchantManager.getInstance().getEnchantStrength(item, this.enchant.getType()); diff --git a/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt b/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt index 9d3f95a..7bf3e4b 100644 --- a/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt +++ b/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt @@ -30,6 +30,8 @@ object EnchantmentUtil { ) = mutableMapOf().apply { putAll(this@combineWith) other.forEach { (enchantment, level) -> + if(!enchantment.isAllowed(player)) return@forEach + // Get max level or 255 if player can bypass val maxLevel = if (player.hasPermission(CustomAnvil.bypassLevelPermission)) { 255 } else @@ -53,7 +55,7 @@ object EnchantmentUtil { } // Enchantment already in result list else { - val oldLevel = this[enchantment]!! // should be true, see the comment above + val oldLevel = this[enchantment]!! // <- should not be null. see the comment above // ... and they are conflicting val conflictType =