84 lines
2.8 KiB
Java
84 lines
2.8 KiB
Java
package net.minecraft.world.level.block.entity;
|
|
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.sounds.SoundEvents;
|
|
import net.minecraft.sounds.SoundSource;
|
|
import net.minecraft.world.Container;
|
|
import net.minecraft.world.entity.player.Player;
|
|
import net.minecraft.world.level.Level;
|
|
import net.minecraft.world.level.block.Blocks;
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
|
|
public class EnderChestBlockEntity extends BlockEntity implements LidBlockEntity {
|
|
private final ChestLidController chestLidController = new ChestLidController();
|
|
private final ContainerOpenersCounter openersCounter = new ContainerOpenersCounter() {
|
|
@Override
|
|
protected void onOpen(Level level, BlockPos pos, BlockState state) {
|
|
level.playSound(
|
|
null, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, SoundEvents.ENDER_CHEST_OPEN, SoundSource.BLOCKS, 0.5F, level.random.nextFloat() * 0.1F + 0.9F
|
|
);
|
|
}
|
|
|
|
@Override
|
|
protected void onClose(Level level, BlockPos pos, BlockState state) {
|
|
level.playSound(
|
|
null, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, SoundEvents.ENDER_CHEST_CLOSE, SoundSource.BLOCKS, 0.5F, level.random.nextFloat() * 0.1F + 0.9F
|
|
);
|
|
}
|
|
|
|
@Override
|
|
protected void openerCountChanged(Level level, BlockPos pos, BlockState state, int count, int openCount) {
|
|
level.blockEvent(EnderChestBlockEntity.this.worldPosition, Blocks.ENDER_CHEST, 1, openCount);
|
|
}
|
|
|
|
@Override
|
|
protected boolean isOwnContainer(Player player) {
|
|
return player.getEnderChestInventory().isActiveChest(EnderChestBlockEntity.this);
|
|
}
|
|
};
|
|
|
|
public EnderChestBlockEntity(BlockPos pos, BlockState blockState) {
|
|
super(BlockEntityType.ENDER_CHEST, pos, blockState);
|
|
}
|
|
|
|
public static void lidAnimateTick(Level level, BlockPos pos, BlockState state, EnderChestBlockEntity blockEntity) {
|
|
blockEntity.chestLidController.tickLid();
|
|
}
|
|
|
|
@Override
|
|
public boolean triggerEvent(int id, int type) {
|
|
if (id == 1) {
|
|
this.chestLidController.shouldBeOpen(type > 0);
|
|
return true;
|
|
} else {
|
|
return super.triggerEvent(id, type);
|
|
}
|
|
}
|
|
|
|
public void startOpen(Player player) {
|
|
if (!this.remove && !player.isSpectator()) {
|
|
this.openersCounter.incrementOpeners(player, this.getLevel(), this.getBlockPos(), this.getBlockState());
|
|
}
|
|
}
|
|
|
|
public void stopOpen(Player player) {
|
|
if (!this.remove && !player.isSpectator()) {
|
|
this.openersCounter.decrementOpeners(player, this.getLevel(), this.getBlockPos(), this.getBlockState());
|
|
}
|
|
}
|
|
|
|
public boolean stillValid(Player player) {
|
|
return Container.stillValidBlockEntity(this, player);
|
|
}
|
|
|
|
public void recheckOpen() {
|
|
if (!this.remove) {
|
|
this.openersCounter.recheckOpeners(this.getLevel(), this.getBlockPos(), this.getBlockState());
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public float getOpenNess(float partialTicks) {
|
|
return this.chestLidController.getOpenness(partialTicks);
|
|
}
|
|
}
|