Change Terminal to cache inventories instead of items directly
This commit is contained in:
parent
e246736486
commit
45052029e9
|
@ -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()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -7,6 +7,7 @@ import net.minecraft.util.Tickable
|
||||||
import net.shadowfacts.phycon.api.PacketSink
|
import net.shadowfacts.phycon.api.PacketSink
|
||||||
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.network.packet.DeviceRemovedPacket
|
||||||
import java.lang.ref.WeakReference
|
import java.lang.ref.WeakReference
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
|
@ -84,4 +85,8 @@ abstract class DeviceBlockEntity(type: BlockEntityType<*>): BlockEntity(type), T
|
||||||
macAddress = MACAddress(tag.getLong("MACAddress"))
|
macAddress = MACAddress(tag.getLong("MACAddress"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun onBreak() {
|
||||||
|
enqueueToAll(DeviceRemovedPacket(this))
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,36 +1,55 @@
|
||||||
package net.shadowfacts.phycon.network.block.terminal
|
package net.shadowfacts.phycon.network.block.terminal
|
||||||
|
|
||||||
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.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.ReadAllPacket
|
import net.shadowfacts.phycon.network.packet.ReadAllPacket
|
||||||
import net.shadowfacts.phycon.network.packet.RequestReadAllPacket
|
import net.shadowfacts.phycon.network.packet.RequestReadAllPacket
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author shadowfacts
|
* @author shadowfacts
|
||||||
*/
|
*/
|
||||||
class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL) {
|
class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL) {
|
||||||
|
|
||||||
private var cachedItems = ItemStackCollections.intMap()
|
private val inventoryCache = mutableMapOf<MACAddress, Map<ItemStack, Int>>()
|
||||||
|
|
||||||
override fun handlePacket(packet: Packet) {
|
override fun handlePacket(packet: Packet) {
|
||||||
when (packet) {
|
when (packet) {
|
||||||
is ReadAllPacket -> handleReadAll(packet)
|
is ReadAllPacket -> handleReadAll(packet)
|
||||||
|
is DeviceRemovedPacket -> handleDeviceRemoved(packet)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun handleReadAll(packet: ReadAllPacket) {
|
private fun handleReadAll(packet: ReadAllPacket) {
|
||||||
packet.items.forEach { (stack, amount) ->
|
inventoryCache[packet.source] = packet.items
|
||||||
cachedItems.mergeInt(stack, amount) { a, b -> a + b }
|
|
||||||
|
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() {
|
fun onActivate() {
|
||||||
if (!world!!.isClient) {
|
if (!world!!.isClient) {
|
||||||
cachedItems.clear()
|
inventoryCache.clear()
|
||||||
enqueueToSingle(RequestReadAllPacket(macAddress))
|
enqueueToSingle(RequestReadAllPacket(macAddress))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
Loading…
Reference in New Issue