<commit_before>package de.craften.plugins.rpgplus.components.inventory;

import de.craften.plugins.rpgplus.util.CustomSkull;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * A matcher for {@link org.bukkit.inventory.ItemStack}s.
 */
public class ItemMatcher {
    private Material type;
    private Integer data;
    private Integer amount;
    private String name;
    private List<String> lore;
    private boolean unbreakable;
    private String skullTexture;
    private String bookTitle;
    private String bookAuthor;
    private List<String> bookPages;
    private boolean hideFlags;
    
    public boolean matches(ItemStack itemStack, boolean ignoreAmount) {
    	
    	boolean isSkull = itemStack.getType() == Material.SKULL_ITEM;
    	
        return typeMatches(itemStack)
                && dataMatches(itemStack)
                && (ignoreAmount || amountMatches(itemStack))
                && (!unbreakable || (itemStack.hasItemMeta() && itemStack.getItemMeta().spigot().isUnbreakable() == unbreakable))
                && nameMatches(itemStack)
                && loreMatches(itemStack)
                && (!isSkull || skullTextureMatches(itemStack))
                && bookPropertiesMatch(itemStack);
    }

    public boolean matches(Inventory inventory) {
        int amount = this.amount != null ? this.amount : 1;
        for (ItemStack itemStack : inventory) {
            if (itemStack != null && matches(itemStack, true)) {
                amount -= itemStack.getAmount();
            }
            if (amount <= 0) {
                return true;
            }
        }
        return false;
    }

    public int putInto(Inventory inventory) {
        Map<Integer, ItemStack> remaining = inventory.addItem(getItemStack());
        return remaining.isEmpty() ? 0 : remaining.get(0).getAmount();
    }

    public ItemStack getItemStack() {
        ItemStack itemStack = new ItemStack(type, amount != null ? amount : 1, data != null ? data.shortValue() : 0);
        ItemMeta meta = Bukkit.getItemFactory().getItemMeta(itemStack.getType());

        if (name != null) {
            meta.setDisplayName(name);
        }
        if (lore != null) {
            meta.setLore(lore);
        }
        if (unbreakable) {
            meta.spigot().setUnbreakable(true);
        }
        if (skullTexture != null) {
            CustomSkull.setTexture(meta, skullTexture);
        }

        if (meta instanceof BookMeta) {
            BookMeta book = (BookMeta) meta;
            if (bookTitle != null) {
                book.setTitle(bookTitle);
            }
            if (bookAuthor != null) {
                book.setAuthor(bookAuthor);
            }
            if (bookPages != null) {
                book.setPages(bookPages);
            }
        }
        
        if (hideFlags && meta != null) {
            meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_POTION_EFFECTS, ItemFlag.HIDE_ENCHANTS, ItemFlag.HIDE_UNBREAKABLE);
        }
        if (meta != null) {
        	itemStack.setItemMeta(meta);
        }
        
        return itemStack;
    }

    public int takeFrom(Inventory inventory) {
        int amount = this.amount != null ? this.amount : 1;
        ItemStack[] contents = inventory.getContents();
        for (int i = 0; i < contents.length; i++) {
            ItemStack itemStack = contents[i];
            if (matches(itemStack, true)) {
                if (itemStack.getAmount() <= amount) {
                    amount -= itemStack.getAmount();
                    contents[i] = null;
                } else {
                    itemStack.setAmount(itemStack.getAmount() - amount);
                    amount = 0;
                }
            }
            if (amount == 0) {
                break;
            }
        }
        inventory.setContents(contents);
        return amount;
    }

    private boolean typeMatches(ItemStack itemStack) {
        return (itemStack == null && type == Material.AIR) || (itemStack != null && itemStack.getType() == type);
    }
