reworked unit repair

This commit is contained in:
alexcrea 2026-07-01 02:06:14 +02:00
parent a78f83f6f4
commit 799008f652
Signed by: alexcrea
GPG key ID: E59DF23EE2A2266C
2 changed files with 172 additions and 74 deletions

View file

@ -1,6 +1,8 @@
package xyz.alexcrea.cuanvil.util
import org.bukkit.NamespacedKey
import org.bukkit.configuration.ConfigurationSection
import org.bukkit.configuration.file.FileConfiguration
import org.bukkit.inventory.ItemStack
import xyz.alexcrea.cuanvil.config.ConfigHolder
import xyz.alexcrea.cuanvil.util.MaterialUtil.customType
@ -22,40 +24,36 @@ object UnitRepairUtil {
): Double? {
if (other == null) return null
val config = ConfigHolder.UNIT_REPAIR_HOLDER.config
// Get configuration section if exist
val otherName = other.customType.key.lowercase()
var section = config.getConfigurationSection(otherName)
if (section == null) {
section = config.getConfigurationSection(otherName.uppercase())
if (section == null) return null
}
// Get repair amount
var userDefault = config.getDouble(UNIT_REPAIR_DEFAULT_PATH, DEFAULT_DEFAULT_UNIT_REPAIR)
if (userDefault <= 0) {
userDefault = DEFAULT_DEFAULT_UNIT_REPAIR
}
val material = other.customType
val selfType = this.customType
return getRepairAmount(this, section, userDefault)
var result = checkSection(config, material.toString(), selfType)
if (result != null) return result
result = checkSection(config, material.key, selfType)
if (result != null) return result
// Get default
val userDefault = config.getDouble(UNIT_REPAIR_DEFAULT_PATH, DEFAULT_DEFAULT_UNIT_REPAIR)
if (userDefault <= 0)
return DEFAULT_DEFAULT_UNIT_REPAIR
return userDefault
}
/**
* Get the item % repaired by this configuration section of a unit repair.
* null if not found.
* If value is set to less than or equal to 0 then it will be set to default
*/
private fun getRepairAmount(item: ItemStack, section: ConfigurationSection, default: Double): Double? {
val itemName = item.customType.key.lowercase()
val repairValue = if (section.isDouble(itemName)) {
section.getDouble(itemName)
} else if (section.isDouble(itemName.uppercase())) {
section.getDouble(itemName.uppercase())
} else {
return null
}
if (repairValue <= 0)
return default
return repairValue
fun checkSection(
config: FileConfiguration,
path: String,
material: NamespacedKey
): Double? {
val section = config.getConfigurationSection(path) ?: return null
if (section.isDouble(material.toString()))
return section.getDouble(material.toString())
if (section.isDouble(material.key))
return section.getDouble(material.key)
return null
}
}