From 6238c9c1c6db38ec7a76302785c725789305b221 Mon Sep 17 00:00:00 2001 From: James58899 Date: Wed, 30 Jun 2021 13:51:51 +0800 Subject: [PATCH] Try fix CustomItem cache --- src/main/kotlin/one/oktw/galaxy/item/CustomItem.kt | 11 +++++------ .../kotlin/one/oktw/galaxy/util/CustomRegistry.kt | 1 - 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/one/oktw/galaxy/item/CustomItem.kt b/src/main/kotlin/one/oktw/galaxy/item/CustomItem.kt index 5d4ca7c13..553b878c3 100644 --- a/src/main/kotlin/one/oktw/galaxy/item/CustomItem.kt +++ b/src/main/kotlin/one/oktw/galaxy/item/CustomItem.kt @@ -26,6 +26,7 @@ import net.minecraft.text.Text import net.minecraft.util.Identifier import one.oktw.galaxy.util.CustomRegistry import one.oktw.galaxy.util.Registrable +import java.util.concurrent.atomic.AtomicReference abstract class CustomItem(override val identifier: Identifier, private val baseItem: Item, private val modelData: Int) : Registrable { companion object { @@ -44,7 +45,7 @@ abstract class CustomItem(override val identifier: Identifier, private val baseI } open val cacheable = true - private lateinit var cacheItemStack: ItemStack + private val cacheItemStack = AtomicReference() open fun getName(): Text? = null @@ -59,9 +60,9 @@ abstract class CustomItem(override val identifier: Identifier, private val baseI } open fun createItemStack(): ItemStack { - if (cacheable && this::cacheItemStack.isInitialized) return cacheItemStack.copy() + if (cacheable) cacheItemStack.get().let { if (it != null) return it.copy() } - val itemStack = ItemStack(baseItem).apply { + return ItemStack(baseItem).apply { orCreateTag.apply { putInt("HideFlags", ItemStack.TooltipSection.values().map(ItemStack.TooltipSection::getFlag).reduce { acc, i -> acc or i }) // ALL putInt("CustomModelData", modelData) @@ -70,8 +71,6 @@ abstract class CustomItem(override val identifier: Identifier, private val baseI } setCustomName(this@CustomItem.getName()) writeCustomNbt(getOrCreateSubTag("GalaxyData")) - } - - return if (cacheable) itemStack.also { cacheItemStack = it } else itemStack + }.also { if (cacheable) cacheItemStack.set(it) } } } diff --git a/src/main/kotlin/one/oktw/galaxy/util/CustomRegistry.kt b/src/main/kotlin/one/oktw/galaxy/util/CustomRegistry.kt index 74f555397..710697e55 100644 --- a/src/main/kotlin/one/oktw/galaxy/util/CustomRegistry.kt +++ b/src/main/kotlin/one/oktw/galaxy/util/CustomRegistry.kt @@ -20,7 +20,6 @@ package one.oktw.galaxy.util import net.minecraft.util.Identifier import java.util.* -import kotlin.collections.HashMap open class CustomRegistry { private val registry = HashMap()