<commit_before>package org.cyclops.cyclopscore.inventory;

import com.google.common.collect.Maps;
import gnu.trove.map.TIntObjectMap;
import gnu.trove.map.hash.TIntObjectHashMap;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import org.apache.logging.log4j.Level;
import org.cyclops.cyclopscore.CyclopsCore;

import java.util.Map;

/**
 * An inventory that adds an index to a regular inventory.
 * @author rubensworks
 *
 */
public class IndexedInventory extends LargeInventory implements IndexedSlotlessItemHandlerWrapper.IInventoryIndexReference {

    private final Map<Item, TIntObjectMap<ItemStack>> index = Maps.newIdentityHashMap();
    private int firstEmptySlot;
    private int lastEmptySlot;
    private int firstNonEmptySlot;
    private int lastNonEmptySlot;

    /**
     * Default constructor for NBT persistence, don't call this yourself.
     */
    public IndexedInventory() {
        this(0, "", 0);
    }

    /**
     * Make a new instance.
     * @param size The amount of slots in the inventory.
     * @param name The name of the inventory, used for NBT storage.
     * @param stackLimit The stack limit for each slot.
     */
    public IndexedInventory(int size, String name, int stackLimit) {
        super(size, name, stackLimit);
        this.firstEmptySlot = 0;
        this.lastEmptySlot = size - 1;
        this.firstNonEmptySlot = -1;
        this.lastNonEmptySlot = -1;
    }

    protected void createIndex() {
        index.clear();
        firstEmptySlot = -1;
        lastEmptySlot = -1;
        firstNonEmptySlot = -1;
        lastNonEmptySlot = -1;
        for (int i = 0; i < getSizeInventory(); i++) {
            ItemStack itemStack = getStackInSlot(i);
            if (itemStack != null) {
                TIntObjectMap<ItemStack> stacks = index.get(itemStack.getItem());
                if (stacks == null) {
                    stacks = new TIntObjectHashMap<ItemStack>();
                    index.put(itemStack.getItem(), stacks);
                }
                stacks.put(i, itemStack);
                if (firstNonEmptySlot < 0) {
                    firstNonEmptySlot = i;
                }
                lastNonEmptySlot = i;
            } else {
                if (firstEmptySlot < 0) {
                    firstEmptySlot = i;
                }
                lastEmptySlot = i;
            }
        }
    }

    @Override
    public void readFromNBT(NBTTagCompound data, String tag) {
        super.readFromNBT(data, tag);
        createIndex();
    }

    @Override
    public void setInventorySlotContents(int slotId, ItemStack itemStack) {
        // Update index
        ItemStack oldStack = getStackInSlot(slotId);
        if (oldStack != null) {
            TIntObjectMap<ItemStack> stacks = index.get(oldStack.getItem());
            stacks.remove(slotId);
        }
        if (itemStack != null) {
            TIntObjectMap<ItemStack> stacks = index.get(itemStack.getItem());
            if (stacks == null) {
                stacks = new TIntObjectHashMap<ItemStack>();
                index.put(itemStack.getItem(), stacks);
            }
            stacks.put(slotId, itemStack);
        }

        // Call super
        super.setInventorySlotContents(slotId, itemStack);
<commit_msg>Add null-check during IndexInventory slot contents setting<commit_after>package org.cyclops.cyclopscore.inventory;

import com.google.common.collect.Maps;
import gnu.trove.map.TIntObjectMap;
import gnu.trove.map.hash.TIntObjectHashMap;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import org.apache.logging.log4j.Level;
import org.cyclops.cyclopscore.CyclopsCore;

import java.util.Map;

/**
 * An inventory that adds an index to a regular inventory.
 * @author rubensworks
 *
 */
public class IndexedInventory extends LargeInventory implements IndexedSlotlessItemHandlerWrapper.IInventoryIndexReference {

    private final Map<Item, TIntObjectMap<ItemStack>> index = Maps.newIdentityHashMap();
    private int firstEmptySlot;
    private int lastEmptySlot;
    private int firstNonEmptySlot;
    private int lastNonEmptySlot;

    /**
     * Default constructor for NBT persistence, don't call this yourself.
     */
    public IndexedInventory() {
        this(0, "", 0);
    }

    /**
     * Make a new instance.
     * @param size The amount of slots in the inventory.
     * @param name The name of the inventory, used for NBT storage.
     * @param stackLimit The stack limit for each slot.
     */
    public IndexedInventory(int size, String name, int stackLimit) {
        super(size, name, stackLimit);
        this.firstEmptySlot = 0;
        this.lastEmptySlot = size - 1;
        this.firstNonEmptySlot = -1;
        this.lastNonEmptySlot = -1;
    }

    protected void createIndex() {
        index.clear();
        firstEmptySlot = -1;
        lastEmptySlot = -1;
        firstNonEmptySlot = -1;
        lastNonEmptySlot = -1;
        for (int i = 0; i < getSizeInventory(); i++) {
            ItemStack itemStack = getStackInSlot(i);
            if (itemStack != null) {
                TIntObjectMap<ItemStack> stacks = index.get(itemStack.getItem());
                if (stacks == null) {
                    stacks = new TIntObjectHashMap<ItemStack>();
                    index.put(itemStack.getItem(), stacks);
                }
                stacks.put(i, itemStack);
                if (firstNonEmptySlot < 0) {
                    firstNonEmptySlot = i;
                }
                lastNonEmptySlot = i;
            } else {
                if (firstEmptySlot < 0) {
                    firstEmptySlot = i;
                }
                lastEmptySlot = i;
            }
        }
    }

    @Override
    public void readFromNBT(NBTTagCompound data, String tag) {
        super.readFromNBT(data, tag);
        createIndex();
    }

    @Override
    public void setInventorySlotContents(int slotId, ItemStack itemStack) {
        // Update index
        ItemStack oldStack = getStackInSlot(slotId);
        if (oldStack != null) {
            TIntObjectMap<ItemStack> stacks = index.get(oldStack.getItem());
            if (stacks != null) {
                stacks.remove(slotId);
            }
        }
        if (itemStack != null) {
            TIntObjectMap<ItemStack> stacks = index.get(itemStack.getItem());
            if (stacks == null) {
                stacks = new TIntObjectHashMap<ItemStack>();
                index.put(itemStack.getItem(), stacks);
            }
            stacks.put(slotId, itemStack);
        }

        // Call super
        super.setInventorySlotContents(slotId, itemStack);

{'fraction_non_alphanumeric': 0.09713777250481981, 'fraction_numerical': 0.004152454397152602, 'mean_word_length': 2.0850869167429096, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1891483', 'n_tokens_mistral': 1947, 'n_tokens_neox': 1885, 'n_words': 455}