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 io.delilaheve.util.ItemUtil;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.HumanEntity;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.EnchantmentStorageMeta; import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
@ -81,13 +82,22 @@ public abstract class WrappedEnchantment {
public final int defaultMaxLevel(){return defaultMaxLevel;} 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. * @return If the enchantment is optimised for group operation.
*/ */
protected boolean isOptimised(){ protected boolean isOptimised(){
return false; 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. * Get current level of the enchantment.
* @param item Item to search the level for. * @param item Item to search the level for.
@ -139,7 +149,6 @@ public abstract class WrappedEnchantment {
public abstract void removeFrom(@NotNull ItemStack item); public abstract void removeFrom(@NotNull ItemStack item);
// Static functions // Static functions
/** /**
* Clear every enchantment from this item. * Clear every enchantment from this item.
* @param item Item to be cleared from enchantments. * @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.enchantments.CustomEnchant;
import me.athlaeos.enchantssquared.managers.CustomEnchantManager; 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.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -29,6 +31,18 @@ public class EnchantSquaredEnchantment extends WrappedEnchantment {
return true; 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 @Override
public int getLevel(@NotNull ItemStack item, @NotNull ItemMeta meta) { public int getLevel(@NotNull ItemStack item, @NotNull ItemMeta meta) {
return CustomEnchantManager.getInstance().getEnchantStrength(item, this.enchant.getType()); return CustomEnchantManager.getInstance().getEnchantStrength(item, this.enchant.getType());

View file

@ -30,6 +30,8 @@ object EnchantmentUtil {
) = mutableMapOf<WrappedEnchantment, Int>().apply { ) = mutableMapOf<WrappedEnchantment, Int>().apply {
putAll(this@combineWith) putAll(this@combineWith)
other.forEach { (enchantment, level) -> other.forEach { (enchantment, level) ->
if(!enchantment.isAllowed(player)) return@forEach
// Get max level or 255 if player can bypass // Get max level or 255 if player can bypass
val maxLevel = if (player.hasPermission(CustomAnvil.bypassLevelPermission)) val maxLevel = if (player.hasPermission(CustomAnvil.bypassLevelPermission))
{ 255 } else { 255 } else
@ -53,7 +55,7 @@ object EnchantmentUtil {
} }
// Enchantment already in result list // Enchantment already in result list
else { 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 // ... and they are conflicting
val conflictType = val conflictType =