From 9aa10779772f75a90faaa13558d9602615d793da Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 1 Mar 2021 21:29:14 -0500 Subject: [PATCH] Add copyWithCount helper --- .../phycon/block/inserter/InserterScreenHandler.kt | 9 +++------ .../shadowfacts/phycon/block/miner/MinerBlockEntity.kt | 4 ++-- .../phycon/block/terminal/TerminalScreenHandler.kt | 5 ++--- .../phycon/component/NetworkStackDispatcher.kt | 4 ++-- .../phycon/networking/C2STerminalRequestItem.kt | 5 ++--- .../kotlin/net/shadowfacts/phycon/util/ItemStack.kt | 10 ++++++++++ 6 files changed, 21 insertions(+), 16 deletions(-) create mode 100644 src/main/kotlin/net/shadowfacts/phycon/util/ItemStack.kt diff --git a/src/main/kotlin/net/shadowfacts/phycon/block/inserter/InserterScreenHandler.kt b/src/main/kotlin/net/shadowfacts/phycon/block/inserter/InserterScreenHandler.kt index dbaa515..81a4444 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/block/inserter/InserterScreenHandler.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/block/inserter/InserterScreenHandler.kt @@ -12,6 +12,7 @@ import net.shadowfacts.phycon.PhysicalConnectivity import net.shadowfacts.phycon.init.PhyBlocks import net.shadowfacts.phycon.init.PhyScreens import net.shadowfacts.phycon.util.GhostSlot +import net.shadowfacts.phycon.util.copyWithCount import kotlin.math.min /** @@ -65,9 +66,7 @@ class InserterScreenHandler( if (player.inventory.cursorStack.isEmpty) { inserter.stackToExtract = ItemStack.EMPTY } else { - val copy = player.inventory.cursorStack.copy() - copy.count = 1 - inserter.stackToExtract = copy + inserter.stackToExtract = player.inventory.cursorStack.copyWithCount(1) } stackToExtractChanged() } @@ -76,9 +75,7 @@ class InserterScreenHandler( override fun transferSlot(player: PlayerEntity, slotId: Int): ItemStack { val slot = slots[slotId] - val copy = slot.stack.copy() - copy.count = 1 - inserter.stackToExtract = copy + inserter.stackToExtract = slot.stack.copyWithCount(1) stackToExtractChanged() return ItemStack.EMPTY } 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 58aeabf..c4b8fc4 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/block/miner/MinerBlockEntity.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/block/miner/MinerBlockEntity.kt @@ -18,6 +18,7 @@ import net.shadowfacts.phycon.component.NetworkStackDispatcher import net.shadowfacts.phycon.component.NetworkStackProvider import net.shadowfacts.phycon.component.handleItemStack import net.shadowfacts.phycon.packet.* +import net.shadowfacts.phycon.util.copyWithCount import kotlin.math.min /** @@ -71,14 +72,13 @@ class MinerBlockEntity: DeviceBlockEntity(PhyBlockEntities.MINER), if (!ItemStackUtil.areEqualIgnoreAmounts(droppedStack, packet.stack)) { continue } - val copy = droppedStack.copy() val toDecr = min(droppedStack.count, remaining) + val copy = droppedStack.copyWithCount(toDecr) droppedStack.decrement(toDecr) remaining -= toDecr // todo: should this try to combine stacks and send as few packets as possible? - copy.count = toDecr sendPacket(ItemStackPacket(copy, ipAddress, packet.source)) } diff --git a/src/main/kotlin/net/shadowfacts/phycon/block/terminal/TerminalScreenHandler.kt b/src/main/kotlin/net/shadowfacts/phycon/block/terminal/TerminalScreenHandler.kt index e5c592a..4cfe6ff 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/block/terminal/TerminalScreenHandler.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/block/terminal/TerminalScreenHandler.kt @@ -15,6 +15,7 @@ import net.shadowfacts.phycon.init.PhyBlocks import net.shadowfacts.phycon.init.PhyScreens import net.shadowfacts.phycon.networking.S2CTerminalUpdateDisplayedItems import net.shadowfacts.phycon.util.SortMode +import net.shadowfacts.phycon.util.copyWithCount import java.lang.ref.WeakReference import kotlin.math.min @@ -37,9 +38,7 @@ class TerminalScreenHandler(syncId: Int, val playerInv: PlayerInventory, val ter field = value if (terminal.world!!.isClient) { itemsForDisplay = value.map { - val stack = it.stack.copy() - stack.count = it.amount - stack + it.stack.copyWithCount(it.amount) } } } diff --git a/src/main/kotlin/net/shadowfacts/phycon/component/NetworkStackDispatcher.kt b/src/main/kotlin/net/shadowfacts/phycon/component/NetworkStackDispatcher.kt index 0e76b57..9c9ae3d 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/component/NetworkStackDispatcher.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/component/NetworkStackDispatcher.kt @@ -7,6 +7,7 @@ import net.shadowfacts.phycon.api.util.IPAddress 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 kotlin.math.min /** @@ -50,8 +51,7 @@ interface NetworkStackDispatcher 127 will result in // an overflow and be negative on the receiving side. - val stackCopy = stack.copy() - stackCopy.count = 1 - buf.writeItemStack(stackCopy) + buf.writeItemStack(stack.copyWithCount(1)) buf.writeVarInt(amount) return ClientPlayNetworking.createC2SPacket(CHANNEL, buf) diff --git a/src/main/kotlin/net/shadowfacts/phycon/util/ItemStack.kt b/src/main/kotlin/net/shadowfacts/phycon/util/ItemStack.kt new file mode 100644 index 0000000..47046e2 --- /dev/null +++ b/src/main/kotlin/net/shadowfacts/phycon/util/ItemStack.kt @@ -0,0 +1,10 @@ +package net.shadowfacts.phycon.util + +import net.minecraft.item.ItemStack + +/** + * @author shadowfacts + */ +fun ItemStack.copyWithCount(count: Int): ItemStack { + return copy().also { it.count = count } +}