aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/java/at/hannibal2/skyhanni/features/misc/PetCandyUsedDisplay.kt18
-rw-r--r--src/main/java/at/hannibal2/skyhanni/mixins/hooks/ItemStackCachedData.java8
-rw-r--r--src/main/java/at/hannibal2/skyhanni/mixins/transformers/MixinItemStack.java16
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/CachedItemData.kt6
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/SkyBlockItemModifierUtils.kt20
-rw-r--r--src/main/resources/mixins.skyhanni.json1
6 files changed, 50 insertions, 19 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/PetCandyUsedDisplay.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/PetCandyUsedDisplay.kt
index ec98aa655..5cfcd881d 100644
--- a/src/main/java/at/hannibal2/skyhanni/features/misc/PetCandyUsedDisplay.kt
+++ b/src/main/java/at/hannibal2/skyhanni/features/misc/PetCandyUsedDisplay.kt
@@ -4,25 +4,10 @@ import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.events.GuiRenderItemEvent
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.getPetCandyUsed
-import com.google.common.cache.CacheBuilder
import net.minecraft.client.renderer.GlStateManager
-import net.minecraft.item.ItemStack
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
-import java.util.concurrent.TimeUnit
class PetCandyUsedDisplay {
- private var cache =
- CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.SECONDS).build<ItemStack, Int>()
-
- private fun getCachedPetCandyUsed(stack: ItemStack): Int? {
- cache.getIfPresent(stack)?.let {
- if (it == -1) return null
- return it
- }
- val candyUsed = stack.getPetCandyUsed()
- cache.put(stack, candyUsed ?: -1)
- return candyUsed
- }
@SubscribeEvent
fun onRenderItemOverlayPost(event: GuiRenderItemEvent.RenderOverlayEvent.GuiRenderItemPost) {
@@ -31,8 +16,7 @@ class PetCandyUsedDisplay {
if (!SkyHanniMod.feature.misc.petCandyUsed) return
- val petCandyUsed = getCachedPetCandyUsed(stack) ?: return
-// val petCandyUsed = stack.getPetCandyUsed() ?: return
+ val petCandyUsed = stack.getPetCandyUsed() ?: return
if (petCandyUsed == 0) return
val stackTip = "§c$petCandyUsed"
diff --git a/src/main/java/at/hannibal2/skyhanni/mixins/hooks/ItemStackCachedData.java b/src/main/java/at/hannibal2/skyhanni/mixins/hooks/ItemStackCachedData.java
new file mode 100644
index 000000000..8fa4fe998
--- /dev/null
+++ b/src/main/java/at/hannibal2/skyhanni/mixins/hooks/ItemStackCachedData.java
@@ -0,0 +1,8 @@
+package at.hannibal2.skyhanni.mixins.hooks;
+
+import at.hannibal2.skyhanni.utils.CachedItemData;
+
+public interface ItemStackCachedData {
+
+ CachedItemData getSkyhanni_cachedData();
+}
diff --git a/src/main/java/at/hannibal2/skyhanni/mixins/transformers/MixinItemStack.java b/src/main/java/at/hannibal2/skyhanni/mixins/transformers/MixinItemStack.java
new file mode 100644
index 000000000..9f78bc8dc
--- /dev/null
+++ b/src/main/java/at/hannibal2/skyhanni/mixins/transformers/MixinItemStack.java
@@ -0,0 +1,16 @@
+package at.hannibal2.skyhanni.mixins.transformers;
+
+import at.hannibal2.skyhanni.mixins.hooks.ItemStackCachedData;
+import at.hannibal2.skyhanni.utils.CachedItemData;
+import net.minecraft.item.ItemStack;
+import org.spongepowered.asm.mixin.Mixin;
+
+@Mixin(ItemStack.class)
+public class MixinItemStack implements ItemStackCachedData {
+
+ public CachedItemData skyhanni_cachedData = new CachedItemData();
+
+ public CachedItemData getSkyhanni_cachedData() {
+ return skyhanni_cachedData;
+ }
+}
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/CachedItemData.kt b/src/main/java/at/hannibal2/skyhanni/utils/CachedItemData.kt
new file mode 100644
index 000000000..01cc55006
--- /dev/null
+++ b/src/main/java/at/hannibal2/skyhanni/utils/CachedItemData.kt
@@ -0,0 +1,6 @@
+package at.hannibal2.skyhanni.utils
+
+data class CachedItemData(
+ /** -1 = not loaded */ var petCandies: Int? = -1,
+ /** "" = not loaded */ var heldItem: String? = null,
+) \ No newline at end of file
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/SkyBlockItemModifierUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/SkyBlockItemModifierUtils.kt
index 83c28c099..d9f121951 100644
--- a/src/main/java/at/hannibal2/skyhanni/utils/SkyBlockItemModifierUtils.kt
+++ b/src/main/java/at/hannibal2/skyhanni/utils/SkyBlockItemModifierUtils.kt
@@ -1,6 +1,7 @@
package at.hannibal2.skyhanni.utils
import at.hannibal2.skyhanni.config.ConfigManager
+import at.hannibal2.skyhanni.mixins.hooks.ItemStackCachedData
import at.hannibal2.skyhanni.utils.ItemUtils.getInternalName
import at.hannibal2.skyhanni.utils.ItemUtils.name
import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher
@@ -34,13 +35,28 @@ object SkyBlockItemModifierUtils {
fun ItemStack.getManaDisintegrators() = getAttributeInt("mana_disintegrator_count")
- fun ItemStack.getPetCandyUsed() = getPetInfo()?.get("candyUsed")?.asInt
+ fun ItemStack.getPetCandyUsed(): Int? {
+ val data = cachedData
+ if (data.petCandies == -1) {
+ data.petCandies = getPetInfo()?.get("candyUsed")?.asInt
+ }
+ return data.petCandies
+ }
- fun ItemStack.getPetItem() = getPetInfo()?.get("heldItem")?.asString
+ fun ItemStack.getPetItem(): String? {
+ val data = cachedData
+ if (data.heldItem == "") {
+ data.heldItem = getPetInfo()?.get("heldItem")?.asString
+ }
+ return data.heldItem
+ }
private fun ItemStack.getPetInfo() =
ConfigManager.gson.fromJson(getExtraAttributes()?.getString("petInfo"), JsonObject::class.java)
+ @Suppress("CAST_NEVER_SUCCEEDS")
+ inline val ItemStack.cachedData get() = (this as ItemStackCachedData).skyhanni_cachedData
+
fun ItemStack.getPetLevel(): Int {
petLevelPattern.matchMatcher(this.displayName) {
return group("level").toInt()
diff --git a/src/main/resources/mixins.skyhanni.json b/src/main/resources/mixins.skyhanni.json
index 91b8b21ff..d9976cab5 100644
--- a/src/main/resources/mixins.skyhanni.json
+++ b/src/main/resources/mixins.skyhanni.json
@@ -8,6 +8,7 @@
"MixinEntityBlaze",
"MixinEntityFireball",
"MixinEntityPlayerSP",
+ "MixinItemStack",
"MixinNetHandlerPlayClient",
"MixinNetworkManager",
"MixinRenderItem",