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

The following examples show how to use cn.nukkit.item.Item#hasMeta() . 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
@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 2
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 3
Source File: CraftingManager.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
public void registerShapedRecipe(ShapedRecipe recipe) {
    Item result = recipe.getResult();
    this.recipes.put(recipe.getId(), recipe);
    Map<Integer, Map<Integer, Item>> ingredients = recipe.getIngredientMap();
    String hash = "";
    for (Map<Integer, Item> v : ingredients.values()) {
        for (Item item : v.values()) {
            if (item != null && item.getId() != Item.AIR) {
                hash += item.getId() + ":" + (!item.hasMeta() ? "?" : item.getDamage()) + "x" + item.getCount() + ",";
            }
        }

        hash += ";";
    }

    String index = result.getId() + ":" + (result.hasMeta() ? result.getDamage() : "");
    if (!this.recipeLookup.containsKey(index)) {
        this.recipeLookup.put(index, new HashMap<>());
    }

    this.recipeLookup.get(index).put(hash, recipe);
}
 
Example 4
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 5
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 6
Source File: BinaryStream.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void putRecipeIngredient(Item ingredient) {
    if (ingredient == null || ingredient.getId() == 0) {
        this.putVarInt(0);
        return;
    }
    this.putVarInt(ingredient.getId());
    int damage;
    if (ingredient.hasMeta()) {
        damage = ingredient.getDamage();
    } else {
        damage = 0x7fff;
    }
    this.putVarInt(damage);
    this.putVarInt(ingredient.getCount());
}
 
Example 7
Source File: BaseInventory.java    From Nukkit 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 8
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 9
Source File: BaseInventory.java    From Nukkit 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 10
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 11
Source File: BaseInventory.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int first(Item item, boolean exact) {
    int count = Math.max(1, item.getCount());
    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) && (entry.getValue().getCount() == count || (!exact && entry.getValue().getCount() > count))) {
            return entry.getKey();
        }
    }

    return -1;
}
 
Example 12
Source File: BaseInventory.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int first(Item item, boolean exact) {
    int count = Math.max(1, item.getCount());
    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) && (entry.getValue().getCount() == count || (!exact && entry.getValue().getCount() > count))) {
            return entry.getKey();
        }
    }

    return -1;
}
 
Example 13
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 14
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 15
Source File: CraftingManager.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public void registerShapelessRecipe(ShapelessRecipe recipe) {
    Item result = recipe.getResult();
    this.recipes.put(recipe.getId(), recipe);
    String hash = "";
    List<Item> ingredients = recipe.getIngredientList();
    ingredients.sort(this.comparator);
    for (Item item : ingredients) {
        hash += item.getId() + ":" + (!item.hasMeta() ? "?" : item.getDamage()) + "x" + item.getCount() + ",";
    }

    if (!this.recipeLookup.containsKey(result.getId() + ":" + result.getDamage())) {
        this.recipeLookup.put(result.getId() + ":" + result.getDamage(), new HashMap<>());
    }
    this.recipeLookup.get(result.getId() + ":" + result.getDamage()).put(hash, recipe);
}
 
Example 16
Source File: BinaryStream.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public void putSlot(Item item) {
    if (item == null || item.getId() == 0) {
        this.putVarInt(0);
        return;
    }

    this.putVarInt(item.getId());
    int auxValue = (((item.hasMeta() ? item.getDamage() : -1) & 0x7fff) << 8) | item.getCount();
    this.putVarInt(auxValue);
    byte[] nbt = item.getCompoundTag();
    this.putLShort(nbt.length);
    this.put(nbt);
    this.putVarInt(0); //TODO CanPlaceOn entry count
    this.putVarInt(0); //TODO CanDestroy entry count
}
 
Example 17
Source File: SlotEntityData.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
public SlotEntityData(int id, Item item) {
    this(id, item.getId(), (byte) (item.hasMeta() ? item.getDamage() : 0), item.getCount());
}
 
Example 18
Source File: SlotEntityData.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public SlotEntityData(int id, Item item) {
    this(id, item.getId(), (byte) (item.hasMeta() ? item.getDamage() : 0), item.getCount());
}
 
Example 19
Source File: SlotEntityData.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void setData(Item data) {
    this.blockId = data.getId();
    this.meta = (data.hasMeta() ? data.getDamage() : 0);
    this.count = data.getCount();
}
 
Example 20
Source File: SlotEntityData.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void setData(Item data) {
    this.blockId = data.getId();
    this.meta = (data.hasMeta() ? data.getDamage() : 0);
    this.count = data.getCount();
}