9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-25 18:09:27 +00:00

fix(bukkit): 修正可堆叠方块行为不还原原版的问题

This commit is contained in:
jhqwqmc
2025-06-19 12:53:37 +08:00
parent 80973618e0
commit cb52b290f6

View File

@@ -8,6 +8,7 @@ import net.momirealms.craftengine.core.block.ImmutableBlockState;
import net.momirealms.craftengine.core.block.UpdateOption;
import net.momirealms.craftengine.core.block.behavior.BlockBehaviorFactory;
import net.momirealms.craftengine.core.block.properties.IntegerProperty;
import net.momirealms.craftengine.core.entity.player.InteractionHand;
import net.momirealms.craftengine.core.entity.player.InteractionResult;
import net.momirealms.craftengine.core.entity.player.Player;
import net.momirealms.craftengine.core.item.Item;
@@ -50,22 +51,36 @@ public class StackableBlockBehavior extends BukkitBlockBehavior {
if (item == null) {
return InteractionResult.PASS;
}
if (this.items.contains(item.id()) && state.get(this.amountProperty) < this.amountProperty.max) {
ImmutableBlockState nextStage = state.cycle(this.amountProperty);
World world = context.getLevel();
BlockPos pos = context.getClickedPos();
Location location = new Location((org.bukkit.World) world.platformWorld(), pos.x(), pos.y(), pos.z());
FastNMS.INSTANCE.method$LevelWriter$setBlock(world.serverWorld(), LocationUtils.toBlockPos(pos), nextStage.customBlockState().handle(), UpdateOption.UPDATE_NONE.flags());
if (stackSound != null) {
location.getWorld().playSound(location, stackSound.id().toString(), SoundCategory.BLOCKS, stackSound.volume(), stackSound.pitch());
}
FastNMS.INSTANCE.method$ItemStack$consume(item.getLiteralObject(), 1, player.serverPlayer());
player.swingHand(context.getHand());
if (!this.items.contains(item.id())) {
return InteractionResult.PASS;
}
BlockPos pos = context.getClickedPos();
World world = context.getLevel();
if (state.get(this.amountProperty) < this.amountProperty.max) {
updateStackableBlock(state, pos, world, item, player, context.getHand());
return InteractionResult.SUCCESS_AND_CANCEL;
}
BlockPos actualPos = pos.relative(context.getClickedFace());
ImmutableBlockState actualState = world.getBlockAt(actualPos).customBlockState();
boolean isValid = actualState != null && !actualState.isEmpty() && actualState.contains(this.amountProperty);
if (isValid && actualState.get(this.amountProperty) < this.amountProperty.max) {
updateStackableBlock(actualState, actualPos, world, item, player, context.getHand());
return InteractionResult.SUCCESS_AND_CANCEL;
}
return InteractionResult.PASS;
}
private void updateStackableBlock(ImmutableBlockState state, BlockPos pos, World world, Item<ItemStack> item, Player player, InteractionHand hand) {
ImmutableBlockState nextStage = state.cycle(this.amountProperty);
Location location = new Location((org.bukkit.World) world.platformWorld(), pos.x(), pos.y(), pos.z());
FastNMS.INSTANCE.method$LevelWriter$setBlock(world.serverWorld(), LocationUtils.toBlockPos(pos), nextStage.customBlockState().handle(), UpdateOption.UPDATE_NONE.flags());
if (stackSound != null) {
location.getWorld().playSound(location, stackSound.id().toString(), SoundCategory.BLOCKS, stackSound.volume(), stackSound.pitch());
}
FastNMS.INSTANCE.method$ItemStack$consume(item.getLiteralObject(), 1, player.serverPlayer());
player.swingHand(hand);
}
public static class Factory implements BlockBehaviorFactory {
@Override