Java Code Examples for cn.nukkit.item.Item#equals()

The following examples show how to use cn.nukkit.item.Item#equals() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseInventory.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public int getFreeSpace(Item item) {
    int maxStackSize = Math.min(item.getMaxStackSize(), this.getMaxStackSize());
    int space = (this.getSize() - this.slots.size()) * maxStackSize;

    for (Item slot : this.getContents().values()) {
        if (slot == null || slot.getId() == 0) {
            space += maxStackSize;
            continue;
        }

        if (slot.equals(item, true, true)) {
            space += maxStackSize - slot.getCount();
        }
    }

    return space;
}
 
Example 2
Source File: BaseInventory.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean contains(Item item) {
    int count = Math.max(1, item.getCount());
    boolean checkDamage = item.hasMeta() && item.getDamage() >= 0;
    boolean checkTag = item.getCompoundTag() != null;
    for (Item i : this.getContents().values()) {
        if (item.equals(i, checkDamage, checkTag)) {
            count -= i.getCount();
            if (count <= 0) {
                return true;
            }
        }
    }

    return false;
}
 
Example 3
Source File: ShapelessRecipe.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
private boolean matchItemList(List<Item> haveItems, List<Item> needItems) {
    for (Item needItem : new ArrayList<>(needItems)) {
        for (Item haveItem : new ArrayList<>(haveItems)) {
            if (needItem.equals(haveItem, needItem.hasMeta(), needItem.hasCompoundTag())) {
                int amount = Math.min(haveItem.getCount(), needItem.getCount());
                needItem.setCount(needItem.getCount() - amount);
                haveItem.setCount(haveItem.getCount() - amount);
                if (haveItem.getCount() == 0) {
                    haveItems.remove(haveItem);
                }
                if (needItem.getCount() == 0) {
                    needItems.remove(needItem);
                    break;
                }
            }
        }
    }
    return haveItems.isEmpty() && needItems.isEmpty();
}
 
Example 4
Source File: BaseInventory.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean canAddItem(Item item) {
    item = item.clone();
    boolean checkDamage = item.hasMeta();
    boolean checkTag = item.getCompoundTag() != null;
    for (int i = 0; i < this.getSize(); ++i) {
        Item slot = this.getItem(i);
        if (item.equals(slot, checkDamage, checkTag)) {
            int diff;
            if ((diff = slot.getMaxStackSize() - slot.getCount()) > 0) {
                item.setCount(item.getCount() - diff);
            }
        } else if (slot.getId() == Item.AIR) {
            item.setCount(item.getCount() - this.getMaxStackSize());
        }

        if (item.getCount() <= 0) {
            return true;
        }
    }

    return false;
}
 
Example 5
Source File: BaseInventory.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
public int getFreeSpace(Item item) {
    int maxStackSize = Math.min(item.getMaxStackSize(), this.getMaxStackSize());
    int space = (this.getSize() - this.slots.size()) * maxStackSize;

    for (Item slot : this.getContents().values()) {
        if (slot == null || slot.getId() == 0) {
            space += maxStackSize;
            continue;
        }

        if (slot.equals(item, true, true)) {
            space += maxStackSize - slot.getCount();
        }
    }

    return space;
}
 
Example 6
Source File: BaseInventory.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public int getFreeSpace(Item item) {
    int maxStackSize = Math.min(item.getMaxStackSize(), this.getMaxStackSize());
    int space = (this.getSize() - this.slots.size()) * maxStackSize;

    for (Item slot : this.getContents().values()) {
        if (slot == null || slot.getId() == 0) {
            space += maxStackSize;
            continue;
        }

        if (slot.equals(item, true, true)) {
            space += maxStackSize - slot.getCount();
        }
    }

    return space;
}
 
Example 7
Source File: BaseInventory.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean canAddItem(Item item) {
    item = item.clone();
    boolean checkDamage = item.hasMeta();
    boolean checkTag = item.getCompoundTag() != null;
    for (int i = 0; i < this.getSize(); ++i) {
        Item slot = this.getItem(i);
        if (item.equals(slot, checkDamage, checkTag)) {
            int diff;
            if ((diff = slot.getMaxStackSize() - slot.getCount()) > 0) {
                item.setCount(item.getCount() - diff);
            }
        } else if (slot.getId() == Item.AIR) {
            item.setCount(item.getCount() - this.getMaxStackSize());
        }

        if (item.getCount() <= 0) {
            return true;
        }
    }

    return false;
}
 
