This commit is contained in:
2026-05-01 00:27:16 +02:00
9 changed files with 35 additions and 20 deletions

View File

@@ -10,6 +10,7 @@ import net.minecraft.network.chat.Component
import net.minecraft.server.level.ServerLevel import net.minecraft.server.level.ServerLevel
import net.minecraft.server.level.ServerPlayer import net.minecraft.server.level.ServerPlayer
import net.minecraft.world.ContainerHelper import net.minecraft.world.ContainerHelper
import net.minecraft.world.Containers
import net.minecraft.world.InteractionResult import net.minecraft.world.InteractionResult
import net.minecraft.world.MenuProvider import net.minecraft.world.MenuProvider
import net.minecraft.world.entity.player.Inventory import net.minecraft.world.entity.player.Inventory
@@ -60,6 +61,7 @@ class RelayEntity(blockPos: BlockPos, blockState: BlockState): SingleDeviceBlock
fun computeRelayCapacity(): Int = computeRelayBufferSize() + computeRelayQueueSize() fun computeRelayCapacity(): Int = computeRelayBufferSize() + computeRelayQueueSize()
val queue = mutableListOf<Networking.ClassicPacket>() val queue = mutableListOf<Networking.ClassicPacket>()
val justReceived = mutableListOf<Networking.ClassicPacket>()
var activityTickLeft = 0 var activityTickLeft = 0
var ticksUntilQueue = 0 var ticksUntilQueue = 0
@@ -68,8 +70,8 @@ class RelayEntity(blockPos: BlockPos, blockState: BlockState): SingleDeviceBlock
override fun received(message: Networking.Message) { override fun received(message: Networking.Message) {
super.received(message) super.received(message)
if(message.sender == this) return if(message.sender == this) return
if(message is Networking.ClassicPacket && message.hopCount < 5 && queue.size < computeRelayCapacity()) { if(message is Networking.ClassicPacket && message.hopCount < 5 && (queue.size + justReceived.size) < computeRelayCapacity()) {
queue.addLast(message) justReceived.addLast(message)
activityTickLeft = 20 activityTickLeft = 20
markChanged() markChanged()
} }
@@ -80,7 +82,7 @@ class RelayEntity(blockPos: BlockPos, blockState: BlockState): SingleDeviceBlock
buf.writeVarInt(computeRelayInterval()) buf.writeVarInt(computeRelayInterval())
buf.writeVarInt(computeRelayBufferSize()) buf.writeVarInt(computeRelayBufferSize())
buf.writeVarInt(computeRelayQueueSize()) buf.writeVarInt(computeRelayQueueSize())
buf.writeVarInt(queue.size) buf.writeVarInt(queue.size + justReceived.size)
} }
override fun writeFullStateCommit(buf: FriendlyByteBuf) { override fun writeFullStateCommit(buf: FriendlyByteBuf) {
@@ -120,6 +122,10 @@ class RelayEntity(blockPos: BlockPos, blockState: BlockState): SingleDeviceBlock
activityTickLeft-- activityTickLeft--
deviceNode.markChanged() deviceNode.markChanged()
} }
queue.addAll(justReceived)
justReceived.clear()
val cap = computeRelayCapacity()
while(queue.size > cap) queue.removeLast()
ticksUntilQueue-- ticksUntilQueue--
if(ticksUntilQueue <= 0) { if(ticksUntilQueue <= 0) {
ticksUntilQueue = computeRelayInterval() ticksUntilQueue = computeRelayInterval()
@@ -129,8 +135,6 @@ class RelayEntity(blockPos: BlockPos, blockState: BlockState): SingleDeviceBlock
} }
} }
deviceNode.markChanged() deviceNode.markChanged()
val cap = computeRelayCapacity()
while(queue.size > cap) queue.removeLast()
} }
override fun getMachineBlockPosition() = blockPos!! override fun getMachineBlockPosition() = blockPos!!
@@ -176,4 +180,15 @@ class RelayBlock: DeviceBlock(Properties.of().sound(SoundType.METAL)) {
} }
return InteractionResult.SUCCESS return InteractionResult.SUCCESS
} }
override fun onRemove(
blockState: BlockState,
level: Level,
blockPos: BlockPos,
blockState2: BlockState,
bl: Boolean
) {
Containers.dropContentsOnDestroy(blockState, blockState2, level, blockPos)
super.onRemove(blockState, level, blockPos, blockState2, bl)
}
} }

View File

@@ -150,9 +150,9 @@ open class DeviceNode(_address: UUID? = null) {
fun getReachable(): Set<DeviceNode> { fun getReachable(): Set<DeviceNode> {
if(reachableCache == null) { if(reachableCache == null) {
reachableCache = computeReachable(); reachableCache = computeReachable()
} }
return reachableCache!!; return reachableCache!!
} }
open fun invalidateReachableCache() { open fun invalidateReachableCache() {
@@ -165,21 +165,21 @@ open class DeviceNode(_address: UUID? = null) {
fun computeReachable(): Set<DeviceNode> { fun computeReachable(): Set<DeviceNode> {
if(reachability == Visibility.NONE) { if(reachability == Visibility.NONE) {
return setOf(); return setOf()
} }
if(reachability == Visibility.SOME) { if(reachability == Visibility.SOME) {
return getPreferredFew() return getPreferredFew()
} }
if(reachability == Visibility.DIRECT) { if(reachability == Visibility.DIRECT) {
return connections.minus(this); return connections.minus(this)
} }
if(reachability == Visibility.NETWORK) { if(reachability == Visibility.NETWORK) {
// absolute cinema // absolute cinema
val working = HashSet<DeviceNode>(); val working = HashSet<DeviceNode>()
val pending = mutableListOf(this); val pending = mutableListOf(this)
var iterCount = 0; var iterCount = 0
while(iterCount < maxHopCount && pending.isNotEmpty()) { while(iterCount < maxHopCount && pending.isNotEmpty()) {
iterCount++; iterCount++
val subnode = pending.removeFirst() val subnode = pending.removeFirst()
if(subnode in working) continue if(subnode in working) continue
working.add(subnode) working.add(subnode)
@@ -192,20 +192,20 @@ open class DeviceNode(_address: UUID? = null) {
} }
} }
// cannot send to itself! // cannot send to itself!
working.remove(this); working.remove(this)
return working; return working
} }
throw NotImplementedError("visibility not implemented"); throw NotImplementedError("visibility not implemented")
} }
fun connectTo(other: DeviceNode) { fun connectTo(other: DeviceNode) {
this.directConnectTo(other); this.directConnectTo(other)
other.directConnectTo(this); other.directConnectTo(this)
} }
fun disconnectFrom(other: DeviceNode) { fun disconnectFrom(other: DeviceNode) {
this.directDisconnectFrom(other); this.directDisconnectFrom(other)
other.directDisconnectFrom(this); other.directDisconnectFrom(this)
} }
fun directConnectTo(other: DeviceNode) { fun directConnectTo(other: DeviceNode) {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 241 B

After

Width:  |  Height:  |  Size: 471 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 618 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 394 B

After

Width:  |  Height:  |  Size: 623 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 417 B

After

Width:  |  Height:  |  Size: 618 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 221 B

After

Width:  |  Height:  |  Size: 465 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 473 B

After

Width:  |  Height:  |  Size: 579 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 472 B

After

Width:  |  Height:  |  Size: 602 B