Cleanup NetworkUtil

This commit is contained in:
Shadowfacts 2019-10-26 21:49:34 -04:00
parent 0eabbf9ec1
commit 6fae09d410
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 8 additions and 3 deletions

View File

@ -14,25 +14,30 @@ import java.util.*
object NetworkUtil { object NetworkUtil {
fun findDestinations(world: World, startPos: BlockPos): List<PacketSink> { fun findDestinations(world: World, startPos: BlockPos): List<PacketSink> {
val results = mutableListOf<PacketSink>() val results = LinkedList<PacketSink>()
val visited = hashSetOf(startPos) val visited = hashSetOf(startPos)
val queue = LinkedList<BlockPos>() val queue = LinkedList<BlockPos>()
addAdjacent(queue, visited, startPos) addAdjacent(queue, visited, startPos)
while (queue.isNotEmpty()) { while (queue.isNotEmpty()) {
val pos = queue.pop() val pos = queue.pop()
val sink = PhyAttributes.PACKET_SINK.getFirstOrNull(world, pos) val sink = PhyAttributes.PACKET_SINK.getFirstOrNull(world, pos)
if (sink != null) { if (sink != null) {
results.add(sink) results.add(sink)
} }
if (pos === startPos || world.getBlockState(pos).block is NetworkCable) {
if (world.getBlockState(pos).block is NetworkCable) {
addAdjacent(queue, visited, pos) addAdjacent(queue, visited, pos)
} }
visited.add(pos) visited.add(pos)
} }
return results return results
} }
fun addAdjacent(queue: MutableList<BlockPos>, visited: Set<BlockPos>, pos: BlockPos) { private fun addAdjacent(queue: MutableList<BlockPos>, visited: Set<BlockPos>, pos: BlockPos) {
for (dir in Direction.values()) { for (dir in Direction.values()) {
val newPos = pos.offset(dir) val newPos = pos.offset(dir)
if (newPos !in visited) { if (newPos !in visited) {