From dfccc5b2ec4aa2b10477be3a84d41f9b5a42bf42 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 28 Oct 2019 11:53:47 -0400 Subject: [PATCH] Add Cable block --- .../shadowfacts/phycon/api/NetworkCable.java | 2 +- .../phycon/api/NetworkComponent.java | 7 ++ .../net/shadowfacts/phycon/init/PhyBlocks.kt | 3 + .../net/shadowfacts/phycon/init/PhyItems.kt | 3 + .../phycon/network/block/cable/CableBlock.kt | 104 ++++++++++++++++++ .../block/netinterface/InterfaceBlock.kt | 3 +- .../network/block/netswitch/SwitchBlock.kt | 3 +- .../network/block/terminal/TerminalBlock.kt | 3 +- .../assets/phycon/blockstates/cable.json | 31 ++++++ .../phycon/models/block/cable_center.json | 17 +++ .../phycon/models/block/cable_side.json | 17 +++ 11 files changed, 189 insertions(+), 4 deletions(-) create mode 100644 src/main/java/net/shadowfacts/phycon/api/NetworkComponent.java create mode 100644 src/main/kotlin/net/shadowfacts/phycon/network/block/cable/CableBlock.kt create mode 100644 src/main/resources/assets/phycon/blockstates/cable.json create mode 100644 src/main/resources/assets/phycon/models/block/cable_center.json create mode 100644 src/main/resources/assets/phycon/models/block/cable_side.json diff --git a/src/main/java/net/shadowfacts/phycon/api/NetworkCable.java b/src/main/java/net/shadowfacts/phycon/api/NetworkCable.java index b90666f..650e6f4 100644 --- a/src/main/java/net/shadowfacts/phycon/api/NetworkCable.java +++ b/src/main/java/net/shadowfacts/phycon/api/NetworkCable.java @@ -3,5 +3,5 @@ package net.shadowfacts.phycon.api; /** * @author shadowfacts */ -public interface NetworkCable { +public interface NetworkCable extends NetworkComponent { } diff --git a/src/main/java/net/shadowfacts/phycon/api/NetworkComponent.java b/src/main/java/net/shadowfacts/phycon/api/NetworkComponent.java new file mode 100644 index 0000000..9bae6af --- /dev/null +++ b/src/main/java/net/shadowfacts/phycon/api/NetworkComponent.java @@ -0,0 +1,7 @@ +package net.shadowfacts.phycon.api; + +/** + * @author shadowfacts + */ +public interface NetworkComponent { +} diff --git a/src/main/kotlin/net/shadowfacts/phycon/init/PhyBlocks.kt b/src/main/kotlin/net/shadowfacts/phycon/init/PhyBlocks.kt index d8d33c8..542ea7a 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/init/PhyBlocks.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/init/PhyBlocks.kt @@ -3,6 +3,7 @@ package net.shadowfacts.phycon.init import net.minecraft.block.Block import net.minecraft.util.Identifier import net.minecraft.util.registry.Registry +import net.shadowfacts.phycon.network.block.cable.CableBlock import net.shadowfacts.phycon.network.block.netinterface.InterfaceBlock import net.shadowfacts.phycon.network.block.netswitch.SwitchBlock import net.shadowfacts.phycon.network.block.terminal.TerminalBlock @@ -15,11 +16,13 @@ object PhyBlocks { val INTERFACE = InterfaceBlock() val TERMINAL = TerminalBlock() val SWITCH = SwitchBlock() + val CABLE = CableBlock() fun init() { register(InterfaceBlock.ID, INTERFACE) register(TerminalBlock.ID, TERMINAL) register(SwitchBlock.ID, SWITCH) + register(CableBlock.ID, CABLE) } private fun register(id: Identifier, block: Block) { diff --git a/src/main/kotlin/net/shadowfacts/phycon/init/PhyItems.kt b/src/main/kotlin/net/shadowfacts/phycon/init/PhyItems.kt index c032dcd..2a3e209 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/init/PhyItems.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/init/PhyItems.kt @@ -4,6 +4,7 @@ import net.minecraft.item.BlockItem import net.minecraft.item.Item import net.minecraft.util.Identifier import net.minecraft.util.registry.Registry +import net.shadowfacts.phycon.network.block.cable.CableBlock import net.shadowfacts.phycon.network.block.netinterface.InterfaceBlock import net.shadowfacts.phycon.network.block.netswitch.SwitchBlock import net.shadowfacts.phycon.network.block.terminal.TerminalBlock @@ -16,11 +17,13 @@ object PhyItems { val INTERFACE = BlockItem(PhyBlocks.INTERFACE, Item.Settings()) val TERMINAL = BlockItem(PhyBlocks.TERMINAL, Item.Settings()) val SWITCH = BlockItem(PhyBlocks.SWITCH, Item.Settings()) + val CABLE = BlockItem(PhyBlocks.CABLE, Item.Settings()) fun init() { register(InterfaceBlock.ID, INTERFACE) register(TerminalBlock.ID, TERMINAL) register(SwitchBlock.ID, SWITCH) + register(CableBlock.ID, CABLE) } private fun register(id: Identifier, item: Item) { 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 new file mode 100644 index 0000000..bceae7f --- /dev/null +++ b/src/main/kotlin/net/shadowfacts/phycon/network/block/cable/CableBlock.kt @@ -0,0 +1,104 @@ +package net.shadowfacts.phycon.network.block.cable + +import net.fabricmc.api.EnvType +import net.fabricmc.api.Environment +import net.minecraft.block.* +import net.minecraft.block.piston.PistonBehavior +import net.minecraft.entity.EntityContext +import net.minecraft.item.ItemPlacementContext +import net.minecraft.state.StateFactory +import net.minecraft.state.property.BooleanProperty +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.IWorld +import net.shadowfacts.phycon.PhysicalConnectivity +import net.shadowfacts.phycon.api.NetworkCable +import net.shadowfacts.phycon.api.NetworkComponent +import java.util.* + +/** + * @author shadowfacts + */ +class CableBlock: Block(Settings.of(CABLE_MATERIAL)), NetworkCable { + companion object { + val ID = Identifier(PhysicalConnectivity.MODID, "cable") + val CABLE_MATERIAL = Material(MaterialColor.IRON, false, false, true, false, true, false, false, PistonBehavior.NORMAL) + val CENTER_SHAPE = createCuboidShape(6.0, 6.0, 6.0, 10.0, 10.0, 10.0) + val SIDE_SHAPES = mapOf( + Direction.DOWN to createCuboidShape(6.0, 0.0, 6.0, 10.0, 6.0, 10.0), + Direction.UP to createCuboidShape(6.0, 10.0, 6.0, 10.0, 16.0, 10.0), + Direction.NORTH to createCuboidShape(6.0, 6.0, 0.0, 10.0, 10.0, 6.0), + Direction.SOUTH to createCuboidShape(6.0, 6.0, 10.0, 10.0, 10.0, 16.0), + Direction.WEST to createCuboidShape(0.0, 6.0, 6.0, 6.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() + val CONNECTIONS: Map = Direction.values().associate { it to BooleanProperty.of(it.name.toLowerCase()) } + + fun getShape(state: BlockState): VoxelShape { + return SHAPE_CACHE.getOrPut(state) { + var shape = CENTER_SHAPE + for ((side, prop) in CONNECTIONS) { + if (state[prop]) { + shape = VoxelShapes.union(shape, SIDE_SHAPES[side]) + } + } + return shape + } + } + } + + init { + defaultState = CONNECTIONS.values.fold(stateFactory.defaultState) { acc, prop -> + acc.with(prop, false) + } + } + + override fun appendProperties(builder: StateFactory.Builder) { + super.appendProperties(builder) + CONNECTIONS.values.forEach { + builder.add(it) + } + } + + override fun getPlacementState(context: ItemPlacementContext): BlockState { + return CONNECTIONS.entries.fold(defaultState, { acc, (dir, prop) -> + acc.with(prop, hasConnectionInDirection(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)) + } + + private fun hasConnectionInDirection(world: IWorld, pos: BlockPos, direction: Direction): Boolean { + val block = world.getBlockState(pos.offset(direction)).block + return block is NetworkComponent + } + + @Environment(EnvType.CLIENT) + override fun getRenderLayer(): BlockRenderLayer { + return BlockRenderLayer.TRANSLUCENT + } + + override fun isOpaque(blockState_1: BlockState?): Boolean { + return false + } + + override fun isTranslucent(blockState_1: BlockState?, blockView_1: BlockView?, blockPos_1: BlockPos?): Boolean { + return true + } + + override fun isSimpleFullBlock(blockState_1: BlockState?, blockView_1: BlockView?, blockPos_1: BlockPos?): Boolean { + return false + } + + override fun getOutlineShape(state: BlockState, world: BlockView, pos: BlockPos, context: EntityContext): VoxelShape { + return getShape(state) + } + +} 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 c2e1e33..5492e08 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 @@ -15,12 +15,13 @@ import net.minecraft.util.math.BlockPos import net.minecraft.world.BlockView import net.minecraft.world.World import net.shadowfacts.phycon.PhysicalConnectivity +import net.shadowfacts.phycon.api.NetworkComponent import net.shadowfacts.phycon.block.BlockWithEntity /** * @author shadowfacts */ -class InterfaceBlock: BlockWithEntity(Settings.of(Material.METAL)), AttributeProvider { +class InterfaceBlock: BlockWithEntity(Settings.of(Material.METAL)), NetworkComponent, AttributeProvider { companion object { val ID = Identifier(PhysicalConnectivity.MODID, "network_interface") val FACING = Properties.FACING diff --git a/src/main/kotlin/net/shadowfacts/phycon/network/block/netswitch/SwitchBlock.kt b/src/main/kotlin/net/shadowfacts/phycon/network/block/netswitch/SwitchBlock.kt index 3b6b553..790773b 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/network/block/netswitch/SwitchBlock.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/network/block/netswitch/SwitchBlock.kt @@ -9,12 +9,13 @@ import net.minecraft.util.math.BlockPos import net.minecraft.world.BlockView import net.minecraft.world.World import net.shadowfacts.phycon.PhysicalConnectivity +import net.shadowfacts.phycon.api.NetworkComponent import net.shadowfacts.phycon.block.BlockWithEntity /** * @author shadowfacts */ -class SwitchBlock: BlockWithEntity(Settings.of(Material.METAL)), AttributeProvider { +class SwitchBlock: BlockWithEntity(Settings.of(Material.METAL)), NetworkComponent, AttributeProvider { companion object { val ID = Identifier(PhysicalConnectivity.MODID, "switch") } diff --git a/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalBlock.kt b/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalBlock.kt index e9d61d8..4acb967 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalBlock.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalBlock.kt @@ -12,12 +12,13 @@ import net.minecraft.util.math.BlockPos import net.minecraft.world.BlockView import net.minecraft.world.World import net.shadowfacts.phycon.PhysicalConnectivity +import net.shadowfacts.phycon.api.NetworkComponent import net.shadowfacts.phycon.block.BlockWithEntity /** * @author shadowfacts */ -class TerminalBlock: BlockWithEntity(Settings.of(Material.METAL)), AttributeProvider { +class TerminalBlock: BlockWithEntity(Settings.of(Material.METAL)), NetworkComponent, AttributeProvider { companion object { val ID = Identifier(PhysicalConnectivity.MODID, "terminal") } diff --git a/src/main/resources/assets/phycon/blockstates/cable.json b/src/main/resources/assets/phycon/blockstates/cable.json new file mode 100644 index 0000000..5cf3bae --- /dev/null +++ b/src/main/resources/assets/phycon/blockstates/cable.json @@ -0,0 +1,31 @@ +{ + "multipart": [ + { + "apply": { "model": "phycon:block/cable_center" } + }, + { + "when": { "down": true }, + "apply": { "model": "phycon:block/cable_side" } + }, + { + "when": { "up": true }, + "apply": { "model": "phycon:block/cable_side", "x": 180 } + }, + { + "when": { "north": true }, + "apply": { "model": "phycon:block/cable_side", "x": 270 } + }, + { + "when": { "south": true }, + "apply": { "model": "phycon:block/cable_side", "x": 90 } + }, + { + "when": { "west": true }, + "apply": { "model": "phycon:block/cable_side", "x": 90, "y": 90 } + }, + { + "when": { "east": true }, + "apply": { "model": "phycon:block/cable_side", "x": 90, "y": 270 } + } + ] +} diff --git a/src/main/resources/assets/phycon/models/block/cable_center.json b/src/main/resources/assets/phycon/models/block/cable_center.json new file mode 100644 index 0000000..df455e7 --- /dev/null +++ b/src/main/resources/assets/phycon/models/block/cable_center.json @@ -0,0 +1,17 @@ +{ + "parent": "block/block", + "elements": [ + { + "from": [6, 6, 6], + "to": [10, 10, 10], + "faces": { + "down": { "texture": "phycon:block/cable_center" }, + "up": { "texture": "phycon:block/cable_center" }, + "north": { "texture": "phycon:block/cable_center" }, + "south": { "texture": "phycon:block/cable_center" }, + "west": { "texture": "phycon:block/cable_center" }, + "east": { "texture": "phycon:block/cable_center" } + } + } + ] +} diff --git a/src/main/resources/assets/phycon/models/block/cable_side.json b/src/main/resources/assets/phycon/models/block/cable_side.json new file mode 100644 index 0000000..afd43ca --- /dev/null +++ b/src/main/resources/assets/phycon/models/block/cable_side.json @@ -0,0 +1,17 @@ +{ + "parent": "block/block", + "elements": [ + { + "from": [6, 0, 6], + "to": [10, 6, 10], + "faces": { + "down": { "texture": "phycon:block/cable_side" }, + "up": { "texture": "phycon:block/cable_side" }, + "north": { "texture": "phycon:block/cable_side" }, + "south": { "texture": "phycon:block/cable_side" }, + "west": { "texture": "phycon:block/cable_side" }, + "east": { "texture": "phycon:block/cable_side" } + } + } + ] +}