9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-29 20:09:13 +00:00

feat(bukkit): 添加沙发

This commit is contained in:
jhqwqmc
2025-09-08 19:49:18 +08:00
parent ea6148cef0
commit a4a812d14f
17 changed files with 823 additions and 82 deletions

View File

@@ -30,7 +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 final Key SOFA_BLOCK = Key.from("craftengine:sofa_block");
public static void init() {
register(EMPTY, (block, args) -> EmptyBlockBehavior.INSTANCE);
@@ -59,6 +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);
register(SOFA_BLOCK, SofaBlockBehavior.FACTORY);
}
}

View File

@@ -1,7 +1,6 @@
package net.momirealms.craftengine.bukkit.block.behavior;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.CoreReflections;
import net.momirealms.craftengine.bukkit.util.BlockStateUtils;
import net.momirealms.craftengine.bukkit.util.LocationUtils;
import net.momirealms.craftengine.core.block.BlockBehavior;
@@ -9,9 +8,7 @@ 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.entity.player.InteractionResult;
import net.momirealms.craftengine.core.item.context.BlockPlaceContext;
import net.momirealms.craftengine.core.item.context.UseOnContext;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import java.util.Map;
@@ -22,34 +19,14 @@ import java.util.concurrent.Callable;
public class LampBlockBehavior extends BukkitBlockBehavior {
public static final Factory FACTORY = new Factory();
private final Property<Boolean> litProperty;
private final boolean canOpenWithHand;
public LampBlockBehavior(CustomBlock block, Property<Boolean> litProperty, boolean canOpenWithHand) {
public LampBlockBehavior(CustomBlock block, Property<Boolean> litProperty) {
super(block);
this.litProperty = litProperty;
this.canOpenWithHand = canOpenWithHand;
}
@Override
public InteractionResult useWithoutItem(UseOnContext context, ImmutableBlockState state) {
if (!this.canOpenWithHand) {
return InteractionResult.PASS;
}
LampBlockBehavior behavior = state.behavior().getAs(LampBlockBehavior.class).orElse(null);
if (behavior == null) return InteractionResult.PASS;
FastNMS.INSTANCE.method$LevelWriter$setBlock(
context.getLevel().serverWorld(),
LocationUtils.toBlockPos(context.getClickedPos()),
state.cycle(behavior.litProperty).customBlockState().literalObject(),
2
);
Optional.ofNullable(context.getPlayer()).ifPresent(p -> p.swingHand(context.getHand()));
return InteractionResult.SUCCESS_AND_CANCEL;
}
@Override
public ImmutableBlockState updateStateForPlacement(BlockPlaceContext context, ImmutableBlockState state) {
if (this.canOpenWithHand) return state;
Object level = context.getLevel().serverWorld();
state = state.with(this.litProperty, FastNMS.INSTANCE.method$SignalGetter$hasNeighborSignal(level, LocationUtils.toBlockPos(context.getClickedPos())));
return state;
@@ -58,10 +35,9 @@ public class LampBlockBehavior extends BukkitBlockBehavior {
@Override
public void tick(Object thisBlock, Object[] args, Callable<Object> superMethod) throws Exception {
Object blockState = args[0];
Object world = args[1];
if (this.canOpenWithHand || !CoreReflections.clazz$ServerLevel.isInstance(world)) return;
Optional<ImmutableBlockState> optionalCustomState = BlockStateUtils.getOptionalCustomBlockState(blockState);
if (optionalCustomState.isEmpty()) return;
Object world = args[1];
Object blockPos = args[2];
ImmutableBlockState customState = optionalCustomState.get();
if (customState.get(this.litProperty) && !FastNMS.INSTANCE.method$SignalGetter$hasNeighborSignal(world, blockPos)) {
@@ -75,10 +51,9 @@ public class LampBlockBehavior extends BukkitBlockBehavior {
@Override
public void neighborChanged(Object thisBlock, Object[] args, Callable<Object> superMethod) {
Object blockState = args[0];
Object world = args[1];
if (this.canOpenWithHand || !CoreReflections.clazz$ServerLevel.isInstance(world)) return;
Optional<ImmutableBlockState> optionalCustomState = BlockStateUtils.getOptionalCustomBlockState(blockState);
if (optionalCustomState.isEmpty()) return;
Object world = args[1];
Object blockPos = args[2];
ImmutableBlockState customState = optionalCustomState.get();
boolean lit = customState.get(this.litProperty);
@@ -99,8 +74,7 @@ public class LampBlockBehavior extends BukkitBlockBehavior {
@Override
public BlockBehavior create(CustomBlock block, Map<String, Object> arguments) {
Property<Boolean> lit = (Property<Boolean>) ResourceConfigUtils.requireNonNullOrThrow(block.getProperty("lit"), "warning.config.block.behavior.lamp.missing_lit");
boolean canOpenWithHand = ResourceConfigUtils.getAsBoolean(arguments.getOrDefault("can-open-with-hand", false), "can-open-with-hand");
return new LampBlockBehavior(block, lit, canOpenWithHand);
return new LampBlockBehavior(block, lit);
}
}
}

View File

@@ -10,7 +10,7 @@ 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.block.state.properties.SofaShape;
import net.momirealms.craftengine.core.item.context.BlockPlaceContext;
import net.momirealms.craftengine.core.util.Direction;
import net.momirealms.craftengine.core.util.HorizontalDirection;
@@ -22,12 +22,12 @@ import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Callable;
public class BottomHalfStairsBlockBehavior extends BukkitBlockBehavior {
public class SofaBlockBehavior extends BukkitBlockBehavior {
public static final Factory FACTORY = new Factory();
private final Property<HorizontalDirection> facingProperty;
private final Property<StairsShape> shapeProperty;
private final Property<SofaShape> shapeProperty;
public BottomHalfStairsBlockBehavior(CustomBlock block, Property<HorizontalDirection> facing, Property<StairsShape> shape) {
public SofaBlockBehavior(CustomBlock block, Property<HorizontalDirection> facing, Property<SofaShape> shape) {
super(block);
this.facingProperty = facing;
this.shapeProperty = shape;
@@ -42,7 +42,7 @@ public class BottomHalfStairsBlockBehavior extends BukkitBlockBehavior {
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));
return blockState.with(this.shapeProperty, getSofaShape(blockState, context.getLevel().serverWorld(), clickedPos));
}
@Override
@@ -57,47 +57,31 @@ public class BottomHalfStairsBlockBehavior extends BukkitBlockBehavior {
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));
SofaShape sofaShape = getSofaShape(customState, level, LocationUtils.fromBlockPos(blockPos));
return direction.axis().isHorizontal()
? customState.with(this.shapeProperty, stairsShape).customBlockState().literalObject()
? customState.with(this.shapeProperty, sofaShape).customBlockState().literalObject()
: superMethod.call();
}
private StairsShape getStairsShape(ImmutableBlockState state, Object level, BlockPos pos) {
private SofaShape getSofaShape(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);
Object relativeBlockState = FastNMS.INSTANCE.method$BlockGetter$getBlockState(level, LocationUtils.toBlockPos(pos.relative(direction.opposite())));
Optional<ImmutableBlockState> optionalCustomState = BlockStateUtils.getOptionalCustomBlockState(relativeBlockState);
if (optionalCustomState.isPresent()) {
ImmutableBlockState customState = optionalCustomState.get();
Optional<SofaBlockBehavior> optionalStairsBlockBehavior = customState.behavior().getAs(SofaBlockBehavior.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())) {
SofaBlockBehavior stairsBlockBehavior = optionalStairsBlockBehavior.get();
Direction direction1 = customState.get(stairsBlockBehavior.facingProperty).toDirection();
if (direction1.axis() != state.get(this.facingProperty).toDirection().axis() && canTakeShape(state, level, pos, direction1)) {
if (direction1 == direction.counterClockWise()) {
return StairsShape.OUTER_LEFT;
return SofaShape.INNER_LEFT;
}
return StairsShape.OUTER_RIGHT;
return SofaShape.INNER_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;
return SofaShape.STRAIGHT;
}
private boolean canTakeShape(ImmutableBlockState state, Object level, BlockPos pos, Direction face) {
@@ -107,11 +91,11 @@ public class BottomHalfStairsBlockBehavior extends BukkitBlockBehavior {
return true;
}
ImmutableBlockState anotherState = optionalAnotherState.get();
Optional<BottomHalfStairsBlockBehavior> optionalBehavior = anotherState.behavior().getAs(BottomHalfStairsBlockBehavior.class);
Optional<SofaBlockBehavior> optionalBehavior = anotherState.behavior().getAs(SofaBlockBehavior.class);
if (optionalBehavior.isEmpty()) {
return true;
}
BottomHalfStairsBlockBehavior anotherBehavior = optionalBehavior.get();
SofaBlockBehavior anotherBehavior = optionalBehavior.get();
return anotherState.get(anotherBehavior.facingProperty) != state.get(this.facingProperty);
}
@@ -120,9 +104,9 @@ public class BottomHalfStairsBlockBehavior extends BukkitBlockBehavior {
@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);
Property<HorizontalDirection> facing = (Property<HorizontalDirection>) ResourceConfigUtils.requireNonNullOrThrow(block.getProperty("facing"), "warning.config.block.behavior.sofa.missing_facing");
Property<SofaShape> shape = (Property<SofaShape>) ResourceConfigUtils.requireNonNullOrThrow(block.getProperty("shape"), "warning.config.block.behavior.sofa.missing_shape");
return new SofaBlockBehavior(block, facing, shape);
}
}
}

View File

@@ -3,11 +3,14 @@ package net.momirealms.craftengine.bukkit.block.behavior;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.CoreReflections;
import net.momirealms.craftengine.bukkit.util.BlockStateUtils;
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.entity.player.InteractionResult;
import net.momirealms.craftengine.core.item.context.UseOnContext;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import java.util.Map;
@@ -18,15 +21,35 @@ public class ToggleableLampBlockBehavior extends BukkitBlockBehavior {
public static final Factory FACTORY = new Factory();
private final Property<Boolean> litProperty;
private final Property<Boolean> poweredProperty;
private final boolean canOpenWithHand;
public ToggleableLampBlockBehavior(CustomBlock block, Property<Boolean> litProperty, Property<Boolean> poweredProperty) {
public ToggleableLampBlockBehavior(CustomBlock block, Property<Boolean> litProperty, Property<Boolean> poweredProperty, boolean canOpenWithHand) {
super(block);
this.litProperty = litProperty;
this.poweredProperty = poweredProperty;
this.canOpenWithHand = canOpenWithHand;
}
@Override
public InteractionResult useWithoutItem(UseOnContext context, ImmutableBlockState state) {
if (!this.canOpenWithHand) {
return InteractionResult.PASS;
}
ToggleableLampBlockBehavior behavior = state.behavior().getAs(ToggleableLampBlockBehavior.class).orElse(null);
if (behavior == null) return InteractionResult.PASS;
FastNMS.INSTANCE.method$LevelWriter$setBlock(
context.getLevel().serverWorld(),
LocationUtils.toBlockPos(context.getClickedPos()),
state.cycle(behavior.litProperty).customBlockState().literalObject(),
2
);
Optional.ofNullable(context.getPlayer()).ifPresent(p -> p.swingHand(context.getHand()));
return InteractionResult.SUCCESS_AND_CANCEL;
}
@Override
public void onPlace(Object thisBlock, Object[] args, Callable<Object> superMethod) {
if (this.canOpenWithHand) return;
Object state = args[0];
Object level = args[1];
Object pos = args[2];
@@ -40,6 +63,7 @@ public class ToggleableLampBlockBehavior extends BukkitBlockBehavior {
@Override
public void neighborChanged(Object thisBlock, Object[] args, Callable<Object> superMethod) {
if (this.canOpenWithHand) return;
Object blockState = args[0];
Object world = args[1];
if (!CoreReflections.clazz$ServerLevel.isInstance(world)) return;
@@ -69,7 +93,8 @@ public class ToggleableLampBlockBehavior extends BukkitBlockBehavior {
public BlockBehavior create(CustomBlock block, Map<String, Object> arguments) {
Property<Boolean> lit = (Property<Boolean>) ResourceConfigUtils.requireNonNullOrThrow(block.getProperty("lit"), "warning.config.block.behavior.toggleable_lamp.missing_lit");
Property<Boolean> powered = (Property<Boolean>) ResourceConfigUtils.requireNonNullOrThrow(block.getProperty("powered"), "warning.config.block.behavior.toggleable_lamp.missing_powered");
return new ToggleableLampBlockBehavior(block, lit, powered);
boolean canOpenWithHand = ResourceConfigUtils.getAsBoolean(arguments.getOrDefault("can-open-with-hand", false), "can-open-with-hand");
return new ToggleableLampBlockBehavior(block, lit, powered, canOpenWithHand);
}
}
}