| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.floats.Float2FloatFunction;
|
| 4 | import java.util.List;
|
| 5 | import java.util.Map;
|
| 6 | import java.util.Optional;
|
| 7 | import java.util.function.BiPredicate;
|
| 8 | import java.util.function.Supplier;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.core.Direction;
|
| 11 | import net.minecraft.network.chat.Component;
|
| 12 | import net.minecraft.resources.Identifier;
|
| 13 | import net.minecraft.server.level.ServerLevel;
|
| 14 | import net.minecraft.sounds.SoundEvent;
|
| 15 | import net.minecraft.stats.Stat;
|
| 16 | import net.minecraft.stats.Stats;
|
| 17 | import net.minecraft.util.RandomSource;
|
| 18 | import net.minecraft.world.CompoundContainer;
|
| 19 | import net.minecraft.world.Container;
|
| 20 | import net.minecraft.world.Containers;
|
| 21 | import net.minecraft.world.InteractionResult;
|
| 22 | import net.minecraft.world.MenuProvider;
|
| 23 | import net.minecraft.world.entity.animal.feline.Cat;
|
| 24 | import net.minecraft.world.entity.monster.piglin.PiglinAi;
|
| 25 | import net.minecraft.world.entity.player.Inventory;
|
| 26 | import net.minecraft.world.entity.player.Player;
|
| 27 | import net.minecraft.world.inventory.AbstractContainerMenu;
|
| 28 | import net.minecraft.world.inventory.ChestMenu;
|
| 29 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 30 | import net.minecraft.world.level.BlockGetter;
|
| 31 | import net.minecraft.world.level.Level;
|
| 32 | import net.minecraft.world.level.LevelAccessor;
|
| 33 | import net.minecraft.world.level.LevelReader;
|
| 34 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 35 | import net.minecraft.world.level.block.entity.BaseContainerBlockEntity;
|
| 36 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 37 | import net.minecraft.world.level.block.entity.BlockEntityTicker;
|
| 38 | import net.minecraft.world.level.block.entity.BlockEntityType;
|
| 39 | import net.minecraft.world.level.block.entity.ChestBlockEntity;
|
| 40 | import net.minecraft.world.level.block.entity.LidBlockEntity;
|
| 41 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 42 | import net.minecraft.world.level.block.state.BlockState;
|
| 43 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 44 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 45 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 46 | import net.minecraft.world.level.block.state.properties.ChestType;
|
| 47 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 48 | import net.minecraft.world.level.material.FluidState;
|
| 49 | import net.minecraft.world.level.material.Fluids;
|
| 50 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 51 | import net.minecraft.world.phys.AABB;
|
| 52 | import net.minecraft.world.phys.BlockHitResult;
|
| 53 | import net.minecraft.world.phys.Vec3;
|
| 54 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 55 | import net.minecraft.world.phys.shapes.Shapes;
|
| 56 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 57 | import org.jspecify.annotations.Nullable;
|
| 58 |
|
| 59 | /**
|
| 60 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 61 | */
|
| 62 | public class ChestBlock extends AbstractChestBlock<ChestBlockEntity> implements SimpleWaterloggedBlock {
|
| 63 | public static final EnumProperty<Direction> FACING = HorizontalDirectionalBlock.FACING;
|
| 64 | public static final EnumProperty<ChestType> TYPE = BlockStateProperties.CHEST_TYPE;
|
| 65 | public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
|
| 66 | public static final int EVENT_SET_OPEN_COUNT = 1;
|
| 67 | private static final VoxelShape SHAPE = Block.column(14.0, 0.0, 14.0);
|
| 68 | private static final Map<Direction, VoxelShape> HALF_SHAPES = Shapes.rotateHorizontal(Block.boxZ(14.0, 0.0, 14.0, 0.0, 15.0));
|
| 69 | private final SoundEvent openSound;
|
| 70 | private final SoundEvent closeSound;
|
| 71 | private static final DoubleBlockCombiner.Combiner<ChestBlockEntity, Optional<Container>> CHEST_COMBINER = new DoubleBlockCombiner.Combiner<ChestBlockEntity, Optional<Container>>() {
|
| 72 | public Optional<Container> acceptDouble(final ChestBlockEntity first, final ChestBlockEntity second) {
|
| 73 | return Optional.of(new CompoundContainer(first, second));
|
| 74 | }
|
| 75 |
|
| 76 | public Optional<Container> acceptSingle(final ChestBlockEntity single) {
|
| 77 | return Optional.of(single);
|
| 78 | }
|
| 79 |
|
| 80 | public Optional<Container> acceptNone() {
|
| 81 | return Optional.empty();
|
| 82 | }
|
| 83 | };
|
| 84 | private static final DoubleBlockCombiner.Combiner<ChestBlockEntity, Optional<MenuProvider>> MENU_PROVIDER_COMBINER = new DoubleBlockCombiner.Combiner<ChestBlockEntity, Optional<MenuProvider>>() {
|
| 85 | public Optional<MenuProvider> acceptDouble(final ChestBlockEntity first, final ChestBlockEntity second) {
|
| 86 | final Container container = new CompoundContainer(first, second);
|
| 87 | return Optional.of(new MenuProvider() {
|
| 88 | @Override
|
| 89 | public @Nullable AbstractContainerMenu createMenu(final int containerId, final Inventory inventory, final Player player) {
|
| 90 | if (first.canOpen(player) && second.canOpen(player)) {
|
| 91 | first.unpackLootTable(inventory.player);
|
| 92 | second.unpackLootTable(inventory.player);
|
| 93 | return ChestMenu.sixRows(containerId, inventory, container);
|
| 94 | }
|
| 95 |
|
| 96 | if (!player.isSpectator()) {
|
| 97 | Direction connectedDirection = ChestBlock.getConnectedDirection(first.getBlockState());
|
| 98 | Vec3 firstCenter = Vec3.atCenterOf(first.getBlockPos());
|
| 99 | Vec3 centerBetweenChests = firstCenter.add(connectedDirection.getStepX() / 2.0, 0.0, connectedDirection.getStepZ() / 2.0);
|
| 100 | BaseContainerBlockEntity.sendChestLockedNotifications(centerBetweenChests, player, this.getDisplayName());
|
| 101 | }
|
| 102 |
|
| 103 | return null;
|
| 104 | }
|
| 105 |
|
| 106 | @Override
|
| 107 | public Component getDisplayName() {
|
| 108 | if (first.hasCustomName()) {
|
| 109 | return first.getDisplayName();
|
| 110 | } else {
|
| 111 | return second.hasCustomName() ? second.getDisplayName() : Component.translatable("container.chestDouble");
|
| 112 | }
|
| 113 | }
|
| 114 | });
|
| 115 | }
|
| 116 |
|
| 117 | public Optional<MenuProvider> acceptSingle(final ChestBlockEntity single) {
|
| 118 | return Optional.of(single);
|
| 119 | }
|
| 120 |
|
| 121 | public Optional<MenuProvider> acceptNone() {
|
| 122 | return Optional.empty();
|
| 123 | }
|
| 124 | };
|
| 125 |
|
| 126 | /**
|
| 127 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 128 | */
|
| 129 | public ChestBlock(
|
| 130 | final Supplier<BlockEntityType<? extends ChestBlockEntity>> blockEntityType,
|
| 131 | final SoundEvent openSound,
|
| 132 | final SoundEvent closeSound,
|
| 133 | final BlockBehaviour.Properties properties
|
| 134 | ) {
|
| 135 | super(properties, blockEntityType);
|
| 136 | this.openSound = openSound;
|
| 137 | this.closeSound = closeSound;
|
| 138 | this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(TYPE, ChestType.SINGLE).setValue(WATERLOGGED, false));
|
| 139 | }
|
| 140 |
|
| 141 | public static DoubleBlockCombiner.BlockType getBlockType(final BlockState state) {
|
| 142 | ChestType type = state.getValue(TYPE);
|
| 143 | if (type == ChestType.SINGLE) {
|
| 144 | return DoubleBlockCombiner.BlockType.SINGLE;
|
| 145 | } else {
|
| 146 | return type == ChestType.RIGHT ? DoubleBlockCombiner.BlockType.FIRST : DoubleBlockCombiner.BlockType.SECOND;
|
| 147 | }
|
| 148 | }
|
| 149 |
|
| 150 | @Override
|
| 151 | protected BlockState updateShape(
|
| 152 | final BlockState state,
|
| 153 | final LevelReader level,
|
| 154 | final ScheduledTickAccess ticks,
|
| 155 | final BlockPos pos,
|
| 156 | final Direction directionToNeighbour,
|
| 157 | final BlockPos neighbourPos,
|
| 158 | final BlockState neighbourState,
|
| 159 | final RandomSource random
|
| 160 | ) {
|
| 161 | if (state.getValue(WATERLOGGED)) {
|
| 162 | ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
|
| 163 | }
|
| 164 |
|
| 165 | if (this.chestCanConnectTo(neighbourState) && directionToNeighbour.getAxis().isHorizontal()) {
|
| 166 | ChestType neighbourType = neighbourState.getValue(TYPE);
|
| 167 | if (state.getValue(TYPE) == ChestType.SINGLE
|
| 168 | && neighbourType != ChestType.SINGLE
|
| 169 | && state.getValue(FACING) == neighbourState.getValue(FACING)
|
| 170 | && getConnectedDirection(neighbourState) == directionToNeighbour.getOpposite()) {
|
| 171 | return state.setValue(TYPE, neighbourType.getOpposite());
|
| 172 | }
|
| 173 | } else if (getConnectedDirection(state) == directionToNeighbour) {
|
| 174 | return state.setValue(TYPE, ChestType.SINGLE);
|
| 175 | }
|
| 176 |
|
| 177 | return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 178 | }
|
| 179 |
|
| 180 | public boolean chestCanConnectTo(final BlockState blockState) {
|
| 181 | return blockState.is(this);
|
| 182 | }
|
| 183 |
|
| 184 | @Override
|
| 185 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 186 | return switch ((ChestType)state.getValue(TYPE)) {
|
| 187 | case SINGLE -> SHAPE;
|
| 188 | case LEFT, RIGHT -> (VoxelShape)HALF_SHAPES.get(getConnectedDirection(state));
|
| 189 | };
|
| 190 | }
|
| 191 |
|
| 192 | public static Direction getConnectedDirection(final BlockState state) {
|
| 193 | Direction facing = state.getValue(FACING);
|
| 194 | return state.getValue(TYPE) == ChestType.LEFT ? facing.getClockWise() : facing.getCounterClockWise();
|
| 195 | }
|
| 196 |
|
| 197 | public static BlockPos getConnectedBlockPos(final BlockPos pos, final BlockState state) {
|
| 198 | Direction connectedDirection = getConnectedDirection(state);
|
| 199 | return pos.relative(connectedDirection);
|
| 200 | }
|
| 201 |
|
| 202 | @Override
|
| 203 | public BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 204 | ChestType type = ChestType.SINGLE;
|
| 205 | Direction facingDirection = context.getHorizontalDirection().getOpposite();
|
| 206 | FluidState replacedFluidState = context.getLevel().getFluidState(context.getClickedPos());
|
| 207 | boolean secondaryUse = context.isSecondaryUseActive();
|
| 208 | Direction clickedFace = context.getClickedFace();
|
| 209 | if (clickedFace.getAxis().isHorizontal() && secondaryUse) {
|
| 210 | Direction neighbourFacing = this.candidatePartnerFacing(context.getLevel(), context.getClickedPos(), clickedFace.getOpposite());
|
| 211 | if (neighbourFacing != null && neighbourFacing.getAxis() != clickedFace.getAxis()) {
|
| 212 | facingDirection = neighbourFacing;
|
| 213 | type = facingDirection.getCounterClockWise() == clickedFace.getOpposite() ? ChestType.RIGHT : ChestType.LEFT;
|
| 214 | }
|
| 215 | }
|
| 216 |
|
| 217 | if (type == ChestType.SINGLE && !secondaryUse) {
|
| 218 | type = this.getChestType(context.getLevel(), context.getClickedPos(), facingDirection);
|
| 219 | }
|
| 220 |
|
| 221 | return this.defaultBlockState().setValue(FACING, facingDirection).setValue(TYPE, type).setValue(WATERLOGGED, replacedFluidState.is(Fluids.WATER));
|
| 222 | }
|
| 223 |
|
| 224 | protected ChestType getChestType(final Level level, final BlockPos pos, final Direction facingDirection) {
|
| 225 | if (facingDirection == this.candidatePartnerFacing(level, pos, facingDirection.getClockWise())) {
|
| 226 | return ChestType.LEFT;
|
| 227 | } else {
|
| 228 | return facingDirection == this.candidatePartnerFacing(level, pos, facingDirection.getCounterClockWise()) ? ChestType.RIGHT : ChestType.SINGLE;
|
| 229 | }
|
| 230 | }
|
| 231 |
|
| 232 | @Override
|
| 233 | protected FluidState getFluidState(final BlockState state) {
|
| 234 | return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
|
| 235 | }
|
| 236 |
|
| 237 | private @Nullable Direction candidatePartnerFacing(final Level level, final BlockPos pos, final Direction neighbourDirection) {
|
| 238 | BlockState state = level.getBlockState(pos.relative(neighbourDirection));
|
| 239 | return this.chestCanConnectTo(state) && state.getValue(TYPE) == ChestType.SINGLE ? state.getValue(FACING) : null;
|
| 240 | }
|
| 241 |
|
| 242 | @Override
|
| 243 | protected void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston) {
|
| 244 | Containers.updateNeighboursAfterDestroy(state, level, pos);
|
| 245 | }
|
| 246 |
|
| 247 | @Override
|
| 248 | protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
|
| 249 | if (level instanceof ServerLevel serverLevel) {
|
| 250 | MenuProvider menuProvider = this.getMenuProvider(state, level, pos);
|
| 251 | if (menuProvider != null) {
|
| 252 | player.openMenu(menuProvider);
|
| 253 | player.awardStat(this.getOpenChestStat());
|
| 254 | PiglinAi.angerNearbyPiglins(serverLevel, player, true);
|
| 255 | }
|
| 256 | }
|
| 257 |
|
| 258 | return InteractionResult.SUCCESS;
|
| 259 | }
|
| 260 |
|
| 261 | protected Stat<Identifier> getOpenChestStat() {
|
| 262 | return Stats.CUSTOM.get(Stats.OPEN_CHEST);
|
| 263 | }
|
| 264 |
|
| 265 | public BlockEntityType<? extends ChestBlockEntity> blockEntityType() {
|
| 266 | return this.blockEntityType.get();
|
| 267 | }
|
| 268 |
|
| 269 | public static @Nullable Container getContainer(
|
| 270 | final ChestBlock block, final BlockState state, final Level level, final BlockPos pos, final boolean ignoreBeingBlocked
|
| 271 | ) {
|
| 272 | return block.combine(state, level, pos, ignoreBeingBlocked).apply(CHEST_COMBINER).orElse(null);
|
| 273 | }
|
| 274 |
|
| 275 | @Override
|
| 276 | public DoubleBlockCombiner.NeighborCombineResult<? extends ChestBlockEntity> combine(
|
| 277 | final BlockState state, final Level level, final BlockPos pos, final boolean ignoreBeingBlocked
|
| 278 | ) {
|
| 279 | BiPredicate<LevelAccessor, BlockPos> predicate;
|
| 280 | if (ignoreBeingBlocked) {
|
| 281 | predicate = (levelAccessor, blockPos) -> false;
|
| 282 | } else {
|
| 283 | predicate = ChestBlock::isChestBlockedAt;
|
| 284 | }
|
| 285 |
|
| 286 | return DoubleBlockCombiner.combineWithNeigbour(
|
| 287 | this.blockEntityType.get(), ChestBlock::getBlockType, ChestBlock::getConnectedDirection, FACING, state, level, pos, predicate
|
| 288 | );
|
| 289 | }
|
| 290 |
|
| 291 | @Override
|
| 292 | protected @Nullable MenuProvider getMenuProvider(final BlockState state, final Level level, final BlockPos pos) {
|
| 293 | return this.combine(state, level, pos, false).apply(MENU_PROVIDER_COMBINER).orElse(null);
|
| 294 | }
|
| 295 |
|
| 296 | public static DoubleBlockCombiner.Combiner<ChestBlockEntity, Float2FloatFunction> opennessCombiner(final LidBlockEntity entity) {
|
| 297 | return new DoubleBlockCombiner.Combiner<ChestBlockEntity, Float2FloatFunction>() {
|
| 298 | public Float2FloatFunction acceptDouble(final ChestBlockEntity first, final ChestBlockEntity second) {
|
| 299 | return partialTickTime -> Math.max(first.getOpenNess(partialTickTime), second.getOpenNess(partialTickTime));
|
| 300 | }
|
| 301 |
|
| 302 | public Float2FloatFunction acceptSingle(final ChestBlockEntity single) {
|
| 303 | return single::getOpenNess;
|
| 304 | }
|
| 305 |
|
| 306 | public Float2FloatFunction acceptNone() {
|
| 307 | return entity::getOpenNess;
|
| 308 | }
|
| 309 | };
|
| 310 | }
|
| 311 |
|
| 312 | @Override
|
| 313 | public BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 314 | return new ChestBlockEntity(worldPosition, blockState);
|
| 315 | }
|
| 316 |
|
| 317 | @Override
|
| 318 | public <T extends BlockEntity> @Nullable BlockEntityTicker<T> getTicker(final Level level, final BlockState blockState, final BlockEntityType<T> type) {
|
| 319 | return level.isClientSide() ? createTickerHelper(type, this.blockEntityType(), ChestBlockEntity::lidAnimateTick) : null;
|
| 320 | }
|
| 321 |
|
| 322 | public static boolean isChestBlockedAt(final LevelAccessor level, final BlockPos pos) {
|
| 323 | return isBlockedChestByBlock(level, pos) || isCatSittingOnChest(level, pos);
|
| 324 | }
|
| 325 |
|
| 326 | private static boolean isBlockedChestByBlock(final BlockGetter level, final BlockPos pos) {
|
| 327 | BlockPos above = pos.above();
|
| 328 | return level.getBlockState(above).isRedstoneConductor(level, above);
|
| 329 | }
|
| 330 |
|
| 331 | private static boolean isCatSittingOnChest(final LevelAccessor level, final BlockPos pos) {
|
| 332 | List<Cat> cats = level.getEntitiesOfClass(Cat.class, new AABB(pos.getX(), pos.getY() + 1, pos.getZ(), pos.getX() + 1, pos.getY() + 2, pos.getZ() + 1));
|
| 333 | if (!cats.isEmpty()) {
|
| 334 | for (Cat cat : cats) {
|
| 335 | if (cat.isInSittingPose()) {
|
| 336 | return true;
|
| 337 | }
|
| 338 | }
|
| 339 | }
|
| 340 |
|
| 341 | return false;
|
| 342 | }
|
| 343 |
|
| 344 | @Override
|
| 345 | protected boolean hasAnalogOutputSignal(final BlockState state) {
|
| 346 | return true;
|
| 347 | }
|
| 348 |
|
| 349 | @Override
|
| 350 | protected int getAnalogOutputSignal(final BlockState state, final Level level, final BlockPos pos, final Direction direction) {
|
| 351 | return AbstractContainerMenu.getRedstoneSignalFromContainer(getContainer(this, state, level, pos, false));
|
| 352 | }
|
| 353 |
|
| 354 | @Override
|
| 355 | protected BlockState rotate(final BlockState state, final Rotation rotation) {
|
| 356 | return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
|
| 357 | }
|
| 358 |
|
| 359 | @Override
|
| 360 | protected BlockState mirror(final BlockState state, final Mirror mirror) {
|
| 361 | return state.rotate(mirror.getRotation(state.getValue(FACING)));
|
| 362 | }
|
| 363 |
|
| 364 | @Override
|
| 365 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 366 | builder.add(FACING, TYPE, WATERLOGGED);
|
| 367 | }
|
| 368 |
|
| 369 | @Override
|
| 370 | protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
|
| 371 | return false;
|
| 372 | }
|
| 373 |
|
| 374 | @Override
|
| 375 | protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 376 | if (level.getBlockEntity(pos) instanceof ChestBlockEntity chestBlockEntity) {
|
| 377 | chestBlockEntity.recheckOpen();
|
| 378 | }
|
| 379 | }
|
| 380 |
|
| 381 | public SoundEvent getOpenChestSound() {
|
| 382 | return this.openSound;
|
| 383 | }
|
| 384 |
|
| 385 | public SoundEvent getCloseChestSound() {
|
| 386 | return this.closeSound;
|
| 387 | }
|
| 388 | }
|
| 389 |
|