diff --git a/src/main/kotlin/com/bibireden/data_attributes/api/attribute/AttributeFormat.kt b/src/main/kotlin/com/bibireden/data_attributes/api/attribute/AttributeFormat.kt index 5890256..106c6b1 100644 --- a/src/main/kotlin/com/bibireden/data_attributes/api/attribute/AttributeFormat.kt +++ b/src/main/kotlin/com/bibireden/data_attributes/api/attribute/AttributeFormat.kt @@ -1,9 +1,40 @@ package com.bibireden.data_attributes.api.attribute import io.wispforest.endec.Endec +import kotlin.math.abs enum class AttributeFormat(val function: (min: Double, max: Double, value: Double) -> String) { - Percentage ({min, max, value -> "%.2f".format((value - min) / ((max - min) / 100)) + "%" }), + Percentage ({min, max, value -> + var minimum = 0.0 + var maximum: Double + + val isNegative = value < 0.0 + var final: Double + + if (isNegative) { + if (max < 0) + { + minimum = max + } + + + maximum = min + + + final = (value + minimum) / (maximum + minimum) + } else { + if (min > 0) + { + minimum = min + } + + maximum = max + + final = (value - minimum)/(maximum-minimum) + } + + "%.2f".format(final * 100) + "%" + }), Whole({_, _, value -> "%.2f".format(value) } ); companion object { diff --git a/src/main/kotlin/com/bibireden/data_attributes/config/entities/EntityTypeEntry.kt b/src/main/kotlin/com/bibireden/data_attributes/config/entities/EntityTypeEntry.kt index fb9086a..cfb117b 100644 --- a/src/main/kotlin/com/bibireden/data_attributes/config/entities/EntityTypeEntry.kt +++ b/src/main/kotlin/com/bibireden/data_attributes/config/entities/EntityTypeEntry.kt @@ -13,7 +13,7 @@ data class EntityTypeEntry(var value: Double = 0.0, var fallback: Double? = null companion object { val ENDEC: Endec = StructEndecBuilder.of( Endec.DOUBLE.fieldOf("value") { it.value }, - Endec.DOUBLE.nullableOf().fieldOf("fallback", { it.fallback }), + Endec.DOUBLE.nullableOf().fieldOf("fallback") { it.fallback }, ::EntityTypeEntry ) } diff --git a/src/main/kotlin/com/bibireden/data_attributes/ui/config/providers/EntityTypesProvider.kt b/src/main/kotlin/com/bibireden/data_attributes/ui/config/providers/EntityTypesProvider.kt index 8471923..0e71f34 100644 --- a/src/main/kotlin/com/bibireden/data_attributes/ui/config/providers/EntityTypesProvider.kt +++ b/src/main/kotlin/com/bibireden/data_attributes/ui/config/providers/EntityTypesProvider.kt @@ -110,10 +110,15 @@ class EntityTypesProvider(val option: Option>) : ct.id(id.toString()) // find fallback - entityTypeEntry.fallback = Registries.ENTITY_TYPE.get(parentId)?.let { entityType -> - val defaultContainer = DefaultAttributeRegistry.get(entityType as EntityType) - Registries.ATTRIBUTE.get(id)?.let(defaultContainer::getBaseValue) - }?.round(2) + try { + entityTypeEntry.fallback = Registries.ENTITY_TYPE.get(parentId)?.let { entityType -> + val defaultContainer = DefaultAttributeRegistry.get(entityType as EntityType) + Registries.ATTRIBUTE.get(id)?.let(defaultContainer::getBaseValue) + }?.round(2) + } + catch (_: IllegalArgumentException) { + entityTypeEntry.fallback = 0.0 + } if (!isDefault) { ct.child(Containers.horizontalFlow(Sizing.fill(100), Sizing.fixed(15))