2019-10-27 02:12:02 +00:00
|
|
|
package net.shadowfacts.phycon.network.block.netinterface
|
2021-02-13 23:24:36 +00:00
|
|
|
//
|
|
|
|
//import alexiil.mc.lib.attributes.SearchOptions
|
|
|
|
//import alexiil.mc.lib.attributes.Simulation
|
|
|
|
//import alexiil.mc.lib.attributes.item.GroupedItemInv
|
|
|
|
//import alexiil.mc.lib.attributes.item.ItemAttributes
|
|
|
|
//import net.minecraft.item.ItemStack
|
|
|
|
//import net.minecraft.util.math.Direction
|
|
|
|
//import net.shadowfacts.phycon.api.Interface
|
|
|
|
//import net.shadowfacts.phycon.api.packet.Packet
|
|
|
|
//import net.shadowfacts.phycon.init.PhyBlockEntities
|
|
|
|
//import net.shadowfacts.phycon.network.BaseInterface
|
|
|
|
//import net.shadowfacts.phycon.network.DeviceBlockEntity
|
|
|
|
//import net.shadowfacts.phycon.network.component.ItemStackPacketHandler
|
|
|
|
//import net.shadowfacts.phycon.network.component.handleItemStack
|
|
|
|
//import net.shadowfacts.phycon.network.packet.*
|
|
|
|
//
|
|
|
|
///**
|
|
|
|
// * @author shadowfacts
|
|
|
|
// */
|
|
|
|
//class InterfaceBlockEntity: DeviceBlockEntity(PhyBlockEntities.INTERFACE), ItemStackPacketHandler {
|
|
|
|
//
|
|
|
|
// override val interfaces = listOf(BaseInterface(this))
|
|
|
|
//
|
|
|
|
// private val facing: Direction
|
|
|
|
// get() = world!!.getBlockState(pos)[InterfaceBlock.FACING]
|
|
|
|
//
|
|
|
|
// // todo: should this be a weak ref?
|
|
|
|
// private var inventory: GroupedItemInv? = null
|
|
|
|
//
|
|
|
|
// fun updateInventory() {
|
|
|
|
// val offsetPos = pos.offset(facing)
|
|
|
|
// val option = SearchOptions.inDirection(facing)
|
|
|
|
// inventory = ItemAttributes.GROUPED_INV.getFirstOrNull(world, offsetPos, option)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// private fun getInventory(): GroupedItemInv? {
|
|
|
|
// // if we don't have an inventory, try to get one
|
|
|
|
// // this happens when readAll is called before a neighbor state changes, such as immediately after world load
|
|
|
|
// if (inventory == null) updateInventory()
|
|
|
|
// return inventory
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// override fun handle(packet: Packet, itf: Interface) {
|
|
|
|
// when (packet) {
|
|
|
|
// is RequestInventoryPacket -> handleRequestInventory(packet)
|
|
|
|
// is LocateStackPacket -> handleLocateStack(packet)
|
|
|
|
// is ExtractStackPacket -> handleExtractStack(packet)
|
|
|
|
// is CheckCapacityPacket -> handleCheckCapacity(packet)
|
|
|
|
// is ItemStackPacket -> handleItemStack(packet)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// private fun handleRequestInventory(packet: RequestInventoryPacket) {
|
|
|
|
// getInventory()?.also { inv ->
|
|
|
|
// sendPacket(ReadInventoryPacket(inv, ipAddress, packet.source))
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// private fun handleLocateStack(packet: LocateStackPacket) {
|
|
|
|
// getInventory()?.also { inv ->
|
|
|
|
// val amount = inv.getAmount(packet.stack)
|
|
|
|
// sendPacket(StackLocationPacket(packet.stack, amount, this, ipAddress, packet.source))
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// private fun handleExtractStack(packet: ExtractStackPacket) {
|
|
|
|
// getInventory()?.also { inv ->
|
|
|
|
// val extracted = inv.extract(packet.stack, packet.amount)
|
|
|
|
// sendPacket(ItemStackPacket(extracted, ipAddress, packet.source))
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// private fun handleCheckCapacity(packet: CheckCapacityPacket) {
|
|
|
|
// getInventory()?.also { inv ->
|
|
|
|
// val remaining = inv.attemptInsertion(packet.stack, Simulation.SIMULATE)
|
|
|
|
// val couldAccept = packet.stack.count - remaining.count
|
|
|
|
// sendPacket(CapacityPacket(packet.stack, couldAccept, this, ipAddress, packet.source))
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// override fun doHandleItemStack(packet: ItemStackPacket): ItemStack {
|
|
|
|
// val inventory = getInventory()
|
|
|
|
// if (inventory != null) {
|
|
|
|
// val remaining = inventory.insert(packet.stack)
|
|
|
|
// // whatever could not be inserted will be sent back to the packet's source
|
|
|
|
// return remaining
|
|
|
|
// } else {
|
|
|
|
// return packet.stack
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
//}
|