From c9bac112928467327bb1d99716a9d6d029184166 Mon Sep 17 00:00:00 2001 From: alexcrea Date: Mon, 2 Mar 2026 20:26:38 +0100 Subject: [PATCH] code cleanup --- .../cuanvil/group/EnchantConflictGroup.kt | 40 ++++++------ .../cuanvil/group/EnchantConflictManager.kt | 63 ++++++++----------- 2 files changed, 46 insertions(+), 57 deletions(-) diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt index dda6d49..403d630 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt @@ -11,8 +11,8 @@ class EnchantConflictGroup( ) { private val enchantments = HashSet() - private val conflictAfterLevel = HashMap() - private val conflictBeforeLevel = HashMap() + private val conflictsAfterLevel = HashMap() + private val conflictsBeforeLevel = HashMap() fun addEnchantment(enchant: CAEnchantment) { enchantments.add(enchant) @@ -23,10 +23,10 @@ class EnchantConflictGroup( private fun canBypassByBeforeLevel(enchants: Map): Boolean { // Either there no "conflict after" - if(conflictAfterLevel.isEmpty()) return false + if(conflictsAfterLevel.isEmpty()) return false // Or we check if any conflict after enchantment is true - for (entry in conflictAfterLevel) { + for (entry in conflictsAfterLevel) { val current = enchants.getOrDefault(entry.key, 0) if(current > entry.value) return false @@ -37,10 +37,10 @@ class EnchantConflictGroup( private fun canBypassByAfterLevel(enchants: Map): Boolean { // Either there no "conflict after" - if(conflictBeforeLevel.isEmpty()) return false + if(conflictsBeforeLevel.isEmpty()) return false // Or we check if any conflict after enchantment is true - for (entry in conflictBeforeLevel) { + for (entry in conflictsBeforeLevel) { val current = enchants.getOrDefault(entry.key, 0) if(current < entry.value) return false @@ -96,33 +96,33 @@ class EnchantConflictGroup( } fun getConflictAfters(): HashMap { - return conflictAfterLevel + return conflictsAfterLevel } fun putConflictAfterLevel(enchantment: CAEnchantment, level: Int): Boolean { return null != ( - if(level < 0) conflictAfterLevel.remove(enchantment) - else conflictAfterLevel.put(enchantment, level)) + if(level < 0) conflictsAfterLevel.remove(enchantment) + else conflictsAfterLevel.put(enchantment, level)) } - fun setConflictAfterLevel(conflictAfterLevel: HashMap) { - this.conflictAfterLevel.clear() - this.conflictAfterLevel.putAll(conflictAfterLevel) + fun setConflictsAfterLevel(conflictAfterLevel: HashMap) { + this.conflictsAfterLevel.clear() + this.conflictsAfterLevel.putAll(conflictAfterLevel) } - fun getConflictBefores(): HashMap { - return conflictBeforeLevel + fun getConflictsBefore(): HashMap { + return conflictsBeforeLevel } - fun putConflictBeforeLevel(enchantment: CAEnchantment, level: Int): Boolean { + fun putConflictsBeforeLevel(enchantment: CAEnchantment, level: Int): Boolean { return null != ( - if(level < 0) conflictBeforeLevel.remove(enchantment) - else conflictBeforeLevel.put(enchantment, level)) + if(level < 0) conflictsBeforeLevel.remove(enchantment) + else conflictsBeforeLevel.put(enchantment, level)) } - fun setConflictBeforeLevel(conflictBeforeLevel: HashMap) { - this.conflictBeforeLevel.clear() - this.conflictBeforeLevel.putAll(conflictBeforeLevel) + fun setConflictsBeforeLevel(conflictBeforeLevel: HashMap) { + this.conflictsBeforeLevel.clear() + this.conflictsBeforeLevel.putAll(conflictBeforeLevel) } fun getRepresentativeMaterial(): Material { diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt index 6782803..f710f76 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt @@ -9,6 +9,7 @@ import xyz.alexcrea.cuanvil.enchant.AdditionalTestEnchantment import xyz.alexcrea.cuanvil.enchant.CAEnchantment import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry import java.util.* +import kotlin.collections.set class EnchantConflictManager { @@ -118,48 +119,36 @@ class EnchantConflictManager { } } - //TODO find a way to dry this two ? - val conflictAfterLevels = section.getConfigurationSection(CONFLICT_AFTER_LEVEL_LIST_PATH) - if(conflictAfterLevels != null) { - for (enchantName in conflictAfterLevels.getKeys(false)) { - val enchants = getEnchantByIdentifier(enchantName) - if (enchants.isEmpty()) { - CustomAnvil.instance.logger.warning("Enchantment $enchantName do not exist but was asked for conflict after level for conflict $conflictName") - continue - } + val conflictsAfterLevel = section.getConfigurationSection(CONFLICT_AFTER_LEVEL_LIST_PATH) + val conflictsAfterMap = conflict.getConflictAfters() + fetchConditionalRestriction(conflictsAfterMap, conflictsAfterLevel, conflictName) - val value = conflictAfterLevels.getInt(enchantName, -1) - if(value < 0) continue - - for (enchant in enchants) { - val previous = conflict.getConflictAfters().getOrDefault(enchant, value) - conflict.putConflictAfterLevel(enchant, value.coerceAtMost(previous)) - } - } - } - - val conflictBeforeLevels = section.getConfigurationSection(CONFLICT_BEFORE_LEVEL_LIST_PATH) - if(conflictBeforeLevels != null) { - for (enchantName in conflictBeforeLevels.getKeys(false)) { - val enchants = getEnchantByIdentifier(enchantName) - if (enchants.isEmpty()) { - CustomAnvil.instance.logger.warning("Enchantment $enchantName do not exist but was asked for conflict after level for conflict $conflictName") - continue - } - - val value = conflictBeforeLevels.getInt(enchantName, -1) - if(value < 0) continue - - for (enchant in enchants) { - val previous = conflict.getConflictBefores().getOrDefault(enchant, value) - conflict.putConflictBeforeLevel(enchant, value.coerceAtMost(previous)) - } - } - } + val conflictsBeforeLevel = section.getConfigurationSection(CONFLICT_BEFORE_LEVEL_LIST_PATH) + val conflictsBeforeMap = conflict.getConflictsBefore() + fetchConditionalRestriction(conflictsBeforeMap, conflictsBeforeLevel, conflictName) return conflict } + private fun fetchConditionalRestriction(restrictions: MutableMap, section: ConfigurationSection?, conflictName: String) { + if(section == null) return + for (enchantName in section.getKeys(false)) { + val enchants = getEnchantByIdentifier(enchantName) + if (enchants.isEmpty()) { + CustomAnvil.instance.logger.warning("Enchantment $enchantName do not exist but was asked for conditional restriction for conflict $conflictName") + continue + } + + val value = section.getInt(enchantName, -1) + if(value < 0) continue + + for (enchant in enchants) { + val previous = restrictions.getOrDefault(enchant, value) + restrictions[enchant] = value.coerceAtMost(previous) + } + } + } + private fun getEnchantByIdentifier(enchantName: String): List { val key = NamespacedKey.fromString(enchantName) if (key != null) {