98 lines
2.7 KiB
Kotlin
98 lines
2.7 KiB
Kotlin
|
package net.shadowfacts.phycon.block.redstone_emitter
|
||
|
|
||
|
import alexiil.mc.lib.attributes.item.GroupedItemInvView
|
||
|
import net.minecraft.item.ItemStack
|
||
|
import net.minecraft.nbt.CompoundTag
|
||
|
import net.shadowfacts.phycon.api.packet.Packet
|
||
|
import net.shadowfacts.phycon.api.util.IPAddress
|
||
|
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.ReadInventoryPacket
|
||
|
import net.shadowfacts.phycon.packet.RequestInventoryPacket
|
||
|
import kotlin.math.round
|
||
|
|
||
|
/**
|
||
|
* @author shadowfacts
|
||
|
*/
|
||
|
class RedstoneEmitterBlockEntity: DeviceBlockEntity(PhyBlockEntities.REDSTONE_EMITTER) {
|
||
|
|
||
|
private val inventoryCache = mutableMapOf<IPAddress, GroupedItemInvView>()
|
||
|
var cachedEmittedPower: Int = 0
|
||
|
private set
|
||
|
|
||
|
var stackToMonitor: ItemStack = ItemStack.EMPTY
|
||
|
var maxAmount = 64
|
||
|
|
||
|
override fun handle(packet: Packet) {
|
||
|
when (packet) {
|
||
|
is ReadInventoryPacket -> handleReadInventory(packet)
|
||
|
is DeviceRemovedPacket -> handleDeviceRemoved(packet)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private fun handleReadInventory(packet: ReadInventoryPacket) {
|
||
|
inventoryCache[packet.source] = packet.inventory
|
||
|
recalculateRedstone()
|
||
|
}
|
||
|
|
||
|
private fun handleDeviceRemoved(packet: DeviceRemovedPacket) {
|
||
|
inventoryCache.remove(packet.source)
|
||
|
recalculateRedstone()
|
||
|
}
|
||
|
|
||
|
override fun tick() {
|
||
|
super.tick()
|
||
|
|
||
|
if (!world!!.isClient && counter % 20 == 0L) {
|
||
|
if (counter % 80 == 0L) {
|
||
|
updateInventories()
|
||
|
} else if (counter % 20 == 0L) {
|
||
|
recalculateRedstone()
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private fun updateInventories() {
|
||
|
sendPacket(RequestInventoryPacket(ipAddress))
|
||
|
}
|
||
|
|
||
|
private fun recalculateRedstone() {
|
||
|
if (stackToMonitor.isEmpty) {
|
||
|
cachedEmittedPower = 0
|
||
|
updateWorld()
|
||
|
return
|
||
|
}
|
||
|
val networkAmount = inventoryCache.values.fold(0) { acc, inv ->
|
||
|
acc + inv.getAmount(stackToMonitor)
|
||
|
}
|
||
|
cachedEmittedPower =
|
||
|
if (networkAmount == 0) {
|
||
|
0
|
||
|
} else {
|
||
|
1 + round(networkAmount / maxAmount.toDouble() * 14).toInt()
|
||
|
}
|
||
|
updateWorld()
|
||
|
}
|
||
|
|
||
|
private fun updateWorld() {
|
||
|
world!!.updateNeighborsAlways(pos, cachedState.block)
|
||
|
world!!.updateNeighborsAlways(pos.offset(cachedState[FaceDeviceBlock.FACING]), cachedState.block)
|
||
|
}
|
||
|
|
||
|
override fun toCommonTag(tag: CompoundTag) {
|
||
|
super.toCommonTag(tag)
|
||
|
tag.putInt("CachedEmittedPower", cachedEmittedPower)
|
||
|
tag.put("StackToMonitor", stackToMonitor.toTag(CompoundTag()))
|
||
|
tag.putInt("MaxAmount", maxAmount)
|
||
|
}
|
||
|
|
||
|
override fun fromCommonTag(tag: CompoundTag) {
|
||
|
super.fromCommonTag(tag)
|
||
|
cachedEmittedPower = tag.getInt("CachedEmittedPower")
|
||
|
stackToMonitor = ItemStack.fromTag(tag.getCompound("StackToMonitor"))
|
||
|
maxAmount = tag.getInt("MaxAmount")
|
||
|
}
|
||
|
}
|