code cleanup

This commit is contained in:
alexcrea 2026-03-02 20:26:38 +01:00
parent 2c7c8f2dd3
commit c9bac11292
Signed by: alexcrea
GPG key ID: E346CD16413450E3
2 changed files with 46 additions and 57 deletions

View file

@ -11,8 +11,8 @@ class EnchantConflictGroup(
) { ) {
private val enchantments = HashSet<CAEnchantment>() private val enchantments = HashSet<CAEnchantment>()
private val conflictAfterLevel = HashMap<CAEnchantment, Int>() private val conflictsAfterLevel = HashMap<CAEnchantment, Int>()
private val conflictBeforeLevel = HashMap<CAEnchantment, Int>() private val conflictsBeforeLevel = HashMap<CAEnchantment, Int>()
fun addEnchantment(enchant: CAEnchantment) { fun addEnchantment(enchant: CAEnchantment) {
enchantments.add(enchant) enchantments.add(enchant)
@ -23,10 +23,10 @@ class EnchantConflictGroup(
private fun canBypassByBeforeLevel(enchants: Map<CAEnchantment, Int>): Boolean { private fun canBypassByBeforeLevel(enchants: Map<CAEnchantment, Int>): Boolean {
// Either there no "conflict after" // 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 // 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) val current = enchants.getOrDefault(entry.key, 0)
if(current > entry.value) if(current > entry.value)
return false return false
@ -37,10 +37,10 @@ class EnchantConflictGroup(
private fun canBypassByAfterLevel(enchants: Map<CAEnchantment, Int>): Boolean { private fun canBypassByAfterLevel(enchants: Map<CAEnchantment, Int>): Boolean {
// Either there no "conflict after" // 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 // 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) val current = enchants.getOrDefault(entry.key, 0)
if(current < entry.value) if(current < entry.value)
return false return false
@ -96,33 +96,33 @@ class EnchantConflictGroup(
} }
fun getConflictAfters(): HashMap<CAEnchantment, Int> { fun getConflictAfters(): HashMap<CAEnchantment, Int> {
return conflictAfterLevel return conflictsAfterLevel
} }
fun putConflictAfterLevel(enchantment: CAEnchantment, level: Int): Boolean { fun putConflictAfterLevel(enchantment: CAEnchantment, level: Int): Boolean {
return null != ( return null != (
if(level < 0) conflictAfterLevel.remove(enchantment) if(level < 0) conflictsAfterLevel.remove(enchantment)
else conflictAfterLevel.put(enchantment, level)) else conflictsAfterLevel.put(enchantment, level))
} }
fun setConflictAfterLevel(conflictAfterLevel: HashMap<CAEnchantment, Int>) { fun setConflictsAfterLevel(conflictAfterLevel: HashMap<CAEnchantment, Int>) {
this.conflictAfterLevel.clear() this.conflictsAfterLevel.clear()
this.conflictAfterLevel.putAll(conflictAfterLevel) this.conflictsAfterLevel.putAll(conflictAfterLevel)
} }
fun getConflictBefores(): HashMap<CAEnchantment, Int> { fun getConflictsBefore(): HashMap<CAEnchantment, Int> {
return conflictBeforeLevel return conflictsBeforeLevel
} }
fun putConflictBeforeLevel(enchantment: CAEnchantment, level: Int): Boolean { fun putConflictsBeforeLevel(enchantment: CAEnchantment, level: Int): Boolean {
return null != ( return null != (
if(level < 0) conflictBeforeLevel.remove(enchantment) if(level < 0) conflictsBeforeLevel.remove(enchantment)
else conflictBeforeLevel.put(enchantment, level)) else conflictsBeforeLevel.put(enchantment, level))
} }
fun setConflictBeforeLevel(conflictBeforeLevel: HashMap<CAEnchantment, Int>) { fun setConflictsBeforeLevel(conflictBeforeLevel: HashMap<CAEnchantment, Int>) {
this.conflictBeforeLevel.clear() this.conflictsBeforeLevel.clear()
this.conflictBeforeLevel.putAll(conflictBeforeLevel) this.conflictsBeforeLevel.putAll(conflictBeforeLevel)
} }
fun getRepresentativeMaterial(): Material { fun getRepresentativeMaterial(): Material {

View file

@ -9,6 +9,7 @@ import xyz.alexcrea.cuanvil.enchant.AdditionalTestEnchantment
import xyz.alexcrea.cuanvil.enchant.CAEnchantment import xyz.alexcrea.cuanvil.enchant.CAEnchantment
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry
import java.util.* import java.util.*
import kotlin.collections.set
class EnchantConflictManager { class EnchantConflictManager {
@ -118,48 +119,36 @@ class EnchantConflictManager {
} }
} }
//TODO find a way to dry this two ? val conflictsAfterLevel = section.getConfigurationSection(CONFLICT_AFTER_LEVEL_LIST_PATH)
val conflictAfterLevels = section.getConfigurationSection(CONFLICT_AFTER_LEVEL_LIST_PATH) val conflictsAfterMap = conflict.getConflictAfters()
if(conflictAfterLevels != null) { fetchConditionalRestriction(conflictsAfterMap, conflictsAfterLevel, conflictName)
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 value = conflictAfterLevels.getInt(enchantName, -1) val conflictsBeforeLevel = section.getConfigurationSection(CONFLICT_BEFORE_LEVEL_LIST_PATH)
if(value < 0) continue val conflictsBeforeMap = conflict.getConflictsBefore()
fetchConditionalRestriction(conflictsBeforeMap, conflictsBeforeLevel, conflictName)
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))
}
}
}
return conflict return conflict
} }
private fun fetchConditionalRestriction(restrictions: MutableMap<CAEnchantment, Int>, 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<CAEnchantment> { private fun getEnchantByIdentifier(enchantName: String): List<CAEnchantment> {
val key = NamespacedKey.fromString(enchantName) val key = NamespacedKey.fromString(enchantName)
if (key != null) { if (key != null) {