Change terminal to read inventories instead of stacks directly

This is slightly more efficient, and removes the need for interfaces to
observe their inventories for changes (something that's not possible in
Vanilla).
This commit is contained in:
Shadowfacts 2019-10-29 11:25:51 -04:00
parent 2cc9d592b2
commit 0c5e353525
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
5 changed files with 29 additions and 36 deletions

View File

@ -3,13 +3,12 @@ package net.shadowfacts.phycon.network.block.netinterface
import alexiil.mc.lib.attributes.SearchOptions
import alexiil.mc.lib.attributes.item.GroupedItemInv
import alexiil.mc.lib.attributes.item.ItemAttributes
import net.minecraft.item.ItemStack
import net.minecraft.util.math.Direction
import net.shadowfacts.phycon.api.packet.Packet
import net.shadowfacts.phycon.init.PhyBlockEntities
import net.shadowfacts.phycon.network.DeviceBlockEntity
import net.shadowfacts.phycon.network.packet.ReadAllPacket
import net.shadowfacts.phycon.network.packet.RequestReadAllPacket
import net.shadowfacts.phycon.network.packet.ReadInventoryPacket
import net.shadowfacts.phycon.network.packet.RequestInventoryPacket
/**
* @author shadowfacts
@ -30,22 +29,17 @@ class InterfaceBlockEntity: DeviceBlockEntity(PhyBlockEntities.INTERFACE) {
override fun handlePacket(packet: Packet) {
when (packet) {
is RequestReadAllPacket -> handleReadAll(packet)
is RequestInventoryPacket -> handleRequestInventory(packet)
}
}
fun handleReadAll(packet: RequestReadAllPacket) {
enqueueToSingle(ReadAllPacket(readAll(), macAddress, packet.source))
}
fun readAll(): Map<ItemStack, Int> {
private fun handleRequestInventory(packet: RequestInventoryPacket) {
// if we don't have an inventory, try to get one
// this happens when readAll is called before a neighbor state changes, such as immediately after world load
if (inventory == null) updateInventory()
return inventory?.let {
it.storedStacks.associateWith(it::getAmount)
} ?: mapOf()
inventory?.also {
enqueueToSingle(ReadInventoryPacket(it, macAddress, packet.source))
}
}
}

View File

@ -1,34 +1,32 @@
package net.shadowfacts.phycon.network.block.terminal
import alexiil.mc.lib.attributes.item.GroupedItemInv
import alexiil.mc.lib.attributes.item.ItemStackCollections
import it.unimi.dsi.fastutil.objects.Object2IntMap
import net.minecraft.item.ItemStack
import net.shadowfacts.phycon.api.packet.Packet
import net.shadowfacts.phycon.api.util.MACAddress
import net.shadowfacts.phycon.init.PhyBlockEntities
import net.shadowfacts.phycon.network.DeviceBlockEntity
import net.shadowfacts.phycon.network.block.netinterface.InterfaceBlockEntity
import net.shadowfacts.phycon.network.packet.DeviceRemovedPacket
import net.shadowfacts.phycon.network.packet.ReadAllPacket
import net.shadowfacts.phycon.network.packet.RequestReadAllPacket
import java.util.*
import net.shadowfacts.phycon.network.packet.ReadInventoryPacket
import net.shadowfacts.phycon.network.packet.RequestInventoryPacket
/**
* @author shadowfacts
*/
class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL) {
private val inventoryCache = mutableMapOf<MACAddress, Map<ItemStack, Int>>()
private val inventoryCache = mutableMapOf<MACAddress, GroupedItemInv>()
override fun handlePacket(packet: Packet) {
when (packet) {
is ReadAllPacket -> handleReadAll(packet)
is ReadInventoryPacket -> handleReadInventory(packet)
is DeviceRemovedPacket -> handleDeviceRemoved(packet)
}
}
private fun handleReadAll(packet: ReadAllPacket) {
inventoryCache[packet.source] = packet.items
private fun handleReadInventory(packet: ReadInventoryPacket) {
inventoryCache[packet.source] = packet.inventory
println("new items: ${computeNetItems()}")
}
@ -39,8 +37,9 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL) {
fun computeNetItems(): Map<ItemStack, Int> {
val net = ItemStackCollections.intMap()
for (map in inventoryCache.values) {
for ((stack, amount) in map) {
for (inventory in inventoryCache.values) {
for (stack in inventory.storedStacks) {
val amount = inventory.getAmount(stack)
net.mergeInt(stack, amount) { a, b -> a + b }
}
}
@ -50,7 +49,7 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL) {
fun onActivate() {
if (!world!!.isClient) {
inventoryCache.clear()
enqueueToSingle(RequestReadAllPacket(macAddress))
enqueueToSingle(RequestInventoryPacket(macAddress))
}
}

View File

@ -1,13 +1,13 @@
package net.shadowfacts.phycon.network.packet
import net.minecraft.item.ItemStack
import alexiil.mc.lib.attributes.item.GroupedItemInv
import net.shadowfacts.phycon.api.util.MACAddress
/**
* @author shadowfacts
*/
class ReadAllPacket(
val items: Map<ItemStack, Int>,
class ReadInventoryPacket(
val inventory: GroupedItemInv,
source: MACAddress,
destination: MACAddress
): BasePacket(source, destination)

View File

@ -0,0 +1,8 @@
package net.shadowfacts.phycon.network.packet
import net.shadowfacts.phycon.api.util.MACAddress
/**
* @author shadowfacts
*/
class RequestInventoryPacket(source: MACAddress, destination: MACAddress = MACAddress.BROADCAST): BasePacket(source, destination)

View File

@ -1,8 +0,0 @@
package net.shadowfacts.phycon.network.packet
import net.shadowfacts.phycon.api.util.MACAddress
/**
* @author shadowfacts
*/
class RequestReadAllPacket(source: MACAddress, destination: MACAddress = MACAddress.BROADCAST): BasePacket(source, destination)