Search in sources :

Example 41 with Item

use of cn.nukkit.item.Item in project Nukkit by Nukkit.

the class ShapedRecipe method matchItems.

@Override
public boolean matchItems(Item[][] input, Item[][] output) {
    if (!matchInputMap(cloneItemArray(input))) {
        for (int i = 0; i < input.length; i++) {
            Item[] old = input[i];
            Item[] newArray = new Item[old.length];
            System.arraycopy(old, 0, newArray, 0, newArray.length);
            Utils.reverseArray(newArray);
            input[i] = newArray;
        }
        if (!matchInputMap(input)) {
            return false;
        }
    }
    // and then, finally, check that the output items are good:
    List<Item> haveItems = new ArrayList<>();
    for (Item[] items : output) {
        haveItems.addAll(Arrays.asList(items));
    }
    List<Item> needItems = this.getExtraResults();
    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() && needItems.isEmpty();
}
Also used : Item(cn.nukkit.item.Item)

Example 42 with Item

use of cn.nukkit.item.Item in project Nukkit by Nukkit.

the class ShapelessRecipe method addIngredient.

public ShapelessRecipe addIngredient(Item item) {
    if (this.ingredients.size() > 9) {
        throw new IllegalArgumentException("Shapeless recipes cannot have more than 9 ingredients");
    }
    Item it = item.clone();
    it.setCount(1);
    while (item.getCount() > 0) {
        this.ingredients.add(it.clone());
        item.setCount(item.getCount() - 1);
    }
    return this;
}
Also used : Item(cn.nukkit.item.Item)

Example 43 with Item

use of cn.nukkit.item.Item in project Nukkit by Nukkit.

the class ShapelessRecipe method matchItemList.

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();
}
Also used : Item(cn.nukkit.item.Item) ArrayList(java.util.ArrayList)

Example 44 with Item

use of cn.nukkit.item.Item in project Nukkit by Nukkit.

the class CraftingTransaction method reindexInputs.

private Item[][] reindexInputs() {
    int xOffset = gridSize;
    int yOffset = gridSize;
    int height = 0;
    int width = 0;
    for (int y = 0; y < this.inputs.length; y++) {
        Item[] row = this.inputs[y];
        for (int x = 0; x < row.length; x++) {
            Item item = row[x];
            if (!item.isNull()) {
                xOffset = Math.min(x, xOffset);
                yOffset = Math.min(y, yOffset);
                height = Math.max(y + 1 - yOffset, height);
                width = Math.max(x + 1 - xOffset, width);
            }
        }
    }
    if (height == 0 || width == 0) {
        return new Item[0][];
    }
    Item air = Item.get(Item.AIR, 0, 0);
    Item[][] reindexed = new Item[height][width];
    for (Item[] i : reindexed) {
        Arrays.fill(i, air);
    }
    for (int y = 0; y < reindexed.length; y++) {
        Item[] row = reindexed[y];
        // hope I converted it right :D
        System.arraycopy(this.inputs[y + yOffset], xOffset, reindexed[y], 0, row.length);
    /*for (int x = 0; x < row.length; x++) {
                reindexed[y][x] = this.inputs[y + yOffset][x + xOffset];
            }*/
    }
    return reindexed;
}
Also used : Item(cn.nukkit.item.Item)

Example 45 with Item

use of cn.nukkit.item.Item in project Nukkit by Nukkit.

the class InventoryTransaction method matchItems.

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();
}
Also used : Item(cn.nukkit.item.Item) InventoryAction(cn.nukkit.inventory.transaction.action.InventoryAction)

Aggregations

Item (cn.nukkit.item.Item)64 Enchantment (cn.nukkit.item.enchantment.Enchantment)13 Player (cn.nukkit.Player)10 ItemBlock (cn.nukkit.item.ItemBlock)10 BlockAir (cn.nukkit.block.BlockAir)8 Random (java.util.Random)7 BlockEntity (cn.nukkit.blockentity.BlockEntity)6 Entity (cn.nukkit.entity.Entity)5 Block (cn.nukkit.block.Block)4 EntityItem (cn.nukkit.entity.item.EntityItem)4 EntityInventoryChangeEvent (cn.nukkit.event.entity.EntityInventoryChangeEvent)4 Inventory (cn.nukkit.inventory.Inventory)3 InventoryHolder (cn.nukkit.inventory.InventoryHolder)3 BlockEntityItemFrame (cn.nukkit.blockentity.BlockEntityItemFrame)2 EntityArmorChangeEvent (cn.nukkit.event.entity.EntityArmorChangeEvent)2 EntityDamageByEntityEvent (cn.nukkit.event.entity.EntityDamageByEntityEvent)2 EntityDamageEvent (cn.nukkit.event.entity.EntityDamageEvent)2 HopperInventory (cn.nukkit.inventory.HopperInventory)2 InventoryAction (cn.nukkit.inventory.transaction.action.InventoryAction)2 TranslationContainer (cn.nukkit.lang.TranslationContainer)2