package net.minecraft.world.level.block.entity;

import net.minecraft.world.level.block.state.AbstractStateHolder;
import net.minecraft.core.Vec3i;
import net.minecraft.sounds.SoundSource;
import net.minecraft.core.Direction;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.BarrelBlock;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.Container;
import net.minecraft.world.inventory.ChestMenu;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.network.chat.Component;
import net.minecraft.world.ContainerHelper;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.ItemStack;
import net.minecraft.core.NonNullList;

public class BarrelBlockEntity extends RandomizableContainerBlockEntity {
    private NonNullList<ItemStack> items;
    private int openCount;
    
    private BarrelBlockEntity(final BlockEntityType<?> bwm) {
        super(bwm);
        this.items = NonNullList.<ItemStack>withSize(27, ItemStack.EMPTY);
    }
    
    public BarrelBlockEntity() {
        this(BlockEntityType.BARREL);
    }
    
    @Override
    public CompoundTag save(final CompoundTag jt) {
        super.save(jt);
        if (!this.trySaveLootTable(jt)) {
            ContainerHelper.saveAllItems(jt, this.items);
        }
        return jt;
    }
    
    @Override
    public void load(final CompoundTag jt) {
        super.load(jt);
        this.items = NonNullList.<ItemStack>withSize(this.getContainerSize(), ItemStack.EMPTY);
        if (!this.tryLoadLootTable(jt)) {
            ContainerHelper.loadAllItems(jt, this.items);
        }
    }
    
    @Override
    public int getContainerSize() {
        return 27;
    }
    
    @Override
    protected NonNullList<ItemStack> getItems() {
        return this.items;
    }
    
    @Override
    protected void setItems(final NonNullList<ItemStack> fy) {
        this.items = fy;
    }
    
    @Override
    protected Component getDefaultName() {
        return new TranslatableComponent("container.barrel", new Object[0]);
    }
    
    @Override
    protected AbstractContainerMenu createMenu(final int integer, final Inventory ayi) {
        return ChestMenu.threeRows(integer, ayi, this);
    }
    
    @Override
    public void startOpen(final Player ayj) {
        if (!ayj.isSpectator()) {
            if (this.openCount < 0) {
                this.openCount = 0;
            }
            ++this.openCount;
            final BlockState byj3 = this.getBlockState();
            final boolean boolean4 = byj3.<Boolean>getValue((Property<Boolean>)BarrelBlock.OPEN);
            if (!boolean4) {
                this.playSound(byj3, SoundEvents.BARREL_OPEN);
                this.updateBlockState(byj3, true);
            }
            this.scheduleRecheck();
        }
    }
    
    private void scheduleRecheck() {
        this.level.getBlockTicks().scheduleTick(this.getBlockPos(), this.getBlockState().getBlock(), 5);
    }
    
    public void recheckOpen() {
        final int integer2 = this.worldPosition.getX();
        final int integer3 = this.worldPosition.getY();
        final int integer4 = this.worldPosition.getZ();
        this.openCount = ChestBlockEntity.getOpenCount(this.level, this, integer2, integer3, integer4);
        if (this.openCount > 0) {
            this.scheduleRecheck();
        }
        else {
            final BlockState byj5 = this.getBlockState();
            if (byj5.getBlock() != Blocks.BARREL) {
                this.setRemoved();
                return;
            }
            final boolean boolean6 = byj5.<Boolean>getValue((Property<Boolean>)BarrelBlock.OPEN);
            if (boolean6) {
                this.playSound(byj5, SoundEvents.BARREL_CLOSE);
                this.updateBlockState(byj5, false);
            }
        }
    }
    
    @Override
    public void stopOpen(final Player ayj) {
        if (!ayj.isSpectator()) {
            --this.openCount;
        }
    }
    
    private void updateBlockState(final BlockState byj, final boolean boolean2) {
        this.level.setBlock(this.getBlockPos(), ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Boolean>setValue((Property<Comparable>)BarrelBlock.OPEN, boolean2), 3);
    }
    
    private void playSound(final BlockState byj, final SoundEvent aah) {
        final Vec3i gg4 = byj.<Direction>getValue((Property<Direction>)BarrelBlock.FACING).getNormal();
        final double double5 = this.worldPosition.getX() + 0.5 + gg4.getX() / 2.0;
        final double double6 = this.worldPosition.getY() + 0.5 + gg4.getY() / 2.0;
        final double double7 = this.worldPosition.getZ() + 0.5 + gg4.getZ() / 2.0;
        this.level.playSound(null, double5, double6, double7, aah, SoundSource.BLOCKS, 0.5f, this.level.random.nextFloat() * 0.1f + 0.9f);
    }
}
