Add Cable block

This commit is contained in:
Shadowfacts 2019-10-28 11:53:47 -04:00
parent a95621e3f1
commit dfccc5b2ec
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
11 changed files with 189 additions and 4 deletions

View File

@ -3,5 +3,5 @@ package net.shadowfacts.phycon.api;
/**
* @author shadowfacts
*/
public interface NetworkCable {
public interface NetworkCable extends NetworkComponent {
}

View File

@ -0,0 +1,7 @@
package net.shadowfacts.phycon.api;
/**
* @author shadowfacts
*/
public interface NetworkComponent {
}

View File

@ -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) {

View File

@ -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) {

View File

@ -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, VoxelShape>(
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<BlockState, VoxelShape>()
val CONNECTIONS: Map<Direction, BooleanProperty> = 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<Block, BlockState>) {
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)
}
}

View File

@ -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<InterfaceBlockEntity>(Settings.of(Material.METAL)), AttributeProvider {
class InterfaceBlock: BlockWithEntity<InterfaceBlockEntity>(Settings.of(Material.METAL)), NetworkComponent, AttributeProvider {
companion object {
val ID = Identifier(PhysicalConnectivity.MODID, "network_interface")
val FACING = Properties.FACING

View File

@ -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<SwitchBlockEntity>(Settings.of(Material.METAL)), AttributeProvider {
class SwitchBlock: BlockWithEntity<SwitchBlockEntity>(Settings.of(Material.METAL)), NetworkComponent, AttributeProvider {
companion object {
val ID = Identifier(PhysicalConnectivity.MODID, "switch")
}

View File

@ -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<TerminalBlockEntity>(Settings.of(Material.METAL)), AttributeProvider {
class TerminalBlock: BlockWithEntity<TerminalBlockEntity>(Settings.of(Material.METAL)), NetworkComponent, AttributeProvider {
companion object {
val ID = Identifier(PhysicalConnectivity.MODID, "terminal")
}

View File

@ -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 }
}
]
}

View File

@ -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" }
}
}
]
}

View File

@ -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" }
}
}
]
}