Sync terminal cached item counts to client

This commit is contained in:
Shadowfacts 2019-10-29 17:33:33 -04:00
parent 64c18e9eae
commit 7bc859eaf6
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 42 additions and 7 deletions

View File

@ -2,6 +2,7 @@ package net.shadowfacts.phycon.network.block.terminal
import alexiil.mc.lib.attributes.item.GroupedItemInv
import alexiil.mc.lib.attributes.item.ItemStackCollections
import net.fabricmc.fabric.api.block.entity.BlockEntityClientSerializable
import net.fabricmc.fabric.api.container.ContainerProviderRegistry
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.inventory.BasicInventory
@ -9,6 +10,7 @@ import net.minecraft.inventory.Inventory
import net.minecraft.inventory.InventoryListener
import net.minecraft.item.ItemStack
import net.minecraft.nbt.CompoundTag
import net.minecraft.nbt.ListTag
import net.shadowfacts.phycon.api.packet.Packet
import net.shadowfacts.phycon.api.util.MACAddress
import net.shadowfacts.phycon.init.PhyBlockEntities
@ -22,11 +24,14 @@ import net.shadowfacts.phycon.util.toTag
/**
* @author shadowfacts
*/
class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), InventoryListener {
class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), InventoryListener, BlockEntityClientSerializable {
private val inventoryCache = mutableMapOf<MACAddress, GroupedItemInv>()
val cachedNetItems = ItemStackCollections.intMap()
val internalBuffer = BasicInventory(18)
var counter = 0
init {
internalBuffer.addListener(this)
}
@ -40,23 +45,32 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), Invento
private fun handleReadInventory(packet: ReadInventoryPacket) {
inventoryCache[packet.source] = packet.inventory
println("new items: ${computeNetItems()}")
}
private fun handleDeviceRemoved(packet: DeviceRemovedPacket) {
inventoryCache.remove(packet.source)
}
fun computeNetItems(): Map<ItemStack, Int> {
val net = ItemStackCollections.intMap()
private fun updateNetItems() {
cachedNetItems.clear()
for (inventory in inventoryCache.values) {
for (stack in inventory.storedStacks) {
val amount = inventory.getAmount(stack)
net.mergeInt(stack, amount) { a, b -> a + b }
cachedNetItems.mergeInt(stack, amount) { a, b -> a + b }
}
}
return net
}
override fun tick() {
super.tick()
if (!world!!.isClient && (++counter % 20) == 0) {
updateNetItems()
sync()
}
if (world!!.isClient && (++counter % 20) == 0) {
println(cachedNetItems)
}
}
fun onActivate(player: PlayerEntity) {
@ -85,4 +99,25 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), Invento
internalBuffer.fromTag(tag.getList("InternalBuffer", 10))
}
override fun toClientTag(tag: CompoundTag): CompoundTag {
val list = ListTag()
tag.put("CachedNetItems", list)
for ((stack, amount) in cachedNetItems) {
val entryTag = stack.toTag(CompoundTag())
entryTag.putInt("NetAmount", amount)
list.add(entryTag)
}
return tag
}
override fun fromClientTag(tag: CompoundTag) {
val list = tag.getList("CachedNetItems", 10)
cachedNetItems.clear()
for (entryTag in list) {
val stack = ItemStack.fromTag(entryTag as CompoundTag)
val netAmount = entryTag.getInt("NetAmount")
cachedNetItems[stack] = netAmount
}
}
}