Search in sources :

Example 1 with PlayerInventory

use of cn.nukkit.inventory.PlayerInventory in project Nukkit by Nukkit.

the class InventoryTransaction method callExecuteEvent.

protected boolean callExecuteEvent() {
    InventoryTransactionEvent ev = new InventoryTransactionEvent(this);
    this.source.getServer().getPluginManager().callEvent(ev);
    SlotChangeAction from = null;
    SlotChangeAction to = null;
    Player who = null;
    for (InventoryAction action : this.actions) {
        if (!(action instanceof SlotChangeAction)) {
            continue;
        }
        SlotChangeAction slotChange = (SlotChangeAction) action;
        if (slotChange.getInventory() instanceof PlayerInventory) {
            who = (Player) slotChange.getInventory().getHolder();
        }
        if (from == null) {
            from = slotChange;
        } else {
            to = slotChange;
        }
    }
    if (who != null && to != null) {
        if (from.getTargetItem().getCount() > from.getSourceItem().getCount()) {
            from = to;
        }
        InventoryClickEvent ev2 = new InventoryClickEvent(who, from.getInventory(), from.getSlot(), from.getSourceItem(), from.getTargetItem());
        this.source.getServer().getPluginManager().callEvent(ev2);
        if (ev2.isCancelled()) {
            return false;
        }
    }
    return !ev.isCancelled();
}
Also used : InventoryClickEvent(cn.nukkit.event.inventory.InventoryClickEvent) Player(cn.nukkit.Player) InventoryTransactionEvent(cn.nukkit.event.inventory.InventoryTransactionEvent) PlayerInventory(cn.nukkit.inventory.PlayerInventory) SlotChangeAction(cn.nukkit.inventory.transaction.action.SlotChangeAction) InventoryAction(cn.nukkit.inventory.transaction.action.InventoryAction)

Example 2 with PlayerInventory

use of cn.nukkit.inventory.PlayerInventory in project Nukkit by Nukkit.

the class EntityHumanType method initEntity.

@Override
protected void initEntity() {
    this.inventory = new PlayerInventory(this);
    if (this.namedTag.contains("Inventory") && this.namedTag.get("Inventory") instanceof ListTag) {
        ListTag<CompoundTag> inventoryList = this.namedTag.getList("Inventory", CompoundTag.class);
        for (CompoundTag item : inventoryList.getAll()) {
            int slot = item.getByte("Slot");
            if (slot >= 0 && slot < 9) {
                // hotbar
                // Old hotbar saving stuff, remove it (useless now)
                inventoryList.remove(item);
            } else if (slot >= 100 && slot < 104) {
                this.inventory.setItem(this.inventory.getSize() + slot - 100, NBTIO.getItemHelper(item));
            } else {
                this.inventory.setItem(slot - 9, NBTIO.getItemHelper(item));
            }
        }
    }
    this.enderChestInventory = new PlayerEnderChestInventory(this);
    if (this.namedTag.contains("EnderItems") && this.namedTag.get("EnderItems") instanceof ListTag) {
        ListTag<CompoundTag> inventoryList = this.namedTag.getList("EnderItems", CompoundTag.class);
        for (CompoundTag item : inventoryList.getAll()) {
            this.enderChestInventory.setItem(item.getByte("Slot"), NBTIO.getItemHelper(item));
        }
    }
    super.initEntity();
}
Also used : PlayerEnderChestInventory(cn.nukkit.inventory.PlayerEnderChestInventory) PlayerInventory(cn.nukkit.inventory.PlayerInventory) ListTag(cn.nukkit.nbt.tag.ListTag) CompoundTag(cn.nukkit.nbt.tag.CompoundTag)

Aggregations

PlayerInventory (cn.nukkit.inventory.PlayerInventory)2 Player (cn.nukkit.Player)1 InventoryClickEvent (cn.nukkit.event.inventory.InventoryClickEvent)1 InventoryTransactionEvent (cn.nukkit.event.inventory.InventoryTransactionEvent)1 PlayerEnderChestInventory (cn.nukkit.inventory.PlayerEnderChestInventory)1 InventoryAction (cn.nukkit.inventory.transaction.action.InventoryAction)1 SlotChangeAction (cn.nukkit.inventory.transaction.action.SlotChangeAction)1 CompoundTag (cn.nukkit.nbt.tag.CompoundTag)1 ListTag (cn.nukkit.nbt.tag.ListTag)1