Skip to content

Commit

Permalink
Fixed creating too many skill timers. hannibal002#1057
Browse files Browse the repository at this point in the history
  • Loading branch information
hannibal002 authored Feb 25, 2024
1 parent cff372a commit ce3ab82
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
16 changes: 8 additions & 8 deletions src/main/java/at/hannibal2/skyhanni/api/SkillAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import io.github.moulberry.notenoughupdates.util.Utils
import net.minecraft.command.CommandBase
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import java.util.LinkedList
import java.util.Timer
import java.util.regex.Matcher
import kotlin.concurrent.fixedRateTimer
import kotlin.time.Duration.Companion.seconds
Expand Down Expand Up @@ -105,9 +106,9 @@ object SkillAPI {
skillXp.lastUpdate = SimpleTimeMark.now()
skillXp.sessionTimerActive = true

if (skillXp.shouldStartTimer) {
runTimer(skillName, skillXp)
skillXp.shouldStartTimer = false

if (skillType.timer == null) {
skillType.timer = runTimer(skillType, skillXp)
}
SkillProgress.updateDisplay()
SkillProgress.hideInActionBar = listOf(component)
Expand Down Expand Up @@ -243,9 +244,10 @@ object SkillAPI {
add("- CustomGoalLevel: ${skillInfo.customGoalLevel}\n")
}

private fun runTimer(skillName: String, info: SkillXPInfo) {
fixedRateTimer(name = "skyhanni-skillprogress-timer-$skillName", initialDelay = 1_000L, period = 1_000L) {
if (info.shouldStartTimer) cancel()
// TODO only use one statuc timer for the whole feature. this timer just ticks the currently active skill.
private fun runTimer(skillType: SkillType, info: SkillXPInfo): Timer =
fixedRateTimer(name = "skyhanni-skillprogress-timer-${skillType.displayName}", initialDelay = 1_000L, period = 1_000L) {
if (skillType.timer != this) cancel()
val time = when (activeSkill) {
SkillType.FARMING -> SkillProgress.etaConfig.farmingPauseTime
SkillType.MINING -> SkillProgress.etaConfig.miningPauseTime
Expand All @@ -261,7 +263,6 @@ object SkillAPI {
info.timeActive++
}
}
}

private fun handleSkillPattern(matcher: Matcher, skillType: SkillType, skillInfo: SkillInfo) {
val currentXp = matcher.group("current").formatNumber()
Expand Down Expand Up @@ -520,6 +521,5 @@ object SkillAPI {
var isActive: Boolean = false,
var lastUpdate: SimpleTimeMark = SimpleTimeMark.farPast(),
var timeActive: Long = 0L,
var shouldStartTimer: Boolean = true,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ object SkillProgress {
add(Renderable.clickAndHover("§7Session: §e$session ${if (xpInfo.sessionTimerActive) "" else "§c(PAUSED)"}",
listOf("§eClick to reset!")) {
xpInfo.sessionTimerActive = false
xpInfo.shouldStartTimer = true
activeSkill.timer = null
xpInfo.timeActive = 0L
chat("Timer for §b${activeSkill.displayName} §ehas been reset!")
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import net.minecraft.init.Blocks
import net.minecraft.init.Items
import net.minecraft.item.Item
import net.minecraft.item.ItemStack
import java.util.Timer

enum class SkillType(val displayName: String, icon: Item) {
COMBAT("Combat", Items.golden_sword),
Expand All @@ -19,6 +20,8 @@ enum class SkillType(val displayName: String, icon: Item) {
TAMING("Taming", Items.spawn_egg),
;

var timer: Timer? = null

constructor(displayName: String, block: Block) : this(displayName, Item.getItemFromBlock(block))

val item: ItemStack by lazy { Utils.createItemStack(icon, displayName) }
Expand Down

0 comments on commit ce3ab82

Please sign in to comment.