package net.minecraft.world.inventory;

import java.util.AbstractList;
import net.minecraft.world.entity.player.Player;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.ItemStack;
import net.minecraft.nbt.ListTag;
import net.minecraft.world.level.block.entity.EnderChestBlockEntity;
import net.minecraft.world.SimpleContainer;

public class PlayerEnderChestContainer extends SimpleContainer {
    private EnderChestBlockEntity activeChest;
    
    public PlayerEnderChestContainer() {
        super(27);
    }
    
    public void setActiveChest(final EnderChestBlockEntity cdx) {
        this.activeChest = cdx;
    }
    
    @Override
    public void fromTag(final ListTag lk) {
        for (int integer3 = 0; integer3 < this.getContainerSize(); ++integer3) {
            this.setItem(integer3, ItemStack.EMPTY);
        }
        for (int integer3 = 0; integer3 < lk.size(); ++integer3) {
            final CompoundTag le4 = lk.getCompound(integer3);
            final int integer4 = le4.getByte("Slot") & 0xFF;
            if (integer4 >= 0 && integer4 < this.getContainerSize()) {
                this.setItem(integer4, ItemStack.of(le4));
            }
        }
    }
    
    @Override
    public ListTag createTag() {
        final ListTag lk2 = new ListTag();
        for (int integer3 = 0; integer3 < this.getContainerSize(); ++integer3) {
            final ItemStack bki4 = this.getItem(integer3);
            if (!bki4.isEmpty()) {
                final CompoundTag le5 = new CompoundTag();
                le5.putByte("Slot", (byte)integer3);
                bki4.save(le5);
                ((AbstractList<CompoundTag>)lk2).add(le5);
            }
        }
        return lk2;
    }
    
    @Override
    public boolean stillValid(final Player bec) {
        return (this.activeChest == null || this.activeChest.stillValid(bec)) && super.stillValid(bec);
    }
    
    @Override
    public void startOpen(final Player bec) {
        if (this.activeChest != null) {
            this.activeChest.startOpen();
        }
        super.startOpen(bec);
    }
    
    @Override
    public void stopOpen(final Player bec) {
        if (this.activeChest != null) {
            this.activeChest.stopOpen();
        }
        super.stopOpen(bec);
        this.activeChest = null;
    }
}
