Allow cable connections on individual sides to be disabled

This commit is contained in:
Shadowfacts 2019-10-28 12:38:55 -04:00
parent 04f0ae6d41
commit f9fd00f611
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
3 changed files with 50 additions and 16 deletions

View File

@ -5,12 +5,16 @@ import net.fabricmc.api.Environment
import net.minecraft.block.* import net.minecraft.block.*
import net.minecraft.block.piston.PistonBehavior import net.minecraft.block.piston.PistonBehavior
import net.minecraft.entity.EntityContext import net.minecraft.entity.EntityContext
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.item.ItemPlacementContext import net.minecraft.item.ItemPlacementContext
import net.minecraft.state.StateFactory import net.minecraft.state.StateFactory
import net.minecraft.state.property.BooleanProperty import net.minecraft.state.property.EnumProperty
import net.minecraft.util.Hand
import net.minecraft.util.Identifier import net.minecraft.util.Identifier
import net.minecraft.util.hit.BlockHitResult
import net.minecraft.util.math.BlockPos import net.minecraft.util.math.BlockPos
import net.minecraft.util.math.Direction import net.minecraft.util.math.Direction
import net.minecraft.util.math.Vec3d
import net.minecraft.util.shape.VoxelShape import net.minecraft.util.shape.VoxelShape
import net.minecraft.util.shape.VoxelShapes import net.minecraft.util.shape.VoxelShapes
import net.minecraft.world.BlockView import net.minecraft.world.BlockView
@ -19,6 +23,7 @@ import net.minecraft.world.World
import net.shadowfacts.phycon.PhysicalConnectivity import net.shadowfacts.phycon.PhysicalConnectivity
import net.shadowfacts.phycon.api.NetworkCable import net.shadowfacts.phycon.api.NetworkCable
import net.shadowfacts.phycon.api.NetworkComponent import net.shadowfacts.phycon.api.NetworkComponent
import net.shadowfacts.phycon.util.CableConnection
import java.util.* import java.util.*
/** /**
@ -38,13 +43,13 @@ class CableBlock: Block(Settings.of(CABLE_MATERIAL)), NetworkCable {
Direction.EAST to createCuboidShape(10.0, 6.0, 6.0, 16.0, 10.0, 10.0) Direction.EAST to createCuboidShape(10.0, 6.0, 6.0, 16.0, 10.0, 10.0)
) )
private val SHAPE_CACHE = mutableMapOf<BlockState, VoxelShape>() private val SHAPE_CACHE = mutableMapOf<BlockState, VoxelShape>()
val CONNECTIONS: Map<Direction, BooleanProperty> = Direction.values().associate { it to BooleanProperty.of(it.name.toLowerCase()) } val CONNECTIONS: Map<Direction, EnumProperty<CableConnection>> = Direction.values().associate { it to EnumProperty.of(it.name.toLowerCase(), CableConnection::class.java) }
fun getShape(state: BlockState): VoxelShape { fun getShape(state: BlockState): VoxelShape {
return SHAPE_CACHE.getOrPut(state) { return SHAPE_CACHE.getOrPut(state) {
var shape = CENTER_SHAPE var shape = CENTER_SHAPE
for ((side, prop) in CONNECTIONS) { for ((side, prop) in CONNECTIONS) {
if (state[prop]) { if (state[prop] == CableConnection.ON) {
shape = VoxelShapes.union(shape, SIDE_SHAPES[side]) shape = VoxelShapes.union(shape, SIDE_SHAPES[side])
} }
} }
@ -55,14 +60,14 @@ class CableBlock: Block(Settings.of(CABLE_MATERIAL)), NetworkCable {
init { init {
defaultState = CONNECTIONS.values.fold(stateFactory.defaultState) { acc, prop -> defaultState = CONNECTIONS.values.fold(stateFactory.defaultState) { acc, prop ->
acc.with(prop, false) acc.with(prop, CableConnection.OFF)
} }
} }
override fun getNetworkConnectedSides(state: BlockState, world: World, pos: BlockPos): Set<Direction> { override fun getNetworkConnectedSides(state: BlockState, world: World, pos: BlockPos): Set<Direction> {
val set = EnumSet.noneOf(Direction::class.java) val set = EnumSet.noneOf(Direction::class.java)
for ((side, prop) in CONNECTIONS) { for ((side, prop) in CONNECTIONS) {
if (state[prop]) { if (state[prop] == CableConnection.ON) {
set.add(side) set.add(side)
} }
} }
@ -78,17 +83,32 @@ class CableBlock: Block(Settings.of(CABLE_MATERIAL)), NetworkCable {
override fun getPlacementState(context: ItemPlacementContext): BlockState { override fun getPlacementState(context: ItemPlacementContext): BlockState {
return CONNECTIONS.entries.fold(defaultState, { acc, (dir, prop) -> return CONNECTIONS.entries.fold(defaultState, { acc, (dir, prop) ->
acc.with(prop, hasConnectionInDirection(context.world, context.blockPos, dir)) acc.with(prop, getConnectionStateInDirection(context.world, context.blockPos, dir))
}) })
} }
override fun getStateForNeighborUpdate(state: BlockState, side: Direction, neighborState: BlockState, world: IWorld, blockPos_1: BlockPos, blockPos_2: BlockPos): BlockState { override fun getStateForNeighborUpdate(state: BlockState, side: Direction, neighborState: BlockState, world: IWorld, blockPos_1: BlockPos, blockPos_2: BlockPos): BlockState {
return state.with(CONNECTIONS[side], hasConnectionInDirection(world, blockPos_1, side)) val prop = CONNECTIONS[side]
val current = state[prop]
return when (current) {
CableConnection.DISABLED -> state
else -> state.with(prop, getConnectionStateInDirection(world, blockPos_1, side))
}
} }
private fun hasConnectionInDirection(world: IWorld, pos: BlockPos, direction: Direction): Boolean { private fun getConnectionStateInDirection(world: IWorld, pos: BlockPos, direction: Direction): CableConnection {
val block = world.getBlockState(pos.offset(direction)).block val state = world.getBlockState(pos.offset(direction))
return block is NetworkComponent return when (state.block) {
this -> {
val prop = CONNECTIONS[direction.opposite]
when (state[prop]) {
CableConnection.DISABLED -> CableConnection.DISABLED
else -> CableConnection.ON
}
}
is NetworkComponent -> CableConnection.ON
else -> CableConnection.OFF
}
} }
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)

View File

@ -0,0 +1,14 @@
package net.shadowfacts.phycon.util
import net.minecraft.util.StringIdentifiable
/**
* @author shadowfacts
*/
enum class CableConnection: StringIdentifiable {
ON,
OFF,
DISABLED;
override fun asString() = name.toLowerCase()
}

