<commit_before>package com.creativemd.littletiles.common.container;

import java.util.List;

import com.creativemd.creativecore.common.gui.controls.container.SlotControl;
import com.creativemd.creativecore.common.gui.event.container.SlotChangeEvent;
import com.creativemd.creativecore.common.gui.premade.SubContainerHeldItem;
import com.creativemd.creativecore.common.utils.mc.WorldUtils;
import com.creativemd.littletiles.LittleTiles;
import com.creativemd.littletiles.client.gui.controls.SlotControlBlockIngredient;
import com.creativemd.littletiles.common.api.ILittleIngredientInventory;
import com.creativemd.littletiles.common.item.ItemBlockIngredient;
import com.creativemd.littletiles.common.item.ItemColorIngredient;
import com.creativemd.littletiles.common.item.ItemColorIngredient.ColorIngredientType;
import com.creativemd.littletiles.common.item.ItemLittleBag;
import com.creativemd.littletiles.common.util.ingredient.BlockIngredient;
import com.creativemd.littletiles.common.util.ingredient.BlockIngredientEntry;
import com.creativemd.littletiles.common.util.ingredient.ColorIngredient;
import com.creativemd.littletiles.common.util.ingredient.LittleIngredient;
import com.creativemd.littletiles.common.util.ingredient.LittleIngredients;
import com.creativemd.littletiles.common.util.ingredient.LittleInventory;
import com.creativemd.littletiles.common.util.ingredient.NotEnoughIngredientsException.NotEnoughSpaceException;
import com.n247s.api.eventapi.eventsystem.CustomEventSubscribe;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.SoundEvents;
import net.minecraft.inventory.InventoryBasic;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;

public class SubContainerBag extends SubContainerHeldItem {
    
    public LittleIngredients bag;
    public InventoryBasic input = new InventoryBasic("input", false, 1);
    
    public SubContainerBag(EntityPlayer player, ItemStack stack, int index) {
        super(player, stack, index);
        
        if (!stack.hasTagCompound())
            stack.setTagCompound(new NBTTagCompound());
        NBTTagCompound nbt = stack.getTagCompound().copy();
        nbt.setBoolean("reload", true);
        sendNBTToGui(nbt);
    }
    
    @CustomEventSubscribe
    public void onSlotChange(SlotChangeEvent event) {
        if (event.source instanceof SlotControl) {
            if (event.source instanceof SlotControlBlockIngredient) {
                SlotControlBlockIngredient slot = (SlotControlBlockIngredient) event.source;
                
                BlockIngredient blocks = new BlockIngredient().setLimits(ItemLittleBag.inventorySize, ItemLittleBag.maxStackSize);
                for (int y = 0; y < ItemLittleBag.inventoryHeight; y++) {
                    for (int x = 0; x < ItemLittleBag.inventoryWidth; x++) {
                        int index = x + y * ItemLittleBag.inventoryWidth;
                        BlockIngredientEntry ingredient = ((SlotControlBlockIngredient) get("item" + index)).getIngredient();
                        if (ingredient != null)
                            blocks.add(ingredient);
                    }
                }
                
                bag.set(blocks.getClass(), blocks);
                ((ItemLittleBag) stack.getItem()).setInventory(stack, bag, null);
                if (player instanceof EntityPlayerMP)
                    ((EntityPlayerMP) player).sendContainerToPlayer(player.inventoryContainer);
                reloadControls();
            } else if (event.source.name.startsWith("input")) {
                
                ItemStack input = ((SlotControl) event.source).slot.getStack();
                
                if (input.getItem() instanceof ILittleIngredientInventory) {
                    
                    LittleIngredients ingredients = ((ILittleIngredientInventory) input.getItem()).getInventory(input);
                    
                    boolean containsBlocks = ingredients.contains(BlockIngredient.class);
                    boolean containsColor = ingredients.contains(ColorIngredient.class);
                    
                    LittleIngredients remaining = bag.add(ingredients);
                    ((ItemLittleBag) stack.getItem()).setInventory(stack, bag, null);
                    
                    if (remaining == null)
                        remaining = new LittleIngredients();
                    
                    if (remaining.copy().sub(ingredients.copy()) != null) {
                        if (containsBlocks) {
                            updateSlots();
                            player.playSound(SoundEvents.ENTITY_ITEMFRAME_PLACE, 1.0F, 1.0F);
                        }
                        
                        if (containsColor) {
                            reloadControls();
                            player.playSound(SoundEvents.BLOCK_BREWING_STAND_BREW, 1.0F, 1.0F);
                        }
                    }
                    
                    ((ILittleIngredientInventory) input.getItem()).setInventory(input, remaining, null);
                } else {
                    LittleIngredients ingredients = LittleIngredient.extractWithoutCount(input, true);
                    if (ingredients != null) {
                        ingredients.scale(input.getCount());
                        
                        boolean containsBlocks = ingredients.contains(BlockIngredient.class);
                        boolean containsColor = ingredients.contains(ColorIngredient.class);
                        
                        LittleIngredients overflow = bag.add(ingredients);
                        if (overflow == null || (!overflow.contains(BlockIngredient.class) && !overflow.contains(ColorIngredient.class))) {
                            
                            input.setCount(0);
                            ((ItemLittleBag) stack.getItem()).setInventory(stack, bag, null);
                            
                            LittleInventory inventory = new LittleInventory(player);
                            try {
                                inventory.give(overflow);
                            } catch (NotEnoughSpaceException e) {}
                            
                            if (containsBlocks) {
                                updateSlots();
                                player.playSound(SoundEvents.ENTITY_ITEMFRAME_PLACE, 1.0F, 1.0F);
                            }
                            
                            if (containsColor) {
                                reloadControls();
                                player.playSound(SoundEvents.BLOCK_BREWING_STAND_BREW, 1.0F, 1.0F);
                            }
                            
                        } else
                            bag = ((ItemLittleBag) stack.getItem()).getInventory(stack);
                        
                    }
                }
                if (player instanceof EntityPlayerMP)
                    ((EntityPlayerMP) player).sendContainerToPlayer(player.inventoryContainer);
            }
            
        }
    }
    
