Change Terminal to cache inventories instead of items directly

This commit is contained in:
Shadowfacts 2019-10-28 17:48:10 -04:00
parent e246736486
commit 45052029e9
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
4 changed files with 60 additions and 6 deletions

View File

@ -0,0 +1,19 @@
package net.shadowfacts.phycon.network
import net.minecraft.block.BlockState
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.util.math.BlockPos
import net.minecraft.world.World
import net.shadowfacts.phycon.block.BlockWithEntity
/**
* @author shadowfacts
*/
abstract class DeviceBlock<T: DeviceBlockEntity>(settings: Settings): BlockWithEntity<T>(settings) {
override fun onBreak(world: World, pos: BlockPos, state: BlockState, player: PlayerEntity) {
super.onBreak(world, pos, state, player)
getBlockEntity(world, pos)!!.onBreak()
}
}

View File

@ -7,6 +7,7 @@ import net.minecraft.util.Tickable
import net.shadowfacts.phycon.api.PacketSink
import net.shadowfacts.phycon.api.packet.Packet
import net.shadowfacts.phycon.api.util.MACAddress
import net.shadowfacts.phycon.network.packet.DeviceRemovedPacket
import java.lang.ref.WeakReference
import java.util.*
@ -84,4 +85,8 @@ abstract class DeviceBlockEntity(type: BlockEntityType<*>): BlockEntity(type), T
macAddress = MACAddress(tag.getLong("MACAddress"))
}
fun onBreak() {
enqueueToAll(DeviceRemovedPacket(this))
}
}

View File

@ -1,36 +1,55 @@
package net.shadowfacts.phycon.network.block.terminal
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.*
/**
* @author shadowfacts
*/
class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL) {
private var cachedItems = ItemStackCollections.intMap()
private val inventoryCache = mutableMapOf<MACAddress, Map<ItemStack, Int>>()
override fun handlePacket(packet: Packet) {
when (packet) {
is ReadAllPacket -> handleReadAll(packet)
is DeviceRemovedPacket -> handleDeviceRemoved(packet)
}
}
fun handleReadAll(packet: ReadAllPacket) {
packet.items.forEach { (stack, amount) ->
cachedItems.mergeInt(stack, amount) { a, b -> a + b }
private fun handleReadAll(packet: ReadAllPacket) {
inventoryCache[packet.source] = packet.items
println("new items: ${computeNetItems()}")
}
println("new cached items: $cachedItems")
private fun handleDeviceRemoved(packet: DeviceRemovedPacket) {
inventoryCache.remove(packet.source)
}
fun computeNetItems(): Map<ItemStack, Int> {
val net = ItemStackCollections.intMap()
for (map in inventoryCache.values) {
for ((stack, amount) in map) {
net.mergeInt(stack, amount) { a, b -> a + b }
}
}
return net
}
fun onActivate() {
if (!world!!.isClient) {
cachedItems.clear()
inventoryCache.clear()
enqueueToSingle(RequestReadAllPacket(macAddress))
}
}

View File

@ -0,0 +1,11 @@
package net.shadowfacts.phycon.network.packet
import net.shadowfacts.phycon.api.util.MACAddress
import net.shadowfacts.phycon.network.DeviceBlockEntity
/**
* @author shadowfacts
*/
class DeviceRemovedPacket(source: MACAddress, destination: MACAddress = MACAddress.BROADCAST): BasePacket(source, destination) {
constructor(device: DeviceBlockEntity): this(device.macAddress)
}