Search in sources :

Example 1 with EntityInventoryChangeEvent

use of cn.nukkit.event.entity.EntityInventoryChangeEvent in project Nukkit by Nukkit.

the class BaseInventory method setItem.

@Override
public boolean setItem(int index, Item item, boolean send) {
    item = item.clone();
    if (index < 0 || index >= this.size) {
        return false;
    } else if (item.getId() == 0 || item.getCount() <= 0) {
        return this.clear(index);
    }
    InventoryHolder holder = this.getHolder();
    if (holder instanceof Entity) {
        EntityInventoryChangeEvent ev = new EntityInventoryChangeEvent((Entity) holder, 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 : Entity(cn.nukkit.entity.Entity) Item(cn.nukkit.item.Item) EntityInventoryChangeEvent(cn.nukkit.event.entity.EntityInventoryChangeEvent)

Example 2 with EntityInventoryChangeEvent

use of cn.nukkit.event.entity.EntityInventoryChangeEvent in project Nukkit by Nukkit.

the class BaseInventory method clear.

@Override
public boolean clear(int index, boolean send) {
    if (this.slots.containsKey(index)) {
        Item item = new ItemBlock(new BlockAir(), null, 0);
        Item old = this.slots.get(index);
        InventoryHolder holder = this.getHolder();
        if (holder instanceof Entity) {
            EntityInventoryChangeEvent ev = new EntityInventoryChangeEvent((Entity) holder, old, item, index);
            Server.getInstance().getPluginManager().callEvent(ev);
            if (ev.isCancelled()) {
                this.sendSlot(index, this.getViewers());
                return false;
            }
            item = ev.getNewItem();
        }
        if (item.getId() != Item.AIR) {
            this.slots.put(index, item.clone());
        } else {
            this.slots.remove(index);
        }
        this.onSlotChange(index, old, send);
    }
    return true;
}
Also used : BlockAir(cn.nukkit.block.BlockAir) Item(cn.nukkit.item.Item) Entity(cn.nukkit.entity.Entity) EntityInventoryChangeEvent(cn.nukkit.event.entity.EntityInventoryChangeEvent) ItemBlock(cn.nukkit.item.ItemBlock)

Example 3 with EntityInventoryChangeEvent

use of cn.nukkit.event.entity.EntityInventoryChangeEvent in project Nukkit by Nukkit.

the class PlayerInventory method clear.

@Override
public boolean clear(int index, boolean send) {
    if (this.slots.containsKey(index)) {
        Item item = new ItemBlock(new BlockAir(), null, 0);
        Item old = this.slots.get(index);
        if (index >= this.getSize() && index < this.size) {
            EntityArmorChangeEvent ev = new EntityArmorChangeEvent(this.getHolder(), old, item, index);
            Server.getInstance().getPluginManager().callEvent(ev);
            if (ev.isCancelled()) {
                if (index >= this.size) {
                    this.sendArmorSlot(index, this.getViewers());
                } else {
                    this.sendSlot(index, this.getViewers());
                }
                return false;
            }
            item = ev.getNewItem();
        } else {
            EntityInventoryChangeEvent ev = new EntityInventoryChangeEvent(this.getHolder(), old, item, index);
            Server.getInstance().getPluginManager().callEvent(ev);
            if (ev.isCancelled()) {
                if (index >= this.size) {
                    this.sendArmorSlot(index, this.getViewers());
                } else {
                    this.sendSlot(index, this.getViewers());
                }
                return false;
            }
            item = ev.getNewItem();
        }
        if (item.getId() != Item.AIR) {
            this.slots.put(index, item.clone());
        } else {
            this.slots.remove(index);
        }
        this.onSlotChange(index, old, send);
    }
    return true;
}
Also used : BlockAir(cn.nukkit.block.BlockAir) Item(cn.nukkit.item.Item) EntityArmorChangeEvent(cn.nukkit.event.entity.EntityArmorChangeEvent) EntityInventoryChangeEvent(cn.nukkit.event.entity.EntityInventoryChangeEvent) ItemBlock(cn.nukkit.item.ItemBlock)

Example 4 with EntityInventoryChangeEvent

use of cn.nukkit.event.entity.EntityInventoryChangeEvent 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)

Aggregations

EntityInventoryChangeEvent (cn.nukkit.event.entity.EntityInventoryChangeEvent)4 Item (cn.nukkit.item.Item)4 BlockAir (cn.nukkit.block.BlockAir)2 Entity (cn.nukkit.entity.Entity)2 EntityArmorChangeEvent (cn.nukkit.event.entity.EntityArmorChangeEvent)2 ItemBlock (cn.nukkit.item.ItemBlock)2