aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorappable <enzospiacitelli@gmail.com>2023-12-22 17:30:01 -0800
committerGitHub <noreply@github.com>2023-12-23 02:30:01 +0100
commit5e976a3a821f07b6f2cee7681c8ce98823cdd32a (patch)
tree130670a4fd49df8cba80dffd81dd3cc608c58087 /src/main/java
parent57a25df8326aa525cb35b84ca0dcce6985da2c5e (diff)
downloadskyhanni-5e976a3a821f07b6f2cee7681c8ce98823cdd32a.tar.gz
skyhanni-5e976a3a821f07b6f2cee7681c8ce98823cdd32a.tar.bz2
skyhanni-5e976a3a821f07b6f2cee7681c8ce98823cdd32a.zip
improve composter empty time precision (#824)
Improve precision of compost empty timer. #824
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/features/garden/composter/ComposterAPI.kt37
-rw-r--r--src/main/java/at/hannibal2/skyhanni/features/garden/composter/ComposterDisplay.kt25
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt6
3 files changed, 44 insertions, 24 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/composter/ComposterAPI.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/composter/ComposterAPI.kt
index 50efa9e42..c9a362e43 100644
--- a/src/main/java/at/hannibal2/skyhanni/features/garden/composter/ComposterAPI.kt
+++ b/src/main/java/at/hannibal2/skyhanni/features/garden/composter/ComposterAPI.kt
@@ -4,6 +4,8 @@ import at.hannibal2.skyhanni.data.model.ComposterUpgrade
import at.hannibal2.skyhanni.features.garden.GardenAPI
import at.hannibal2.skyhanni.utils.NumberUtil.formatNumber
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
+import at.hannibal2.skyhanni.utils.TimeUtils
+import kotlin.math.floor
import kotlin.time.Duration
import kotlin.time.Duration.Companion.minutes
@@ -14,6 +16,41 @@ object ComposterAPI {
fun ComposterUpgrade.getLevel(addOne: ComposterUpgrade?) =
(composterUpgrades?.get(this) ?: 0) + if (addOne == this) 1 else 0
+ fun estimateEmptyTimeFromTab(): Duration? {
+ if (composterUpgrades.isNullOrEmpty()) {
+ return null
+ }
+
+ val nextCompostTime = tabListData[ComposterDisplay.DataType.TIME_LEFT]?.removeColor()?.let {
+ if (it != "INACTIVE") TimeUtils.getDuration(it) else null
+ } ?: Duration.ZERO
+
+ val timePerCompost = timePerCompost(null)
+ val fractionRemaining = nextCompostTime / timePerCompost
+
+ val remainingTimeByOrganicMatter = getDurationUntilEndOfResource(
+ getOrganicMatter(), fractionRemaining, organicMatterRequiredPer(null), timePerCompost
+ )
+
+ val remainingTimeByFuel = getDurationUntilEndOfResource(
+ getFuel(), fractionRemaining, fuelRequiredPer(null), timePerCompost
+ )
+
+ return nextCompostTime + minOf(remainingTimeByOrganicMatter, remainingTimeByFuel)
+ }
+
+ private fun getDurationUntilEndOfResource(
+ amount: Long,
+ fractionOfCompostRemaining: Double,
+ requiredPer: Double,
+ timePerCompost: Duration
+ ): Duration {
+ val resourceConsumedByNextCompost = fractionOfCompostRemaining * requiredPer
+ val resourceRemainingAfterNextCompostFinishes = amount - resourceConsumedByNextCompost
+ val compostRemainingAfterNextCompostFinishes = floor(resourceRemainingAfterNextCompostFinishes / requiredPer)
+ return timePerCompost * compostRemainingAfterNextCompostFinishes
+ }
+
fun getFuel() = tabListData[ComposterDisplay.DataType.FUEL]?.removeColor()?.formatNumber() ?: 0
fun getOrganicMatter() = tabListData[ComposterDisplay.DataType.ORGANIC_MATTER]?.removeColor()?.formatNumber() ?: 0
diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/composter/ComposterDisplay.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/composter/ComposterDisplay.kt
index cf06da7f6..04e2bee61 100644
--- a/src/main/java/at/hannibal2/skyhanni/features/garden/composter/ComposterDisplay.kt
+++ b/src/main/java/at/hannibal2/skyhanni/features/garden/composter/ComposterDisplay.kt
@@ -12,7 +12,6 @@ import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher
import at.hannibal2.skyhanni.utils.TimeUtils
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import java.util.Collections
-import kotlin.math.floor
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds
import kotlin.time.DurationUnit
@@ -49,34 +48,12 @@ class ComposterDisplay {
readData(event.tabList)
if (tabListData.isNotEmpty()) {
- calculateEmptyTime()
+ composterEmptyTime = ComposterAPI.estimateEmptyTimeFromTab()
updateDisplay()
sendNotify()
}
}
- private fun calculateEmptyTime() {
- val organicMatter = ComposterAPI.getOrganicMatter()
- val fuel = ComposterAPI.getFuel()
-
- if (ComposterAPI.composterUpgrades.isNullOrEmpty()) {
- composterEmptyTime = null
- return
- }
-
- val timePerCompost = ComposterAPI.timePerCompost(null)
-
- val organicMatterRequired = ComposterAPI.organicMatterRequiredPer(null)
- val fuelRequired = ComposterAPI.fuelRequiredPer(null)
-
- val organicMatterRemaining = floor(organicMatter / organicMatterRequired)
- val fuelRemaining = floor(fuel / fuelRequired)
-
- val endOfOrganicMatter = timePerCompost * organicMatterRemaining
- val endOfFuel = timePerCompost * fuelRemaining
- composterEmptyTime = if (endOfOrganicMatter > endOfFuel) endOfFuel else endOfOrganicMatter
- }
-
private fun updateDisplay() {
val newDisplay = mutableListOf<List<Any>>()
newDisplay.addAsSingletonList("§bComposter")
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt
index a9ac0ca82..9ac80f7f3 100644
--- a/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt
+++ b/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt
@@ -38,6 +38,12 @@ object StringUtils {
private val formattingChars by lazy { "kmolnr".toCharArray() + "kmolnr".uppercase().toCharArray() }
+ /**
+ * Removes color and optionally formatting codes from the given string, leaving plain text.
+ *
+ * @param keepFormatting Boolean indicating whether to retain non-color formatting codes (default: false).
+ * @return A string with color codes removed (and optionally formatting codes if specified).
+ */
fun String.removeColor(keepFormatting: Boolean = false): String {
val builder = StringBuilder(this.length)