    public void reloadControls() {
        controls.clear();
        createControls();
        refreshControls();
        NBTTagCompound nbt = stack.getTagCompound().copy();
        nbt.setBoolean("reload", true);<commit_msg>Fixed crash when putting tiles into the bag and there is no overflow<commit_after>package com.creativemd.littletiles.common.container;

import java.util.List;

import com.creativemd.creativecore.common.gui.controls.container.SlotControl;
import com.creativemd.creativecore.common.gui.event.container.SlotChangeEvent;
import com.creativemd.creativecore.common.gui.premade.SubContainerHeldItem;
import com.creativemd.creativecore.common.utils.mc.WorldUtils;
import com.creativemd.littletiles.LittleTiles;
import com.creativemd.littletiles.client.gui.controls.SlotControlBlockIngredient;
import com.creativemd.littletiles.common.api.ILittleIngredientInventory;
import com.creativemd.littletiles.common.item.ItemBlockIngredient;
import com.creativemd.littletiles.common.item.ItemColorIngredient;
import com.creativemd.littletiles.common.item.ItemColorIngredient.ColorIngredientType;
import com.creativemd.littletiles.common.item.ItemLittleBag;
import com.creativemd.littletiles.common.util.ingredient.BlockIngredient;
import com.creativemd.littletiles.common.util.ingredient.BlockIngredientEntry;
import com.creativemd.littletiles.common.util.ingredient.ColorIngredient;
import com.creativemd.littletiles.common.util.ingredient.LittleIngredient;
import com.creativemd.littletiles.common.util.ingredient.LittleIngredients;
import com.creativemd.littletiles.common.util.ingredient.LittleInventory;
import com.creativemd.littletiles.common.util.ingredient.NotEnoughIngredientsException.NotEnoughSpaceException;
import com.n247s.api.eventapi.eventsystem.CustomEventSubscribe;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.SoundEvents;
import net.minecraft.inventory.InventoryBasic;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;

public class SubContainerBag extends SubContainerHeldItem {
    
    public LittleIngredients bag;
    public InventoryBasic input = new InventoryBasic("input", false, 1);
    
    public SubContainerBag(EntityPlayer player, ItemStack stack, int index) {
        super(player, stack, index);
        
        if (!stack.hasTagCompound())
            stack.setTagCompound(new NBTTagCompound());
        NBTTagCompound nbt = stack.getTagCompound().copy();
        nbt.setBoolean("reload", true);
        sendNBTToGui(nbt);
    }
    
    @CustomEventSubscribe
    public void onSlotChange(SlotChangeEvent event) {
        if (event.source instanceof SlotControl) {
            if (event.source instanceof SlotControlBlockIngredient) {
                SlotControlBlockIngredient slot = (SlotControlBlockIngredient) event.source;
                
                BlockIngredient blocks = new BlockIngredient().setLimits(ItemLittleBag.inventorySize, ItemLittleBag.maxStackSize);
                for (int y = 0; y < ItemLittleBag.inventoryHeight; y++) {
                    for (int x = 0; x < ItemLittleBag.inventoryWidth; x++) {
                        int index = x + y * ItemLittleBag.inventoryWidth;
                        BlockIngredientEntry ingredient = ((SlotControlBlockIngredient) get("item" + index)).getIngredient();
                        if (ingredient != null)
                            blocks.add(ingredient);
                    }
                }
                
                bag.set(blocks.getClass(), blocks);
                ((ItemLittleBag) stack.getItem()).setInventory(stack, bag, null);
                if (player instanceof EntityPlayerMP)
                    ((EntityPlayerMP) player).sendContainerToPlayer(player.inventoryContainer);
                reloadControls();
            } else if (event.source.name.startsWith("input")) {
                
                ItemStack input = ((SlotControl) event.source).slot.getStack();
                
                if (input.getItem() instanceof ILittleIngredientInventory) {
                    
                    LittleIngredients ingredients = ((ILittleIngredientInventory) input.getItem()).getInventory(input);
                    
                    boolean containsBlocks = ingredients.contains(BlockIngredient.class);
                    boolean containsColor = ingredients.contains(ColorIngredient.class);
                    
                    LittleIngredients remaining = bag.add(ingredients);
                    ((ItemLittleBag) stack.getItem()).setInventory(stack, bag, null);
                    
                    if (remaining == null)
                        remaining = new LittleIngredients();
                    
                    if (remaining.copy().sub(ingredients.copy()) != null) {
                        if (containsBlocks) {
                            updateSlots();
                            player.playSound(SoundEvents.ENTITY_ITEMFRAME_PLACE, 1.0F, 1.0F);
                        }
                        
                        if (containsColor) {
                            reloadControls();
                            player.playSound(SoundEvents.BLOCK_BREWING_STAND_BREW, 1.0F, 1.0F);
                        }
                    }
                    
                    ((ILittleIngredientInventory) input.getItem()).setInventory(input, remaining, null);
                } else {
                    LittleIngredients ingredients = LittleIngredient.extractWithoutCount(input, true);
                    if (ingredients != null) {
                        ingredients.scale(input.getCount());
                        
                        boolean containsBlocks = ingredients.contains(BlockIngredient.class);
                        boolean containsColor = ingredients.contains(ColorIngredient.class);
                        
                        LittleIngredients overflow = bag.add(ingredients);
                        if (overflow == null || (!overflow.contains(BlockIngredient.class) && !overflow.contains(ColorIngredient.class))) {
                            
                            input.setCount(0);
                            ((ItemLittleBag) stack.getItem()).setInventory(stack, bag, null);
                            
                            LittleInventory inventory = new LittleInventory(player);
                            try {
                                if (overflow != null)
                                    inventory.give(overflow);
                            } catch (NotEnoughSpaceException e) {}
                            
                            if (containsBlocks) {
                                updateSlots();
                                player.playSound(SoundEvents.ENTITY_ITEMFRAME_PLACE, 1.0F, 1.0F);
                            }
                            
                            if (containsColor) {
                                reloadControls();
                                player.playSound(SoundEvents.BLOCK_BREWING_STAND_BREW, 1.0F, 1.0F);
                            }
                            
                        } else
                            bag = ((ItemLittleBag) stack.getItem()).getInventory(stack);
                        
                    }
                }
                if (player instanceof EntityPlayerMP)
                    ((EntityPlayerMP) player).sendContainerToPlayer(player.inventoryContainer);
            }
            
        }
    }
    
    public void reloadControls() {
        controls.clear();
        createControls();
        refreshControls();
        NBTTagCompound nbt = stack.getTagCompound().copy();
        nbt.setBoolean("reload", true);
{'fraction_non_alphanumeric': 0.08615997344839031, 'fraction_numerical': 0.0030534351145038168, 'mean_word_length': 1.7915508615897722, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3439534', 'n_tokens_mistral': 3957, 'n_tokens_neox': 3763, 'n_words': 568}