From 4e32bfb5109ac5e06550ccd6db395f4dc65ce387 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 14 Jul 2024 11:25:54 -0700 Subject: [PATCH] Prepare for LibBlockAttributes removal --- .../phycon/block/inserter/InserterBlockEntity.kt | 4 ++-- .../phycon/block/miner/MinerBlockEntity.kt | 4 ++-- .../phycon/block/netinterface/InterfaceBlock.kt | 9 +-------- .../phycon/block/netswitch/SwitchBlock.kt | 10 +--------- .../block/terminal/AbstractTerminalBlockEntity.kt | 5 ++--- .../block/terminal/CraftingTerminalBlockEntity.kt | 7 +++---- .../block/terminal/TerminalBufferInventory.kt | 4 ++-- .../phycon/component/NetworkStackDispatcher.kt | 4 ++-- .../phycon/packet/ReadGroupedInventoryPacket.kt | 1 + .../phycon/util/BasicInventoryExtensions.kt | 3 +-- .../net/shadowfacts/phycon/util/ItemStackUtils.kt | 13 +++++++++++++ 11 files changed, 30 insertions(+), 34 deletions(-) create mode 100644 src/main/kotlin/net/shadowfacts/phycon/util/ItemStackUtils.kt diff --git a/src/main/kotlin/net/shadowfacts/phycon/block/inserter/InserterBlockEntity.kt b/src/main/kotlin/net/shadowfacts/phycon/block/inserter/InserterBlockEntity.kt index 8fbc9ef..346644c 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/block/inserter/InserterBlockEntity.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/block/inserter/InserterBlockEntity.kt @@ -4,7 +4,6 @@ import alexiil.mc.lib.attributes.SearchOptions import alexiil.mc.lib.attributes.Simulation import alexiil.mc.lib.attributes.item.ItemAttributes import alexiil.mc.lib.attributes.item.ItemInsertable -import alexiil.mc.lib.attributes.item.ItemStackUtil import net.minecraft.block.BlockState import net.minecraft.item.ItemStack import net.minecraft.nbt.NbtCompound @@ -22,6 +21,7 @@ import net.shadowfacts.phycon.packet.* import net.shadowfacts.phycon.util.ActivationMode import net.shadowfacts.phycon.util.ClientConfigurableDevice import net.shadowfacts.phycon.util.GhostInv +import net.shadowfacts.phycon.util.equalsIgnoringAmount import kotlin.math.min /** @@ -83,7 +83,7 @@ class InserterBlockEntity(pos: BlockPos, state: BlockState) : DeviceBlockEntity( private fun handleStackLocation(packet: StackLocationPacket) { val request = currentRequest - if (request != null && ItemStackUtil.areEqualIgnoreAmounts(request.stack, packet.stack)) { + if (request != null && request.stack.equalsIgnoringAmount(packet.stack)) { request.results.add(packet.amount to packet.stackProvider) if (request.isFinishable(counter)) { finishRequest() diff --git a/src/main/kotlin/net/shadowfacts/phycon/block/miner/MinerBlockEntity.kt b/src/main/kotlin/net/shadowfacts/phycon/block/miner/MinerBlockEntity.kt index c67704d..e47a14b 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/block/miner/MinerBlockEntity.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/block/miner/MinerBlockEntity.kt @@ -1,7 +1,6 @@ package net.shadowfacts.phycon.block.miner import alexiil.mc.lib.attributes.item.GroupedItemInvView -import alexiil.mc.lib.attributes.item.ItemStackUtil import alexiil.mc.lib.attributes.item.filter.ItemFilter import net.minecraft.block.Block import net.minecraft.block.BlockState @@ -22,6 +21,7 @@ import net.shadowfacts.phycon.packet.* import net.shadowfacts.phycon.util.ActivationMode import net.shadowfacts.phycon.util.ClientConfigurableDevice import net.shadowfacts.phycon.util.copyWithCount +import net.shadowfacts.phycon.util.equalsIgnoringAmount import kotlin.math.min /** @@ -89,7 +89,7 @@ class MinerBlockEntity(pos: BlockPos, state: BlockState) : DeviceBlockEntity(Phy if (remaining <= 0) { break } - if (!ItemStackUtil.areEqualIgnoreAmounts(droppedStack, packet.stack)) { + if (!droppedStack.equalsIgnoringAmount(packet.stack)) { continue } diff --git a/src/main/kotlin/net/shadowfacts/phycon/block/netinterface/InterfaceBlock.kt b/src/main/kotlin/net/shadowfacts/phycon/block/netinterface/InterfaceBlock.kt index 2de7dbe..ed7667f 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/block/netinterface/InterfaceBlock.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/block/netinterface/InterfaceBlock.kt @@ -1,7 +1,5 @@ package net.shadowfacts.phycon.block.netinterface -import alexiil.mc.lib.attributes.AttributeList -import alexiil.mc.lib.attributes.AttributeProvider import net.minecraft.block.* import net.minecraft.entity.LivingEntity import net.minecraft.item.ItemStack @@ -23,8 +21,7 @@ class InterfaceBlock : FaceDeviceBlock( .strength(1.5f) .sounds(BlockSoundGroup.METAL) ), - NetworkComponentBlock, - AttributeProvider { + NetworkComponentBlock { companion object { val ID = Identifier(PhysicalConnectivity.MODID, "network_interface") @@ -61,8 +58,4 @@ class InterfaceBlock : FaceDeviceBlock( } } - override fun addAllAttributes(world: World, pos: BlockPos, state: BlockState, to: AttributeList<*>) { - to.offer(getBlockEntity(world, pos)) - } - } diff --git a/src/main/kotlin/net/shadowfacts/phycon/block/netswitch/SwitchBlock.kt b/src/main/kotlin/net/shadowfacts/phycon/block/netswitch/SwitchBlock.kt index 198797d..f4563c9 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/block/netswitch/SwitchBlock.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/block/netswitch/SwitchBlock.kt @@ -1,7 +1,5 @@ package net.shadowfacts.phycon.block.netswitch -import alexiil.mc.lib.attributes.AttributeList -import alexiil.mc.lib.attributes.AttributeProvider import net.minecraft.block.BlockState import net.minecraft.block.Material import net.minecraft.block.entity.BlockEntity @@ -11,7 +9,6 @@ import net.minecraft.sound.BlockSoundGroup import net.minecraft.util.Identifier import net.minecraft.util.math.BlockPos import net.minecraft.util.math.Direction -import net.minecraft.world.BlockView import net.minecraft.world.World import net.minecraft.world.WorldAccess import net.shadowfacts.phycon.PhysicalConnectivity @@ -28,8 +25,7 @@ class SwitchBlock : BlockWithEntity( .strength(1.5f) .sounds(BlockSoundGroup.METAL) ), - NetworkComponentBlock, - AttributeProvider { + NetworkComponentBlock { companion object { val ID = Identifier(PhysicalConnectivity.MODID, "switch") @@ -63,8 +59,4 @@ class SwitchBlock : BlockWithEntity( } } } - - override fun addAllAttributes(world: World, pos: BlockPos, state: BlockState, to: AttributeList<*>) { - to.offer(getBlockEntity(world, pos)) - } } diff --git a/src/main/kotlin/net/shadowfacts/phycon/block/terminal/AbstractTerminalBlockEntity.kt b/src/main/kotlin/net/shadowfacts/phycon/block/terminal/AbstractTerminalBlockEntity.kt index 93221bd..3629b3e 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/block/terminal/AbstractTerminalBlockEntity.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/block/terminal/AbstractTerminalBlockEntity.kt @@ -2,7 +2,6 @@ package net.shadowfacts.phycon.block.terminal import alexiil.mc.lib.attributes.item.GroupedItemInvView import alexiil.mc.lib.attributes.item.ItemStackCollections -import alexiil.mc.lib.attributes.item.ItemStackUtil import net.minecraft.block.BlockState import net.minecraft.block.entity.BlockEntityType import net.minecraft.entity.player.PlayerEntity @@ -18,9 +17,9 @@ import net.shadowfacts.phycon.api.packet.Packet import net.shadowfacts.phycon.api.util.IPAddress import net.shadowfacts.phycon.block.DeviceBlockEntity import net.shadowfacts.phycon.component.* -import net.shadowfacts.phycon.frame.NetworkSplitFrame import net.shadowfacts.phycon.packet.* import net.shadowfacts.phycon.util.NetworkUtil +import net.shadowfacts.phycon.util.equalsIgnoringAmount import java.lang.ref.WeakReference import java.util.* import java.util.function.IntBinaryOperator @@ -98,7 +97,7 @@ abstract class AbstractTerminalBlockEntity(type: BlockEntityType<*>, pos: BlockP private fun handleStackLocation(packet: StackLocationPacket) { val request = pendingRequests.firstOrNull { - ItemStackUtil.areEqualIgnoreAmounts(it.stack, packet.stack) + it.stack.equalsIgnoringAmount(packet.stack) } if (request != null) { request.results.add(packet.amount to packet.stackProvider) diff --git a/src/main/kotlin/net/shadowfacts/phycon/block/terminal/CraftingTerminalBlockEntity.kt b/src/main/kotlin/net/shadowfacts/phycon/block/terminal/CraftingTerminalBlockEntity.kt index 60f9e38..2b4ea54 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/block/terminal/CraftingTerminalBlockEntity.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/block/terminal/CraftingTerminalBlockEntity.kt @@ -1,7 +1,6 @@ package net.shadowfacts.phycon.block.terminal import alexiil.mc.lib.attributes.item.ItemStackCollections -import alexiil.mc.lib.attributes.item.ItemStackUtil import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory import net.minecraft.block.BlockState import net.minecraft.entity.player.PlayerEntity @@ -17,7 +16,7 @@ import net.minecraft.util.math.BlockPos import net.shadowfacts.phycon.init.PhyBlockEntities import net.shadowfacts.phycon.packet.ItemStackPacket import net.shadowfacts.phycon.packet.LocateStackPacket -import net.shadowfacts.phycon.packet.RequestInventoryPacket +import net.shadowfacts.phycon.util.equalsIgnoringAmount import net.shadowfacts.phycon.util.fromTag import net.shadowfacts.phycon.util.toTag import java.util.LinkedList @@ -82,14 +81,14 @@ class CraftingTerminalBlockEntity(pos: BlockPos, state: BlockState) : override fun doHandleItemStack(packet: ItemStackPacket): ItemStack { val craftingReq = - completedCraftingStackRequests.find { ItemStackUtil.areEqualIgnoreAmounts(it.stack, packet.stack) } + completedCraftingStackRequests.find { it.stack.equalsIgnoringAmount(packet.stack) } if (craftingReq != null) { var remaining = packet.stack.copy() for (i in 0 until craftingInv.size()) { val currentStack = craftingInv.getStack(i) if (currentStack.count >= currentStack.maxCount) continue - if (!ItemStackUtil.areEqualIgnoreAmounts(currentStack, remaining)) continue + if (!currentStack.equalsIgnoringAmount(remaining)) continue val toInsert = minOf(remaining.count, currentStack.maxCount - currentStack.count, craftingReq.amountPerSlot[i]) diff --git a/src/main/kotlin/net/shadowfacts/phycon/block/terminal/TerminalBufferInventory.kt b/src/main/kotlin/net/shadowfacts/phycon/block/terminal/TerminalBufferInventory.kt index eb77192..5d9f951 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/block/terminal/TerminalBufferInventory.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/block/terminal/TerminalBufferInventory.kt @@ -1,10 +1,10 @@ package net.shadowfacts.phycon.block.terminal -import alexiil.mc.lib.attributes.item.ItemStackUtil import net.minecraft.inventory.SimpleInventory import net.minecraft.item.ItemStack import net.minecraft.nbt.NbtCompound import net.minecraft.nbt.NbtIntArray +import net.shadowfacts.phycon.util.equalsIgnoringAmount import net.shadowfacts.phycon.util.fromTag import net.shadowfacts.phycon.util.toTag import kotlin.math.min @@ -54,7 +54,7 @@ class TerminalBufferInventory(size: Int) : SimpleInventory(size) { setStack(slot, stack) markSlot(slot, mode) return ItemStack.EMPTY - } else if (ItemStackUtil.areEqualIgnoreAmounts(stack, current)) { + } else if (stack.equalsIgnoringAmount(current)) { val toTransfer = min(current.maxCount - current.count, stack.count) current.count += toTransfer stack.count -= toTransfer diff --git a/src/main/kotlin/net/shadowfacts/phycon/component/NetworkStackDispatcher.kt b/src/main/kotlin/net/shadowfacts/phycon/component/NetworkStackDispatcher.kt index 9d0944c..6803e8c 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/component/NetworkStackDispatcher.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/component/NetworkStackDispatcher.kt @@ -1,6 +1,5 @@ package net.shadowfacts.phycon.component -import alexiil.mc.lib.attributes.item.ItemStackUtil import net.minecraft.block.entity.BlockEntity import net.minecraft.item.ItemStack import net.shadowfacts.phycon.api.util.IPAddress @@ -8,6 +7,7 @@ import net.shadowfacts.phycon.packet.CapacityPacket import net.shadowfacts.phycon.packet.CheckCapacityPacket import net.shadowfacts.phycon.packet.ItemStackPacket import net.shadowfacts.phycon.util.copyWithCount +import net.shadowfacts.phycon.util.equalsIgnoringAmount import kotlin.math.min /** @@ -31,7 +31,7 @@ interface NetworkStackDispatcher - ItemStackUtil.areEqualIgnoreAmounts(packet.stack, insertion.stack) && + packet.stack.equalsIgnoringAmount(insertion.stack) && insertion.results.none { it.second.ipAddress == packet.source } }?.also { insertion -> insertion.results.add(packet.capacity to packet.stackReceiver) diff --git a/src/main/kotlin/net/shadowfacts/phycon/packet/ReadGroupedInventoryPacket.kt b/src/main/kotlin/net/shadowfacts/phycon/packet/ReadGroupedInventoryPacket.kt index 083d6c6..9e1a056 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/packet/ReadGroupedInventoryPacket.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/packet/ReadGroupedInventoryPacket.kt @@ -6,6 +6,7 @@ import net.shadowfacts.phycon.api.util.IPAddress /** * @author shadowfacts */ +@Deprecated("Use Fabric Transfer API") class ReadGroupedInventoryPacket( val inventory: GroupedItemInvView, source: IPAddress, diff --git a/src/main/kotlin/net/shadowfacts/phycon/util/BasicInventoryExtensions.kt b/src/main/kotlin/net/shadowfacts/phycon/util/BasicInventoryExtensions.kt index ac75b64..4ed0bae 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/util/BasicInventoryExtensions.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/util/BasicInventoryExtensions.kt @@ -1,6 +1,5 @@ package net.shadowfacts.phycon.util -import alexiil.mc.lib.attributes.item.ItemStackUtil import net.minecraft.inventory.SimpleInventory import net.minecraft.item.ItemStack import net.minecraft.nbt.NbtCompound @@ -45,7 +44,7 @@ fun SimpleInventory.insert(stack: ItemStack, slot: Int): ItemStack { if (current.isEmpty) { setStack(slot, stack) return ItemStack.EMPTY - } else if (ItemStackUtil.areEqualIgnoreAmounts(stack, current)) { + } else if (stack.equalsIgnoringAmount(current)) { val toTransfer = min(current.maxCount - current.count, stack.count) current.count += toTransfer stack.count -= toTransfer diff --git a/src/main/kotlin/net/shadowfacts/phycon/util/ItemStackUtils.kt b/src/main/kotlin/net/shadowfacts/phycon/util/ItemStackUtils.kt new file mode 100644 index 0000000..d12d83f --- /dev/null +++ b/src/main/kotlin/net/shadowfacts/phycon/util/ItemStackUtils.kt @@ -0,0 +1,13 @@ +package net.shadowfacts.phycon.util + +import net.minecraft.item.ItemStack + +/** + * @author shadowfacts + */ + +fun ItemStack.equalsIgnoringAmount(other: ItemStack): Boolean { + if (this.isEmpty) return other.isEmpty + if (other.isEmpty) return false + return this.item === other.item && this.nbt == other.nbt; +}