From 6105ca797beb3f39c03eb36510bbd1f4e0c79fd8 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Wed, 12 Jul 2023 14:41:40 +0700 Subject: [PATCH] If it ever happens, avoid dividing by zero --- .../mc/otm/capability/energy/IMatteryEnergyStorage.kt | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/energy/IMatteryEnergyStorage.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/energy/IMatteryEnergyStorage.kt index 98ad13ab6..ffd4e221d 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/energy/IMatteryEnergyStorage.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/energy/IMatteryEnergyStorage.kt @@ -362,9 +362,15 @@ fun IMatteryEnergyStorage.transferChecked(other: IMatteryEnergyStorage, amount: } fun IMatteryEnergyStorage.getBarWidth(): Int { - return ((batteryLevel / maxBatteryLevel).toFloat().coerceAtLeast(0f).coerceAtMost(1f) * 13f).roundToInt() + if (!maxBatteryLevel.isPositive) + return 0 + + return ((batteryLevel / maxBatteryLevel).toFloat().coerceIn(0f, 1f) * 13f).roundToInt() } fun IMatteryEnergyStorage.getBarColor(): Int { + if (!maxBatteryLevel.isPositive) + return 0 + return RGBAColor.LOW_POWER.linearInterpolation((batteryLevel / maxBatteryLevel).toFloat(), RGBAColor.FULL_POWER).toRGB() }