<commit_msg>Added a check if the itemstack is equals null. (#89)<commit_after>package de.craften.plugins.rpgplus.components.inventory;

import de.craften.plugins.rpgplus.util.CustomSkull;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * A matcher for {@link org.bukkit.inventory.ItemStack}s.
 */
public class ItemMatcher {
    private Material type;
    private Integer data;
    private Integer amount;
    private String name;
    private List<String> lore;
    private boolean unbreakable;
    private String skullTexture;
    private String bookTitle;
    private String bookAuthor;
    private List<String> bookPages;
    private boolean hideFlags;
    
    public boolean matches(ItemStack itemStack, boolean ignoreAmount) {
    	
    	boolean isSkull = itemStack.getType() == Material.SKULL_ITEM;
    	
        return typeMatches(itemStack)
                && dataMatches(itemStack)
                && (ignoreAmount || amountMatches(itemStack))
                && (!unbreakable || (itemStack.hasItemMeta() && itemStack.getItemMeta().spigot().isUnbreakable() == unbreakable))
                && nameMatches(itemStack)
                && loreMatches(itemStack)
                && (!isSkull || skullTextureMatches(itemStack))
                && bookPropertiesMatch(itemStack);
    }

    public boolean matches(Inventory inventory) {
        int amount = this.amount != null ? this.amount : 1;
        for (ItemStack itemStack : inventory) {
            if (itemStack != null && matches(itemStack, true)) {
                amount -= itemStack.getAmount();
            }
            if (amount <= 0) {
                return true;
            }
        }
        return false;
    }

    public int putInto(Inventory inventory) {
        Map<Integer, ItemStack> remaining = inventory.addItem(getItemStack());
        return remaining.isEmpty() ? 0 : remaining.get(0).getAmount();
    }

    public ItemStack getItemStack() {
        ItemStack itemStack = new ItemStack(type, amount != null ? amount : 1, data != null ? data.shortValue() : 0);
        ItemMeta meta = Bukkit.getItemFactory().getItemMeta(itemStack.getType());

        if (name != null) {
            meta.setDisplayName(name);
        }
        if (lore != null) {
            meta.setLore(lore);
        }
        if (unbreakable) {
            meta.spigot().setUnbreakable(true);
        }
        if (skullTexture != null) {
            CustomSkull.setTexture(meta, skullTexture);
        }

        if (meta instanceof BookMeta) {
            BookMeta book = (BookMeta) meta;
            if (bookTitle != null) {
                book.setTitle(bookTitle);
            }
            if (bookAuthor != null) {
                book.setAuthor(bookAuthor);
            }
            if (bookPages != null) {
                book.setPages(bookPages);
            }
        }
        
        if (hideFlags && meta != null) {
            meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_POTION_EFFECTS, ItemFlag.HIDE_ENCHANTS, ItemFlag.HIDE_UNBREAKABLE);
        }
        if (meta != null) {
        	itemStack.setItemMeta(meta);
        }
        
        return itemStack;
    }

    public int takeFrom(Inventory inventory) {
        int amount = this.amount != null ? this.amount : 1;
        ItemStack[] contents = inventory.getContents();
        for (int i = 0; i < contents.length; i++) {
            ItemStack itemStack = contents[i];
            if (itemStack != null) {
            	if (matches(itemStack, true)) {
                    if (itemStack.getAmount() <= amount) {
                        amount -= itemStack.getAmount();
                        contents[i] = null;
                    } else {
                        itemStack.setAmount(itemStack.getAmount() - amount);
                        amount = 0;
                    }
                }
            }
            if (amount == 0) {
                break;
            }
        }
        inventory.setContents(contents);
        return amount;
    }

    private boolean typeMatches(ItemStack itemStack) {
        return (itemStack == null && type == Material.AIR) || (itemStack != null && itemStack.getType() == type);
    }

{'fraction_non_alphanumeric': 0.10933060575065348, 'fraction_numerical': 0.002500284123195818, 'mean_word_length': 1.9589778076664426, 'pattern_counts': {'":': 0, '<': 15, '<?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': '3636535', 'n_tokens_mistral': 2495, 'n_tokens_neox': 2418, 'n_words': 522}