View File

@ -4,27 +4,27 @@
"apply": { "model": "phycon:block/cable_center" } "apply": { "model": "phycon:block/cable_center" }
}, },
{ {
"when": { "down": true }, "when": { "down": "on" },
"apply": { "model": "phycon:block/cable_side" } "apply": { "model": "phycon:block/cable_side" }
}, },
{ {
"when": { "up": true }, "when": { "up": "on" },
"apply": { "model": "phycon:block/cable_side", "x": 180 } "apply": { "model": "phycon:block/cable_side", "x": 180 }
}, },
{ {
"when": { "north": true }, "when": { "north": "on" },
"apply": { "model": "phycon:block/cable_side", "x": 270 } "apply": { "model": "phycon:block/cable_side", "x": 270 }
}, },
{ {
"when": { "south": true }, "when": { "south": "on" },
"apply": { "model": "phycon:block/cable_side", "x": 90 } "apply": { "model": "phycon:block/cable_side", "x": 90 }
}, },
{ {
"when": { "west": true }, "when": { "west": "on" },
"apply": { "model": "phycon:block/cable_side", "x": 90, "y": 90 } "apply": { "model": "phycon:block/cable_side", "x": 90, "y": 90 }
}, },
{ {
"when": { "east": true }, "when": { "east": "on" },
"apply": { "model": "phycon:block/cable_side", "x": 90, "y": 270 } "apply": { "model": "phycon:block/cable_side", "x": 90, "y": 270 }
} }
] ]