From 22d5bb7162e600c35ec80e8800d0d5840378bf5f Mon Sep 17 00:00:00 2001 From: jhqwqmc <2110242767@qq.com> Date: Sun, 7 Sep 2025 09:00:23 +0800 Subject: [PATCH] =?UTF-8?q?feat(bukkit):=20=E6=B7=BB=E5=8A=A0=20BottomHalf?= =?UTF-8?q?StairsBlockBehavior?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BottomHalfStairsBlockBehavior.java | 128 ++++++++++++++++++ .../block/behavior/BukkitBlockBehaviors.java | 2 + .../src/main/resources/translations/en.yml | 2 + .../src/main/resources/translations/zh_cn.yml | 2 + gradle.properties | 2 +- 5 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/BottomHalfStairsBlockBehavior.java diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/BottomHalfStairsBlockBehavior.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/BottomHalfStairsBlockBehavior.java new file mode 100644 index 000000000..ad4a00690 --- /dev/null +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/BottomHalfStairsBlockBehavior.java @@ -0,0 +1,128 @@ +package net.momirealms.craftengine.bukkit.block.behavior; + +import net.momirealms.craftengine.bukkit.nms.FastNMS; +import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MFluids; +import net.momirealms.craftengine.bukkit.util.BlockStateUtils; +import net.momirealms.craftengine.bukkit.util.DirectionUtils; +import net.momirealms.craftengine.bukkit.util.LocationUtils; +import net.momirealms.craftengine.core.block.BlockBehavior; +import net.momirealms.craftengine.core.block.CustomBlock; +import net.momirealms.craftengine.core.block.ImmutableBlockState; +import net.momirealms.craftengine.core.block.behavior.BlockBehaviorFactory; +import net.momirealms.craftengine.core.block.properties.Property; +import net.momirealms.craftengine.core.block.state.properties.StairsShape; +import net.momirealms.craftengine.core.item.context.BlockPlaceContext; +import net.momirealms.craftengine.core.util.Direction; +import net.momirealms.craftengine.core.util.HorizontalDirection; +import net.momirealms.craftengine.core.util.ResourceConfigUtils; +import net.momirealms.craftengine.core.util.VersionHelper; +import net.momirealms.craftengine.core.world.BlockPos; + +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.Callable; + +public class BottomHalfStairsBlockBehavior extends BukkitBlockBehavior { + public static final Factory FACTORY = new Factory(); + private final Property facingProperty; + private final Property shapeProperty; + + public BottomHalfStairsBlockBehavior(CustomBlock block, Property facing, Property shape) { + super(block); + this.facingProperty = facing; + this.shapeProperty = shape; + } + + @Override + public ImmutableBlockState updateStateForPlacement(BlockPlaceContext context, ImmutableBlockState state) { + BlockPos clickedPos = context.getClickedPos(); + ImmutableBlockState blockState = state.owner().value().defaultState() + .with(this.facingProperty, context.getHorizontalDirection().toHorizontalDirection()); + if (super.waterloggedProperty != null) { + Object fluidState = FastNMS.INSTANCE.method$BlockGetter$getFluidState(context.getLevel().serverWorld(), LocationUtils.toBlockPos(clickedPos)); + blockState = blockState.with(this.waterloggedProperty, FastNMS.INSTANCE.method$FluidState$getType(fluidState) == MFluids.WATER); + } + return blockState.with(this.shapeProperty, getStairsShape(blockState, context.getLevel().serverWorld(), clickedPos)); + } + + @Override + public Object updateShape(Object thisBlock, Object[] args, Callable superMethod) throws Exception { + Object level = args[updateShape$level]; + Object blockPos = args[updateShape$blockPos]; + Object blockState = args[0]; + Optional optionalCustomState = BlockStateUtils.getOptionalCustomBlockState(blockState); + if (optionalCustomState.isEmpty()) return blockState; + ImmutableBlockState customState = optionalCustomState.get(); + if (super.waterloggedProperty != null && customState.get(this.waterloggedProperty)) { + FastNMS.INSTANCE.method$ScheduledTickAccess$scheduleFluidTick(args[updateShape$level], args[updateShape$blockPos], MFluids.WATER, 5); + } + Direction direction = DirectionUtils.fromNMSDirection(VersionHelper.isOrAbove1_21_2() ? args[4] : args[1]); + StairsShape stairsShape = getStairsShape(customState, level, LocationUtils.fromBlockPos(blockPos)); + return direction.axis().isHorizontal() + ? customState.with(this.shapeProperty, stairsShape).customBlockState().literalObject() + : superMethod.call(); + } + + private StairsShape getStairsShape(ImmutableBlockState state, Object level, BlockPos pos) { + Direction direction = state.get(this.facingProperty).toDirection(); + Object relativeBlockState1 = FastNMS.INSTANCE.method$BlockGetter$getBlockState(level, LocationUtils.toBlockPos(pos.relative(direction))); + Optional optionalCustomState1 = BlockStateUtils.getOptionalCustomBlockState(relativeBlockState1); + if (optionalCustomState1.isPresent()) { + ImmutableBlockState customState1 = optionalCustomState1.get(); + Optional optionalStairsBlockBehavior = customState1.behavior().getAs(BottomHalfStairsBlockBehavior.class); + if (optionalStairsBlockBehavior.isPresent()) { + BottomHalfStairsBlockBehavior stairsBlockBehavior = optionalStairsBlockBehavior.get(); + Direction direction1 = customState1.get(stairsBlockBehavior.facingProperty).toDirection(); + if (direction1.axis() != state.get(this.facingProperty).toDirection().axis() && canTakeShape(state, level, pos, direction1.opposite())) { + if (direction1 == direction.counterClockWise()) { + return StairsShape.OUTER_LEFT; + } + return StairsShape.OUTER_RIGHT; + } + } + } + Object relativeBlockState2 = FastNMS.INSTANCE.method$BlockGetter$getBlockState(level, LocationUtils.toBlockPos(pos.relative(direction.opposite()))); + Optional optionalCustomState2 = BlockStateUtils.getOptionalCustomBlockState(relativeBlockState2); + if (optionalCustomState2.isPresent()) { + ImmutableBlockState customState2 = optionalCustomState2.get(); + Optional optionalStairsBlockBehavior = customState2.behavior().getAs(BottomHalfStairsBlockBehavior.class); + if (optionalStairsBlockBehavior.isPresent()) { + BottomHalfStairsBlockBehavior stairsBlockBehavior = optionalStairsBlockBehavior.get(); + Direction direction2 = customState2.get(stairsBlockBehavior.facingProperty).toDirection(); + if (direction2.axis() != state.get(this.facingProperty).toDirection().axis() && canTakeShape(state, level, pos, direction2)) { + if (direction2 == direction.counterClockWise()) { + return StairsShape.INNER_LEFT; + } + return StairsShape.INNER_RIGHT; + } + } + } + return StairsShape.STRAIGHT; + } + + private boolean canTakeShape(ImmutableBlockState state, Object level, BlockPos pos, Direction face) { + Object blockState = FastNMS.INSTANCE.method$BlockGetter$getBlockState(level, LocationUtils.toBlockPos(pos.relative(face))); + Optional optionalAnotherState = BlockStateUtils.getOptionalCustomBlockState(blockState); + if (optionalAnotherState.isEmpty()) { + return true; + } + ImmutableBlockState anotherState = optionalAnotherState.get(); + Optional optionalBehavior = anotherState.behavior().getAs(BottomHalfStairsBlockBehavior.class); + if (optionalBehavior.isEmpty()) { + return true; + } + BottomHalfStairsBlockBehavior anotherBehavior = optionalBehavior.get(); + return anotherState.get(anotherBehavior.facingProperty) != state.get(this.facingProperty); + } + + public static class Factory implements BlockBehaviorFactory { + + @Override + @SuppressWarnings("unchecked") + public BlockBehavior create(CustomBlock block, Map arguments) { + Property facing = (Property) ResourceConfigUtils.requireNonNullOrThrow(block.getProperty("facing"), "warning.config.block.behavior.bottom_half_stairs.missing_facing"); + Property shape = (Property) ResourceConfigUtils.requireNonNullOrThrow(block.getProperty("shape"), "warning.config.block.behavior.bottom_half_stairs.missing_shape"); + return new BottomHalfStairsBlockBehavior(block, facing, shape); + } + } +} diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/BukkitBlockBehaviors.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/BukkitBlockBehaviors.java index 4a622ed7a..5062787f0 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/BukkitBlockBehaviors.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/BukkitBlockBehaviors.java @@ -30,6 +30,7 @@ public class BukkitBlockBehaviors extends BlockBehaviors { public static final Key CHANGE_OVER_TIME_BLOCK = Key.from("craftengine:change_over_time_block"); public static final Key SIMPLE_STORAGE_BLOCK = Key.from("craftengine:simple_storage_block"); public static final Key TOGGLEABLE_LAMP_BLOCK = Key.from("craftengine:toggleable_lamp_block"); + public static final Key BOTTOM_HALF_STAIRS_BLOCK = Key.from("craftengine:bottom_half_stairs_block"); public static void init() { register(EMPTY, (block, args) -> EmptyBlockBehavior.INSTANCE); @@ -58,5 +59,6 @@ public class BukkitBlockBehaviors extends BlockBehaviors { register(CHANGE_OVER_TIME_BLOCK, ChangeOverTimeBlockBehavior.FACTORY); register(SIMPLE_STORAGE_BLOCK, SimpleStorageBlockBehavior.FACTORY); register(TOGGLEABLE_LAMP_BLOCK, ToggleableLampBlockBehavior.FACTORY); + register(BOTTOM_HALF_STAIRS_BLOCK, BottomHalfStairsBlockBehavior.FACTORY); } } diff --git a/common-files/src/main/resources/translations/en.yml b/common-files/src/main/resources/translations/en.yml index 2b7c873da..79ee371ca 100644 --- a/common-files/src/main/resources/translations/en.yml +++ b/common-files/src/main/resources/translations/en.yml @@ -305,6 +305,8 @@ warning.config.block.behavior.slab.missing_type: "Issue found in file Issue found in file - The block '' is missing the required 'facing' property for 'stairs_block' behavior." warning.config.block.behavior.stairs.missing_half: "Issue found in file - The block '' is missing the required 'half' property for 'stairs_block' behavior." warning.config.block.behavior.stairs.missing_shape: "Issue found in file - The block '' is missing the required 'shape' property for 'stairs_block' behavior." +warning.config.block.behavior.bottom_half_stairs.missing_facing: "Issue found in file - The block '' is missing the required 'facing' property for 'bottom_half_stairs_block' behavior." +warning.config.block.behavior.bottom_half_stairs.missing_shape: "Issue found in file - The block '' is missing the required 'shape' property for 'bottom_half_stairs_block' behavior." warning.config.block.behavior.pressure_plate.missing_powered: "Issue found in file - The block '' is missing the required 'powered' property for 'pressure_plate_block' behavior." warning.config.block.behavior.grass.missing_feature: "Issue found in file - The block '' is missing the required 'feature' argument for 'grass_block' behavior." warning.config.block.behavior.double_high.missing_half: "Issue found in file - The block '' is missing the required 'half' property for 'double_block' behavior." diff --git a/common-files/src/main/resources/translations/zh_cn.yml b/common-files/src/main/resources/translations/zh_cn.yml index fe5b6fb32..1df5b5430 100644 --- a/common-files/src/main/resources/translations/zh_cn.yml +++ b/common-files/src/main/resources/translations/zh_cn.yml @@ -305,6 +305,8 @@ warning.config.block.behavior.slab.missing_type: "在文件 发 warning.config.block.behavior.stairs.missing_facing: "在文件 发现问题 - 方块 '' 的 'stairs_block' 行为缺少必需的 'facing' 属性" warning.config.block.behavior.stairs.missing_half: "在文件 发现问题 - 方块 '' 的 'stairs_block' 行为缺少必需的 'half' 属性" warning.config.block.behavior.stairs.missing_shape: "在文件 发现问题 - 方块 '' 的 'stairs_block' 行为缺少必需的 'shape' 属性" +warning.config.block.behavior.bottom_half_stairs.missing_facing: "在文件 发现问题 - 方块 '' 的 'bottom_half_stairs_block' 行为缺少必需的 'facing' 属性" +warning.config.block.behavior.bottom_half_stairs.missing_shape: "在文件 发现问题 - 方块 '' 的 'bottom_half_stairs_block' 行为缺少必需的 'shape' 属性" warning.config.block.behavior.pressure_plate.missing_powered: "在文件 发现问题 - 方块 '' 的 'pressure_plate_block' 行为缺少必需的 'powered' 属性" warning.config.block.behavior.grass.missing_feature: "在文件 发现问题 - 方块 '' 的 'grass_block' 行为缺少必需的 'feature' 参数" warning.config.block.behavior.double_high.missing_half: "在文件 发现问题 - 方块 '' 的 'double_block' 行为缺少必需的 'half' 属性" diff --git a/gradle.properties b/gradle.properties index 7f34bd693..bd5f82cca 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ org.gradle.jvmargs=-Xmx1G # Rule: [major update].[feature update].[bug fix] project_version=0.0.62.15 config_version=45 -lang_version=25 +lang_version=26 project_group=net.momirealms latest_supported_version=1.21.8