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

The following examples show how to use cn.nukkit.item.Item#getCompoundTag() . 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 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 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: 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 4
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 5
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 6
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 7
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 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 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 11
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 12
Source File: BinaryStream.java    From Nukkit 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 13
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 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: 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 16
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 17
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
}