Add copyWithCount helper

This commit is contained in:
Shadowfacts 2021-03-01 21:29:14 -05:00
parent c976c3f607
commit 9aa1077977
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
6 changed files with 21 additions and 16 deletions

View File

@ -12,6 +12,7 @@ import net.shadowfacts.phycon.PhysicalConnectivity
import net.shadowfacts.phycon.init.PhyBlocks import net.shadowfacts.phycon.init.PhyBlocks
import net.shadowfacts.phycon.init.PhyScreens import net.shadowfacts.phycon.init.PhyScreens
import net.shadowfacts.phycon.util.GhostSlot import net.shadowfacts.phycon.util.GhostSlot
import net.shadowfacts.phycon.util.copyWithCount
import kotlin.math.min import kotlin.math.min
/** /**
@ -65,9 +66,7 @@ class InserterScreenHandler(
if (player.inventory.cursorStack.isEmpty) { if (player.inventory.cursorStack.isEmpty) {
inserter.stackToExtract = ItemStack.EMPTY inserter.stackToExtract = ItemStack.EMPTY
} else { } else {
val copy = player.inventory.cursorStack.copy() inserter.stackToExtract = player.inventory.cursorStack.copyWithCount(1)
copy.count = 1
inserter.stackToExtract = copy
} }
stackToExtractChanged() stackToExtractChanged()
} }
@ -76,9 +75,7 @@ class InserterScreenHandler(
override fun transferSlot(player: PlayerEntity, slotId: Int): ItemStack { override fun transferSlot(player: PlayerEntity, slotId: Int): ItemStack {
val slot = slots[slotId] val slot = slots[slotId]
val copy = slot.stack.copy() inserter.stackToExtract = slot.stack.copyWithCount(1)
copy.count = 1
inserter.stackToExtract = copy
stackToExtractChanged() stackToExtractChanged()
return ItemStack.EMPTY return ItemStack.EMPTY
} }

View File

@ -18,6 +18,7 @@ import net.shadowfacts.phycon.component.NetworkStackDispatcher
import net.shadowfacts.phycon.component.NetworkStackProvider import net.shadowfacts.phycon.component.NetworkStackProvider
import net.shadowfacts.phycon.component.handleItemStack import net.shadowfacts.phycon.component.handleItemStack
import net.shadowfacts.phycon.packet.* import net.shadowfacts.phycon.packet.*
import net.shadowfacts.phycon.util.copyWithCount
import kotlin.math.min import kotlin.math.min
/** /**
@ -71,14 +72,13 @@ class MinerBlockEntity: DeviceBlockEntity(PhyBlockEntities.MINER),
if (!ItemStackUtil.areEqualIgnoreAmounts(droppedStack, packet.stack)) { if (!ItemStackUtil.areEqualIgnoreAmounts(droppedStack, packet.stack)) {
continue continue
} }
val copy = droppedStack.copy()
val toDecr = min(droppedStack.count, remaining) val toDecr = min(droppedStack.count, remaining)
val copy = droppedStack.copyWithCount(toDecr)
droppedStack.decrement(toDecr) droppedStack.decrement(toDecr)
remaining -= toDecr remaining -= toDecr
// todo: should this try to combine stacks and send as few packets as possible? // todo: should this try to combine stacks and send as few packets as possible?
copy.count = toDecr
sendPacket(ItemStackPacket(copy, ipAddress, packet.source)) sendPacket(ItemStackPacket(copy, ipAddress, packet.source))
} }

View File

@ -15,6 +15,7 @@ import net.shadowfacts.phycon.init.PhyBlocks
import net.shadowfacts.phycon.init.PhyScreens import net.shadowfacts.phycon.init.PhyScreens
import net.shadowfacts.phycon.networking.S2CTerminalUpdateDisplayedItems import net.shadowfacts.phycon.networking.S2CTerminalUpdateDisplayedItems
import net.shadowfacts.phycon.util.SortMode import net.shadowfacts.phycon.util.SortMode
import net.shadowfacts.phycon.util.copyWithCount
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
import kotlin.math.min import kotlin.math.min
@ -37,9 +38,7 @@ class TerminalScreenHandler(syncId: Int, val playerInv: PlayerInventory, val ter
field = value field = value
if (terminal.world!!.isClient) { if (terminal.world!!.isClient) {
itemsForDisplay = value.map { itemsForDisplay = value.map {
val stack = it.stack.copy() it.stack.copyWithCount(it.amount)
stack.count = it.amount
stack
} }
} }
} }

View File

@ -7,6 +7,7 @@ import net.shadowfacts.phycon.api.util.IPAddress
import net.shadowfacts.phycon.packet.CapacityPacket import net.shadowfacts.phycon.packet.CapacityPacket
import net.shadowfacts.phycon.packet.CheckCapacityPacket import net.shadowfacts.phycon.packet.CheckCapacityPacket
import net.shadowfacts.phycon.packet.ItemStackPacket import net.shadowfacts.phycon.packet.ItemStackPacket
import net.shadowfacts.phycon.util.copyWithCount
import kotlin.math.min import kotlin.math.min
/** /**
@ -50,8 +51,7 @@ interface NetworkStackDispatcher<Insertion: NetworkStackDispatcher.PendingInsert
while (!remaining.isEmpty && sortedResults.isNotEmpty()) { while (!remaining.isEmpty && sortedResults.isNotEmpty()) {
val (capacity, receivingInterface) = sortedResults.removeFirst() val (capacity, receivingInterface) = sortedResults.removeFirst()
if (capacity <= 0) continue if (capacity <= 0) continue
val copy = remaining.copy() val copy = remaining.copyWithCount(min(capacity, remaining.count))
copy.count = min(capacity, copy.count)
sendPacket(ItemStackPacket(copy, ipAddress, receivingInterface.ipAddress)) sendPacket(ItemStackPacket(copy, ipAddress, receivingInterface.ipAddress))
// todo: the destination should confirm how much was actually inserted, in case of race condition // todo: the destination should confirm how much was actually inserted, in case of race condition
remaining.count -= capacity remaining.count -= capacity

View File

@ -14,6 +14,7 @@ import net.minecraft.util.registry.Registry
import net.minecraft.util.registry.RegistryKey import net.minecraft.util.registry.RegistryKey
import net.shadowfacts.phycon.PhysicalConnectivity import net.shadowfacts.phycon.PhysicalConnectivity
import net.shadowfacts.phycon.block.terminal.TerminalBlockEntity import net.shadowfacts.phycon.block.terminal.TerminalBlockEntity
import net.shadowfacts.phycon.util.copyWithCount
/** /**
* @author shadowfacts * @author shadowfacts
@ -30,9 +31,7 @@ object C2STerminalRequestItem: ServerReceiver {
// Force the count of the stack to be 1 before serializing for the network because // Force the count of the stack to be 1 before serializing for the network because
// PacketByteBuf serializes the stack count as a byte. Otherwise counts > 127 will result in // PacketByteBuf serializes the stack count as a byte. Otherwise counts > 127 will result in
// an overflow and be negative on the receiving side. // an overflow and be negative on the receiving side.
val stackCopy = stack.copy() buf.writeItemStack(stack.copyWithCount(1))
stackCopy.count = 1
buf.writeItemStack(stackCopy)
buf.writeVarInt(amount) buf.writeVarInt(amount)
return ClientPlayNetworking.createC2SPacket(CHANNEL, buf) return ClientPlayNetworking.createC2SPacket(CHANNEL, buf)

View File

@ -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 }
}