修复集装箱连点问题

This commit is contained in:
Light_Quanta 2025-03-15 01:14:15 +08:00
parent 415d04edaf
commit 604c99550f
No known key found for this signature in database
GPG key ID: 11A39A1B8C890959

View file

@ -33,8 +33,10 @@ import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.phys.BlockHitResult; 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;
import org.jetbrains.annotations.NotNull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.List; import java.util.List;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@ -49,12 +51,18 @@ public class ContainerBlock extends BaseEntityBlock {
} }
@Override @Override
public InteractionResult use(BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer, InteractionHand pHand, BlockHitResult pHit) { @ParametersAreNonnullByDefault
if (!pLevel.isClientSide) { public @NotNull InteractionResult use(BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer, InteractionHand pHand, BlockHitResult pHit) {
if (pLevel.isClientSide
|| pState.getValue(OPENED)
|| !(pLevel.getBlockEntity(pPos) instanceof ContainerBlockEntity containerBlockEntity)
) return InteractionResult.PASS;
ItemStack stack = pPlayer.getItemInHand(pHand); ItemStack stack = pPlayer.getItemInHand(pHand);
if (stack.is(ModItems.CROWBAR.get())) { if (!stack.is(ModItems.CROWBAR.get())) {
BlockEntity blockEntity = pLevel.getBlockEntity(pPos); pPlayer.displayClientMessage(Component.translatable("des.superbwarfare.container.fail.crowbar"), true);
if (!(blockEntity instanceof ContainerBlockEntity containerBlockEntity)) return InteractionResult.PASS; return InteractionResult.PASS;
}
if (!hasEntity(pLevel, pPos)) { if (!hasEntity(pLevel, pPos)) {
pPlayer.displayClientMessage(Component.translatable("des.superbwarfare.container.fail.empty"), true); pPlayer.displayClientMessage(Component.translatable("des.superbwarfare.container.fail.empty"), true);
@ -63,21 +71,14 @@ public class ContainerBlock extends BaseEntityBlock {
if (canOpen(pLevel, pPos, containerBlockEntity.entityType, containerBlockEntity.entity)) { if (canOpen(pLevel, pPos, containerBlockEntity.entityType, containerBlockEntity.entity)) {
pLevel.setBlockAndUpdate(pPos, pState.setValue(OPENED, true)); pLevel.setBlockAndUpdate(pPos, pState.setValue(OPENED, true));
if (!pLevel.isClientSide()) {
pLevel.playSound(null, BlockPos.containing(pPos.getX(), pPos.getY(), pPos.getZ()), ModSounds.OPEN.get(), SoundSource.BLOCKS, 1, 1); pLevel.playSound(null, BlockPos.containing(pPos.getX(), pPos.getY(), pPos.getZ()), ModSounds.OPEN.get(), SoundSource.BLOCKS, 1, 1);
} else {
pLevel.playLocalSound(pPos.getX(), pPos.getY(), pPos.getZ(), ModSounds.OPEN.get(), SoundSource.BLOCKS, 1, 1, false);
}
return InteractionResult.SUCCESS; return InteractionResult.SUCCESS;
} else { } else {
pPlayer.displayClientMessage(Component.translatable("des.superbwarfare.container.fail.open"), true); pPlayer.displayClientMessage(Component.translatable("des.superbwarfare.container.fail.open"), true);
}
} else {
pPlayer.displayClientMessage(Component.translatable("des.superbwarfare.container.fail.crowbar"), true);
}
}
return InteractionResult.PASS; return InteractionResult.PASS;
} }
}
public boolean hasEntity(Level pLevel, BlockPos pPos) { public boolean hasEntity(Level pLevel, BlockPos pPos) {
BlockEntity blockEntity = pLevel.getBlockEntity(pPos); BlockEntity blockEntity = pLevel.getBlockEntity(pPos);
@ -120,7 +121,7 @@ public class ContainerBlock extends BaseEntityBlock {
@Nullable @Nullable
@Override @Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level pLevel, BlockState pState, BlockEntityType<T> pBlockEntityType) { public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level pLevel, @NotNull BlockState pState, @NotNull BlockEntityType<T> pBlockEntityType) {
if (!pLevel.isClientSide) { if (!pLevel.isClientSide) {
return createTickerHelper(pBlockEntityType, ModBlockEntities.CONTAINER.get(), ContainerBlockEntity::serverTick); return createTickerHelper(pBlockEntityType, ModBlockEntities.CONTAINER.get(), ContainerBlockEntity::serverTick);
} }
@ -128,7 +129,7 @@ public class ContainerBlock extends BaseEntityBlock {
} }
@Override @Override
public void appendHoverText(ItemStack pStack, @Nullable BlockGetter pLevel, List<Component> pTooltip, TooltipFlag pFlag) { public void appendHoverText(@NotNull ItemStack pStack, @Nullable BlockGetter pLevel, @NotNull List<Component> pTooltip, @NotNull TooltipFlag pFlag) {
super.appendHoverText(pStack, pLevel, pTooltip, pFlag); super.appendHoverText(pStack, pLevel, pTooltip, pFlag);
CompoundTag tag = BlockItem.getBlockEntityData(pStack); CompoundTag tag = BlockItem.getBlockEntityData(pStack);
if (tag != null && tag.contains("EntityType")) { if (tag != null && tag.contains("EntityType")) {
@ -169,18 +170,19 @@ public class ContainerBlock extends BaseEntityBlock {
} }
@Override @Override
public VoxelShape getShape(BlockState state, BlockGetter world, BlockPos pos, CollisionContext context) { @ParametersAreNonnullByDefault
public @NotNull VoxelShape getShape(BlockState state, BlockGetter world, BlockPos pos, CollisionContext context) {
return state.getValue(OPENED) ? box(1, 0, 1, 15, 14, 15) : box(0, 0, 0, 16, 15, 16); return state.getValue(OPENED) ? box(1, 0, 1, 15, 14, 15) : box(0, 0, 0, 16, 15, 16);
} }
@Override @Override
public RenderShape getRenderShape(BlockState state) { public @NotNull RenderShape getRenderShape(@NotNull BlockState state) {
return RenderShape.ENTITYBLOCK_ANIMATED; return RenderShape.ENTITYBLOCK_ANIMATED;
} }
@Nullable @Nullable
@Override @Override
public BlockEntity newBlockEntity(BlockPos blockPos, BlockState blockState) { public BlockEntity newBlockEntity(@NotNull BlockPos blockPos, @NotNull BlockState blockState) {
return new ContainerBlockEntity(blockPos, blockState); return new ContainerBlockEntity(blockPos, blockState);
} }
@ -195,7 +197,8 @@ public class ContainerBlock extends BaseEntityBlock {
} }
@Override @Override
public ItemStack getCloneItemStack(BlockGetter pLevel, BlockPos pPos, BlockState pState) { @ParametersAreNonnullByDefault
public @NotNull ItemStack getCloneItemStack(BlockGetter pLevel, BlockPos pPos, BlockState pState) {
ItemStack itemstack = super.getCloneItemStack(pLevel, pPos, pState); ItemStack itemstack = super.getCloneItemStack(pLevel, pPos, pState);
pLevel.getBlockEntity(pPos, ModBlockEntities.CONTAINER.get()).ifPresent((blockEntity) -> blockEntity.saveToItem(itemstack)); pLevel.getBlockEntity(pPos, ModBlockEntities.CONTAINER.get()).ifPresent((blockEntity) -> blockEntity.saveToItem(itemstack));
return itemstack; return itemstack;