From b43a7571ff4be8eaa4ac978e5ebddb176ecf19fa Mon Sep 17 00:00:00 2001 From: dhi_holo <91051135+dhi-holo@users.noreply.github.com> Date: Wed, 5 Aug 2026 16:13:24 +1000 Subject: [PATCH] Eject Button Now with cleaner code, I hope this is more acceptable --- .../cobblemon/mod/common/CobblemonBlocks.kt | 5 +- .../cobblemon/mod/common/CobblemonItems.kt | 2 +- .../mod/common/block/EjectButtonBlock.kt | 143 ++++++++++++++++++ 3 files changed, 147 insertions(+), 3 deletions(-) create mode 100644 common/src/main/kotlin/com/cobblemon/mod/common/block/EjectButtonBlock.kt diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/CobblemonBlocks.kt b/common/src/main/kotlin/com/cobblemon/mod/common/CobblemonBlocks.kt index 1180ce4bfe..00db649a2d 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/CobblemonBlocks.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/CobblemonBlocks.kt @@ -663,8 +663,9 @@ object CobblemonBlocks : PlatformRegistry, ResourceKey, ResourceKey) { + builder.add(FACING, FACE, POWERED, WATERLOGGED) + } + + override fun getFluidState(blockState: BlockState): FluidState? { + return if (blockState.getValue(WATERLOGGED)) Fluids.WATER.getSource(false) else super.getFluidState(blockState); + } + + override fun getStateForPlacement(context: BlockPlaceContext): BlockState? { + val fluidState = context.level.getFluidState(context.clickedPos) + for (direction in context.nearestLookingDirections) { + val blockState: BlockState + if (direction.getAxis() === Direction.Axis.Y) { + blockState = (defaultBlockState() + .setValue(FACE, if (direction == Direction.UP) AttachFace.CEILING else AttachFace.FLOOR) as BlockState) + .setValue(FACING, context.horizontalDirection.opposite) + .setValue(WATERLOGGED, fluidState.type === Fluids.WATER + ) as BlockState + } else { + blockState = (defaultBlockState() + .setValue(FACE, AttachFace.WALL) as BlockState) + .setValue(FACING, direction.opposite) + .setValue(WATERLOGGED, fluidState.type === Fluids.WATER + ) as BlockState + } + if (blockState.canSurvive(context.getLevel(), context.getClickedPos())) { + return blockState + } + } + return null + } + + override fun getShape(state: BlockState, blockGetter: BlockGetter, pos: BlockPos, collisionContext: CollisionContext): VoxelShape { + val direction = state.getValue(FACING) + return when (state.getValue(FACE)) { + AttachFace.FLOOR -> if (state.getValue(POWERED)) {FLOOR_POWERED} else {FLOOR_UNPOWERED} + AttachFace.WALL -> + when (direction) { + Direction.NORTH, Direction.UP, Direction.DOWN -> if (state.getValue(POWERED)) {WALL_POWERED} else {WALL_UNPOWERED} + else -> rotateShape(Direction.NORTH, direction, if (state.getValue(POWERED)) {WALL_POWERED} else {WALL_UNPOWERED}) + } + AttachFace.CEILING -> if (state.getValue(POWERED)) {CEILING_POWERED} else {CEILING_UNPOWERED} + } + } + override fun getCollisionShape(state: BlockState, level: BlockGetter, pos: BlockPos, context: CollisionContext): VoxelShape { + val direction = state.getValue(FACING) + return when (state.getValue(FACE)) { + AttachFace.FLOOR -> FLOOR_PANEL + AttachFace.WALL -> when (direction) { + Direction.NORTH, Direction.UP, Direction.DOWN -> WALL_PANEL + else -> rotateShape(Direction.NORTH, direction, WALL_PANEL) + } + AttachFace.CEILING -> CEILING_PANEL + } + } + + override fun updateShape(state: BlockState, direction: Direction, neighborState: BlockState, world: LevelAccessor, pos: BlockPos, neighborPos: BlockPos): BlockState { + if (state.getValue(WATERLOGGED)) { + world.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(world)) + } + return if (getConnectedDirection(state).opposite == direction && !state.canSurvive(world, pos)) Blocks.AIR.defaultBlockState() + else super.updateShape(state, direction, neighborState, world, pos, neighborPos) + } + + override fun canSurvive(state: BlockState, level: LevelReader, pos: BlockPos): Boolean { + return if (state.getValue(FACE) != AttachFace.WALL) { + val direction = getConnectedDirection(state) + canSupportCenter(level, pos.relative(direction.opposite), direction) + } else { + level.getBlockState(pos.relative(state.getValue(FACING).opposite)).isSolid + } + } +}