| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.core.particles.ParticleTypes;
|
| 6 | import net.minecraft.network.chat.Component;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.stats.Stats;
|
| 9 | import net.minecraft.util.RandomSource;
|
| 10 | import net.minecraft.world.InteractionResult;
|
| 11 | import net.minecraft.world.SimpleMenuProvider;
|
| 12 | import net.minecraft.world.entity.monster.piglin.PiglinAi;
|
| 13 | import net.minecraft.world.entity.player.Player;
|
| 14 | import net.minecraft.world.inventory.ChestMenu;
|
| 15 | import net.minecraft.world.inventory.PlayerEnderChestContainer;
|
| 16 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 17 | import net.minecraft.world.level.BlockGetter;
|
| 18 | import net.minecraft.world.level.Level;
|
| 19 | import net.minecraft.world.level.LevelReader;
|
| 20 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 21 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 22 | import net.minecraft.world.level.block.entity.BlockEntityTicker;
|
| 23 | import net.minecraft.world.level.block.entity.BlockEntityType;
|
| 24 | import net.minecraft.world.level.block.entity.BlockEntityTypes;
|
| 25 | import net.minecraft.world.level.block.entity.ChestBlockEntity;
|
| 26 | import net.minecraft.world.level.block.entity.EnderChestBlockEntity;
|
| 27 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 28 | import net.minecraft.world.level.block.state.BlockState;
|
| 29 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 30 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 31 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 32 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 33 | import net.minecraft.world.level.material.FluidState;
|
| 34 | import net.minecraft.world.level.material.Fluids;
|
| 35 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 36 | import net.minecraft.world.phys.BlockHitResult;
|
| 37 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 38 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 39 | import org.jspecify.annotations.Nullable;
|
| 40 |
|
| 41 | /**
|
| 42 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 43 | */
|
| 44 | public class EnderChestBlock extends AbstractChestBlock<EnderChestBlockEntity> implements SimpleWaterloggedBlock {
|
| 45 | public static final EnumProperty<Direction> FACING = HorizontalDirectionalBlock.FACING;
|
| 46 | public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
|
| 47 | private static final VoxelShape SHAPE = Block.column(14.0, 0.0, 14.0);
|
| 48 | private static final Component CONTAINER_TITLE = Component.translatable("container.enderchest");
|
| 49 |
|
| 50 | /**
|
| 51 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 52 | */
|
| 53 | public EnderChestBlock(final BlockBehaviour.Properties properties) {
|
| 54 | super(properties, () -> BlockEntityTypes.ENDER_CHEST);
|
| 55 | this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(WATERLOGGED, false));
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | public DoubleBlockCombiner.NeighborCombineResult<? extends ChestBlockEntity> combine(
|
| 60 | final BlockState state, final Level level, final BlockPos pos, final boolean ignoreBeingBlocked
|
| 61 | ) {
|
| 62 | return DoubleBlockCombiner.Combiner::acceptNone;
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 67 | return SHAPE;
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | public BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 72 | FluidState replacedFluidState = context.getLevel().getFluidState(context.getClickedPos());
|
| 73 | return this.defaultBlockState().setValue(FACING, context.getHorizontalDirection().getOpposite()).setValue(WATERLOGGED, replacedFluidState.is(Fluids.WATER));
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
|
| 78 | PlayerEnderChestContainer container = player.getEnderChestInventory();
|
| 79 | if (container != null && level.getBlockEntity(pos) instanceof EnderChestBlockEntity enderChest) {
|
| 80 | BlockPos above = pos.above();
|
| 81 | if (level.getBlockState(above).isRedstoneConductor(level, above)) {
|
| 82 | return InteractionResult.SUCCESS;
|
| 83 | }
|
| 84 |
|
| 85 | if (level instanceof ServerLevel serverLevel) {
|
| 86 | container.setActiveChest(enderChest);
|
| 87 | player.openMenu(new SimpleMenuProvider((containerId, inventory, p) -> ChestMenu.threeRows(containerId, inventory, container), CONTAINER_TITLE));
|
| 88 | player.awardStat(Stats.OPEN_ENDERCHEST);
|
| 89 | PiglinAi.angerNearbyPiglins(serverLevel, player, true);
|
| 90 | }
|
| 91 |
|
| 92 | return InteractionResult.SUCCESS;
|
| 93 | } else {
|
| 94 | return InteractionResult.SUCCESS;
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | public BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 100 | return new EnderChestBlockEntity(worldPosition, blockState);
|
| 101 | }
|
| 102 |
|
| 103 | @Override
|
| 104 | public <T extends BlockEntity> @Nullable BlockEntityTicker<T> getTicker(final Level level, final BlockState blockState, final BlockEntityType<T> type) {
|
| 105 | return level.isClientSide() ? createTickerHelper(type, BlockEntityTypes.ENDER_CHEST, EnderChestBlockEntity::lidAnimateTick) : null;
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | public void animateTick(final BlockState state, final Level level, final BlockPos pos, final RandomSource random) {
|
| 110 | for (int i = 0; i < 3; i++) {
|
| 111 | int flipX = random.nextInt(2) * 2 - 1;
|
| 112 | int flipZ = random.nextInt(2) * 2 - 1;
|
| 113 | double x = pos.getX() + 0.5 + 0.25 * flipX;
|
| 114 | double y = pos.getY() + random.nextFloat();
|
| 115 | double z = pos.getZ() + 0.5 + 0.25 * flipZ;
|
| 116 | double xa = random.nextFloat() * flipX;
|
| 117 | double ya = (random.nextFloat() - 0.5) * 0.125;
|
| 118 | double za = random.nextFloat() * flipZ;
|
| 119 | level.addParticle(ParticleTypes.PORTAL, x, y, z, xa, ya, za);
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | protected BlockState rotate(final BlockState state, final Rotation rotation) {
|
| 125 | return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
|
| 126 | }
|
| 127 |
|
| 128 | @Override
|
| 129 | protected BlockState mirror(final BlockState state, final Mirror mirror) {
|
| 130 | return state.rotate(mirror.getRotation(state.getValue(FACING)));
|
| 131 | }
|
| 132 |
|
| 133 | @Override
|
| 134 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 135 | builder.add(FACING, WATERLOGGED);
|
| 136 | }
|
| 137 |
|
| 138 | @Override
|
| 139 | protected FluidState getFluidState(final BlockState state) {
|
| 140 | return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
|
| 141 | }
|
| 142 |
|
| 143 | @Override
|
| 144 | protected BlockState updateShape(
|
| 145 | final BlockState state,
|
| 146 | final LevelReader level,
|
| 147 | final ScheduledTickAccess ticks,
|
| 148 | final BlockPos pos,
|
| 149 | final Direction directionToNeighbour,
|
| 150 | final BlockPos neighbourPos,
|
| 151 | final BlockState neighbourState,
|
| 152 | final RandomSource random
|
| 153 | ) {
|
| 154 | if (state.getValue(WATERLOGGED)) {
|
| 155 | ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
|
| 156 | }
|
| 157 |
|
| 158 | return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 159 | }
|
| 160 |
|
| 161 | @Override
|
| 162 | protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
|
| 163 | return false;
|
| 164 | }
|
| 165 |
|
| 166 | @Override
|
| 167 | protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 168 | if (level.getBlockEntity(pos) instanceof EnderChestBlockEntity enderChestBlockEntity) {
|
| 169 | enderChestBlockEntity.recheckOpen();
|
| 170 | }
|
| 171 | }
|
| 172 | }
|
| 173 |
|