48 lines
1.6 KiB
Kotlin
48 lines
1.6 KiB
Kotlin
package net.shadowfacts.phycon.block.p2p
|
|
|
|
import net.fabricmc.fabric.api.transfer.v1.item.ItemStorage
|
|
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.util.math.BlockPos
|
|
import net.shadowfacts.phycon.api.packet.Packet
|
|
import net.shadowfacts.phycon.block.DeviceBlockEntity
|
|
import net.shadowfacts.phycon.block.FaceDeviceBlock
|
|
import net.shadowfacts.phycon.init.PhyBlockEntities
|
|
import net.shadowfacts.phycon.packet.ReadItemStoragePacket
|
|
import net.shadowfacts.phycon.packet.RequestInventoryPacket
|
|
|
|
/**
|
|
* @author shadowfacts
|
|
*/
|
|
class P2PInterfaceBlockEntity(pos: BlockPos, state: BlockState): DeviceBlockEntity(PhyBlockEntities.P2P_INTERFACE, pos, state) {
|
|
|
|
private var inventory: Storage<ItemVariant>? = null
|
|
|
|
private fun updateInventory() {
|
|
val facing = cachedState[FaceDeviceBlock.FACING]
|
|
inventory = ItemStorage.SIDED.find(world!!, pos.offset(facing), facing.opposite)
|
|
}
|
|
|
|
private fun getInventory(): Storage<ItemVariant>? {
|
|
if (inventory == null) updateInventory()
|
|
return inventory
|
|
}
|
|
|
|
override fun handle(packet: Packet) {
|
|
when (packet) {
|
|
is RequestInventoryPacket -> handleRequestInventory(packet)
|
|
}
|
|
}
|
|
|
|
private fun handleRequestInventory(packet: RequestInventoryPacket) {
|
|
if (packet.kind != RequestInventoryPacket.Kind.SIDED) {
|
|
return
|
|
}
|
|
getInventory()?.also {
|
|
sendPacket(ReadItemStoragePacket(it, ipAddress, packet.source))
|
|
}
|
|
}
|
|
|
|
}
|