Free rotation on gravitation stabilizer

This commit is contained in:
DBotThePony 2022-01-18 20:12:33 +07:00
parent 4271db62c8
commit c38c48c6f1
Signed by: DBot
GPG Key ID: DCC23B5715498507
5 changed files with 46 additions and 14 deletions

View File

@ -18,6 +18,17 @@ fun Direction.toYRotBlockstate(): Int {
}
}
fun Direction.toXRotBlockstate(): Int {
return when (this) {
Direction.DOWN -> -90
Direction.UP -> 90
Direction.NORTH -> 0
Direction.SOUTH -> 0
Direction.WEST -> 0
Direction.EAST -> 0
}
}
fun <T : Comparable<T>> BlockState.getValueNullable(prop: Property<T>): T? {
if (hasProperty(prop)) {
return getValue(prop)

View File

@ -11,6 +11,7 @@ import ru.dbotthepony.mc.otm.block.BlockMatteryRotatable
import ru.dbotthepony.mc.otm.block.entity.worker.WorkerState
import ru.dbotthepony.mc.otm.datagen.DataGen
import ru.dbotthepony.mc.otm.datagen.getValueNullable
import ru.dbotthepony.mc.otm.datagen.toXRotBlockstate
import ru.dbotthepony.mc.otm.datagen.toYRotBlockstate
typealias AdvancedBlockStateFunction = (BlockState, ConfiguredModel.Builder<*>, String) -> String?
@ -23,6 +24,11 @@ private fun initialTransform(it: BlockState, modelPath: String, builder: Configu
builder.rotationY(it.toYRotBlockstate())
}
it.getValueNullable(BlockMatteryRotatable.FACING_FULL)?.let {
builder.rotationY(it.toYRotBlockstate())
builder.rotationX(it.toXRotBlockstate())
}
it.getValueNullable(WorkerState.WORKER_STATE)?.let {
modelPath += "_" + it.name.lowercase()
}

View File

@ -31,6 +31,8 @@ class BlockGravitationStabilizer : BlockMatteryRotatable(Properties.of(Material.
return BlockEntityTicker { level, _, _, tile -> if (tile is BlockEntityGravitationStabilizer) tile.tick(level) }
}
override val hasFreeRotation: Boolean get() = true
override fun createBlockStateDefinition(builder: StateDefinition.Builder<Block, BlockState>) {
super.createBlockStateDefinition(builder)
builder.add(WorkerState.SEMI_WORKER_STATE)

View File

@ -89,24 +89,35 @@ abstract class BlockMattery @JvmOverloads constructor(
abstract class BlockMatteryRotatable @JvmOverloads constructor(properties: Properties = DEFAULT_PROPERTIES) : BlockMattery(properties) {
init {
registerDefaultState(getStateDefinition().any().setValue(FACING, Direction.SOUTH))
registerDefaultState(getStateDefinition().any().setValue(facingProperty(), Direction.SOUTH))
}
override fun createBlockStateDefinition(builder: StateDefinition.Builder<Block, BlockState>) {
builder.add(FACING)
builder.add(facingProperty())
}
override fun getStateForPlacement(context: BlockPlaceContext): BlockState? {
return defaultBlockState().setValue(
FACING,
if (faceToPlayer(context)) context.horizontalDirection.opposite else context.horizontalDirection
)
if (hasFreeRotation) {
return defaultBlockState().setValue(
FACING_FULL,
if (faceToPlayer(context)) context.nearestLookingDirection.opposite else context.nearestLookingDirection
)
} else {
return defaultBlockState().setValue(
FACING,
if (faceToPlayer(context)) context.horizontalDirection.opposite else context.horizontalDirection
)
}
}
fun facingProperty() = if (hasFreeRotation) FACING_FULL else FACING
open fun faceToPlayer(context: BlockPlaceContext): Boolean {
return true
}
open val hasFreeRotation get() = false
companion object {
@JvmField
val FACING = EnumProperty.create(
@ -117,5 +128,8 @@ abstract class BlockMatteryRotatable @JvmOverloads constructor(properties: Prope
Direction.NORTH,
Direction.EAST
)
@JvmField
val FACING_FULL = EnumProperty.create("facing", Direction::class.java)
}
}

View File

@ -24,7 +24,7 @@ class BlockEntityGravitationStabilizer(p_155229_: BlockPos, p_155230_: BlockStat
fun tick(level: Level) {
var findBlackHole: BlockEntityBlackHole? = null
val dir = blockState.getValue(BlockMatteryRotatable.FACING).normal
val dir = blockState.getValue(BlockMatteryRotatable.FACING_FULL).normal
for (i in 1 .. RANGE) {
val pos = blockPos + dir * i
@ -34,12 +34,11 @@ class BlockEntityGravitationStabilizer(p_155229_: BlockPos, p_155230_: BlockStat
if (!state.isAir) {
val tile = chunk.getBlockEntity(pos)
if (tile != null && tile !is BlockEntityBlackHole) {
break
}
if (tile != null) {
if (tile is BlockEntityBlackHole) {
findBlackHole = tile
}
if (tile != null && tile is BlockEntityBlackHole) {
findBlackHole = tile
break
}
@ -68,7 +67,7 @@ class BlockEntityGravitationStabilizer(p_155229_: BlockPos, p_155230_: BlockStat
if (!level.isClientSide) {
OverdriveThatMatters.tickOnce(level) {
for (face in BlockMatteryRotatable.FACING.possibleValues) {
for (face in BlockMatteryRotatable.FACING_FULL.possibleValues) {
val dir = face.normal
for (i in 1 .. RANGE) {
@ -78,7 +77,7 @@ class BlockEntityGravitationStabilizer(p_155229_: BlockPos, p_155230_: BlockStat
if (!state.isAir) {
if (chunk.getBlockEntity(pos) is BlockEntityBlackHole) {
level.setBlock(blockPos, blockState.setValue(BlockMatteryRotatable.FACING, face), Block.UPDATE_ALL)
level.setBlock(blockPos, blockState.setValue(BlockMatteryRotatable.FACING_FULL, face), Block.UPDATE_ALL)
return@tickOnce
}
}