diff --git a/src/main/kotlin/net/shadowfacts/phycon/network/block/cable/CableBlock.kt b/src/main/kotlin/net/shadowfacts/phycon/network/block/cable/CableBlock.kt index fd6e7a0..206d40f 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/network/block/cable/CableBlock.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/network/block/cable/CableBlock.kt @@ -5,12 +5,16 @@ import net.fabricmc.api.Environment import net.minecraft.block.* import net.minecraft.block.piston.PistonBehavior import net.minecraft.entity.EntityContext +import net.minecraft.entity.player.PlayerEntity import net.minecraft.item.ItemPlacementContext 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.hit.BlockHitResult import net.minecraft.util.math.BlockPos import net.minecraft.util.math.Direction +import net.minecraft.util.math.Vec3d import net.minecraft.util.shape.VoxelShape import net.minecraft.util.shape.VoxelShapes import net.minecraft.world.BlockView @@ -19,6 +23,7 @@ import net.minecraft.world.World import net.shadowfacts.phycon.PhysicalConnectivity import net.shadowfacts.phycon.api.NetworkCable import net.shadowfacts.phycon.api.NetworkComponent +import net.shadowfacts.phycon.util.CableConnection 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) ) private val SHAPE_CACHE = mutableMapOf() - val CONNECTIONS: Map = Direction.values().associate { it to BooleanProperty.of(it.name.toLowerCase()) } + val CONNECTIONS: Map> = Direction.values().associate { it to EnumProperty.of(it.name.toLowerCase(), CableConnection::class.java) } fun getShape(state: BlockState): VoxelShape { return SHAPE_CACHE.getOrPut(state) { var shape = CENTER_SHAPE for ((side, prop) in CONNECTIONS) { - if (state[prop]) { + if (state[prop] == CableConnection.ON) { shape = VoxelShapes.union(shape, SIDE_SHAPES[side]) } } @@ -55,14 +60,14 @@ class CableBlock: Block(Settings.of(CABLE_MATERIAL)), NetworkCable { init { 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 { val set = EnumSet.noneOf(Direction::class.java) for ((side, prop) in CONNECTIONS) { - if (state[prop]) { + if (state[prop] == CableConnection.ON) { set.add(side) } } @@ -78,17 +83,32 @@ class CableBlock: Block(Settings.of(CABLE_MATERIAL)), NetworkCable { override fun getPlacementState(context: ItemPlacementContext): BlockState { 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 { - 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 { - val block = world.getBlockState(pos.offset(direction)).block - return block is NetworkComponent + private fun getConnectionStateInDirection(world: IWorld, pos: BlockPos, direction: Direction): CableConnection { + val state = world.getBlockState(pos.offset(direction)) + 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) diff --git a/src/main/kotlin/net/shadowfacts/phycon/util/CableConnection.kt b/src/main/kotlin/net/shadowfacts/phycon/util/CableConnection.kt new file mode 100644 index 0000000..8177b11 --- /dev/null +++ b/src/main/kotlin/net/shadowfacts/phycon/util/CableConnection.kt @@ -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() +} diff --git a/src/main/resources/assets/phycon/blockstates/cable.json b/src/main/resources/assets/phycon/blockstates/cable.json index 5cf3bae..94c665d 100644 --- a/src/main/resources/assets/phycon/blockstates/cable.json +++ b/src/main/resources/assets/phycon/blockstates/cable.json @@ -4,27 +4,27 @@ "apply": { "model": "phycon:block/cable_center" } }, { - "when": { "down": true }, + "when": { "down": "on" }, "apply": { "model": "phycon:block/cable_side" } }, { - "when": { "up": true }, + "when": { "up": "on" }, "apply": { "model": "phycon:block/cable_side", "x": 180 } }, { - "when": { "north": true }, + "when": { "north": "on" }, "apply": { "model": "phycon:block/cable_side", "x": 270 } }, { - "when": { "south": true }, + "when": { "south": "on" }, "apply": { "model": "phycon:block/cable_side", "x": 90 } }, { - "when": { "west": true }, + "when": { "west": "on" }, "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 } } ]