| 1 | package net.minecraft.world.level.block.entity;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.sounds.SoundEvents;
|
| 5 | import net.minecraft.sounds.SoundSource;
|
| 6 | import net.minecraft.world.Container;
|
| 7 | import net.minecraft.world.entity.ContainerUser;
|
| 8 | import net.minecraft.world.entity.player.Player;
|
| 9 | import net.minecraft.world.level.Level;
|
| 10 | import net.minecraft.world.level.block.Blocks;
|
| 11 | import net.minecraft.world.level.block.state.BlockState;
|
| 12 |
|
| 13 | public class EnderChestBlockEntity extends BlockEntity implements LidBlockEntity {
|
| 14 | private final ChestLidController chestLidController = new ChestLidController();
|
| 15 | private final ContainerOpenersCounter openersCounter = new ContainerOpenersCounter() {
|
| 16 | @Override
|
| 17 | protected void onOpen(final Level level, final BlockPos pos, final BlockState blockState) {
|
| 18 | level.playSound(
|
| 19 | null,
|
| 20 | pos.getX() + 0.5,
|
| 21 | pos.getY() + 0.5,
|
| 22 | pos.getZ() + 0.5,
|
| 23 | SoundEvents.ENDER_CHEST_OPEN,
|
| 24 | SoundSource.BLOCKS,
|
| 25 | 0.5F,
|
| 26 | level.getRandom().nextFloat() * 0.1F + 0.9F
|
| 27 | );
|
| 28 | }
|
| 29 |
|
| 30 | @Override
|
| 31 | protected void onClose(final Level level, final BlockPos pos, final BlockState blockState) {
|
| 32 | level.playSound(
|
| 33 | null,
|
| 34 | pos.getX() + 0.5,
|
| 35 | pos.getY() + 0.5,
|
| 36 | pos.getZ() + 0.5,
|
| 37 | SoundEvents.ENDER_CHEST_CLOSE,
|
| 38 | SoundSource.BLOCKS,
|
| 39 | 0.5F,
|
| 40 | level.getRandom().nextFloat() * 0.1F + 0.9F
|
| 41 | );
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | protected void openerCountChanged(final Level level, final BlockPos pos, final BlockState blockState, final int previous, final int current) {
|
| 46 | level.blockEvent(EnderChestBlockEntity.this.worldPosition, Blocks.ENDER_CHEST, 1, current);
|
| 47 | }
|
| 48 |
|
| 49 | @Override
|
| 50 | public boolean isOwnContainer(final Player player) {
|
| 51 | return player.getEnderChestInventory().isActiveChest(EnderChestBlockEntity.this);
|
| 52 | }
|
| 53 | };
|
| 54 |
|
| 55 | public EnderChestBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 56 | super(BlockEntityTypes.ENDER_CHEST, worldPosition, blockState);
|
| 57 | }
|
| 58 |
|
| 59 | public static void lidAnimateTick(final Level level, final BlockPos pos, final BlockState state, final EnderChestBlockEntity entity) {
|
| 60 | entity.chestLidController.tickLid();
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | public boolean triggerEvent(final int b0, final int b1) {
|
| 65 | if (b0 == 1) {
|
| 66 | this.chestLidController.shouldBeOpen(b1 > 0);
|
| 67 | return true;
|
| 68 | } else {
|
| 69 | return super.triggerEvent(b0, b1);
|
| 70 | }
|
| 71 | }
|
| 72 |
|
| 73 | public void startOpen(final ContainerUser containerUser) {
|
| 74 | if (!this.remove && !containerUser.getLivingEntity().isSpectator()) {
|
| 75 | this.openersCounter
|
| 76 | .incrementOpeners(containerUser.getLivingEntity(), this.getLevel(), this.getBlockPos(), this.getBlockState(), containerUser.getContainerInteractionRange());
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | public void stopOpen(final ContainerUser containerUser) {
|
| 81 | if (!this.remove && !containerUser.getLivingEntity().isSpectator()) {
|
| 82 | this.openersCounter.decrementOpeners(containerUser.getLivingEntity(), this.getLevel(), this.getBlockPos(), this.getBlockState());
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | public boolean stillValid(final Player player) {
|
| 87 | return Container.stillValidBlockEntity(this, player);
|
| 88 | }
|
| 89 |
|
| 90 | public void recheckOpen() {
|
| 91 | if (!this.remove) {
|
| 92 | this.openersCounter.recheckOpeners(this.getLevel(), this.getBlockPos(), this.getBlockState());
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | @Override
|
| 97 | public float getOpenNess(final float a) {
|
| 98 | return this.chestLidController.getOpenness(a);
|
| 99 | }
|
| 100 | }
|
| 101 |
|