| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.stats.Stats;
|
| 7 | import net.minecraft.util.RandomSource;
|
| 8 | import net.minecraft.world.Containers;
|
| 9 | import net.minecraft.world.InteractionResult;
|
| 10 | import net.minecraft.world.entity.monster.piglin.PiglinAi;
|
| 11 | import net.minecraft.world.entity.player.Player;
|
| 12 | import net.minecraft.world.inventory.AbstractContainerMenu;
|
| 13 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 14 | import net.minecraft.world.level.Level;
|
| 15 | import net.minecraft.world.level.block.entity.BarrelBlockEntity;
|
| 16 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 17 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 18 | import net.minecraft.world.level.block.state.BlockState;
|
| 19 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 20 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 21 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 22 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 23 | import net.minecraft.world.phys.BlockHitResult;
|
| 24 | import org.jspecify.annotations.Nullable;
|
| 25 |
|
| 26 | public class BarrelBlock extends BaseEntityBlock {
|
| 27 | public static final EnumProperty<Direction> FACING = BlockStateProperties.FACING;
|
| 28 | public static final BooleanProperty OPEN = BlockStateProperties.OPEN;
|
| 29 |
|
| 30 | public BarrelBlock(final BlockBehaviour.Properties properties) {
|
| 31 | super(properties);
|
| 32 | this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(OPEN, false));
|
| 33 | }
|
| 34 |
|
| 35 | @Override
|
| 36 | protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
|
| 37 | if (level instanceof ServerLevel serverLevel && level.getBlockEntity(pos) instanceof BarrelBlockEntity barrelBlockEntity) {
|
| 38 | player.openMenu(barrelBlockEntity);
|
| 39 | player.awardStat(Stats.OPEN_BARREL);
|
| 40 | PiglinAi.angerNearbyPiglins(serverLevel, player, true);
|
| 41 | }
|
| 42 |
|
| 43 | return InteractionResult.SUCCESS;
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | protected void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston) {
|
| 48 | Containers.updateNeighboursAfterDestroy(state, level, pos);
|
| 49 | }
|
| 50 |
|
| 51 | @Override
|
| 52 | protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 53 | if (level.getBlockEntity(pos) instanceof BarrelBlockEntity barrelBlockEntity) {
|
| 54 | barrelBlockEntity.recheckOpen();
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | public @Nullable BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 60 | return new BarrelBlockEntity(worldPosition, blockState);
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | protected boolean hasAnalogOutputSignal(final BlockState state) {
|
| 65 | return true;
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | protected int getAnalogOutputSignal(final BlockState state, final Level level, final BlockPos pos, final Direction direction) {
|
| 70 | return AbstractContainerMenu.getRedstoneSignalFromBlockEntity(level.getBlockEntity(pos));
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | protected BlockState rotate(final BlockState state, final Rotation rotation) {
|
| 75 | return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | protected BlockState mirror(final BlockState state, final Mirror mirror) {
|
| 80 | return state.rotate(mirror.getRotation(state.getValue(FACING)));
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 85 | builder.add(FACING, OPEN);
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | public BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 90 | return this.defaultBlockState().setValue(FACING, context.getNearestLookingDirection().getOpposite());
|
| 91 | }
|
| 92 | }
|
| 93 |
|