From cd03dcaacb864b0d181040c48dcd23f7e7ccdd68 Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal00212@users.noreply.github.com> Date: Mon, 29 May 2023 15:19:52 +0200 Subject: Added ItemStack.cachedData to store candyUsed and heldItem --- .../skyhanni/features/misc/PetCandyUsedDisplay.kt | 18 +----------------- .../skyhanni/mixins/hooks/ItemStackCachedData.java | 8 ++++++++ .../skyhanni/mixins/transformers/MixinItemStack.java | 16 ++++++++++++++++ .../at/hannibal2/skyhanni/utils/CachedItemData.kt | 6 ++++++ .../skyhanni/utils/SkyBlockItemModifierUtils.kt | 20 ++++++++++++++++++-- 5 files changed, 49 insertions(+), 19 deletions(-) create mode 100644 src/main/java/at/hannibal2/skyhanni/mixins/hooks/ItemStackCachedData.java create mode 100644 src/main/java/at/hannibal2/skyhanni/mixins/transformers/MixinItemStack.java create mode 100644 src/main/java/at/hannibal2/skyhanni/utils/CachedItemData.kt (limited to 'src/main/java/at/hannibal2/skyhanni') 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() - - 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() -- cgit