fix permission check

This commit is contained in:
DelilahEve 2022-08-21 07:45:32 -04:00
parent 271d173f5f
commit 7ee3a37b13
2 changed files with 23 additions and 8 deletions

View file

@ -3,6 +3,7 @@ package io.delilaheve
import io.delilaheve.util.ConfigOptions import io.delilaheve.util.ConfigOptions
import io.delilaheve.util.EnchantmentUtil.calculateValue import io.delilaheve.util.EnchantmentUtil.calculateValue
import io.delilaheve.util.EnchantmentUtil.combineWith import io.delilaheve.util.EnchantmentUtil.combineWith
import io.delilaheve.util.EnchantmentUtil.hasConflicts
import io.delilaheve.util.ItemUtil.canMergeWith import io.delilaheve.util.ItemUtil.canMergeWith
import io.delilaheve.util.ItemUtil.findEnchantments import io.delilaheve.util.ItemUtil.findEnchantments
import io.delilaheve.util.ItemUtil.isBook import io.delilaheve.util.ItemUtil.isBook
@ -75,14 +76,13 @@ class AnvilEventListener : Listener {
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
fun anvilExtractionCheck(event: InventoryClickEvent) { fun anvilExtractionCheck(event: InventoryClickEvent) {
val player = event.whoClicked as? Player ?: return val player = event.whoClicked as? Player ?: return
if (player.hasPermission(requirePermission)) {
val inventory = event.inventory as? AnvilInventory ?: return 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 (event.rawSlot != ANVIL_OUTPUT_SLOT) { return }
val output = inventory.getItem(2) ?: return
if (output.type == Material.AIR) { return } if (output.type == Material.AIR) { return }
if (player.level < inventory.repairCost) { return } if (player.level < inventory.repairCost) { return }
event.result = Event.Result.ALLOW event.result = Event.Result.ALLOW
} }
}
} }

View file

@ -53,6 +53,21 @@ object EnchantmentUtil {
} }
} }
/**
* Check if a set of enchantments has any conflicts
*/
fun Map<Enchantment, Int>.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 * Calculate the value of a set of enchantments
*/ */