aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/notenoughupdates/repo/ItemCache.kt
diff options
context:
space:
mode:
authornea <romangraef@gmail.com>2022-07-29 20:51:53 +0200
committernea <romangraef@gmail.com>2022-07-29 20:51:53 +0200
commitf4bf70032a45b4daa7805ca38f2d820645dc9a6b (patch)
treedc91fa8e13b78ac9a7fcda24a26894e430f7fc84 /src/main/kotlin/moe/nea/notenoughupdates/repo/ItemCache.kt
parent386ee78026801bf50d9cba24de541ed087f145d6 (diff)
downloadfirmament-f4bf70032a45b4daa7805ca38f2d820645dc9a6b.tar.gz
firmament-f4bf70032a45b4daa7805ca38f2d820645dc9a6b.tar.bz2
firmament-f4bf70032a45b4daa7805ca38f2d820645dc9a6b.zip
no arch
Diffstat (limited to 'src/main/kotlin/moe/nea/notenoughupdates/repo/ItemCache.kt')
-rw-r--r--src/main/kotlin/moe/nea/notenoughupdates/repo/ItemCache.kt75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/main/kotlin/moe/nea/notenoughupdates/repo/ItemCache.kt b/src/main/kotlin/moe/nea/notenoughupdates/repo/ItemCache.kt
new file mode 100644
index 0000000..aa93fec
--- /dev/null
+++ b/src/main/kotlin/moe/nea/notenoughupdates/repo/ItemCache.kt
@@ -0,0 +1,75 @@
+package moe.nea.notenoughupdates.repo
+
+import com.mojang.serialization.Dynamic
+import io.github.moulberry.repo.IReloadable
+import io.github.moulberry.repo.NEURepository
+import io.github.moulberry.repo.data.NEUItem
+import moe.nea.notenoughupdates.util.LegacyTagParser
+import moe.nea.notenoughupdates.util.appendLore
+import net.minecraft.nbt.CompoundTag
+import net.minecraft.nbt.NbtOps
+import net.minecraft.network.chat.Component
+import net.minecraft.resources.ResourceLocation
+import net.minecraft.util.datafix.DataFixers
+import net.minecraft.util.datafix.fixes.References
+import net.minecraft.world.item.ItemStack
+import net.minecraft.world.item.Items
+import java.util.concurrent.ConcurrentHashMap
+
+object ItemCache : IReloadable {
+ val cache: MutableMap<String, ItemStack> = ConcurrentHashMap()
+ val df = DataFixers.getDataFixer()
+ var isFlawless = true
+
+ private fun NEUItem.get10809CompoundTag(): CompoundTag = CompoundTag().apply {
+ put("tag", LegacyTagParser.parse(nbttag))
+ putString("id", minecraftItemId)
+ putByte("Count", 1)
+ putShort("Damage", damage.toShort())
+ }
+
+ private fun CompoundTag.transformFrom10809ToModern(): CompoundTag? =
+ try {
+ df.update(
+ References.ITEM_STACK,
+ Dynamic(NbtOps.INSTANCE, this),
+ -1,
+ 2975
+ ).value as CompoundTag
+ } catch (e: Exception) {
+ e.printStackTrace()
+ isFlawless = false
+ null
+ }
+
+ private fun NEUItem.asItemStackNow(): ItemStack {
+ val oldItemTag = get10809CompoundTag()
+ val modernItemTag = oldItemTag.transformFrom10809ToModern()
+ ?: return ItemStack(Items.PAINTING).apply {
+ setHoverName(Component.literal(this@asItemStackNow.displayName))
+ appendLore(listOf(Component.literal("Exception rendering item: $skyblockItemId")))
+ }
+ val itemInstance = ItemStack.of(modernItemTag)
+ if (itemInstance.tag?.contains("Enchantments") == true) {
+ itemInstance.enchantmentTags.add(CompoundTag())
+ }
+ return itemInstance
+ }
+
+ fun NEUItem.asItemStack(): ItemStack {
+ var s = cache[this.skyblockItemId]
+ if (s == null) {
+ s = asItemStackNow()
+ cache[this.skyblockItemId] = s
+ }
+ return s
+ }
+
+ fun NEUItem.getResourceLocation() =
+ ResourceLocation("skyblockitem", skyblockItemId.lowercase().replace(";", "__"))
+
+
+ override fun reload(repository: NEURepository) {
+ cache.clear()
+ }
+}