Move Item Monitor to new container API

This commit is contained in:
DBotThePony 2025-03-12 18:42:04 +07:00
parent e699147f9f
commit ade2c0499d
Signed by: DBot
GPG Key ID: DCC23B5715498507
3 changed files with 33 additions and 14 deletions

View File

@ -29,12 +29,15 @@ import ru.dbotthepony.mc.otm.client.render.UVWindingOrder
import ru.dbotthepony.mc.otm.client.render.Widgets8 import ru.dbotthepony.mc.otm.client.render.Widgets8
import ru.dbotthepony.mc.otm.config.MachinesConfig import ru.dbotthepony.mc.otm.config.MachinesConfig
import ru.dbotthepony.mc.otm.container.CombinedContainer import ru.dbotthepony.mc.otm.container.CombinedContainer
import ru.dbotthepony.mc.otm.container.EnhancedContainer
import ru.dbotthepony.mc.otm.container.IEnhancedCraftingContainer
import ru.dbotthepony.mc.otm.container.MatteryCraftingContainer import ru.dbotthepony.mc.otm.container.MatteryCraftingContainer
import ru.dbotthepony.mc.otm.container.util.slotIterator import ru.dbotthepony.mc.otm.container.util.slotIterator
import ru.dbotthepony.mc.otm.core.TranslatableComponent import ru.dbotthepony.mc.otm.core.TranslatableComponent
import ru.dbotthepony.mc.otm.core.collect.map import ru.dbotthepony.mc.otm.core.collect.map
import ru.dbotthepony.mc.otm.core.collect.toList import ru.dbotthepony.mc.otm.core.collect.toList
import ru.dbotthepony.mc.otm.core.isNotEmpty import ru.dbotthepony.mc.otm.core.isNotEmpty
import ru.dbotthepony.mc.otm.core.nbt.map
import ru.dbotthepony.mc.otm.core.nbt.mapString import ru.dbotthepony.mc.otm.core.nbt.mapString
import ru.dbotthepony.mc.otm.core.nbt.set import ru.dbotthepony.mc.otm.core.nbt.set
import ru.dbotthepony.mc.otm.core.util.ItemStorageStackSorter import ru.dbotthepony.mc.otm.core.util.ItemStorageStackSorter
@ -216,17 +219,13 @@ class ItemMonitorBlockEntity(blockPos: BlockPos, blockState: BlockState) : Matte
fun howMuchPlayerCrafted(ply: Player): Int = craftingAmount.getInt(ply) fun howMuchPlayerCrafted(ply: Player): Int = craftingAmount.getInt(ply)
fun lastCraftingRecipe(ply: Player) = lastCraftingRecipe[ply] fun lastCraftingRecipe(ply: Player) = lastCraftingRecipe[ply]
// a lot of code is hardcoded to take CraftingContainer as it's input val craftingGrid = IEnhancedCraftingContainer.Wrapper(EnhancedContainer.withListener(3 * 3) {
// hence we are forced to work around this by providing proxy container markDirtyFast()
val craftingGrid = object : MatteryCraftingContainer(::markDirtyFast, 3, 3) {
override fun setChanged(slot: Int, new: ItemStack, old: ItemStack) {
markDirtyFast()
if (!inProcessOfCraft) { if (!inProcessOfCraft) {
scanCraftingGrid(false) scanCraftingGrid(false)
}
} }
}.also(::addDroppableContainer) }, 3, 3).also(::addDroppableContainer)
private fun scanCraftingGrid(justCheckForRecipeChange: Boolean): Boolean { private fun scanCraftingGrid(justCheckForRecipeChange: Boolean): Boolean {
val level = level ?: return false val level = level ?: return false
@ -346,8 +345,8 @@ class ItemMonitorBlockEntity(blockPos: BlockPos, blockState: BlockState) : Matte
check(residue.size == craftingGrid.containerSize) { "Container and residue list sizes mismatch: ${residue.size} != ${craftingGrid.containerSize}" } check(residue.size == craftingGrid.containerSize) { "Container and residue list sizes mismatch: ${residue.size} != ${craftingGrid.containerSize}" }
val combinedInventory = craftingPlayer.matteryPlayer?.inventoryAndExopack val combinedInventory = craftingPlayer.matteryPlayer.inventoryAndExopack
val copy = craftingGrid.iterator(false).map { it.copy() }.toList() val copy = craftingGrid.parent.copyToList()
// удаляем по одному предмету из сетки крафта // удаляем по одному предмету из сетки крафта
for (slot in 0 until craftingGrid.containerSize) { for (slot in 0 until craftingGrid.containerSize) {
@ -381,7 +380,7 @@ class ItemMonitorBlockEntity(blockPos: BlockPos, blockState: BlockState) : Matte
remaining = poweredView?.insertStack(ItemStorageStack(remaining), false)?.toItemStack() ?: remaining remaining = poweredView?.insertStack(ItemStorageStack(remaining), false)?.toItemStack() ?: remaining
} }
remaining = combinedInventory?.addItem(remaining, false) ?: remaining remaining = combinedInventory.addItem(remaining, false)
if (remaining.isNotEmpty) { if (remaining.isNotEmpty) {
craftingPlayer.spawnAtLocation(remaining) craftingPlayer.spawnAtLocation(remaining)
@ -425,7 +424,7 @@ class ItemMonitorBlockEntity(blockPos: BlockPos, blockState: BlockState) : Matte
} }
}) })
nbt["crafting_grid"] = craftingGrid.serializeNBT(registry) nbt["crafting_grid"] = craftingGrid.parent.serializeNBT(registry)
} }
override fun loadAdditional(nbt: CompoundTag, registry: HolderLookup.Provider) { override fun loadAdditional(nbt: CompoundTag, registry: HolderLookup.Provider) {
@ -438,7 +437,7 @@ class ItemMonitorBlockEntity(blockPos: BlockPos, blockState: BlockState) : Matte
check(this.settings.put(UUID.fromString(key), ItemMonitorPlayerSettings().also { it.deserializeNBT(registry, settings.getCompound(key)) }) == null) check(this.settings.put(UUID.fromString(key), ItemMonitorPlayerSettings().also { it.deserializeNBT(registry, settings.getCompound(key)) }) == null)
} }
craftingGrid.deserializeNBT(registry, nbt["crafting_grid"]) nbt.map("crafting_grid", craftingGrid.parent::deserializeNBT, registry)
} }
fun getSettings(ply: ServerPlayer): ItemMonitorPlayerSettings { fun getSettings(ply: ServerPlayer): ItemMonitorPlayerSettings {

View File

@ -216,6 +216,16 @@ interface IEnhancedContainer : IContainer, RecipeInput, Iterable<ItemStack>, Sta
return list return list
} }
fun copyToList(): MutableList<ItemStack> {
val list = ArrayList<ItemStack>(containerSize)
for (i in 0 until containerSize) {
list.add(this[i].copy())
}
return list
}
fun addItem(stack: ItemStack, simulate: Boolean, slots: IntCollection = slotRange, onlyIntoExisting: Boolean = false, popTime: Int? = null): ItemStack { fun addItem(stack: ItemStack, simulate: Boolean, slots: IntCollection = slotRange, onlyIntoExisting: Boolean = false, popTime: Int? = null): ItemStack {
if (stack.isEmpty || slots.isEmpty()) if (stack.isEmpty || slots.isEmpty())
return stack return stack

View File

@ -59,6 +59,16 @@ inline fun <R, reified T : Tag?> CompoundTag.map(key: String, consumer: (T) -> R
return null return null
} }
inline fun <R, reified T : Tag?, A0> CompoundTag.map(key: String, consumer: (A0, T) -> R, arg0: A0): R? {
val tag = get(key)
if (tag is T) {
return consumer(arg0, tag)
}
return null
}
inline fun <R, reified T : Tag> CompoundTag.mapPresent(key: String, consumer: (T) -> R): R? { inline fun <R, reified T : Tag> CompoundTag.mapPresent(key: String, consumer: (T) -> R): R? {
val tag = get(key) val tag = get(key)