From 7ee3a37b130c7b6bfe3c69bfc41cba648fe970e2 Mon Sep 17 00:00:00 2001 From: DelilahEve Date: Sun, 21 Aug 2022 07:45:32 -0400 Subject: [PATCH] fix permission check --- .../kotlin/io/delilaheve/AnvilEventListener.kt | 16 ++++++++-------- .../kotlin/io/delilaheve/util/EnchantmentUtil.kt | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/io/delilaheve/AnvilEventListener.kt b/src/main/kotlin/io/delilaheve/AnvilEventListener.kt index 77d2aeb..aedd6d6 100644 --- a/src/main/kotlin/io/delilaheve/AnvilEventListener.kt +++ b/src/main/kotlin/io/delilaheve/AnvilEventListener.kt @@ -3,6 +3,7 @@ package io.delilaheve import io.delilaheve.util.ConfigOptions import io.delilaheve.util.EnchantmentUtil.calculateValue import io.delilaheve.util.EnchantmentUtil.combineWith +import io.delilaheve.util.EnchantmentUtil.hasConflicts import io.delilaheve.util.ItemUtil.canMergeWith import io.delilaheve.util.ItemUtil.findEnchantments import io.delilaheve.util.ItemUtil.isBook @@ -75,14 +76,13 @@ class AnvilEventListener : Listener { @EventHandler(ignoreCancelled = true) fun anvilExtractionCheck(event: InventoryClickEvent) { val player = event.whoClicked as? Player ?: return - if (player.hasPermission(requirePermission)) { - val inventory = event.inventory as? AnvilInventory ?: return - if (event.rawSlot != ANVIL_OUTPUT_SLOT) { return } - val output = inventory.getItem(2) ?: return - if (output.type == Material.AIR) { return } - if (player.level < inventory.repairCost) { return } - event.result = Event.Result.ALLOW - } + val inventory = event.inventory as? AnvilInventory ?: return + val output = inventory.getItem(ANVIL_OUTPUT_SLOT) ?: return + if (output.findEnchantments().hasConflicts() && !player.hasPermission(requirePermission)) { return } + if (event.rawSlot != ANVIL_OUTPUT_SLOT) { return } + if (output.type == Material.AIR) { return } + if (player.level < inventory.repairCost) { return } + event.result = Event.Result.ALLOW } } diff --git a/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt b/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt index 0341d13..2065a7a 100644 --- a/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt +++ b/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt @@ -53,6 +53,21 @@ object EnchantmentUtil { } } + /** + * Check if a set of enchantments has any conflicts + */ + fun Map.hasConflicts() : Boolean { + forEach { (enchantment1, _) -> + val hasConflict = any { (enchantment2, _) -> + enchantment2.conflictsWith(enchantment1) + } + if (hasConflict) { + return hasConflict + } + } + return false + } + /** * Calculate the value of a set of enchantments */