Example 8
Source File: ShapelessRecipe.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
private boolean matchItemList(List<Item> haveItems, List<Item> needItems) {
    for (Item haveItem : new ArrayList<>(haveItems)) {
        if (haveItem.isNull()) {
            haveItems.remove(haveItem);
            continue;
        }


        for (Item needItem : new ArrayList<>(needItems)) {
            if (needItem.equals(haveItem, needItem.hasMeta(), needItem.hasCompoundTag()) && needItem.getCount() == haveItem.getCount()) {
                haveItems.remove(haveItem);
                needItems.remove(needItem);
                break;
            }
        }
    }

    return haveItems.isEmpty() && haveItems.isEmpty();
}
 
Example 9
Source File: CraftingGrid.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public void removeFromAll(Item item) {
    int count = item.getCount();

    for (int i = 0; i < this.size; i++) {
        Item target = this.getItem(i);

        if (target.equals(item, true, false)) {
            count--;
            target.count--;
            this.setItem(i, target);
            if (count <= 0) break;
        }
    }

    if (count != 0) {
        MainLogger.getLogger().debug("Unexpected ingredient count (" + count + ") in crafting grid");
    }
}
 
Example 10
Source File: ShapedRecipe.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
private boolean matchItemList(List<Item> haveItems, List<Item> needItems) {
    for (Item needItem : new ArrayList<>(needItems)) {
        for (Item haveItem : new ArrayList<>(haveItems)) {
            if (needItem.equals(haveItem, needItem.hasMeta(), needItem.hasCompoundTag())) {
                int amount = Math.min(haveItem.getCount(), needItem.getCount());
                needItem.setCount(needItem.getCount() - amount);
                haveItem.setCount(haveItem.getCount() - amount);
                if (haveItem.getCount() == 0) {
                    haveItems.remove(haveItem);
                }
                if (needItem.getCount() == 0) {
                    needItems.remove(needItem);
                    break;
                }
            }
        }
    }
    return haveItems.isEmpty() && needItems.isEmpty();
}
 
Example 11
Source File: ShapelessRecipe.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public ShapelessRecipe(String recipeId, int priority, Item result, Collection<Item> ingredients) {
    this.recipeId = recipeId;
    this.priority = priority;
    this.output = result.clone();
    if (ingredients.size() > 9) {
        throw new IllegalArgumentException("Shapeless recipes cannot have more than 9 ingredients");
    }

    this.ingredients = new ArrayList<>();
    this.ingredientsAggregate = new ArrayList<>();

    for (Item item : ingredients) {
        if (item.getCount() < 1) {
            throw new IllegalArgumentException("Recipe '" + recipeId + "' Ingredient amount was not 1 (value: " + item.getCount() + ")");
        }
        boolean found = false;
        for (Item existingIngredient : this.ingredientsAggregate) {
            if (existingIngredient.equals(item, item.hasMeta(), item.hasCompoundTag())) {
                existingIngredient.setCount(existingIngredient.getCount() + item.getCount());
                found = true;
                break;
            }
        }
        if (!found)
            this.ingredientsAggregate.add(item.clone());
        this.ingredients.add(item.clone());
    }

    this.ingredientsAggregate.sort(CraftingManager.recipeComparator);
}
 
Example 12
Source File: SimpleInventoryTransaction.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
protected boolean matchItems(List<Item> needItems, List<Item> haveItems) {
    for (InventoryAction action : this.actions) {
        if (action.getTargetItem().getId() != Item.AIR) {
            needItems.add(action.getTargetItem());
        }

        if (!action.isValid(this.source)) {
            return false;
        }

        if (action.getSourceItem().getId() != Item.AIR) {
            haveItems.add(action.getSourceItem());
        }
    }

    for (Item needItem : new ArrayList<>(needItems)) {
        for (Item haveItem : new ArrayList<>(haveItems)) {
            if (needItem.equals(haveItem)) {
                int amount = Math.min(haveItem.getCount(), needItem.getCount());
                needItem.setCount(needItem.getCount() - amount);
                haveItem.setCount(haveItem.getCount() - amount);
                if (haveItem.getCount() == 0) {
                    haveItems.remove(haveItem);
                }
                if (needItem.getCount() == 0) {
                    needItems.remove(needItem);
                    break;
                }
            }
        }
    }

    return haveItems.isEmpty() && needItems.isEmpty();
}
 
