diff --git a/src/main/kotlin/net/shadowfacts/phycon/network/DeviceBlockEntity.kt b/src/main/kotlin/net/shadowfacts/phycon/network/DeviceBlockEntity.kt index 92da52a..97a443d 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/network/DeviceBlockEntity.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/network/DeviceBlockEntity.kt @@ -31,7 +31,7 @@ abstract class DeviceBlockEntity(type: BlockEntityType<*>): BlockEntity(type), P protected abstract fun handlePacket(packet: Packet) - protected fun acceptsPacket(packet: Packet): Boolean { + protected open fun acceptsPacket(packet: Packet): Boolean { return when (packet.destination.type) { MACAddress.Type.BROADCAST -> true MACAddress.Type.UNICAST -> macAddress == packet.destination diff --git a/src/main/kotlin/net/shadowfacts/phycon/network/block/netswitch/SwitchBlockEntity.kt b/src/main/kotlin/net/shadowfacts/phycon/network/block/netswitch/SwitchBlockEntity.kt index 4a06711..0352b2f 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/network/block/netswitch/SwitchBlockEntity.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/network/block/netswitch/SwitchBlockEntity.kt @@ -1,5 +1,7 @@ package net.shadowfacts.phycon.network.block.netswitch +import net.minecraft.entity.ItemEntity +import net.minecraft.util.Tickable import net.minecraft.util.math.Direction import net.shadowfacts.phycon.api.PacketSink import net.shadowfacts.phycon.api.packet.Packet @@ -7,20 +9,35 @@ import net.shadowfacts.phycon.api.util.MACAddress import net.shadowfacts.phycon.init.PhyBlockEntities import net.shadowfacts.phycon.network.DeviceBlockEntity import net.shadowfacts.phycon.network.NetworkUtil +import net.shadowfacts.phycon.network.packet.ItemStackPacket import java.lang.RuntimeException /** * @author shadowfacts */ -class SwitchBlockEntity: DeviceBlockEntity(PhyBlockEntities.SWITCH) { +class SwitchBlockEntity: DeviceBlockEntity(PhyBlockEntities.SWITCH), Tickable { - private val macTable = mutableMapOf() - - override fun handlePacket(packet: Packet) { - throw RuntimeException("Unreachable") + companion object { + var SWITCHING_CAPACITY = 256 } - override fun handle(packet: Packet) { + private val macTable = mutableMapOf() + private var packetsHandledThisTick = 0 + + override fun acceptsPacket(packet: Packet) = true + + override fun handlePacket(packet: Packet) { + if (packetsHandledThisTick >= SWITCHING_CAPACITY) { + if (packet is ItemStackPacket) { + // todo: calculate entity spawn point by finding non-obstructed location + val entity = ItemEntity(world!!, pos.x.toDouble(), pos.y + 1.0, pos.z.toDouble(), packet.stack) + world!!.spawnEntity(entity) + } + return + } + + packetsHandledThisTick++ + if (packet.destination == MACAddress.BROADCAST) { val allDestinations = NetworkUtil.findDestinations(world!!, pos).filter { it.macAddress != packet.source } sendToAll(packet, allDestinations) @@ -58,4 +75,8 @@ class SwitchBlockEntity: DeviceBlockEntity(PhyBlockEntities.SWITCH) { } } + override fun tick() { + packetsHandledThisTick = 0 + } + }