Switch: Implement switching capacity

This commit is contained in:
Shadowfacts 2019-10-30 14:05:53 -04:00
parent 98134825a6
commit 654baa81a1
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 28 additions and 7 deletions

View File

@ -31,7 +31,7 @@ abstract class DeviceBlockEntity(type: BlockEntityType<*>): BlockEntity(type), P
protected abstract fun handlePacket(packet: Packet) protected abstract fun handlePacket(packet: Packet)
protected fun acceptsPacket(packet: Packet): Boolean { protected open fun acceptsPacket(packet: Packet): Boolean {
return when (packet.destination.type) { return when (packet.destination.type) {
MACAddress.Type.BROADCAST -> true MACAddress.Type.BROADCAST -> true
MACAddress.Type.UNICAST -> macAddress == packet.destination MACAddress.Type.UNICAST -> macAddress == packet.destination

View File

@ -1,5 +1,7 @@
package net.shadowfacts.phycon.network.block.netswitch 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.minecraft.util.math.Direction
import net.shadowfacts.phycon.api.PacketSink import net.shadowfacts.phycon.api.PacketSink
import net.shadowfacts.phycon.api.packet.Packet 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.init.PhyBlockEntities
import net.shadowfacts.phycon.network.DeviceBlockEntity import net.shadowfacts.phycon.network.DeviceBlockEntity
import net.shadowfacts.phycon.network.NetworkUtil import net.shadowfacts.phycon.network.NetworkUtil
import net.shadowfacts.phycon.network.packet.ItemStackPacket
import java.lang.RuntimeException import java.lang.RuntimeException
/** /**
* @author shadowfacts * @author shadowfacts
*/ */
class SwitchBlockEntity: DeviceBlockEntity(PhyBlockEntities.SWITCH) { class SwitchBlockEntity: DeviceBlockEntity(PhyBlockEntities.SWITCH), Tickable {
private val macTable = mutableMapOf<MACAddress, Direction>() companion object {
var SWITCHING_CAPACITY = 256
override fun handlePacket(packet: Packet) {
throw RuntimeException("Unreachable")
} }
override fun handle(packet: Packet) { private val macTable = mutableMapOf<MACAddress, Direction>()
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) { if (packet.destination == MACAddress.BROADCAST) {
val allDestinations = NetworkUtil.findDestinations(world!!, pos).filter { it.macAddress != packet.source } val allDestinations = NetworkUtil.findDestinations(world!!, pos).filter { it.macAddress != packet.source }
sendToAll(packet, allDestinations) sendToAll(packet, allDestinations)
@ -58,4 +75,8 @@ class SwitchBlockEntity: DeviceBlockEntity(PhyBlockEntities.SWITCH) {
} }
} }
override fun tick() {
packetsHandledThisTick = 0
}
} }