<commit_before>package com.jaquadro.minecraft.storagedrawers.inventory;

import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer;
import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerGroup;
import com.jaquadro.minecraft.storagedrawers.api.storage.IPriorityGroup;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandler;

public class DrawerItemHandler implements IItemHandler
{
    private IDrawerGroup group;

    public DrawerItemHandler (IDrawerGroup group) {
        this.group = group;
    }

    @Override
    public int getSlots () {
        return group.getDrawerCount();
    }

    @Override
    public ItemStack getStackInSlot (int slot) {
        if (group instanceof IPriorityGroup) {
            int[] order = ((IPriorityGroup) group).getAccessibleDrawerSlots();
            slot = (slot >= 0 && slot < order.length) ? order[slot] : -1;
        }

        if (!group.isDrawerEnabled(slot))
            return null;

        IDrawer drawer = group.getDrawer(slot);
        if (drawer.isEmpty())
            return null;

        return drawer.getStoredItemCopy();
    }

    @Override
    public ItemStack insertItem (int slot, ItemStack stack, boolean simulate) {
        if (group instanceof IPriorityGroup) {
            int[] order = ((IPriorityGroup) group).getAccessibleDrawerSlots();
            slot = (slot >= 0 && slot < order.length) ? order[slot] : -1;
        }

        if (!group.isDrawerEnabled(slot))
            return stack;

        IDrawer drawer = group.getDrawer(slot);
        if (!drawer.canItemBeStored(stack))
            return stack;

        int availableCount = drawer.isEmpty() ? drawer.getMaxCapacity(stack) : drawer.getRemainingCapacity();
        int insertCount = Math.min(stack.stackSize, availableCount);
        int remainder = stack.stackSize - insertCount;

        if (remainder == stack.stackSize)
            return stack;

        if (!simulate) {
            if (drawer.isEmpty())
                drawer.setStoredItem(stack, insertCount);
            else
                drawer.setStoredItemCount(drawer.getStoredItemCount() + insertCount);
        }

        if (remainder == 0)
            return null;

        ItemStack returnStack = stack.copy();
        returnStack.stackSize = remainder;

        return returnStack;
    }

    @Override
    public ItemStack extractItem (int slot, int amount, boolean simulate) {
        if (group instanceof IPriorityGroup) {
            int[] order = ((IPriorityGroup) group).getAccessibleDrawerSlots();
            slot = (slot >= 0 && slot < order.length) ? order[slot] : -1;
        }

        if (!group.isDrawerEnabled(slot))
            return null;

        IDrawer drawer = group.getDrawer(slot);
        if (drawer.isEmpty())
            return null;

        ItemStack returnStack = drawer.getStoredItemCopy();
        returnStack.stackSize = Math.min(returnStack.stackSize, amount);
        returnStack.stackSize = Math.min(returnStack.stackSize, returnStack.getMaxStackSize());

        if (!simulate)
            drawer.setStoredItemCount(drawer.getStoredItemCount() - returnStack.stackSize);

        return returnStack;
    }
}
<commit_msg>Fix extract items contract<commit_after>package com.jaquadro.minecraft.storagedrawers.inventory;

import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer;
import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerGroup;
import com.jaquadro.minecraft.storagedrawers.api.storage.IPriorityGroup;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandler;

public class DrawerItemHandler implements IItemHandler
{
    private IDrawerGroup group;

    public DrawerItemHandler (IDrawerGroup group) {
        this.group = group;
    }

    @Override
    public int getSlots () {
        return group.getDrawerCount();
    }

    @Override
    public ItemStack getStackInSlot (int slot) {
        if (group instanceof IPriorityGroup) {
            int[] order = ((IPriorityGroup) group).getAccessibleDrawerSlots();
            slot = (slot >= 0 && slot < order.length) ? order[slot] : -1;
        }

        if (!group.isDrawerEnabled(slot))
            return null;

        IDrawer drawer = group.getDrawer(slot);
        if (drawer.isEmpty())
            return null;

        return drawer.getStoredItemCopy();
    }

    @Override
    public ItemStack insertItem (int slot, ItemStack stack, boolean simulate) {
        if (group instanceof IPriorityGroup) {
            int[] order = ((IPriorityGroup) group).getAccessibleDrawerSlots();
            slot = (slot >= 0 && slot < order.length) ? order[slot] : -1;
        }

        if (!group.isDrawerEnabled(slot))
            return stack;

        IDrawer drawer = group.getDrawer(slot);
        if (!drawer.canItemBeStored(stack))
            return stack;

        int availableCount = drawer.isEmpty() ? drawer.getMaxCapacity(stack) : drawer.getRemainingCapacity();
        int insertCount = Math.min(stack.stackSize, availableCount);
        int remainder = stack.stackSize - insertCount;

        if (remainder == stack.stackSize)
            return stack;

        if (!simulate) {
            if (drawer.isEmpty())
                drawer.setStoredItem(stack, insertCount);
            else
                drawer.setStoredItemCount(drawer.getStoredItemCount() + insertCount);
        }

        if (remainder == 0)
            return null;

        ItemStack returnStack = stack.copy();
        returnStack.stackSize = remainder;

        return returnStack;
    }

    @Override
    public ItemStack extractItem (int slot, int amount, boolean simulate) {
        if (group instanceof IPriorityGroup) {
            int[] order = ((IPriorityGroup) group).getAccessibleDrawerSlots();
            slot = (slot >= 0 && slot < order.length) ? order[slot] : -1;
        }

        if (!group.isDrawerEnabled(slot))
            return null;

        IDrawer drawer = group.getDrawer(slot);
        if (drawer.isEmpty() || drawer.getStoredItemCount() == 0)
            return null;

        ItemStack returnStack = drawer.getStoredItemCopy();
        returnStack.stackSize = Math.min(returnStack.stackSize, amount);
        returnStack.stackSize = Math.min(returnStack.stackSize, returnStack.getMaxStackSize());

        if (!simulate)
            drawer.setStoredItemCount(drawer.getStoredItemCount() - returnStack.stackSize);

        return returnStack;
    }
}

{'fraction_non_alphanumeric': 0.10271107668474051, 'fraction_numerical': 0.0023237800154918666, 'mean_word_length': 2.5259421081376296, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '535548', 'n_tokens_mistral': 1851, 'n_tokens_neox': 1821, 'n_words': 389}