Compare commits
No commits in common. "b64a05e0ad6460cd3c742be3f1d5088083a99d92" and "9200dea350f2fd7068185bec6036e37aa7d7c864" have entirely different histories.
b64a05e0ad
...
9200dea350
22
build.gradle
22
build.gradle
|
@ -86,17 +86,17 @@ dependencies {
|
||||||
implementation project(":kiwi-java")
|
implementation project(":kiwi-java")
|
||||||
include project(":kiwi-java")
|
include project(":kiwi-java")
|
||||||
|
|
||||||
// modRuntimeOnly "de.siphalor:mousewheelie-1.18:${project.mousewheelie_version}"
|
modRuntimeOnly "de.siphalor:mousewheelie-1.18:${project.mousewheelie_version}"
|
||||||
// runtimeOnly(project(":plugin:mousewheelie")) {
|
runtimeOnly(project(":plugin:mousewheelie")) {
|
||||||
// transitive = false
|
transitive = false
|
||||||
// }
|
}
|
||||||
// include project(":plugin:mousewheelie")
|
include project(":plugin:mousewheelie")
|
||||||
//
|
|
||||||
// modRuntimeOnly "me.shedaniel:RoughlyEnoughItems-fabric:${project.rei_version}"
|
modRuntimeOnly "me.shedaniel:RoughlyEnoughItems-fabric:${project.rei_version}"
|
||||||
// runtimeOnly(project(":plugin:rei")) {
|
runtimeOnly(project(":plugin:rei")) {
|
||||||
// transitive = false
|
transitive = false
|
||||||
// }
|
}
|
||||||
// include project(":plugin:rei")
|
include project(":plugin:rei")
|
||||||
|
|
||||||
// runtimeOnly project(":plugin:techreborn")
|
// runtimeOnly project(":plugin:techreborn")
|
||||||
// include project(":plugin:techreborn")
|
// include project(":plugin:techreborn")
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Subproject commit 1cbaea53d207f1e16c6e5ee2e6bf6e3c1440ac44
|
Subproject commit abe783ae7ce45ccf296ed7b7e5c1b194e7c560fa
|
|
@ -16,6 +16,4 @@ public interface Interface {
|
||||||
|
|
||||||
void send(@NotNull EthernetFrame frame);
|
void send(@NotNull EthernetFrame frame);
|
||||||
|
|
||||||
default void cableDisconnected() {}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,10 +19,8 @@ import net.shadowfacts.phycon.api.util.MACAddress
|
||||||
import net.shadowfacts.phycon.frame.ARPQueryFrame
|
import net.shadowfacts.phycon.frame.ARPQueryFrame
|
||||||
import net.shadowfacts.phycon.frame.ARPResponseFrame
|
import net.shadowfacts.phycon.frame.ARPResponseFrame
|
||||||
import net.shadowfacts.phycon.frame.BasePacketFrame
|
import net.shadowfacts.phycon.frame.BasePacketFrame
|
||||||
import net.shadowfacts.phycon.frame.NetworkSplitFrame
|
|
||||||
import net.shadowfacts.phycon.packet.*
|
import net.shadowfacts.phycon.packet.*
|
||||||
import net.shadowfacts.phycon.util.NetworkUtil
|
import net.shadowfacts.phycon.util.NetworkUtil
|
||||||
import java.lang.ref.WeakReference
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -45,7 +43,6 @@ abstract class DeviceBlockEntity(type: BlockEntityType<*>, pos: BlockPos, state:
|
||||||
|
|
||||||
private val arpTable = mutableMapOf<IPAddress, MACAddress>()
|
private val arpTable = mutableMapOf<IPAddress, MACAddress>()
|
||||||
private val packetQueue = LinkedList<PendingPacket>()
|
private val packetQueue = LinkedList<PendingPacket>()
|
||||||
private var cachedDestination: WeakReference<Interface>? = null
|
|
||||||
|
|
||||||
var counter: Long = 0
|
var counter: Long = 0
|
||||||
|
|
||||||
|
@ -76,7 +73,6 @@ abstract class DeviceBlockEntity(type: BlockEntityType<*>, pos: BlockPos, state:
|
||||||
when (frame) {
|
when (frame) {
|
||||||
is ARPQueryFrame -> handleARPQuery(frame)
|
is ARPQueryFrame -> handleARPQuery(frame)
|
||||||
is ARPResponseFrame -> handleARPResponse(frame)
|
is ARPResponseFrame -> handleARPResponse(frame)
|
||||||
is NetworkSplitFrame -> handleNetworkSplit()
|
|
||||||
is PacketFrame -> {
|
is PacketFrame -> {
|
||||||
if (frame.packet.destination.isBroadcast || frame.packet.destination == ipAddress) {
|
if (frame.packet.destination.isBroadcast || frame.packet.destination == ipAddress) {
|
||||||
doHandlePacket(frame.packet)
|
doHandlePacket(frame.packet)
|
||||||
|
@ -109,11 +105,6 @@ abstract class DeviceBlockEntity(type: BlockEntityType<*>, pos: BlockPos, state:
|
||||||
packetQueue.removeAll(toRemove)
|
packetQueue.removeAll(toRemove)
|
||||||
}
|
}
|
||||||
|
|
||||||
protected open fun handleNetworkSplit() {
|
|
||||||
arpTable.clear()
|
|
||||||
cachedDestination = null
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun sendPacket(packet: Packet) {
|
override fun sendPacket(packet: Packet) {
|
||||||
if (packet.destination.isBroadcast) {
|
if (packet.destination.isBroadcast) {
|
||||||
send(BasePacketFrame(packet, macAddress, MACAddress.BROADCAST))
|
send(BasePacketFrame(packet, macAddress, MACAddress.BROADCAST))
|
||||||
|
@ -128,28 +119,14 @@ abstract class DeviceBlockEntity(type: BlockEntityType<*>, pos: BlockPos, state:
|
||||||
}
|
}
|
||||||
|
|
||||||
open fun findDestination(): Interface? {
|
open fun findDestination(): Interface? {
|
||||||
val cachedDestination = this.cachedDestination?.get()
|
|
||||||
if (cachedDestination != null) {
|
|
||||||
return cachedDestination
|
|
||||||
}
|
|
||||||
|
|
||||||
val sides = (cachedState.block as NetworkComponentBlock).getNetworkConnectedSides(cachedState, world!!, pos)
|
val sides = (cachedState.block as NetworkComponentBlock).getNetworkConnectedSides(cachedState, world!!, pos)
|
||||||
return when (sides.size) {
|
return when (sides.size) {
|
||||||
0 -> null
|
0 -> null
|
||||||
1 -> {
|
1 -> NetworkUtil.findConnectedInterface(world!!, pos, sides.first())
|
||||||
NetworkUtil.findConnectedInterface(world!!, pos, sides.first())?.also {
|
|
||||||
this.cachedDestination = WeakReference(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else -> throw RuntimeException("DeviceBlockEntity.findDestination must be overridden by devices which have more than 1 network connected side")
|
else -> throw RuntimeException("DeviceBlockEntity.findDestination must be overridden by devices which have more than 1 network connected side")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun cableDisconnected() {
|
|
||||||
cachedDestination = null
|
|
||||||
handleNetworkSplit()
|
|
||||||
}
|
|
||||||
|
|
||||||
open fun tick() {
|
open fun tick() {
|
||||||
counter++
|
counter++
|
||||||
|
|
||||||
|
|
|
@ -2,10 +2,8 @@ package net.shadowfacts.phycon.block.cable
|
||||||
|
|
||||||
import net.fabricmc.fabric.api.`object`.builder.v1.block.FabricBlockSettings
|
import net.fabricmc.fabric.api.`object`.builder.v1.block.FabricBlockSettings
|
||||||
import net.minecraft.block.*
|
import net.minecraft.block.*
|
||||||
import net.minecraft.block.entity.BlockEntity
|
|
||||||
import net.minecraft.entity.player.PlayerEntity
|
import net.minecraft.entity.player.PlayerEntity
|
||||||
import net.minecraft.item.ItemPlacementContext
|
import net.minecraft.item.ItemPlacementContext
|
||||||
import net.minecraft.item.ItemStack
|
|
||||||
import net.minecraft.state.StateManager
|
import net.minecraft.state.StateManager
|
||||||
import net.minecraft.state.property.EnumProperty
|
import net.minecraft.state.property.EnumProperty
|
||||||
import net.minecraft.util.ActionResult
|
import net.minecraft.util.ActionResult
|
||||||
|
@ -25,11 +23,9 @@ import net.shadowfacts.phycon.PhysicalConnectivity
|
||||||
import net.shadowfacts.phycon.api.Interface
|
import net.shadowfacts.phycon.api.Interface
|
||||||
import net.shadowfacts.phycon.api.NetworkCableBlock
|
import net.shadowfacts.phycon.api.NetworkCableBlock
|
||||||
import net.shadowfacts.phycon.api.NetworkComponentBlock
|
import net.shadowfacts.phycon.api.NetworkComponentBlock
|
||||||
import net.shadowfacts.phycon.block.DeviceBlockEntity
|
|
||||||
import net.shadowfacts.phycon.init.PhyItems
|
import net.shadowfacts.phycon.init.PhyItems
|
||||||
import net.shadowfacts.phycon.item.FaceDeviceBlockItem
|
import net.shadowfacts.phycon.item.FaceDeviceBlockItem
|
||||||
import net.shadowfacts.phycon.util.CableConnection
|
import net.shadowfacts.phycon.util.CableConnection
|
||||||
import net.shadowfacts.phycon.util.NetworkUtil
|
|
||||||
import net.shadowfacts.phycon.util.containsInclusive
|
import net.shadowfacts.phycon.util.containsInclusive
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
|
@ -199,18 +195,4 @@ class CableBlock(
|
||||||
return getShape(state)
|
return getShape(state)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onBreak(world: World, pos: BlockPos, state: BlockState, player: PlayerEntity) {
|
|
||||||
super.onBreak(world, pos, state, player)
|
|
||||||
if (!world.isClient) {
|
|
||||||
world.server?.execute {
|
|
||||||
// notify devices on either end that the connection was broken (i.e., unset the cached receivers)
|
|
||||||
val connectedSides = getNetworkConnectedSides(state, world, pos)
|
|
||||||
for (side in connectedSides) {
|
|
||||||
val dest = NetworkUtil.findConnectedInterface(world, pos, side)
|
|
||||||
dest?.cableDisconnected()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,6 @@ import net.shadowfacts.phycon.component.NetworkStackReceiver
|
||||||
import net.shadowfacts.phycon.component.handleItemStack
|
import net.shadowfacts.phycon.component.handleItemStack
|
||||||
import net.shadowfacts.phycon.packet.*
|
import net.shadowfacts.phycon.packet.*
|
||||||
import net.shadowfacts.phycon.util.ClientConfigurableDevice
|
import net.shadowfacts.phycon.util.ClientConfigurableDevice
|
||||||
import java.lang.ref.WeakReference
|
|
||||||
import kotlin.math.min
|
import kotlin.math.min
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -38,21 +37,20 @@ class InterfaceBlockEntity(pos: BlockPos, state: BlockState): DeviceBlockEntity(
|
||||||
override var receiverPriority = 0
|
override var receiverPriority = 0
|
||||||
var syncPriorities = true
|
var syncPriorities = true
|
||||||
|
|
||||||
private var inventory: WeakReference<GroupedItemInv>? = null
|
// todo: should this be a weak ref?
|
||||||
|
private var inventory: GroupedItemInv? = null
|
||||||
|
|
||||||
fun updateInventory() {
|
fun updateInventory() {
|
||||||
val offsetPos = pos.offset(facing)
|
val offsetPos = pos.offset(facing)
|
||||||
val option = SearchOptions.inDirection(facing)
|
val option = SearchOptions.inDirection(facing)
|
||||||
inventory = ItemAttributes.GROUPED_INV.getFirstOrNull(world, offsetPos, option).let {
|
inventory = ItemAttributes.GROUPED_INV.getFirstOrNull(world, offsetPos, option)
|
||||||
WeakReference(it)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getInventory(): GroupedItemInv? {
|
private fun getInventory(): GroupedItemInv? {
|
||||||
// if we don't have an inventory, try to get one
|
// 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
|
// this happens when readAll is called before a neighbor state changes, such as immediately after world load
|
||||||
if (inventory?.get() == null) updateInventory()
|
if (inventory == null) updateInventory()
|
||||||
return inventory?.get()
|
return inventory
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun handle(packet: Packet) {
|
override fun handle(packet: Packet) {
|
||||||
|
|
|
@ -17,7 +17,6 @@ import net.shadowfacts.phycon.api.frame.PacketFrame
|
||||||
import net.shadowfacts.phycon.api.util.IPAddress
|
import net.shadowfacts.phycon.api.util.IPAddress
|
||||||
import net.shadowfacts.phycon.api.util.MACAddress
|
import net.shadowfacts.phycon.api.util.MACAddress
|
||||||
import net.shadowfacts.phycon.frame.BasePacketFrame
|
import net.shadowfacts.phycon.frame.BasePacketFrame
|
||||||
import net.shadowfacts.phycon.frame.NetworkSplitFrame
|
|
||||||
import net.shadowfacts.phycon.init.PhyBlockEntities
|
import net.shadowfacts.phycon.init.PhyBlockEntities
|
||||||
import net.shadowfacts.phycon.packet.ItemStackPacket
|
import net.shadowfacts.phycon.packet.ItemStackPacket
|
||||||
import net.shadowfacts.phycon.util.NetworkUtil
|
import net.shadowfacts.phycon.util.NetworkUtil
|
||||||
|
@ -37,7 +36,6 @@ class SwitchBlockEntity(pos: BlockPos, state: BlockState): BlockEntity(PhyBlockE
|
||||||
val interfaces = Direction.values().map { SwitchInterface(it, WeakReference(this), MACAddress.random()) }
|
val interfaces = Direction.values().map { SwitchInterface(it, WeakReference(this), MACAddress.random()) }
|
||||||
|
|
||||||
private val macTable = mutableMapOf<MACAddress, Direction>()
|
private val macTable = mutableMapOf<MACAddress, Direction>()
|
||||||
private val destinationCache = Array<WeakReference<Interface>?>(6) { null }
|
|
||||||
private var packetsHandledThisTick = 0
|
private var packetsHandledThisTick = 0
|
||||||
private var delayedPackets: Deque<Pair<PacketFrame, SwitchInterface>> = LinkedList()
|
private var delayedPackets: Deque<Pair<PacketFrame, SwitchInterface>> = LinkedList()
|
||||||
|
|
||||||
|
@ -67,12 +65,12 @@ class SwitchBlockEntity(pos: BlockPos, state: BlockState): BlockEntity(PhyBlockE
|
||||||
PhysicalConnectivity.NETWORK_LOGGER.debug("{} ({}, {}) forwarding {} to side {}", this, fromItf.side, fromItf.macAddress, frame, dir)
|
PhysicalConnectivity.NETWORK_LOGGER.debug("{} ({}, {}) forwarding {} to side {}", this, fromItf.side, fromItf.macAddress, frame, dir)
|
||||||
interfaceForSide(dir).send(frame)
|
interfaceForSide(dir).send(frame)
|
||||||
} else {
|
} else {
|
||||||
|
PhysicalConnectivity.NETWORK_LOGGER.debug("{} ({}, {}) flooding {}", this, fromItf.side, fromItf.macAddress, frame)
|
||||||
flood(frame, fromItf)
|
flood(frame, fromItf)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun flood(frame: EthernetFrame, source: SwitchInterface) {
|
private fun flood(frame: EthernetFrame, source: Interface) {
|
||||||
PhysicalConnectivity.NETWORK_LOGGER.debug("{} ({}, {}) flooding {}", this, source.side, source.macAddress, frame)
|
|
||||||
for (itf in interfaces) {
|
for (itf in interfaces) {
|
||||||
if (source == itf) continue
|
if (source == itf) continue
|
||||||
itf.send(frame)
|
itf.send(frame)
|
||||||
|
@ -81,20 +79,7 @@ class SwitchBlockEntity(pos: BlockPos, state: BlockState): BlockEntity(PhyBlockE
|
||||||
|
|
||||||
private fun findDestination(fromItf: Interface): Interface? {
|
private fun findDestination(fromItf: Interface): Interface? {
|
||||||
val side = (fromItf as SwitchInterface).side
|
val side = (fromItf as SwitchInterface).side
|
||||||
return destinationCache[side.ordinal]?.get()
|
return NetworkUtil.findConnectedInterface(world!!, pos, side)
|
||||||
?: NetworkUtil.findConnectedInterface(world!!, pos, side)?.also {
|
|
||||||
destinationCache[side.ordinal] = WeakReference(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun cableDisconnected(itf: SwitchInterface) {
|
|
||||||
macTable.entries.filter {
|
|
||||||
it.value == itf.side
|
|
||||||
}.forEach {
|
|
||||||
macTable.remove(it.key)
|
|
||||||
}
|
|
||||||
destinationCache[itf.side.ordinal] = null
|
|
||||||
flood(NetworkSplitFrame(itf.macAddress), itf)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun tick() {
|
fun tick() {
|
||||||
|
@ -173,10 +158,6 @@ class SwitchBlockEntity(pos: BlockPos, state: BlockState): BlockEntity(PhyBlockE
|
||||||
override fun send(frame: EthernetFrame) {
|
override fun send(frame: EthernetFrame) {
|
||||||
switch.get()?.findDestination(this)?.receive(frame)
|
switch.get()?.findDestination(this)?.receive(frame)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun cableDisconnected() {
|
|
||||||
switch.get()?.cableDisconnected(this)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,11 +12,9 @@ import net.shadowfacts.phycon.api.packet.Packet
|
||||||
import net.shadowfacts.phycon.api.util.IPAddress
|
import net.shadowfacts.phycon.api.util.IPAddress
|
||||||
import net.shadowfacts.phycon.block.DeviceBlockEntity
|
import net.shadowfacts.phycon.block.DeviceBlockEntity
|
||||||
import net.shadowfacts.phycon.block.FaceDeviceBlock
|
import net.shadowfacts.phycon.block.FaceDeviceBlock
|
||||||
import net.shadowfacts.phycon.frame.NetworkSplitFrame
|
|
||||||
import net.shadowfacts.phycon.init.PhyBlockEntities
|
import net.shadowfacts.phycon.init.PhyBlockEntities
|
||||||
import net.shadowfacts.phycon.packet.*
|
import net.shadowfacts.phycon.packet.*
|
||||||
import net.shadowfacts.phycon.util.ClientConfigurableDevice
|
import net.shadowfacts.phycon.util.ClientConfigurableDevice
|
||||||
import java.lang.ref.WeakReference
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author shadowfacts
|
* @author shadowfacts
|
||||||
|
@ -38,8 +36,6 @@ class P2PReceiverBlockEntity(pos: BlockPos, state: BlockState): DeviceBlockEntit
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val REQUEST_INVENTORY_TIMEOUT: Long = 100 // ticks
|
|
||||||
|
|
||||||
fun provideItemStorage(be: P2PReceiverBlockEntity, side: Direction): Storage<ItemVariant>? {
|
fun provideItemStorage(be: P2PReceiverBlockEntity, side: Direction): Storage<ItemVariant>? {
|
||||||
if (side == be.cachedState[FaceDeviceBlock.FACING]) {
|
if (side == be.cachedState[FaceDeviceBlock.FACING]) {
|
||||||
return be.getTargetInventory()
|
return be.getTargetInventory()
|
||||||
|
@ -50,26 +46,21 @@ class P2PReceiverBlockEntity(pos: BlockPos, state: BlockState): DeviceBlockEntit
|
||||||
|
|
||||||
var target: IPAddress? = null
|
var target: IPAddress? = null
|
||||||
var status = Status.NO_TARGET
|
var status = Status.NO_TARGET
|
||||||
private var requestTimestamp: Long = 0
|
|
||||||
|
|
||||||
var clientObserver: (() -> Unit)? = null
|
var clientObserver: (() -> Unit)? = null
|
||||||
|
|
||||||
private var isFirstTick = true
|
private var isFirstTick = true
|
||||||
private var targetInventory: WeakReference<Storage<ItemVariant>>? = null
|
// todo: need some way of removing this when there's no network path to the p2p interface
|
||||||
|
private var targetInventory: Storage<ItemVariant>? = null
|
||||||
|
|
||||||
override fun handle(packet: Packet) {
|
override fun handle(packet: Packet) {
|
||||||
when (packet) {
|
when (packet) {
|
||||||
is PongPacket -> if (packet.source == target) status = Status.OK
|
is PongPacket -> if (packet.source == target) status = Status.OK
|
||||||
is ReadItemStoragePacket -> targetInventory = WeakReference(packet.inventory)
|
is ReadItemStoragePacket -> targetInventory = packet.inventory
|
||||||
is DeviceRemovedPacket -> if (packet.source == target) targetInventory = null
|
is DeviceRemovedPacket -> if (packet.source == target) targetInventory = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun handleNetworkSplit() {
|
|
||||||
super.handleNetworkSplit()
|
|
||||||
targetInventory = null
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun tick() {
|
override fun tick() {
|
||||||
super.tick()
|
super.tick()
|
||||||
|
|
||||||
|
@ -83,12 +74,10 @@ class P2PReceiverBlockEntity(pos: BlockPos, state: BlockState): DeviceBlockEntit
|
||||||
if (target == null) {
|
if (target == null) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
if (targetInventory?.get() == null && (counter - requestTimestamp) >= REQUEST_INVENTORY_TIMEOUT) {
|
if (targetInventory == null) {
|
||||||
status = Status.WAITING_FOR_RESPONSE
|
|
||||||
requestTimestamp = counter
|
|
||||||
sendPacket(RequestInventoryPacket(RequestInventoryPacket.Kind.SIDED, ipAddress, target!!))
|
sendPacket(RequestInventoryPacket(RequestInventoryPacket.Kind.SIDED, ipAddress, target!!))
|
||||||
}
|
}
|
||||||
return targetInventory?.get()
|
return targetInventory
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateStatus() {
|
private fun updateStatus() {
|
||||||
|
|
|
@ -18,7 +18,6 @@ import net.shadowfacts.phycon.api.packet.Packet
|
||||||
import net.shadowfacts.phycon.api.util.IPAddress
|
import net.shadowfacts.phycon.api.util.IPAddress
|
||||||
import net.shadowfacts.phycon.block.DeviceBlockEntity
|
import net.shadowfacts.phycon.block.DeviceBlockEntity
|
||||||
import net.shadowfacts.phycon.component.*
|
import net.shadowfacts.phycon.component.*
|
||||||
import net.shadowfacts.phycon.frame.NetworkSplitFrame
|
|
||||||
import net.shadowfacts.phycon.packet.*
|
import net.shadowfacts.phycon.packet.*
|
||||||
import net.shadowfacts.phycon.util.NetworkUtil
|
import net.shadowfacts.phycon.util.NetworkUtil
|
||||||
import java.lang.ref.WeakReference
|
import java.lang.ref.WeakReference
|
||||||
|
@ -39,8 +38,7 @@ abstract class AbstractTerminalBlockEntity(type: BlockEntityType<*>, pos: BlockP
|
||||||
// the locate/insertion timeouts are only 1 tick because that's long enough to hear from every device on the network
|
// the locate/insertion timeouts are only 1 tick because that's long enough to hear from every device on the network
|
||||||
// in a degraded state (when there's latency in the network), not handling interface priorities correctly is acceptable
|
// in a degraded state (when there's latency in the network), not handling interface priorities correctly is acceptable
|
||||||
val LOCATE_REQUEST_TIMEOUT: Long = 1 // ticks
|
val LOCATE_REQUEST_TIMEOUT: Long = 1 // ticks
|
||||||
val INSERTION_TIMEOUT: Long = 1 // ticks
|
val INSERTION_TIMEOUT: Long = 1
|
||||||
val REQUEST_INVENTORY_TIMEOUT: Long = 1 // ticks
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected val inventoryCache = mutableMapOf<IPAddress, GroupedItemInvView>()
|
protected val inventoryCache = mutableMapOf<IPAddress, GroupedItemInvView>()
|
||||||
|
@ -50,8 +48,8 @@ abstract class AbstractTerminalBlockEntity(type: BlockEntityType<*>, pos: BlockP
|
||||||
override val pendingInsertions = mutableListOf<PendingInsertion>()
|
override val pendingInsertions = mutableListOf<PendingInsertion>()
|
||||||
override val dispatchStackTimeout = INSERTION_TIMEOUT
|
override val dispatchStackTimeout = INSERTION_TIMEOUT
|
||||||
|
|
||||||
|
private var observers = 0
|
||||||
val cachedNetItems = ItemStackCollections.intMap()
|
val cachedNetItems = ItemStackCollections.intMap()
|
||||||
private var requestInventoryTimestamp: Long? = null
|
|
||||||
|
|
||||||
// todo: multiple players could have the terminal open simultaneously
|
// todo: multiple players could have the terminal open simultaneously
|
||||||
var netItemObserver: WeakReference<NetItemObserver>? = null
|
var netItemObserver: WeakReference<NetItemObserver>? = null
|
||||||
|
@ -70,11 +68,6 @@ abstract class AbstractTerminalBlockEntity(type: BlockEntityType<*>, pos: BlockP
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun handleNetworkSplit() {
|
|
||||||
super.handleNetworkSplit()
|
|
||||||
inventoryCache.clear()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun handle(packet: Packet) {
|
override fun handle(packet: Packet) {
|
||||||
when (packet) {
|
when (packet) {
|
||||||
is ReadGroupedInventoryPacket -> handleReadInventory(packet)
|
is ReadGroupedInventoryPacket -> handleReadInventory(packet)
|
||||||
|
@ -171,26 +164,14 @@ abstract class AbstractTerminalBlockEntity(type: BlockEntityType<*>, pos: BlockP
|
||||||
if (!world!!.isClient) {
|
if (!world!!.isClient) {
|
||||||
finishPendingRequests()
|
finishPendingRequests()
|
||||||
finishTimedOutPendingInsertions()
|
finishTimedOutPendingInsertions()
|
||||||
|
}
|
||||||
|
|
||||||
if (counter % 20 == 0L) {
|
if (counter % 20 == 0L && !world!!.isClient) {
|
||||||
beginInsertions()
|
beginInsertions()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (requestInventoryTimestamp != null && (counter - requestInventoryTimestamp!!) >= REQUEST_INVENTORY_TIMEOUT) {
|
|
||||||
updateAndSync()
|
|
||||||
requestInventoryTimestamp = null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
open fun onActivate(player: PlayerEntity) {
|
open fun onActivate(player: PlayerEntity) {
|
||||||
if (!world!!.isClient) {
|
|
||||||
updateAndSync()
|
|
||||||
|
|
||||||
inventoryCache.clear()
|
|
||||||
sendPacket(RequestInventoryPacket(RequestInventoryPacket.Kind.GROUPED, ipAddress))
|
|
||||||
requestInventoryTimestamp = counter
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun requestItem(stack: ItemStack, amount: Int = stack.count) {
|
fun requestItem(stack: ItemStack, amount: Int = stack.count) {
|
||||||
|
@ -231,8 +212,7 @@ abstract class AbstractTerminalBlockEntity(type: BlockEntityType<*>, pos: BlockP
|
||||||
// as with extracting, we "know" the new amounts and so can update instantly without actually sending out packets
|
// as with extracting, we "know" the new amounts and so can update instantly without actually sending out packets
|
||||||
updateAndSync()
|
updateAndSync()
|
||||||
|
|
||||||
// don't start a second insertion, since remaining will be dispatched at the next beginInsertions
|
return remaining
|
||||||
return ItemStack.EMPTY
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onInventoryChanged(inv: Inventory) {
|
override fun onInventoryChanged(inv: Inventory) {
|
||||||
|
|
|
@ -33,9 +33,11 @@ class CraftingTerminalBlockEntity(pos: BlockPos, state: BlockState): AbstractTer
|
||||||
private val completedCraftingStackRequests = LinkedList<CraftingStackLocateRequest>()
|
private val completedCraftingStackRequests = LinkedList<CraftingStackLocateRequest>()
|
||||||
|
|
||||||
override fun onActivate(player: PlayerEntity) {
|
override fun onActivate(player: PlayerEntity) {
|
||||||
super.onActivate(player)
|
|
||||||
|
|
||||||
if (!world!!.isClient) {
|
if (!world!!.isClient) {
|
||||||
|
updateAndSync()
|
||||||
|
|
||||||
|
inventoryCache.clear()
|
||||||
|
sendPacket(RequestInventoryPacket(RequestInventoryPacket.Kind.GROUPED, ipAddress))
|
||||||
val factory = object: ExtendedScreenHandlerFactory {
|
val factory = object: ExtendedScreenHandlerFactory {
|
||||||
override fun createMenu(syncId: Int, playerInv: PlayerInventory, player: PlayerEntity): ScreenHandler? {
|
override fun createMenu(syncId: Int, playerInv: PlayerInventory, player: PlayerEntity): ScreenHandler? {
|
||||||
return CraftingTerminalScreenHandler(syncId, playerInv, this@CraftingTerminalBlockEntity)
|
return CraftingTerminalScreenHandler(syncId, playerInv, this@CraftingTerminalBlockEntity)
|
||||||
|
|
|
@ -18,9 +18,11 @@ import net.shadowfacts.phycon.packet.RequestInventoryPacket
|
||||||
class TerminalBlockEntity(pos: BlockPos, state: BlockState): AbstractTerminalBlockEntity(PhyBlockEntities.TERMINAL, pos, state) {
|
class TerminalBlockEntity(pos: BlockPos, state: BlockState): AbstractTerminalBlockEntity(PhyBlockEntities.TERMINAL, pos, state) {
|
||||||
|
|
||||||
override fun onActivate(player: PlayerEntity) {
|
override fun onActivate(player: PlayerEntity) {
|
||||||
super.onActivate(player)
|
|
||||||
|
|
||||||
if (!world!!.isClient) {
|
if (!world!!.isClient) {
|
||||||
|
updateAndSync()
|
||||||
|
|
||||||
|
inventoryCache.clear()
|
||||||
|
sendPacket(RequestInventoryPacket(RequestInventoryPacket.Kind.GROUPED, ipAddress))
|
||||||
val factory = object: ExtendedScreenHandlerFactory {
|
val factory = object: ExtendedScreenHandlerFactory {
|
||||||
override fun createMenu(syncId: Int, playerInv: PlayerInventory, player: PlayerEntity): ScreenHandler {
|
override fun createMenu(syncId: Int, playerInv: PlayerInventory, player: PlayerEntity): ScreenHandler {
|
||||||
return TerminalScreenHandler(syncId, playerInv, this@TerminalBlockEntity)
|
return TerminalScreenHandler(syncId, playerInv, this@TerminalBlockEntity)
|
||||||
|
|
|
@ -36,9 +36,7 @@ interface NetworkStackDispatcher<Insertion: NetworkStackDispatcher.PendingInsert
|
||||||
insertion.results.add(packet.capacity to packet.stackReceiver)
|
insertion.results.add(packet.capacity to packet.stackReceiver)
|
||||||
if (insertion.isFinishable(this)) {
|
if (insertion.isFinishable(this)) {
|
||||||
val remaining = finishInsertion(insertion)
|
val remaining = finishInsertion(insertion)
|
||||||
if (!remaining.isEmpty) {
|
// todo: do something with remaining
|
||||||
pendingInsertions.add(createPendingInsertion(remaining))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,13 +56,12 @@ interface NetworkStackDispatcher<Insertion: NetworkStackDispatcher.PendingInsert
|
||||||
// copy the insertion stack so subclasses that override this method can still see the originally dispatched stack after the super call
|
// copy the insertion stack so subclasses that override this method can still see the originally dispatched stack after the super call
|
||||||
val remaining = insertion.stack.copy()
|
val remaining = insertion.stack.copy()
|
||||||
while (!remaining.isEmpty && sortedResults.isNotEmpty()) {
|
while (!remaining.isEmpty && sortedResults.isNotEmpty()) {
|
||||||
val (capacity, receiver) = sortedResults.removeFirst()
|
val (capacity, receivingInterface) = sortedResults.removeFirst()
|
||||||
if (capacity <= 0) continue
|
if (capacity <= 0) continue
|
||||||
val sentCount = min(capacity, remaining.count)
|
val copy = remaining.copyWithCount(min(capacity, remaining.count))
|
||||||
val copy = remaining.copyWithCount(sentCount)
|
sendPacket(ItemStackPacket(copy, ipAddress, receivingInterface.ipAddress))
|
||||||
sendPacket(ItemStackPacket(copy, ipAddress, receiver.ipAddress))
|
|
||||||
// todo: the destination should confirm how much was actually inserted, in case of race condition
|
// todo: the destination should confirm how much was actually inserted, in case of race condition
|
||||||
remaining.count -= sentCount
|
remaining.count -= capacity
|
||||||
}
|
}
|
||||||
|
|
||||||
return remaining
|
return remaining
|
||||||
|
@ -92,11 +89,7 @@ fun <Self, Insertion: NetworkStackDispatcher.PendingInsertion<Insertion>> Self.f
|
||||||
|
|
||||||
val finishable = pendingInsertions.filter { it.isFinishable(this) }
|
val finishable = pendingInsertions.filter { it.isFinishable(this) }
|
||||||
// finishInsertion removes the object from pendingInsertions
|
// finishInsertion removes the object from pendingInsertions
|
||||||
for (insertion in finishable) {
|
finishable.forEach(::finishInsertion)
|
||||||
val remaining = finishInsertion(insertion)
|
// todo: do something with remaining?
|
||||||
if (!remaining.isEmpty) {
|
|
||||||
pendingInsertions.add(createPendingInsertion(remaining))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// todo: if a timed-out insertion can't be finished, we should probably retry after some time (exponential backoff?)
|
// todo: if a timed-out insertion can't be finished, we should probably retry after some time (exponential backoff?)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package net.shadowfacts.phycon.frame
|
package net.shadowfacts.phycon.frame
|
||||||
|
|
||||||
import net.shadowfacts.phycon.api.Interface
|
|
||||||
import net.shadowfacts.phycon.api.util.IPAddress
|
import net.shadowfacts.phycon.api.util.IPAddress
|
||||||
import net.shadowfacts.phycon.api.util.MACAddress
|
import net.shadowfacts.phycon.api.util.MACAddress
|
||||||
|
|
||||||
|
|
|
@ -1,8 +0,0 @@
|
||||||
package net.shadowfacts.phycon.frame
|
|
||||||
|
|
||||||
import net.shadowfacts.phycon.api.util.MACAddress
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author shadowfacts
|
|
||||||
*/
|
|
||||||
class NetworkSplitFrame(source: MACAddress): BaseFrame(source, MACAddress.BROADCAST)
|
|
Loading…
Reference in New Issue