添加开启判定

This commit is contained in:
17146 2024-11-28 20:22:24 +08:00
parent 1008a673ce
commit 858d6c3e88

View file

@ -5,11 +5,15 @@ import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component; 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.BlockItem;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag; import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.context.BlockPlaceContext; import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.*; import net.minecraft.world.level.block.*;
import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour; 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.StateDefinition;
import net.minecraft.world.level.block.state.properties.BooleanProperty; import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.DirectionProperty; 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.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape; 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)); 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 @Override
public void appendHoverText(ItemStack pStack, @Nullable BlockGetter pLevel, List<Component> pTooltip, TooltipFlag pFlag) { public void appendHoverText(ItemStack pStack, @Nullable BlockGetter pLevel, List<Component> pTooltip, TooltipFlag pFlag) {
super.appendHoverText(pStack, pLevel, pTooltip, pFlag); super.appendHoverText(pStack, pLevel, pTooltip, pFlag);
@ -50,7 +89,7 @@ public class ContainerBlock extends BaseEntityBlock {
@Override @Override
public VoxelShape getShape(BlockState state, BlockGetter world, BlockPos pos, CollisionContext context) { 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 @Override