From 034e0645dd4a27f0f5f5ad84c72d8f4ed0901ea6 Mon Sep 17 00:00:00 2001 From: Boy Date: Sun, 13 Oct 2024 17:07:20 +0200 Subject: [PATCH] refactor: set default values for optional properties in tool-component --- .../serialization/ToolComponentSerializer.kt | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/idofront-serializers/src/main/kotlin/com/mineinabyss/idofront/serialization/ToolComponentSerializer.kt b/idofront-serializers/src/main/kotlin/com/mineinabyss/idofront/serialization/ToolComponentSerializer.kt index 96e76ae..e2fe750 100644 --- a/idofront-serializers/src/main/kotlin/com/mineinabyss/idofront/serialization/ToolComponentSerializer.kt +++ b/idofront-serializers/src/main/kotlin/com/mineinabyss/idofront/serialization/ToolComponentSerializer.kt @@ -9,7 +9,6 @@ import kotlinx.serialization.descriptors.SerialDescriptor import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Encoder import net.kyori.adventure.key.Key -import net.kyori.adventure.util.TriState import org.bukkit.Bukkit import org.bukkit.Material import org.bukkit.NamespacedKey @@ -22,30 +21,34 @@ import org.bukkit.inventory.meta.components.ToolComponent.ToolRule @SerialName("ToolComponent") private class ToolComponentSurrogate( val rules: List = emptyList(), - val defaultMiningSpeed: Float, - val damagePerBlock: Int + val defaultMiningSpeed: Float = 1f, + val damagePerBlock: Int = 1 ) { + init { + require(damagePerBlock >= 0) { "ToolComponent must have a non-negative damagePerBlock value"} + } + @Serializable data class Rule( val blockTypes: List<@Serializable(KeySerializer::class) Key>, val speed: Float? = null, - val correctForDrops: TriState + val correctForDrops: Boolean? = null ) { - constructor(rule: ToolRule) : this(rule.blocks.map(Material::key), rule.speed, TriState.byBoolean(rule.isCorrectForDrops)) + constructor(rule: ToolRule) : this(rule.blocks.map(Material::key), rule.speed, rule.isCorrectForDrops) companion object { fun List.toToolRules(): List { - return map { rule -> + return mapNotNull { rule -> val nonLegacyMaterials = Material.entries.filterNot(Material::isLegacy) val materials = rule.blockTypes.map { rule -> nonLegacyMaterials.filter { it.key() == rule.key() } }.flatten() val tags = rule.blockTypes.mapNotNull { rule -> runCatching { Bukkit.getTag(Tag.REGISTRY_BLOCKS, NamespacedKey.fromString(rule.key().asString())!!, Material::class.java) }.getOrNull() } if (materials.isEmpty() && tags.isEmpty()) idofrontLogger.w("Failed to find tag for ${rule}, skipping...") - tags.map { ItemStack.of(Material.PAPER).itemMeta.tool.addRule(it, rule.speed, rule.correctForDrops.toBoolean()) } - .plus(ItemStack.of(Material.PAPER).itemMeta.tool.addRule(materials, rule.speed, rule.correctForDrops.toBoolean())) - }.flatten() + tags.map { ItemStack.of(Material.PAPER).itemMeta.tool.addRule(it, rule.speed, rule.correctForDrops) } + .plus(ItemStack.of(Material.PAPER).itemMeta.tool.addRule(materials, rule.speed, rule.correctForDrops)) + }.flatten().filter { it.blocks.isNotEmpty() } } }