From 45052029e922ad1d46ca16d8dd9debe990de2fc4 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 28 Oct 2019 17:48:10 -0400 Subject: [PATCH] Change Terminal to cache inventories instead of items directly --- .../shadowfacts/phycon/network/DeviceBlock.kt | 19 ++++++++++++ .../phycon/network/DeviceBlockEntity.kt | 5 +++ .../block/terminal/TerminalBlockEntity.kt | 31 +++++++++++++++---- .../network/packet/DeviceRemovedPacket.kt | 11 +++++++ 4 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 src/main/kotlin/net/shadowfacts/phycon/network/DeviceBlock.kt create mode 100644 src/main/kotlin/net/shadowfacts/phycon/network/packet/DeviceRemovedPacket.kt diff --git a/src/main/kotlin/net/shadowfacts/phycon/network/DeviceBlock.kt b/src/main/kotlin/net/shadowfacts/phycon/network/DeviceBlock.kt new file mode 100644 index 0000000..ee624ce --- /dev/null +++ b/src/main/kotlin/net/shadowfacts/phycon/network/DeviceBlock.kt @@ -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(settings: Settings): BlockWithEntity(settings) { + + override fun onBreak(world: World, pos: BlockPos, state: BlockState, player: PlayerEntity) { + super.onBreak(world, pos, state, player) + getBlockEntity(world, pos)!!.onBreak() + } + +} diff --git a/src/main/kotlin/net/shadowfacts/phycon/network/DeviceBlockEntity.kt b/src/main/kotlin/net/shadowfacts/phycon/network/DeviceBlockEntity.kt index fe11dac..80faa8b 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/network/DeviceBlockEntity.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/network/DeviceBlockEntity.kt @@ -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)) + } + } diff --git a/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalBlockEntity.kt b/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalBlockEntity.kt index f492f8c..8967ee2 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalBlockEntity.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalBlockEntity.kt @@ -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>() 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()}") + } + + private fun handleDeviceRemoved(packet: DeviceRemovedPacket) { + inventoryCache.remove(packet.source) + } + + fun computeNetItems(): Map { + val net = ItemStackCollections.intMap() + for (map in inventoryCache.values) { + for ((stack, amount) in map) { + net.mergeInt(stack, amount) { a, b -> a + b } + } } - println("new cached items: $cachedItems") + return net } fun onActivate() { if (!world!!.isClient) { - cachedItems.clear() + inventoryCache.clear() enqueueToSingle(RequestReadAllPacket(macAddress)) } } diff --git a/src/main/kotlin/net/shadowfacts/phycon/network/packet/DeviceRemovedPacket.kt b/src/main/kotlin/net/shadowfacts/phycon/network/packet/DeviceRemovedPacket.kt new file mode 100644 index 0000000..fa576c6 --- /dev/null +++ b/src/main/kotlin/net/shadowfacts/phycon/network/packet/DeviceRemovedPacket.kt @@ -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) +}