Example 13
Source File: BaseInventory.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Map<Integer, Item> all(Item item) {
    Map<Integer, Item> slots = new HashMap<>();
    boolean checkDamage = item.hasMeta() && item.getDamage() >= 0;
    boolean checkTag = item.getCompoundTag() != null;
    for (Map.Entry<Integer, Item> entry : this.getContents().entrySet()) {
        if (item.equals(entry.getValue(), checkDamage, checkTag)) {
            slots.put(entry.getKey(), entry.getValue());
        }
    }

    return slots;
}
 
Example 14
Source File: BaseInventory.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Map<Integer, Item> all(Item item) {
    Map<Integer, Item> slots = new HashMap<>();
    boolean checkDamage = item.hasMeta() && item.getDamage() >= 0;
    boolean checkTag = item.getCompoundTag() != null;
    for (Map.Entry<Integer, Item> entry : this.getContents().entrySet()) {
        if (item.equals(entry.getValue(), checkDamage, checkTag)) {
            slots.put(entry.getKey(), entry.getValue());
        }
    }

    return slots;
}
 
Example 15
Source File: InventoryTransaction.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
protected boolean matchItems(List<Item> needItems, List<Item> haveItems) {
    for (InventoryAction action : this.actions) {
        if (action.getTargetItem().getId() != Item.AIR) {
            needItems.add(action.getTargetItem());
        }

        if (!action.isValid(this.source)) {
            return false;
        }

        if (action.getSourceItem().getId() != Item.AIR) {
            haveItems.add(action.getSourceItem());
        }
    }

    for (Item needItem : new ArrayList<>(needItems)) {
        for (Item haveItem : new ArrayList<>(haveItems)) {
            if (needItem.equals(haveItem)) {
                int amount = Math.min(haveItem.getCount(), needItem.getCount());
                needItem.setCount(needItem.getCount() - amount);
                haveItem.setCount(haveItem.getCount() - amount);
                if (haveItem.getCount() == 0) {
                    haveItems.remove(haveItem);
                }
                if (needItem.getCount() == 0) {
                    needItems.remove(needItem);
                    break;
                }
            }
        }
    }

    return haveItems.isEmpty() && needItems.isEmpty();
}
 
Example 16
Source File: ShapedRecipe.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
private boolean matchInputMap(Item[][] input) {
    Map<Integer, Map<Integer, Item>> map = this.getIngredientMap();

    //match the given items to the requested items
    for (int y = 0, y2 = this.getHeight(); y < y2; ++y) {
        for (int x = 0, x2 = this.getWidth(); x < x2; ++x) {
            Item given = input[y][x];
            Item required = map.get(y).get(x);

            if (given == null || !required.equals(given, required.hasMeta(), required.hasCompoundTag()) || required.getCount() != given.getCount()) {
                return false;
            }

            input[y][x] = null;
        }
    }

    //check if there are any items left in the grid outside of the recipe
    for (Item[] items : input) {
        for (Item item : items) {
            if (item != null && !item.isNull()) {
                return false;
            }
        }
    }

    return true;
}
 
