Skip to content

Commit

Permalink
Try fix CustomItem cache
Browse files Browse the repository at this point in the history
  • Loading branch information
james58899 committed Jun 30, 2021
1 parent 250a4c6 commit 6238c9c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
11 changes: 5 additions & 6 deletions src/main/kotlin/one/oktw/galaxy/item/CustomItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<ItemStack>()

open fun getName(): Text? = null

Expand All @@ -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)
Expand All @@ -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) }
}
}
1 change: 0 additions & 1 deletion src/main/kotlin/one/oktw/galaxy/util/CustomRegistry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package one.oktw.galaxy.util

import net.minecraft.util.Identifier
import java.util.*
import kotlin.collections.HashMap

open class CustomRegistry<T : Registrable> {
private val registry = HashMap<Identifier, T>()
Expand Down

0 comments on commit 6238c9c

Please sign in to comment.