<commit_before>package de.iani.cubequest.util;

import de.iani.cubequest.CubeQuest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import org.bukkit.ChatColor;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionEffect;

public class ItemStackUtil {
    
    public static final Comparator<ItemStack> ITEMSTACK_COMPARATOR = (i1, i2) -> {
        int result = i1.getType().compareTo(i2.getType());
        if (result != 0) {
            return result;
        }
        
        return i1.toString().compareTo(i2.toString());
    };
    
    public static final Comparator<ItemStack[]> ITEMSTACK_ARRAY_COMPARATOR = (i1, i2) -> {
        ItemStack[] copy1 = i1.clone();
        ItemStack[] copy2 = i2.clone();
        
        Arrays.sort(copy1, ITEMSTACK_COMPARATOR);
        Arrays.sort(copy2, ITEMSTACK_COMPARATOR);
        
        int index1 = 0;
        int index2 = 0;
        ItemStack stack1 = null;
        ItemStack stack2 = null;
        
        for (;;) {
            int count1 = 0;
            for (; index1 < copy1.length; index1++) {
                ItemStack next = copy1[index1];
                if (stack1 == null || stack1.isSimilar(next)) {
                    count1 += next.getAmount();
                    stack1 = next;
                } else {
                    stack2 = null;
                    break;
                }
            }
            
            int count2 = 0;
            for (; index2 < copy2.length; index2++) {
                ItemStack next = copy2[index2];
                if (stack2 == null || stack2.isSimilar(next)) {
                    count2 += next.getAmount();
                    stack2 = next;
                } else {
                    stack2 = null;
                    break;
                }
            }
            
            int result = count1 - count2;
            if (result != 0) {
                return result;
            } else if (index1 >= copy1.length && index2 >= copy2.length) {
                return 0;
            }
        }
    };
    
    public static ItemStack[] shrinkItemStack(ItemStack[] items) {
        List<ItemStack> stackList = new ArrayList<>(Arrays.asList(items));
        stackList.removeIf(
                item -> item == null || item.getAmount() == 0 || item.getType() == Material.AIR);
        items = stackList.toArray(new ItemStack[stackList.size()]);
        return items;
    }
    
    public static boolean isEmpty(ItemStack[] items) {
        for (ItemStack item : items) {
            if (item != null && item.getAmount() > 0 && item.getType() != Material.AIR) {
                return false;
            }
        }
        return true;
    }
    
    public static ItemStack[] deepCopy(ItemStack[] of) {
        ItemStack[] result = new ItemStack[of.length];
        for (int i = 0; i < result.length; i++) {
            result[i] = of[i] == null ? null : of[i].clone();
        }
        return result;<commit_msg>Fix potential bug in in ItemStack array deepCopy.<commit_after>package de.iani.cubequest.util;

import de.iani.cubequest.CubeQuest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import org.bukkit.ChatColor;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionEffect;

public class ItemStackUtil {
    
    public static final Comparator<ItemStack> ITEMSTACK_COMPARATOR = (i1, i2) -> {
        int result = i1.getType().compareTo(i2.getType());
        if (result != 0) {
            return result;
        }
        
        return i1.toString().compareTo(i2.toString());
    };
    
    public static final Comparator<ItemStack[]> ITEMSTACK_ARRAY_COMPARATOR = (i1, i2) -> {
        ItemStack[] copy1 = i1.clone();
        ItemStack[] copy2 = i2.clone();
        
        Arrays.sort(copy1, ITEMSTACK_COMPARATOR);
        Arrays.sort(copy2, ITEMSTACK_COMPARATOR);
        
        int index1 = 0;
        int index2 = 0;
        ItemStack stack1 = null;
        ItemStack stack2 = null;
        
        for (;;) {
            int count1 = 0;
            for (; index1 < copy1.length; index1++) {
                ItemStack next = copy1[index1];
                if (stack1 == null || stack1.isSimilar(next)) {
                    count1 += next.getAmount();
                    stack1 = next;
                } else {
                    stack2 = null;
                    break;
                }
            }
            
            int count2 = 0;
            for (; index2 < copy2.length; index2++) {
                ItemStack next = copy2[index2];
                if (stack2 == null || stack2.isSimilar(next)) {
                    count2 += next.getAmount();
                    stack2 = next;
                } else {
                    stack2 = null;
                    break;
                }
            }
            
            int result = count1 - count2;
            if (result != 0) {
                return result;
            } else if (index1 >= copy1.length && index2 >= copy2.length) {
                return 0;
            }
        }
    };
    
    public static ItemStack[] shrinkItemStack(ItemStack[] items) {
        List<ItemStack> stackList = new ArrayList<>(Arrays.asList(items));
        stackList.removeIf(
                item -> item == null || item.getAmount() == 0 || item.getType() == Material.AIR);
        items = stackList.toArray(new ItemStack[stackList.size()]);
        return items;
    }
    
    public static boolean isEmpty(ItemStack[] items) {
        for (ItemStack item : items) {
            if (item != null && item.getAmount() > 0 && item.getType() != Material.AIR) {
                return false;
            }
        }
        return true;
    }
    
    public static ItemStack[] deepCopy(ItemStack[] of) {
        ItemStack[] result = new ItemStack[of.length];
        for (int i = 0; i < result.length; i++) {
            result[i] = of[i] == null ? null : new ItemStack(of[i]);
        }
        return result;
{'fraction_non_alphanumeric': 0.1157439687630735, 'fraction_numerical': 0.015618463254776182, 'mean_word_length': 1.8269609775325186, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, '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': '6983722', 'n_tokens_mistral': 2196, 'n_tokens_neox': 2108, 'n_words': 455}