Add permission check for Enchantment² enchantments.

This commit is contained in:
alexcrea 2024-06-18 03:13:40 +02:00
parent b1ca8bd91c
commit 6553122819
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
3 changed files with 28 additions and 3 deletions

View file

@ -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.

View file

@ -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());