From ade2c0499dde8603a5325cc1c22e0c0f537a5c59 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Wed, 12 Mar 2025 18:42:04 +0700 Subject: [PATCH] Move Item Monitor to new container API --- .../entity/storage/ItemMonitorBlockEntity.kt | 27 +++++++++---------- .../mc/otm/container/IEnhancedContainer.kt | 10 +++++++ .../mc/otm/core/nbt/CompoundTagExt.kt | 10 +++++++ 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/storage/ItemMonitorBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/storage/ItemMonitorBlockEntity.kt index 41f92f2e7..946bc3551 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/storage/ItemMonitorBlockEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/storage/ItemMonitorBlockEntity.kt @@ -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.config.MachinesConfig 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.util.slotIterator import ru.dbotthepony.mc.otm.core.TranslatableComponent import ru.dbotthepony.mc.otm.core.collect.map import ru.dbotthepony.mc.otm.core.collect.toList 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.set 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 lastCraftingRecipe(ply: Player) = lastCraftingRecipe[ply] - // a lot of code is hardcoded to take CraftingContainer as it's input - // hence we are forced to work around this by providing proxy container - val craftingGrid = object : MatteryCraftingContainer(::markDirtyFast, 3, 3) { - override fun setChanged(slot: Int, new: ItemStack, old: ItemStack) { - markDirtyFast() + val craftingGrid = IEnhancedCraftingContainer.Wrapper(EnhancedContainer.withListener(3 * 3) { + markDirtyFast() - if (!inProcessOfCraft) { - scanCraftingGrid(false) - } + if (!inProcessOfCraft) { + scanCraftingGrid(false) } - }.also(::addDroppableContainer) + }, 3, 3).also(::addDroppableContainer) private fun scanCraftingGrid(justCheckForRecipeChange: Boolean): Boolean { 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}" } - val combinedInventory = craftingPlayer.matteryPlayer?.inventoryAndExopack - val copy = craftingGrid.iterator(false).map { it.copy() }.toList() + val combinedInventory = craftingPlayer.matteryPlayer.inventoryAndExopack + val copy = craftingGrid.parent.copyToList() // удаляем по одному предмету из сетки крафта 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 = combinedInventory?.addItem(remaining, false) ?: remaining + remaining = combinedInventory.addItem(remaining, false) if (remaining.isNotEmpty) { 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) { @@ -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) } - craftingGrid.deserializeNBT(registry, nbt["crafting_grid"]) + nbt.map("crafting_grid", craftingGrid.parent::deserializeNBT, registry) } fun getSettings(ply: ServerPlayer): ItemMonitorPlayerSettings { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/container/IEnhancedContainer.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/container/IEnhancedContainer.kt index 1c2a1994b..795e2e481 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/container/IEnhancedContainer.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/container/IEnhancedContainer.kt @@ -216,6 +216,16 @@ interface IEnhancedContainer : IContainer, RecipeInput, Iterable, Sta return list } + fun copyToList(): MutableList { + val list = ArrayList(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 { if (stack.isEmpty || slots.isEmpty()) return stack diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/nbt/CompoundTagExt.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/nbt/CompoundTagExt.kt index 9387c88e2..0370d3536 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/nbt/CompoundTagExt.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/nbt/CompoundTagExt.kt @@ -59,6 +59,16 @@ inline fun CompoundTag.map(key: String, consumer: (T) -> R return null } +inline fun 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 CompoundTag.mapPresent(key: String, consumer: (T) -> R): R? { val tag = get(key)