Util InventoryFilter - Find items easily

Discussion in 'Resources' started by FisheyLP, Jul 11, 2016.

Thread Status:
Not open for further replies.
  1. Hey guys,

    I made this class to find items in an inventory easily by various filters :)

    Class (open)
    Code:
    import java.utils.List;
    import java.utils.ArrayList;
    import java.utils.Arrays;
    
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.material.Material;
    
    /*
    * Created by FisheyLP
    */
    
    public class InventoryFilter {
    
        private Material material;
        private String displayName;
        private List<String> lore;
        private short minDurability = -1, maxDurability = -1;
        private int minAmount = -1, maxAmount = -1;
        private byte data = -1;
    
        public InventoryFilter material(Material material) {
            this.material = material;
            return this;
        }
    
        public InventoryFilter amount(int amount) {
            minAmount = maxAmount = amount;
            return this;
        }
    
        public InventoryFilter minAmount(int amount) {
            minAmount = amount;
            return this;
        }
    
        public InventoryFilter maxAmount(int amount) {
            maxAmount = amount;
            return this;
        }
    
        public InventoryFilter displayName(String displayName) {
            this.displayName = displayName;
            return this;
        }
    
        public InventoryFilter lore(String... lore) {
            if (lore == null) return lore((List<String>) null);
            return lore(Arrays.asList(lore));
        }
    
        public InventoryFilter lore(List<String> lore) {
            this.lore = lore;
            return this;
        }
    
        public InventoryFilter durability(short durability) {
            minDurability = maxDurability = durability;
            return this;
        }
    
        public InventoryFilter minDurability(short durability) {
            this.minDurability = durability;
            return this;
        }
    
        public InventoryFilter maxDurability(short durability) {
            this.maxDurability = durability;
            return this;
        }
    
        public InventoryFilter data(byte data) {
            this.data = data;
            return this;
        }
    
        public List<ItemStack> apply(Inventory inv) {
            List<ItemStack> items = new ArrayList<ItemStack>();
    
            for (ItemStack item : inv.getContent())
                if (appliesTo(item)) items.add(item);
    
            return items;
        }
    
        public List<ItemStack> applyReverse(Inventory inv) {
            List<ItemStack> items = new ArrayList<ItemStack>();
    
            for (ItemStack item : inv.getContent())
                if (!appliesTo(item)) items.add(item);
    
            return items;
        }
    
        public boolean appliesTo(ItemStack item) {
            if (item == null) return false;
    
            if (material != null && material != item.getType()) return false;
    
            if (minAmount != -1 && item.getAmount() < minAmount) return false;
            if (maxAmount != -1 && item.getAmount() > maxAmount) return false;
    
            if (minDurability != -1 && item.getDurability() < minDurability) return false;
            if (maxDurability != -1 && item.getDurability() > maxDurability) return false;
    
            if (data != -1 && data != item.getData().getData()) return false;
    
            if (item.hasItemMeta()) {
                ItemMeta meta = item.getItemMeta();
                if (displayName != null && !displayName.equals(meta.getDisplayName())) return false;
                if (lore != null && !lore.equals(meta.getLore())) return false;
            }
    
            return true;
        }
    
    }


    Example usage:
    Code:
    Inventory inv = ...
    
    InventoryFilter filter = new InventoryFilter().material(Material.DIAMOND_SWORD)
    .displayName("┬žaExcalibur").maxDurability(100);
    
    List<ItemStack> results = filter.apply(inv);

    You can now do something with the results, for example change the displayname for all of them.
    Code:
    for (ItemStack item : results) {
    ItemMeta meta = item.getItemMeta();
    meta.setDisplayName("┬žeExcalibur");
    item.setItemMeta(meta);
    
    // update item in inventory
    }

    You can edit the filter easily:
    Code:
    filter.displayName("┬žeExcalibur").maxDurability(50);

    And for checking single items:
    Code:
    ItemStack item = ...
    
    if (filter.appliesTo(item)) {
    // do stuff
    }

    Or get every item that doesn't match the filter:
    Code:
    List<ItemStack> result = filter.applyReverse(inv);

    To remove single filters, either set it to -1 if it's parameter is a number, or null if not:
    Code:
    filter = filter.durability(-1).lore(null);

    Methods explained (open)

    Code:
    material(Material material)
    item's material is the same as material

    Code:
    amount(int amount)
    item's amount is the same as amount

    Code:
    minAmount(int amount)
    item's amount is >= amount

    Code:
    maxAmount(int amount)
    item's amount is <= amount

    Code:
    displayName(String displayName)
    item's displayName equals displayName

    Code:
    lore(String... lore)
    lore(List<String> lore)
    item's lore equals lore

    Code:
    durability(short durability)
    item's durability is the same as durability

    Code:
    minDurability(short durability)
    item's durability is >= durability

    Code:
    maxDurability(short durability)
    item's durability is <= durability

    Code:
    data(byte data)
    item's data is the same as data

    Code:
    apply(Inventory inv)
    returns a List<ItemStack> with the items that match the filter

    Code:
    applyReverse(Inventory inv)
    returns a List<ItemStack> with the items that don't match the filter

    Code:
    appliesTo(ItemStack item)
    returns true if the item matches the filter
    Funfact (open)
    I wrote all this in the bukkit forums new-thread-editor and didn't even test it :p But it should work ... (I hope it does :oops:)
    Didn't know how to implement Enchantments (too complicated) so I left them out lel
     
    Last edited: Jul 12, 2016
    ChipDev likes this.
  2. Offline

    GLookLike

    Code:
    public InventoryFilter minDurability(short durability){
            this.minDurability = minDurability;
            return this;
        }
    There's an error in this.minDurability = minDurability;, it says the assignment to minDurability has no effects.
    Code:
    if(material != null && material != item.getMaterial())
                return false;
    There's an error in item.getMaterial, it says the method is undefined for the type ItemStack.
     
  3. He has fixed both these issues in the edited post.
     
  4. Offline

    hsndmrts98

    oh nice class ;( i have to use it immediately.
     
Thread Status:
Not open for further replies.

Share This Page