Skip to content

Commit

Permalink
improve composter empty time precision
Browse files Browse the repository at this point in the history
  • Loading branch information
appable0 committed Dec 22, 2023
1 parent 0102ddc commit fcfad35
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit fcfad35

Please sign in to comment.