PhysicalConnectivity/src/main/kotlin/net/shadowfacts/phycon/block/redstone_controller/RedstoneControllerBlock.kt

76 lines
2.7 KiB
Kotlin
Raw Normal View History

2021-03-01 03:03:29 +00:00
package net.shadowfacts.phycon.block.redstone_controller
2021-02-24 03:05:05 +00:00
import net.minecraft.block.Block
import net.minecraft.block.BlockState
import net.minecraft.block.Material
import net.minecraft.item.ItemPlacementContext
2021-02-28 23:56:04 +00:00
import net.minecraft.sound.BlockSoundGroup
2021-02-24 03:05:05 +00:00
import net.minecraft.state.StateManager
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.world.BlockView
import net.minecraft.world.World
import net.shadowfacts.phycon.PhysicalConnectivity
2021-02-28 18:48:39 +00:00
import net.shadowfacts.phycon.block.FaceDeviceBlock
2021-02-24 03:05:05 +00:00
/**
* @author shadowfacts
*/
2021-02-28 23:56:04 +00:00
class RedstoneControllerBlock: FaceDeviceBlock<RedstoneControllerBlockEntity>(
Settings.of(Material.METAL)
.strength(1.5f)
.sounds(BlockSoundGroup.METAL)
) {
2021-02-24 03:05:05 +00:00
companion object {
val ID = Identifier(PhysicalConnectivity.MODID, "redstone_controller")
val POWERED = Properties.POWERED
2021-02-24 03:05:05 +00:00
}
2021-02-24 23:58:22 +00:00
override val faceThickness = 3.0
2021-02-24 03:05:05 +00:00
override val faceShapes = mapOf(
2021-02-24 23:58:22 +00:00
Direction.DOWN to createCuboidShape(0.0, 0.0, 0.0, 16.0, 3.0, 16.0),
Direction.UP to createCuboidShape(0.0, 13.0, 0.0, 16.0, 16.0, 16.0),
Direction.NORTH to createCuboidShape(0.0, 0.0, 0.0, 16.0, 16.0, 3.0),
Direction.SOUTH to createCuboidShape(0.0, 0.0, 13.0, 16.0, 16.0, 16.0),
Direction.WEST to createCuboidShape(0.0, 0.0, 0.0, 3.0, 16.0, 16.0),
Direction.EAST to createCuboidShape(13.0, 0.0, 0.0, 16.0, 16.0, 16.0)
2021-02-24 03:05:05 +00:00
)
override fun appendProperties(builder: StateManager.Builder<Block, BlockState>) {
super.appendProperties(builder)
builder.add(POWERED)
2021-02-24 03:05:05 +00:00
}
2021-12-22 23:59:51 +00:00
override fun createBlockEntity(pos: BlockPos, state: BlockState) = RedstoneControllerBlockEntity(pos, state)
2021-02-24 03:05:05 +00:00
override fun getPlacementState(context: ItemPlacementContext): BlockState {
val state = super.getPlacementState(context)
return state.with(POWERED, isPowered(context.world, context.blockPos, state[FACING]))
2021-02-24 03:05:05 +00:00
}
override fun neighborUpdate(state: BlockState, world: World, pos: BlockPos, neighborBlock: Block, neighborPos: BlockPos, bl: Boolean) {
2021-02-24 23:58:22 +00:00
// this can't be done in getStateForNeighborUpdate because getEmittedRedstonePower is defined in World not WorldAccess
2021-02-24 03:05:05 +00:00
if (!world.isClient) {
val wasLit = state[POWERED]
2021-02-24 03:05:05 +00:00
val isLit = isPowered(world, pos, state[FACING])
if (wasLit != isLit) {
2021-02-24 23:58:22 +00:00
toggleLit(state, world, pos)
2021-02-24 03:05:05 +00:00
}
}
}
private fun isPowered(world: World, pos: BlockPos, facing: Direction): Boolean {
val offset = pos.offset(facing)
return world.getEmittedRedstonePower(offset, facing) > 0
}
private fun toggleLit(state: BlockState, world: World, pos: BlockPos) {
world.setBlockState(pos, state.cycle(POWERED), 2)
2021-02-24 03:05:05 +00:00
getBlockEntity(world, pos)!!.redstoneStateChanged()
}
}