Use cable connections in search instead of checking every direction

This commit is contained in:
Shadowfacts 2019-10-28 12:00:17 -04:00
parent dfccc5b2ec
commit c7da691ffd
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
3 changed files with 36 additions and 3 deletions

View File

@ -1,7 +1,17 @@
package net.shadowfacts.phycon.api;
import net.minecraft.block.BlockState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
import java.util.Set;
/**
* @author shadowfacts
*/
public interface NetworkCable extends NetworkComponent {
Set<Direction> getNetworkConnectedSides(BlockState state, World world, BlockPos pos);
}

View File

@ -31,9 +31,7 @@ object NetworkUtil {
results.add(sink)
}
if (world.getBlockState(pos).block is NetworkCable) {
addAdjacent(queue, visited, pos)
}
findEdges(queue, visited, world, pos)
visited.add(pos)
}
@ -41,6 +39,20 @@ object NetworkUtil {
return results
}
private fun findEdges(queue: MutableList<BlockPos>, visited: Set<BlockPos>, world: World, pos: BlockPos) {
val state = world.getBlockState(pos)
val block = state.block
if (block is NetworkCable) {
val connections = block.getNetworkConnectedSides(state, world, pos)
for (side in connections) {
val newPos = pos.offset(side)
if (newPos !in visited) {
queue.add(newPos)
}
}
}
}
private fun addAdjacent(queue: MutableList<BlockPos>, visited: Set<BlockPos>, pos: BlockPos) {
for (dir in Direction.values()) {
val newPos = pos.offset(dir)

View File

@ -15,6 +15,7 @@ import net.minecraft.util.shape.VoxelShape
import net.minecraft.util.shape.VoxelShapes
import net.minecraft.world.BlockView
import net.minecraft.world.IWorld
import net.minecraft.world.World
import net.shadowfacts.phycon.PhysicalConnectivity
import net.shadowfacts.phycon.api.NetworkCable
import net.shadowfacts.phycon.api.NetworkComponent
@ -58,6 +59,16 @@ class CableBlock: Block(Settings.of(CABLE_MATERIAL)), NetworkCable {
}
}
override fun getNetworkConnectedSides(state: BlockState, world: World, pos: BlockPos): Set<Direction> {
val set = EnumSet.noneOf(Direction::class.java)
for ((side, prop) in CONNECTIONS) {
if (state[prop]) {
set.add(side)
}
}
return set
}
override fun appendProperties(builder: StateFactory.Builder<Block, BlockState>) {
super.appendProperties(builder)
CONNECTIONS.values.forEach {