Skip to content

Commit

Permalink
[fix] Resolve exception & formatting (2.0.1)
Browse files Browse the repository at this point in the history
  • Loading branch information
bibi-reden committed Oct 12, 2024
1 parent 194d384 commit ec0387b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ data class EntityTypeEntry(var value: Double = 0.0, var fallback: Double? = null
companion object {
val ENDEC: Endec<EntityTypeEntry> = StructEndecBuilder.of(
Endec.DOUBLE.fieldOf("value") { it.value },
Endec.DOUBLE.nullableOf().fieldOf("fallback", { it.fallback }),
Endec.DOUBLE.nullableOf().fieldOf("fallback") { it.fallback },
::EntityTypeEntry
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,15 @@ class EntityTypesProvider(val option: Option<Map<Identifier, EntityTypeData>>) :
ct.id(id.toString())

// find fallback
entityTypeEntry.fallback = Registries.ENTITY_TYPE.get(parentId)?.let { entityType ->
val defaultContainer = DefaultAttributeRegistry.get(entityType as EntityType<LivingEntity>)
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<LivingEntity>)
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))
Expand Down

0 comments on commit ec0387b

Please sign in to comment.