9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-23 08:59:27 +00:00

feat(bukkit): 添加 BottomHalfStairsBlockBehavior

This commit is contained in:
jhqwqmc
2025-09-07 09:00:23 +08:00
parent 039d616af2
commit 22d5bb7162
5 changed files with 135 additions and 1 deletions

View File

@@ -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<HorizontalDirection> facingProperty;
private final Property<StairsShape> shapeProperty;
public BottomHalfStairsBlockBehavior(CustomBlock block, Property<HorizontalDirection> facing, Property<StairsShape> 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<Object> superMethod) throws Exception {
Object level = args[updateShape$level];
Object blockPos = args[updateShape$blockPos];
Object blockState = args[0];
Optional<ImmutableBlockState> 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<ImmutableBlockState> optionalCustomState1 = BlockStateUtils.getOptionalCustomBlockState(relativeBlockState1);
if (optionalCustomState1.isPresent()) {
ImmutableBlockState customState1 = optionalCustomState1.get();
Optional<BottomHalfStairsBlockBehavior> 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<ImmutableBlockState> optionalCustomState2 = BlockStateUtils.getOptionalCustomBlockState(relativeBlockState2);
if (optionalCustomState2.isPresent()) {
ImmutableBlockState customState2 = optionalCustomState2.get();
Optional<BottomHalfStairsBlockBehavior> 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<ImmutableBlockState> optionalAnotherState = BlockStateUtils.getOptionalCustomBlockState(blockState);
if (optionalAnotherState.isEmpty()) {
return true;
}
ImmutableBlockState anotherState = optionalAnotherState.get();
Optional<BottomHalfStairsBlockBehavior> 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<String, Object> arguments) {
Property<HorizontalDirection> facing = (Property<HorizontalDirection>) ResourceConfigUtils.requireNonNullOrThrow(block.getProperty("facing"), "warning.config.block.behavior.bottom_half_stairs.missing_facing");
Property<StairsShape> shape = (Property<StairsShape>) ResourceConfigUtils.requireNonNullOrThrow(block.getProperty("shape"), "warning.config.block.behavior.bottom_half_stairs.missing_shape");
return new BottomHalfStairsBlockBehavior(block, facing, shape);
}
}
}

View File

@@ -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);
}
}

View File

@@ -305,6 +305,8 @@ warning.config.block.behavior.slab.missing_type: "<yellow>Issue found in file <a
warning.config.block.behavior.stairs.missing_facing: "<yellow>Issue found in file <arg:0> - The block '<arg:1>' is missing the required 'facing' property for 'stairs_block' behavior.</yellow>"
warning.config.block.behavior.stairs.missing_half: "<yellow>Issue found in file <arg:0> - The block '<arg:1>' is missing the required 'half' property for 'stairs_block' behavior.</yellow>"
warning.config.block.behavior.stairs.missing_shape: "<yellow>Issue found in file <arg:0> - The block '<arg:1>' is missing the required 'shape' property for 'stairs_block' behavior.</yellow>"
warning.config.block.behavior.bottom_half_stairs.missing_facing: "<yellow>Issue found in file <arg:0> - The block '<arg:1>' is missing the required 'facing' property for 'bottom_half_stairs_block' behavior.</yellow>"
warning.config.block.behavior.bottom_half_stairs.missing_shape: "<yellow>Issue found in file <arg:0> - The block '<arg:1>' is missing the required 'shape' property for 'bottom_half_stairs_block' behavior.</yellow>"
warning.config.block.behavior.pressure_plate.missing_powered: "<yellow>Issue found in file <arg:0> - The block '<arg:1>' is missing the required 'powered' property for 'pressure_plate_block' behavior.</yellow>"
warning.config.block.behavior.grass.missing_feature: "<yellow>Issue found in file <arg:0> - The block '<arg:1>' is missing the required 'feature' argument for 'grass_block' behavior.</yellow>"
warning.config.block.behavior.double_high.missing_half: "<yellow>Issue found in file <arg:0> - The block '<arg:1>' is missing the required 'half' property for 'double_block' behavior.</yellow>"

View File

@@ -305,6 +305,8 @@ warning.config.block.behavior.slab.missing_type: "<yellow>在文件 <arg:0> 发
warning.config.block.behavior.stairs.missing_facing: "<yellow>在文件 <arg:0> 发现问题 - 方块 '<arg:1>' 的 'stairs_block' 行为缺少必需的 'facing' 属性</yellow>"
warning.config.block.behavior.stairs.missing_half: "<yellow>在文件 <arg:0> 发现问题 - 方块 '<arg:1>' 的 'stairs_block' 行为缺少必需的 'half' 属性</yellow>"
warning.config.block.behavior.stairs.missing_shape: "<yellow>在文件 <arg:0> 发现问题 - 方块 '<arg:1>' 的 'stairs_block' 行为缺少必需的 'shape' 属性</yellow>"
warning.config.block.behavior.bottom_half_stairs.missing_facing: "<yellow>在文件 <arg:0> 发现问题 - 方块 '<arg:1>' 的 'bottom_half_stairs_block' 行为缺少必需的 'facing' 属性</yellow>"
warning.config.block.behavior.bottom_half_stairs.missing_shape: "<yellow>在文件 <arg:0> 发现问题 - 方块 '<arg:1>' 的 'bottom_half_stairs_block' 行为缺少必需的 'shape' 属性</yellow>"
warning.config.block.behavior.pressure_plate.missing_powered: "<yellow>在文件 <arg:0> 发现问题 - 方块 '<arg:1>' 的 'pressure_plate_block' 行为缺少必需的 'powered' 属性</yellow>"
warning.config.block.behavior.grass.missing_feature: "<yellow>在文件 <arg:0> 发现问题 - 方块 '<arg:1>' 的 'grass_block' 行为缺少必需的 'feature' 参数</yellow>"
warning.config.block.behavior.double_high.missing_half: "<yellow>在文件 <arg:0> 发现问题 - 方块 '<arg:1>' 的 'double_block' 行为缺少必需的 'half' 属性</yellow>"

View File

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