From 03fd8ff6999009f32a8162e76d64b6e78c0e7935 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 14 Jul 2024 15:06:37 -0700 Subject: [PATCH] Convert redstone emitter to fabric transfer API --- .../RedstoneEmitterBlockEntity.kt | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/net/shadowfacts/phycon/block/redstone_emitter/RedstoneEmitterBlockEntity.kt b/src/main/kotlin/net/shadowfacts/phycon/block/redstone_emitter/RedstoneEmitterBlockEntity.kt index 0d9a1b8..dee32fd 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/block/redstone_emitter/RedstoneEmitterBlockEntity.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/block/redstone_emitter/RedstoneEmitterBlockEntity.kt @@ -1,6 +1,7 @@ package net.shadowfacts.phycon.block.redstone_emitter -import alexiil.mc.lib.attributes.item.GroupedItemInvView +import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant +import net.fabricmc.fabric.api.transfer.v1.storage.Storage import net.minecraft.block.BlockState import net.minecraft.item.ItemStack import net.minecraft.nbt.NbtCompound @@ -12,11 +13,10 @@ import net.shadowfacts.phycon.block.DeviceBlockEntity import net.shadowfacts.phycon.block.FaceDeviceBlock import net.shadowfacts.phycon.init.PhyBlockEntities import net.shadowfacts.phycon.packet.DeviceRemovedPacket -import net.shadowfacts.phycon.packet.ReadGroupedInventoryPacket +import net.shadowfacts.phycon.packet.ReadItemStoragePacket import net.shadowfacts.phycon.packet.RequestInventoryPacket import net.shadowfacts.phycon.util.ClientConfigurableDevice import net.shadowfacts.phycon.util.GhostInv -import kotlin.math.round /** * @author shadowfacts @@ -26,7 +26,7 @@ class RedstoneEmitterBlockEntity(pos: BlockPos, state: BlockState) : ClientConfigurableDevice, GhostInv { - private val inventoryCache = mutableMapOf() + private val inventoryCache = mutableMapOf>() var cachedEmittedPower: Int = 0 private set @@ -45,12 +45,12 @@ class RedstoneEmitterBlockEntity(pos: BlockPos, state: BlockState) : override fun handle(packet: Packet) { when (packet) { - is ReadGroupedInventoryPacket -> handleReadInventory(packet) + is ReadItemStoragePacket -> handleReadItemStorage(packet) is DeviceRemovedPacket -> handleDeviceRemoved(packet) } } - private fun handleReadInventory(packet: ReadGroupedInventoryPacket) { + private fun handleReadItemStorage(packet: ReadItemStoragePacket) { inventoryCache[packet.source] = packet.inventory recalculateRedstone() } @@ -66,9 +66,8 @@ class RedstoneEmitterBlockEntity(pos: BlockPos, state: BlockState) : if (!world!!.isClient && counter % 20 == 0L) { if (counter % 80 == 0L) { updateInventories() - } else if (counter % 20 == 0L) { - recalculateRedstone() } + recalculateRedstone() } } @@ -84,15 +83,16 @@ class RedstoneEmitterBlockEntity(pos: BlockPos, state: BlockState) : updateWorld() return } + val variant = ItemVariant.of(stackToMonitor) val networkAmount = inventoryCache.values.fold(0) { acc, inv -> - acc + inv.getAmount(stackToMonitor) + acc + inv.simulateExtract(variant, Long.MAX_VALUE, null).toInt() } cachedEmittedPower = when (mode) { Mode.ANALOG -> if (networkAmount == 0) { 0 } else { - 1 + round(networkAmount / maxAmount.toDouble() * 14).toInt() + 1 + (networkAmount / maxAmount.toDouble() * 14).toInt() } Mode.DIGITAL -> if (networkAmount >= maxAmount) 15 else 0