From ecb40c801ab44118059c1ef07438dfacd75e3927 Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal00212@users.noreply.github.com> Date: Tue, 15 Aug 2023 14:10:59 +0200 Subject: code cleanup and only detecting neu setting once per second --- .../hannibal2/skyhanni/features/misc/ChestValue.kt | 40 ++++++++++++++-------- .../hannibal2/skyhanni/utils/RecalculatingValue.kt | 16 +++++++++ 2 files changed, 42 insertions(+), 14 deletions(-) create mode 100644 src/main/java/at/hannibal2/skyhanni/utils/RecalculatingValue.kt (limited to 'src') diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/ChestValue.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/ChestValue.kt index cced77692..f603734f8 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/ChestValue.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/ChestValue.kt @@ -24,6 +24,7 @@ import net.minecraftforge.fml.common.eventhandler.EventPriority import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import java.awt.Color import java.io.File +import kotlin.time.Duration.Companion.seconds class ChestValue { @@ -123,7 +124,8 @@ class ChestValue { text, tips, stack = stack, - indexes = index) + indexes = index + ) add(" ยง7- ") if (config.showStacks) add(stack) add(renderable) @@ -221,19 +223,29 @@ class ChestValue { COMPACT("Aligned") } - private fun String.isValidStorage() = Minecraft.getMinecraft().currentScreen is GuiChest && (( - this == "Chest" || - this == "Large Chest") || - (contains("Minion") && !contains("Recipe") && LorenzUtils.skyBlockIsland == IslandType.PRIVATE_ISLAND) || - this == "Personal Vault") || - ((contains("Backpack") && contains("Slot #") || startsWith("Ender Chest (")) - && !isNeuStorageEnabled()) - - private fun isNeuStorageEnabled(): Boolean { - val file = File(OtherMod.NEU.configPath) - if (!file.exists()) return false - return ConfigManager.gson.fromJson(APIUtil.readFile(File(OtherMod.NEU.configPath)), - com.google.gson.JsonObject::class.java)["storageGUI"].asJsonObject["enableStorageGUI3"].asBoolean + private fun String.isValidStorage(): Boolean { + if (Minecraft.getMinecraft().currentScreen !is GuiChest) return false + + if ((contains("Backpack") && contains("Slot #") || startsWith("Ender Chest (")) && + !isNeuStorageEnabled.getValue() + ) { + return true + } + + val inMinion = contains("Minion") && !contains("Recipe") && + LorenzUtils.skyBlockIsland == IslandType.PRIVATE_ISLAND + return this == "Chest" || this == "Large Chest" || inMinion || this == "Personal Vault" + } + + private val isNeuStorageEnabled = RecalculatingValue(1.seconds) { + val configPath = OtherMod.NEU.configPath + if (File(configPath).exists()) { + val json = ConfigManager.gson.fromJson( + APIUtil.readFile(File(configPath)), + com.google.gson.JsonObject::class.java + ) + json["storageGUI"].asJsonObject["enableStorageGUI3"].asBoolean + } else false } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/RecalculatingValue.kt b/src/main/java/at/hannibal2/skyhanni/utils/RecalculatingValue.kt new file mode 100644 index 000000000..7c734c57e --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/utils/RecalculatingValue.kt @@ -0,0 +1,16 @@ +package at.hannibal2.skyhanni.utils + +import kotlin.time.Duration + +class RecalculatingValue(private val expireTime: Duration, val calculation: () -> T) { + private var currentValue = calculation() + private var lastAccessTime = SimpleTimeMark.farPast() + + fun getValue(): T { + if (lastAccessTime.passedSince() > expireTime) { + currentValue = calculation() + lastAccessTime = SimpleTimeMark.now() + } + return currentValue + } +} \ No newline at end of file -- cgit