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:
parent
2cc9d592b2
commit
0c5e353525
|
@ -3,13 +3,12 @@ package net.shadowfacts.phycon.network.block.netinterface
|
||||||
import alexiil.mc.lib.attributes.SearchOptions
|
import alexiil.mc.lib.attributes.SearchOptions
|
||||||
import alexiil.mc.lib.attributes.item.GroupedItemInv
|
import alexiil.mc.lib.attributes.item.GroupedItemInv
|
||||||
import alexiil.mc.lib.attributes.item.ItemAttributes
|
import alexiil.mc.lib.attributes.item.ItemAttributes
|
||||||
import net.minecraft.item.ItemStack
|
|
||||||
import net.minecraft.util.math.Direction
|
import net.minecraft.util.math.Direction
|
||||||
import net.shadowfacts.phycon.api.packet.Packet
|
import net.shadowfacts.phycon.api.packet.Packet
|
||||||
import net.shadowfacts.phycon.init.PhyBlockEntities
|
import net.shadowfacts.phycon.init.PhyBlockEntities
|
||||||
import net.shadowfacts.phycon.network.DeviceBlockEntity
|
import net.shadowfacts.phycon.network.DeviceBlockEntity
|
||||||
import net.shadowfacts.phycon.network.packet.ReadAllPacket
|
import net.shadowfacts.phycon.network.packet.ReadInventoryPacket
|
||||||
import net.shadowfacts.phycon.network.packet.RequestReadAllPacket
|
import net.shadowfacts.phycon.network.packet.RequestInventoryPacket
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author shadowfacts
|
* @author shadowfacts
|
||||||
|
@ -30,22 +29,17 @@ class InterfaceBlockEntity: DeviceBlockEntity(PhyBlockEntities.INTERFACE) {
|
||||||
|
|
||||||
override fun handlePacket(packet: Packet) {
|
override fun handlePacket(packet: Packet) {
|
||||||
when (packet) {
|
when (packet) {
|
||||||
is RequestReadAllPacket -> handleReadAll(packet)
|
is RequestInventoryPacket -> handleRequestInventory(packet)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun handleReadAll(packet: RequestReadAllPacket) {
|
private fun handleRequestInventory(packet: RequestInventoryPacket) {
|
||||||
enqueueToSingle(ReadAllPacket(readAll(), macAddress, packet.source))
|
|
||||||
}
|
|
||||||
|
|
||||||
fun readAll(): Map<ItemStack, Int> {
|
|
||||||
// if we don't have an inventory, try to get one
|
// 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
|
// this happens when readAll is called before a neighbor state changes, such as immediately after world load
|
||||||
if (inventory == null) updateInventory()
|
if (inventory == null) updateInventory()
|
||||||
|
inventory?.also {
|
||||||
return inventory?.let {
|
enqueueToSingle(ReadInventoryPacket(it, macAddress, packet.source))
|
||||||
it.storedStacks.associateWith(it::getAmount)
|
}
|
||||||
} ?: mapOf()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,34 +1,32 @@
|
||||||
package net.shadowfacts.phycon.network.block.terminal
|
package net.shadowfacts.phycon.network.block.terminal
|
||||||
|
|
||||||
|
import alexiil.mc.lib.attributes.item.GroupedItemInv
|
||||||
import alexiil.mc.lib.attributes.item.ItemStackCollections
|
import alexiil.mc.lib.attributes.item.ItemStackCollections
|
||||||
import it.unimi.dsi.fastutil.objects.Object2IntMap
|
|
||||||
import net.minecraft.item.ItemStack
|
import net.minecraft.item.ItemStack
|
||||||
import net.shadowfacts.phycon.api.packet.Packet
|
import net.shadowfacts.phycon.api.packet.Packet
|
||||||
import net.shadowfacts.phycon.api.util.MACAddress
|
import net.shadowfacts.phycon.api.util.MACAddress
|
||||||
import net.shadowfacts.phycon.init.PhyBlockEntities
|
import net.shadowfacts.phycon.init.PhyBlockEntities
|
||||||
import net.shadowfacts.phycon.network.DeviceBlockEntity
|
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.DeviceRemovedPacket
|
||||||
import net.shadowfacts.phycon.network.packet.ReadAllPacket
|
import net.shadowfacts.phycon.network.packet.ReadInventoryPacket
|
||||||
import net.shadowfacts.phycon.network.packet.RequestReadAllPacket
|
import net.shadowfacts.phycon.network.packet.RequestInventoryPacket
|
||||||
import java.util.*
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author shadowfacts
|
* @author shadowfacts
|
||||||
*/
|
*/
|
||||||
class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL) {
|
class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL) {
|
||||||
|
|
||||||
private val inventoryCache = mutableMapOf<MACAddress, Map<ItemStack, Int>>()
|
private val inventoryCache = mutableMapOf<MACAddress, GroupedItemInv>()
|
||||||
|
|
||||||
override fun handlePacket(packet: Packet) {
|
override fun handlePacket(packet: Packet) {
|
||||||
when (packet) {
|
when (packet) {
|
||||||
is ReadAllPacket -> handleReadAll(packet)
|
is ReadInventoryPacket -> handleReadInventory(packet)
|
||||||
is DeviceRemovedPacket -> handleDeviceRemoved(packet)
|
is DeviceRemovedPacket -> handleDeviceRemoved(packet)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun handleReadAll(packet: ReadAllPacket) {
|
private fun handleReadInventory(packet: ReadInventoryPacket) {
|
||||||
inventoryCache[packet.source] = packet.items
|
inventoryCache[packet.source] = packet.inventory
|
||||||
|
|
||||||
println("new items: ${computeNetItems()}")
|
println("new items: ${computeNetItems()}")
|
||||||
}
|
}
|
||||||
|
@ -39,8 +37,9 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL) {
|
||||||
|
|
||||||
fun computeNetItems(): Map<ItemStack, Int> {
|
fun computeNetItems(): Map<ItemStack, Int> {
|
||||||
val net = ItemStackCollections.intMap()
|
val net = ItemStackCollections.intMap()
|
||||||
for (map in inventoryCache.values) {
|
for (inventory in inventoryCache.values) {
|
||||||
for ((stack, amount) in map) {
|
for (stack in inventory.storedStacks) {
|
||||||
|
val amount = inventory.getAmount(stack)
|
||||||
net.mergeInt(stack, amount) { a, b -> a + b }
|
net.mergeInt(stack, amount) { a, b -> a + b }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,7 +49,7 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL) {
|
||||||
fun onActivate() {
|
fun onActivate() {
|
||||||
if (!world!!.isClient) {
|
if (!world!!.isClient) {
|
||||||
inventoryCache.clear()
|
inventoryCache.clear()
|
||||||
enqueueToSingle(RequestReadAllPacket(macAddress))
|
enqueueToSingle(RequestInventoryPacket(macAddress))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
package net.shadowfacts.phycon.network.packet
|
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
|
import net.shadowfacts.phycon.api.util.MACAddress
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author shadowfacts
|
* @author shadowfacts
|
||||||
*/
|
*/
|
||||||
class ReadAllPacket(
|
class ReadInventoryPacket(
|
||||||
val items: Map<ItemStack, Int>,
|
val inventory: GroupedItemInv,
|
||||||
source: MACAddress,
|
source: MACAddress,
|
||||||
destination: MACAddress
|
destination: MACAddress
|
||||||
): BasePacket(source, destination)
|
): BasePacket(source, destination)
|
|
@ -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)
|
|
@ -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)
|
|
Loading…
Reference in New Issue