From 700817919ab4a2537c838b6ea390f3ca32f9ee5e Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 21 Feb 2021 15:35:20 -0500 Subject: [PATCH] Move face device block logic to separate base class The extractor currently isn't considered a "face block" because its network connection is always in the opposite direction of its facing, unlike the interace. --- .../phycon/network/FaceDeviceBlock.kt | 102 ++++++++++++++++++ .../block/netinterface/InterfaceBlock.kt | 97 ++--------------- .../netinterface/InterfaceBlockEntity.kt | 3 +- .../phycon/blockstates/network_interface.json | 1 - 4 files changed, 115 insertions(+), 88 deletions(-) create mode 100644 src/main/kotlin/net/shadowfacts/phycon/network/FaceDeviceBlock.kt diff --git a/src/main/kotlin/net/shadowfacts/phycon/network/FaceDeviceBlock.kt b/src/main/kotlin/net/shadowfacts/phycon/network/FaceDeviceBlock.kt new file mode 100644 index 0000000..e81c754 --- /dev/null +++ b/src/main/kotlin/net/shadowfacts/phycon/network/FaceDeviceBlock.kt @@ -0,0 +1,102 @@ +package net.shadowfacts.phycon.network + +import net.minecraft.block.Block +import net.minecraft.block.BlockState +import net.minecraft.block.ShapeContext +import net.minecraft.item.ItemPlacementContext +import net.minecraft.state.StateManager +import net.minecraft.state.property.EnumProperty +import net.minecraft.state.property.Properties +import net.minecraft.util.math.BlockPos +import net.minecraft.util.math.Direction +import net.minecraft.util.shape.VoxelShape +import net.minecraft.util.shape.VoxelShapes +import net.minecraft.world.BlockView +import net.minecraft.world.World +import net.minecraft.world.WorldAccess +import net.shadowfacts.phycon.api.Interface +import net.shadowfacts.phycon.api.NetworkComponentBlock +import net.shadowfacts.phycon.network.block.cable.CableBlock +import java.util.* + + +/** + * @author shadowfacts + */ +abstract class FaceDeviceBlock(settings: Settings): DeviceBlock(settings) { + companion object { + val FACING = Properties.FACING + val CABLE_CONNECTION = EnumProperty.of("cable_connection", Direction::class.java) + } + + protected abstract val faceThickness: Double + protected abstract val faceShapes: Map + private val centerShapes: Map by lazy { + mapOf( + Direction.DOWN to createCuboidShape(6.0, faceThickness, 6.0, 10.0, 10.0, 10.0), + Direction.UP to createCuboidShape(6.0, 6.0, 6.0, 10.0, 16.0 - faceThickness, 10.0), + Direction.NORTH to createCuboidShape(6.0, 6.0, faceThickness, 10.0, 10.0, 10.0), + Direction.SOUTH to createCuboidShape(6.0, 6.0, 6.0, 10.0, 10.0, 16.0 - faceThickness), + Direction.WEST to createCuboidShape(faceThickness, 6.0, 6.0, 10.0, 10.0, 10.0), + Direction.EAST to createCuboidShape(6.0, 6.0, 6.0, 16.0 - faceThickness, 10.0, 10.0) + ) + } + private val shapeCache = mutableMapOf, VoxelShape>() + + fun getShape(facing: Direction, cableConnection: Direction): VoxelShape { + return shapeCache.getOrPut(facing to cableConnection) { + VoxelShapes.union( + faceShapes[facing], + centerShapes[facing], + CableBlock.SIDE_SHAPES[cableConnection] + ) + } + } + + override fun getNetworkConnectedSides(state: BlockState, world: WorldAccess, pos: BlockPos): Collection { + return EnumSet.of(state[CABLE_CONNECTION]) + } + + override fun getNetworkInterfaceForSide(side: Direction, state: BlockState, world: WorldAccess, pos: BlockPos): Interface? { + return if (side == state[FACING]) { + null + } else { + getBlockEntity(world, pos) + } + } + + override fun appendProperties(builder: StateManager.Builder) { + super.appendProperties(builder) + builder.add(FACING) + builder.add(CABLE_CONNECTION) + } + + override fun getPlacementState(context: ItemPlacementContext): BlockState? { + val facing = if (context.player?.isSneaking == true) context.side.opposite else context.playerFacing.opposite + val cableConnection = getCableConnectedSide(context.world, context.blockPos) ?: facing.opposite + return defaultState + .with(FACING, facing) + .with(CABLE_CONNECTION, cableConnection) + } + + protected fun getCableConnectedSide(world: World, pos: BlockPos): Direction? { + for (side in Direction.values()) { + val offsetPos = pos.offset(side) + if (world.getBlockState(offsetPos) is NetworkComponentBlock) { + return side + } + } + return null + } + + override fun getStateForNeighborUpdate(state: BlockState, side: Direction, neighborState: BlockState, world: WorldAccess, pos: BlockPos, neighborPos: BlockPos): BlockState { + if (neighborState.block is NetworkComponentBlock && world.getBlockState(pos.offset(state[CABLE_CONNECTION])).block !is NetworkComponentBlock) { + return state.with(CABLE_CONNECTION, side) + } + return state + } + + override fun getOutlineShape(state: BlockState, world: BlockView, pos: BlockPos, context: ShapeContext): VoxelShape { + return getShape(state[FACING], state[CABLE_CONNECTION]) + } +} \ No newline at end of file diff --git a/src/main/kotlin/net/shadowfacts/phycon/network/block/netinterface/InterfaceBlock.kt b/src/main/kotlin/net/shadowfacts/phycon/network/block/netinterface/InterfaceBlock.kt index cab6826..0a091ff 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/network/block/netinterface/InterfaceBlock.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/network/block/netinterface/InterfaceBlock.kt @@ -4,103 +4,39 @@ import alexiil.mc.lib.attributes.AttributeList import alexiil.mc.lib.attributes.AttributeProvider import net.minecraft.block.* import net.minecraft.entity.LivingEntity -import net.minecraft.item.ItemPlacementContext import net.minecraft.item.ItemStack -import net.minecraft.state.StateManager -import net.minecraft.state.property.EnumProperty -import net.minecraft.state.property.Properties import net.minecraft.util.Identifier import net.minecraft.util.math.BlockPos import net.minecraft.util.math.Direction -import net.minecraft.util.shape.VoxelShape -import net.minecraft.util.shape.VoxelShapes import net.minecraft.world.BlockView import net.minecraft.world.World -import net.minecraft.world.WorldAccess import net.shadowfacts.phycon.PhysicalConnectivity import net.shadowfacts.phycon.api.NetworkComponentBlock -import net.shadowfacts.phycon.api.Interface -import net.shadowfacts.phycon.network.DeviceBlock -import net.shadowfacts.phycon.network.block.cable.CableBlock -import java.util.* +import net.shadowfacts.phycon.network.FaceDeviceBlock /** * @author shadowfacts */ -class InterfaceBlock: DeviceBlock(Settings.of(Material.METAL)), +class InterfaceBlock: FaceDeviceBlock(Settings.of(Material.METAL)), NetworkComponentBlock, AttributeProvider { companion object { val ID = Identifier(PhysicalConnectivity.MODID, "network_interface") - val FACING = Properties.FACING - val CABLE_CONNECTION = EnumProperty.of("cable_connection", Direction::class.java) - private val SIDE_SHAPES = mapOf( - Direction.DOWN to createCuboidShape(2.0, 0.0, 2.0, 14.0, 2.0, 14.0), - Direction.UP to createCuboidShape(2.0, 14.0, 2.0, 14.0, 16.0, 14.0), - Direction.NORTH to createCuboidShape(2.0, 2.0, 0.0, 14.0, 14.0, 2.0), - Direction.SOUTH to createCuboidShape(2.0, 2.0, 14.0, 14.0, 14.0, 16.0), - Direction.WEST to createCuboidShape(0.0, 2.0, 2.0, 2.0, 14.0, 14.0), - Direction.EAST to createCuboidShape(14.0, 2.0, 2.0, 16.0, 14.0, 14.0) - ) - private val CENTER_SHAPES = mapOf( - Direction.DOWN to createCuboidShape(6.0, 2.0, 6.0, 10.0, 10.0, 10.0), - Direction.UP to createCuboidShape(6.0, 6.0, 6.0, 10.0, 14.0, 10.0), - Direction.NORTH to createCuboidShape(6.0, 6.0, 2.0, 10.0, 10.0, 10.0), - Direction.SOUTH to createCuboidShape(6.0, 6.0, 6.0, 10.0, 10.0, 14.0), - Direction.WEST to createCuboidShape(2.0, 6.0, 6.0, 10.0, 10.0, 10.0), - Direction.EAST to createCuboidShape(6.0, 6.0, 6.0, 14.0, 10.0, 10.0) - ) - - private val shapeCache = mutableMapOf, VoxelShape>() - fun getShape(facing: Direction, cableConnection: Direction): VoxelShape { - return shapeCache.getOrPut(facing to cableConnection) { - VoxelShapes.union( - VoxelShapes.union(SIDE_SHAPES[facing], CENTER_SHAPES[facing]), - CableBlock.SIDE_SHAPES[cableConnection] - ) - } - } } - override fun getNetworkConnectedSides(state: BlockState, world: WorldAccess, pos: BlockPos): Collection { - val set = EnumSet.of(state[CABLE_CONNECTION]) - set.remove(state[FACING]) - return set - } - - override fun getNetworkInterfaceForSide(side: Direction, state: BlockState, world: WorldAccess, pos: BlockPos): Interface? { - return if (side == state[FACING]) { - null - } else { - getBlockEntity(world, pos) - } - } - - override fun appendProperties(builder: StateManager.Builder) { - super.appendProperties(builder) - builder.add(FACING) - builder.add(CABLE_CONNECTION) - } + override val faceThickness = 2.0 + override val faceShapes = mapOf( + Direction.DOWN to createCuboidShape(2.0, 0.0, 2.0, 14.0, 2.0, 14.0), + Direction.UP to createCuboidShape(2.0, 14.0, 2.0, 14.0, 16.0, 14.0), + Direction.NORTH to createCuboidShape(2.0, 2.0, 0.0, 14.0, 14.0, 2.0), + Direction.SOUTH to createCuboidShape(2.0, 2.0, 14.0, 14.0, 14.0, 16.0), + Direction.WEST to createCuboidShape(0.0, 2.0, 2.0, 2.0, 14.0, 14.0), + Direction.EAST to createCuboidShape(14.0, 2.0, 2.0, 16.0, 14.0, 14.0) + ) override fun createBlockEntity(world: BlockView) = InterfaceBlockEntity() - override fun getPlacementState(context: ItemPlacementContext): BlockState { - val facing = if (context.player?.isSneaking == true) context.side.opposite else context.playerFacing.opposite - val cableConnection = getCableConnectionSide(context.world, context.blockPos) ?: facing.opposite - return defaultState.with(FACING, facing).with(CABLE_CONNECTION, cableConnection) - } - - private fun getCableConnectionSide(world: World, pos: BlockPos): Direction? { - for (side in Direction.values()) { - val offsetPos = pos.offset(side) - if (world.getBlockState(offsetPos).block is NetworkComponentBlock) { - return side - } - } - return null - } - override fun onPlaced(world: World, pos: BlockPos, state: BlockState, placer: LivingEntity?, stack: ItemStack) { if (!world.isClient) { getBlockEntity(world, pos)!!.updateInventory() @@ -113,19 +49,8 @@ class InterfaceBlock: DeviceBlock(Settings.of(Material.MET } } - override fun getStateForNeighborUpdate(state: BlockState, side: Direction, neighborState: BlockState, world: WorldAccess, pos: BlockPos, neighborPos: BlockPos): BlockState { - if (neighborState.block is NetworkComponentBlock && world.getBlockState(pos.offset(state[CABLE_CONNECTION])).block !is NetworkComponentBlock) { - return state.with(CABLE_CONNECTION, side) - } - return state - } - override fun addAllAttributes(world: World, pos: BlockPos, state: BlockState, to: AttributeList<*>) { to.offer(getBlockEntity(world, pos)) } - override fun getOutlineShape(state: BlockState, world: BlockView, pos: BlockPos, context: ShapeContext): VoxelShape { - return getShape(state[FACING], state[CABLE_CONNECTION]) - } - } diff --git a/src/main/kotlin/net/shadowfacts/phycon/network/block/netinterface/InterfaceBlockEntity.kt b/src/main/kotlin/net/shadowfacts/phycon/network/block/netinterface/InterfaceBlockEntity.kt index 841324e..9cb58ae 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/network/block/netinterface/InterfaceBlockEntity.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/network/block/netinterface/InterfaceBlockEntity.kt @@ -9,6 +9,7 @@ import net.minecraft.util.math.Direction import net.shadowfacts.phycon.api.packet.Packet import net.shadowfacts.phycon.init.PhyBlockEntities import net.shadowfacts.phycon.network.DeviceBlockEntity +import net.shadowfacts.phycon.network.FaceDeviceBlock import net.shadowfacts.phycon.network.component.ItemStackPacketHandler import net.shadowfacts.phycon.network.component.NetworkStackProvider import net.shadowfacts.phycon.network.component.NetworkStackReceiver @@ -25,7 +26,7 @@ class InterfaceBlockEntity: DeviceBlockEntity(PhyBlockEntities.INTERFACE), NetworkStackReceiver { private val facing: Direction - get() = cachedState[InterfaceBlock.FACING] + get() = cachedState[FaceDeviceBlock.FACING] // todo: should this be a weak ref? private var inventory: GroupedItemInv? = null diff --git a/src/main/resources/assets/phycon/blockstates/network_interface.json b/src/main/resources/assets/phycon/blockstates/network_interface.json index b1166ed..6d851e4 100644 --- a/src/main/resources/assets/phycon/blockstates/network_interface.json +++ b/src/main/resources/assets/phycon/blockstates/network_interface.json @@ -140,7 +140,6 @@ }, { - "when": {"cable_connection": "down", "facing": "east"}, "apply": { "model": "phycon:block/interface_cable_corner", "x": 90, "y": 270 } },