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

View File

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

View File

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

View File

@ -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<Insertion: NetworkStackDispatcher.PendingInsert
while (!remaining.isEmpty && sortedResults.isNotEmpty()) {
val (capacity, receivingInterface) = sortedResults.removeFirst()
if (capacity <= 0) continue
val copy = remaining.copy()
copy.count = min(capacity, copy.count)
val copy = remaining.copyWithCount(min(capacity, remaining.count))
sendPacket(ItemStackPacket(copy, ipAddress, receivingInterface.ipAddress))
// todo: the destination should confirm how much was actually inserted, in case of race condition
remaining.count -= capacity

View File

@ -14,6 +14,7 @@ import net.minecraft.util.registry.Registry
import net.minecraft.util.registry.RegistryKey
import net.shadowfacts.phycon.PhysicalConnectivity
import net.shadowfacts.phycon.block.terminal.TerminalBlockEntity
import net.shadowfacts.phycon.util.copyWithCount
/**
* @author shadowfacts
@ -30,9 +31,7 @@ object C2STerminalRequestItem: ServerReceiver {
// 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
// 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)

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