Example 17
Source File: InventoryTransaction.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
protected boolean matchItems(List<Item> needItems, List<Item> haveItems) {
    for (InventoryAction action : this.actions) {
        if (action.getTargetItem().getId() != Item.AIR) {
            needItems.add(action.getTargetItem());
        }

        if (!action.isValid(this.source)) {
            return false;
        }

        if (action.getSourceItem().getId() != Item.AIR) {
            haveItems.add(action.getSourceItem());
        }
    }

    for (Item needItem : new ArrayList<>(needItems)) {
        for (Item haveItem : new ArrayList<>(haveItems)) {
            if (needItem.equals(haveItem)) {
                int amount = Math.min(haveItem.getCount(), needItem.getCount());
                needItem.setCount(needItem.getCount() - amount);
                haveItem.setCount(haveItem.getCount() - amount);
                if (haveItem.getCount() == 0) {
                    haveItems.remove(haveItem);
                }
                if (needItem.getCount() == 0) {
                    needItems.remove(needItem);
                    break;
                }
            }
        }
    }

    return haveItems.isEmpty() && needItems.isEmpty();
}
 
Example 18
Source File: BaseInventory.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void remove(Item item) {
    boolean checkDamage = item.hasMeta();
    boolean checkTag = item.getCompoundTag() != null;
    for (Map.Entry<Integer, Item> entry : this.getContents().entrySet()) {
        if (item.equals(entry.getValue(), checkDamage, checkTag)) {
            this.clear(entry.getKey());
        }
    }
}
 
Example 19
Source File: BaseInventory.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Map<Integer, Item> all(Item item) {
    Map<Integer, Item> slots = new HashMap<>();
    boolean checkDamage = item.hasMeta() && item.getDamage() >= 0;
    boolean checkTag = item.getCompoundTag() != null;
    for (Map.Entry<Integer, Item> entry : this.getContents().entrySet()) {
        if (item.equals(entry.getValue(), checkDamage, checkTag)) {
            slots.put(entry.getKey(), entry.getValue());
        }
    }

    return slots;
}
 
Example 20
Source File: AnvilInventory.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public boolean onRename(Player player, Item resultItem) {
    Item local = getItem(TARGET);
    Item second = getItem(SACRIFICE);

    if (!resultItem.equals(local, true, false) || resultItem.getCount() != local.getCount()) {
        //Item does not match target item. Everything must match except the tags.
        return false;
    }

    if (local.equals(resultItem)) {
        //just item transaction
        return true;
    }

    if (local.getId() != 0 && second.getId() == 0) { //only rename
        local.setCustomName(resultItem.getCustomName());
        setItem(RESULT, local);
        player.getInventory().addItem(local);
        clearAll();
        player.getInventory().sendContents(player);
        sendContents(player);

        player.getLevel().addSound(player, Sound.RANDOM_ANVIL_USE);
        return true;
    } else if (local.getId() != 0 && second.getId() != 0) { //enchants combining
        if (!local.equals(second, true, false)) {
            return false;
        }

        if (local.getId() != 0 && second.getId() != 0) {
            Item result = local.clone();
            int enchants = 0;

            ArrayList<Enchantment> enchantments = new ArrayList<>(Arrays.asList(second.getEnchantments()));

            ArrayList<Enchantment> baseEnchants = new ArrayList<>();

            for (Enchantment ench : local.getEnchantments()) {
                if (ench.isMajor()) {
                    baseEnchants.add(ench);
                }
            }

            for (Enchantment enchantment : enchantments) {
                if (enchantment.getLevel() < 0 || enchantment.getId() < 0) {
                    continue;
                }

                if (enchantment.isMajor()) {
                    boolean same = false;
                    boolean another = false;

                    for (Enchantment baseEnchant : baseEnchants) {
                        if (baseEnchant.getId() == enchantment.getId())
                            same = true;
                        else {
                            another = true;
                        }
                    }

                    if (!same && another) {
                        continue;
                    }
                }

                Enchantment localEnchantment = local.getEnchantment(enchantment.getId());

                if (localEnchantment != null) {
                    int level = Math.max(localEnchantment.getLevel(), enchantment.getLevel());

                    if (localEnchantment.getLevel() == enchantment.getLevel())
                        level++;

                    enchantment.setLevel(level);
                    result.addEnchantment(enchantment);
                    continue;
                }

                result.addEnchantment(enchantment);
                enchants++;
            }

            result.setCustomName(resultItem.getCustomName());

            player.getInventory().addItem(result);
            player.getInventory().sendContents(player);
            clearAll();
            sendContents(player);

            player.getLevel().addSound(player, Sound.RANDOM_ANVIL_USE);
            return true;
        }
    }

    return false;
}