From 858d6c3e887fb1068cb29446a27ff5524b93bafa Mon Sep 17 00:00:00 2001 From: 17146 <1714673995@qq.com> Date: Thu, 28 Nov 2024 20:22:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=BC=80=E5=90=AF=E5=88=A4?= =?UTF-8?q?=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../superbwarfare/block/ContainerBlock.java | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/atsuishio/superbwarfare/block/ContainerBlock.java b/src/main/java/com/atsuishio/superbwarfare/block/ContainerBlock.java index fbb806302..79b0d3449 100644 --- a/src/main/java/com/atsuishio/superbwarfare/block/ContainerBlock.java +++ b/src/main/java/com/atsuishio/superbwarfare/block/ContainerBlock.java @@ -5,11 +5,15 @@ import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.Component; +import net.minecraft.world.InteractionHand; +import net.minecraft.world.InteractionResult; +import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.TooltipFlag; import net.minecraft.world.item.context.BlockPlaceContext; import net.minecraft.world.level.BlockGetter; +import net.minecraft.world.level.Level; import net.minecraft.world.level.block.*; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.state.BlockBehaviour; @@ -17,6 +21,7 @@ import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.StateDefinition; import net.minecraft.world.level.block.state.properties.BooleanProperty; import net.minecraft.world.level.block.state.properties.DirectionProperty; +import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.VoxelShape; @@ -34,6 +39,40 @@ public class ContainerBlock extends BaseEntityBlock { this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(OPENED, false)); } + @Override + public InteractionResult use(BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer, InteractionHand pHand, BlockHitResult pHit) { + if (pLevel.isClientSide) { + return InteractionResult.PASS; + } else { + if (canOpen(pLevel, pPos)) { + pLevel.setBlockAndUpdate(pPos, pState.setValue(OPENED, true)); + + return InteractionResult.SUCCESS; + } + pPlayer.displayClientMessage(Component.literal("打不开哼哼啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊"), true); + return InteractionResult.PASS; + } + } + + public boolean canOpen(Level pLevel, BlockPos pPos) { + boolean flag = true; + + for (int i = -4; i < 5; i++) { + for (int j = 0; j < 7; j++) { + for (int k = -4; k < 5; k++) { + if (i == 0 && j == 0 && k == 0) { + continue; + } + if (!pLevel.getBlockState(pPos.offset(i, j, k)).isAir()) { + flag = false; + } + } + } + } + + return flag; + } + @Override public void appendHoverText(ItemStack pStack, @Nullable BlockGetter pLevel, List pTooltip, TooltipFlag pFlag) { super.appendHoverText(pStack, pLevel, pTooltip, pFlag); @@ -50,7 +89,7 @@ public class ContainerBlock extends BaseEntityBlock { @Override public VoxelShape getShape(BlockState state, BlockGetter world, BlockPos pos, CollisionContext context) { - return state.getValue(OPENED) ? box(0, 0, 0, 16, 1, 16) : box(0, 0, 0, 16, 16, 16); + return state.getValue(OPENED) ? box(1, 0, 1, 15, 14, 15) : box(0, 0, 0, 16, 16, 16); } @Override