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 bwx) {
        this.activeChest = bwx;
    }
    
    public void fromTag(final ListTag jz) {
        for (int integer3 = 0; integer3 < this.getContainerSize(); ++integer3) {
            this.setItem(integer3, ItemStack.EMPTY);
        }
        for (int integer3 = 0; integer3 < jz.size(); ++integer3) {
            final CompoundTag jt4 = jz.getCompound(integer3);
            final int integer4 = jt4.getByte("Slot") & 0xFF;
            if (integer4 >= 0 && integer4 < this.getContainerSize()) {
                this.setItem(integer4, ItemStack.of(jt4));
            }
        }
    }
    
    public ListTag createTag() {
        final ListTag jz2 = new ListTag();
        for (int integer3 = 0; integer3 < this.getContainerSize(); ++integer3) {
            final ItemStack ben4 = this.getItem(integer3);
            if (!ben4.isEmpty()) {
                final CompoundTag jt5 = new CompoundTag();
                jt5.putByte("Slot", (byte)integer3);
                ben4.save(jt5);
                ((AbstractList<CompoundTag>)jz2).add(jt5);
            }
        }
        return jz2;
    }
    
    @Override
    public boolean stillValid(final Player ayj) {
        return (this.activeChest == null || this.activeChest.stillValid(ayj)) && super.stillValid(ayj);
    }
    
    @Override
    public void startOpen(final Player ayj) {
        if (this.activeChest != null) {
            this.activeChest.startOpen();
        }
        super.startOpen(ayj);
    }
    
    @Override
    public void stopOpen(final Player ayj) {
        if (this.activeChest != null) {
            this.activeChest.stopOpen();
        }
        super.stopOpen(ayj);
        this.activeChest = null;
    }
}
