Search in sources :

Example 36 with Item

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

the class Binary method readMetadata.

public static EntityMetadata readMetadata(byte[] payload) {
    BinaryStream stream = new BinaryStream();
    stream.setBuffer(payload);
    long count = stream.getUnsignedVarInt();
    EntityMetadata m = new EntityMetadata();
    for (int i = 0; i < count; i++) {
        int key = (int) stream.getUnsignedVarInt();
        int type = (int) stream.getUnsignedVarInt();
        EntityData value = null;
        switch(type) {
            case Entity.DATA_TYPE_BYTE:
                value = new ByteEntityData(key, stream.getByte());
                break;
            case Entity.DATA_TYPE_SHORT:
                value = new ShortEntityData(key, stream.getLShort());
                break;
            case Entity.DATA_TYPE_INT:
                value = new IntEntityData(key, stream.getVarInt());
                break;
            case Entity.DATA_TYPE_FLOAT:
                value = new FloatEntityData(key, stream.getLFloat());
                break;
            case Entity.DATA_TYPE_STRING:
                value = new StringEntityData(key, stream.getString());
                break;
            case Entity.DATA_TYPE_SLOT:
                Item item = stream.getSlot();
                value = new SlotEntityData(key, item.getId(), item.getDamage(), item.getCount());
                break;
            case Entity.DATA_TYPE_POS:
                BlockVector3 v3 = stream.getSignedBlockPosition();
                value = new IntPositionEntityData(key, v3.x, v3.y, v3.z);
                break;
            case Entity.DATA_TYPE_LONG:
                value = new LongEntityData(key, stream.getVarLong());
                break;
            case Entity.DATA_TYPE_VECTOR3F:
                value = new Vector3fEntityData(key, stream.getVector3f());
                break;
        }
        if (value != null)
            m.put(value);
    }
    return m;
}
Also used : BlockVector3(cn.nukkit.math.BlockVector3) Item(cn.nukkit.item.Item)

Example 37 with Item

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

the class BlockEntityChest method initBlockEntity.

@Override
protected void initBlockEntity() {
    this.inventory = new ChestInventory(this);
    if (!this.namedTag.contains("Items") || !(this.namedTag.get("Items") instanceof ListTag)) {
        this.namedTag.putList(new ListTag<CompoundTag>("Items"));
    }
    /* for (int i = 0; i < this.getSize(); i++) {
            this.inventory.setItem(i, this.getItem(i));
        } */
    ListTag<CompoundTag> list = (ListTag<CompoundTag>) this.namedTag.getList("Items");
    for (CompoundTag compound : list.getAll()) {
        Item item = NBTIO.getItemHelper(compound);
        this.inventory.slots.put(compound.getByte("Slot"), item);
    }
    super.initBlockEntity();
}
Also used : Item(cn.nukkit.item.Item) ChestInventory(cn.nukkit.inventory.ChestInventory) DoubleChestInventory(cn.nukkit.inventory.DoubleChestInventory) ListTag(cn.nukkit.nbt.tag.ListTag) CompoundTag(cn.nukkit.nbt.tag.CompoundTag)

Example 38 with Item

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

the class BlockStairs method toItem.

@Override
public Item toItem() {
    Item item = super.toItem();
    item.setDamage(0);
    return item;
}
Also used : Item(cn.nukkit.item.Item)

Example 39 with Item

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

the class PlayerInventory method setItem.

private boolean setItem(int index, Item item, boolean send, boolean ignoreArmorEvents) {
    if (index < 0 || index >= this.size) {
        return false;
    } else if (item.getId() == 0 || item.getCount() <= 0) {
        return this.clear(index);
    }
    // Armor change
    if (!ignoreArmorEvents && index >= this.getSize()) {
        EntityArmorChangeEvent ev = new EntityArmorChangeEvent(this.getHolder(), this.getItem(index), item, index);
        Server.getInstance().getPluginManager().callEvent(ev);
        if (ev.isCancelled() && this.getHolder() != null) {
            this.sendArmorSlot(index, this.getViewers());
            return false;
        }
        item = ev.getNewItem();
    } else {
        EntityInventoryChangeEvent ev = new EntityInventoryChangeEvent(this.getHolder(), this.getItem(index), item, index);
        Server.getInstance().getPluginManager().callEvent(ev);
        if (ev.isCancelled()) {
            this.sendSlot(index, this.getViewers());
            return false;
        }
        item = ev.getNewItem();
    }
    Item old = this.getItem(index);
    this.slots.put(index, item.clone());
    this.onSlotChange(index, old, send);
    return true;
}
Also used : Item(cn.nukkit.item.Item) EntityArmorChangeEvent(cn.nukkit.event.entity.EntityArmorChangeEvent) EntityInventoryChangeEvent(cn.nukkit.event.entity.EntityInventoryChangeEvent)

Example 40 with Item

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

the class PlayerInventory method sendHeldItem.

public void sendHeldItem(Player... players) {
    Item item = this.getItemInHand();
    MobEquipmentPacket pk = new MobEquipmentPacket();
    pk.item = item;
    pk.inventorySlot = pk.hotbarSlot = this.getHeldItemIndex();
    for (Player player : players) {
        pk.eid = this.getHolder().getId();
        if (player.equals(this.getHolder())) {
            pk.eid = player.getId();
            this.sendSlot(this.getHeldItemIndex(), player);
        }
        player.dataPacket(pk);
    }
}
Also used : Item(cn.nukkit.item.Item) Player(cn.nukkit.Player) MobEquipmentPacket(cn.nukkit.network.protocol.MobEquipmentPacket)

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