package net.minecraft.world;

import java.util.AbstractList;
import net.minecraft.nbt.Tag;
import net.minecraft.nbt.ListTag;
import net.minecraft.core.NonNullList;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.ItemStack;
import java.util.List;

public class ContainerHelper {
    public static ItemStack removeItem(final List<ItemStack> list, final int integer2, final int integer3) {
        if (integer2 < 0 || integer2 >= list.size() || list.get(integer2).isEmpty() || integer3 <= 0) {
            return ItemStack.EMPTY;
        }
        return list.get(integer2).split(integer3);
    }
    
    public static ItemStack takeItem(final List<ItemStack> list, final int integer) {
        if (integer < 0 || integer >= list.size()) {
            return ItemStack.EMPTY;
        }
        return list.set(integer, ItemStack.EMPTY);
    }
    
    public static CompoundTag saveAllItems(final CompoundTag jt, final NonNullList<ItemStack> fy) {
        return saveAllItems(jt, fy, true);
    }
    
    public static CompoundTag saveAllItems(final CompoundTag jt, final NonNullList<ItemStack> fy, final boolean boolean3) {
        final ListTag jz4 = new ListTag();
        for (int integer5 = 0; integer5 < fy.size(); ++integer5) {
            final ItemStack ben6 = fy.get(integer5);
            if (!ben6.isEmpty()) {
                final CompoundTag jt2 = new CompoundTag();
                jt2.putByte("Slot", (byte)integer5);
                ben6.save(jt2);
                ((AbstractList<CompoundTag>)jz4).add(jt2);
            }
        }
        if (!jz4.isEmpty() || boolean3) {
            jt.put("Items", jz4);
        }
        return jt;
    }
    
    public static void loadAllItems(final CompoundTag jt, final NonNullList<ItemStack> fy) {
        final ListTag jz3 = jt.getList("Items", 10);
        for (int integer4 = 0; integer4 < jz3.size(); ++integer4) {
            final CompoundTag jt2 = jz3.getCompound(integer4);
            final int integer5 = jt2.getByte("Slot") & 0xFF;
            if (integer5 >= 0 && integer5 < fy.size()) {
                fy.set(integer5, ItemStack.of(jt2));
            }
        }
    }
}
