Search in sources :

Example 1 with Inventory

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

the class BlockEntityHopper method pickupDroppedItems.

public boolean pickupDroppedItems() {
    if (this.inventory.isFull()) {
        return false;
    }
    boolean update = false;
    for (Entity entity : this.level.getCollidingEntities(this.pickupArea)) {
        if (!(entity instanceof EntityItem)) {
            continue;
        }
        EntityItem itemEntity = (EntityItem) entity;
        Item item = itemEntity.getItem();
        if (item.getId() == 0 || item.getCount() < 1) {
            continue;
        }
        int originalCount = item.getCount();
        Item[] items = this.inventory.addItem(item);
        if (items.length == 0) {
            entity.close();
            update = true;
            continue;
        }
        if (items[0].getCount() != originalCount) {
            update = true;
        }
    }
    BlockEntity blockEntity = this.level.getBlockEntity(this.up());
    if (blockEntity instanceof InventoryHolder) {
        Inventory inv = ((InventoryHolder) blockEntity).getInventory();
        for (int i = 0; i < inv.getSize(); i++) {
            Item item = inv.getItem(i);
            if (item.getId() != 0 && item.getCount() > 0) {
                Item itemToAdd = item.clone();
                itemToAdd.count = 1;
                Item[] items = this.inventory.addItem(itemToAdd);
                if (items.length >= 1) {
                    continue;
                }
                item.count--;
                if (item.count <= 0) {
                    item = Item.get(0);
                }
                inv.setItem(i, item);
                update = true;
                break;
            }
        }
    }
    // TODO: check for minecart
    return update;
}
Also used : Entity(cn.nukkit.entity.Entity) EntityItem(cn.nukkit.entity.item.EntityItem) Item(cn.nukkit.item.Item) InventoryHolder(cn.nukkit.inventory.InventoryHolder) EntityItem(cn.nukkit.entity.item.EntityItem) HopperInventory(cn.nukkit.inventory.HopperInventory) Inventory(cn.nukkit.inventory.Inventory)

Example 2 with Inventory

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

the class BlockEntityHopper method transferItemsOut.

public boolean transferItemsOut() {
    if (this.inventory.isEmpty()) {
        return false;
    }
    if (!(this.level.getBlockEntity(this.down()) instanceof BlockEntityHopper)) {
        BlockEntity be = this.level.getBlockEntity(this.getSide(BlockFace.fromIndex(this.level.getBlockDataAt(this.getFloorX(), this.getFloorY(), this.getFloorZ()))));
        if (be instanceof InventoryHolder) {
            Inventory inventory = ((InventoryHolder) be).getInventory();
            if (inventory.isFull()) {
                return false;
            }
            for (int i = 0; i < inventory.getSize(); i++) {
                Item item = this.inventory.getItem(i);
                if (item.getId() != 0 && item.getCount() > 0) {
                    Item itemToAdd = item.clone();
                    itemToAdd.setCount(1);
                    Item[] items = inventory.addItem(itemToAdd);
                    if (items.length > 0) {
                        continue;
                    }
                    // whats wrong?
                    inventory.sendContents(inventory.getViewers());
                    item.count--;
                    this.inventory.setItem(i, item);
                    return true;
                }
            }
        }
    // TODO: check for minecart
    }
    return false;
}
Also used : EntityItem(cn.nukkit.entity.item.EntityItem) Item(cn.nukkit.item.Item) InventoryHolder(cn.nukkit.inventory.InventoryHolder) HopperInventory(cn.nukkit.inventory.HopperInventory) Inventory(cn.nukkit.inventory.Inventory)

Example 3 with Inventory

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

the class NetworkInventoryAction method createInventoryAction.

public InventoryAction createInventoryAction(Player player) {
    switch(this.sourceType) {
        case SOURCE_CONTAINER:
            if (this.windowId == ContainerIds.ARMOR) {
                // TODO: HACK!
                this.inventorySlot += 36;
                this.windowId = ContainerIds.INVENTORY;
            }
            Inventory window = player.getWindowById(this.windowId);
            if (window != null) {
                return new SlotChangeAction(window, this.inventorySlot, this.oldItem, this.newItem);
            }
            player.getServer().getLogger().debug("Player " + player.getName() + " has no open container with window ID " + this.windowId);
            return null;
        case SOURCE_WORLD:
            if (this.inventorySlot != InventoryTransactionPacket.ACTION_MAGIC_SLOT_DROP_ITEM) {
                player.getServer().getLogger().debug("Only expecting drop-item world actions from the client!");
                return null;
            }
            return new DropItemAction(this.oldItem, this.newItem);
        case SOURCE_CREATIVE:
            int type;
            switch(this.inventorySlot) {
                case InventoryTransactionPacket.ACTION_MAGIC_SLOT_CREATIVE_DELETE_ITEM:
                    type = CreativeInventoryAction.TYPE_DELETE_ITEM;
                    break;
                case InventoryTransactionPacket.ACTION_MAGIC_SLOT_CREATIVE_CREATE_ITEM:
                    type = CreativeInventoryAction.TYPE_CREATE_ITEM;
                    break;
                default:
                    player.getServer().getLogger().debug("Unexpected creative action type " + this.inventorySlot);
                    return null;
            }
            return new CreativeInventoryAction(this.oldItem, this.newItem, type);
        case SOURCE_TODO:
            // These types need special handling.
            switch(this.windowId) {
                case SOURCE_TYPE_CRAFTING_ADD_INGREDIENT:
                case SOURCE_TYPE_CRAFTING_REMOVE_INGREDIENT:
                    window = player.getCraftingGrid();
                    return new SlotChangeAction(window, this.inventorySlot, this.oldItem, this.newItem);
                case SOURCE_TYPE_CRAFTING_RESULT:
                    return new CraftingTakeResultAction(this.oldItem, this.newItem);
                case SOURCE_TYPE_CRAFTING_USE_INGREDIENT:
                    return new CraftingTransferMaterialAction(this.oldItem, this.newItem, this.inventorySlot);
                case SOURCE_TYPE_CONTAINER_DROP_CONTENTS:
                    window = player.getCraftingGrid();
                    inventorySlot = window.first(this.oldItem, true);
                    if (inventorySlot == -1) {
                        return null;
                    }
                    return new SlotChangeAction(window, inventorySlot, this.oldItem, this.newItem);
            }
            if (this.windowId >= SOURCE_TYPE_ANVIL_OUTPUT && this.windowId <= SOURCE_TYPE_ANVIL_INPUT) {
                // anvil actions
                Inventory inv = player.getWindowById(Player.ANVIL_WINDOW_ID);
                if (!(inv instanceof AnvilInventory)) {
                    player.getServer().getLogger().debug("Player " + player.getName() + " has no open anvil inventory");
                    return null;
                }
                AnvilInventory anvil = (AnvilInventory) inv;
                switch(this.windowId) {
                    case SOURCE_TYPE_ANVIL_INPUT:
                        // System.out.println("action input");
                        this.inventorySlot = 0;
                        return new SlotChangeAction(anvil, this.inventorySlot, this.oldItem, this.newItem);
                    case SOURCE_TYPE_ANVIL_MATERIAL:
                        // System.out.println("material");
                        this.inventorySlot = 1;
                        return new SlotChangeAction(anvil, this.inventorySlot, this.oldItem, this.newItem);
                    case SOURCE_TYPE_ANVIL_OUTPUT:
                        // System.out.println("action output");
                        break;
                    case SOURCE_TYPE_ANVIL_RESULT:
                        this.inventorySlot = 2;
                        anvil.clear(0);
                        anvil.clear(1);
                        anvil.setItem(2, this.oldItem);
                        // System.out.println("action result");
                        return new SlotChangeAction(anvil, this.inventorySlot, this.oldItem, this.newItem);
                }
            }
            if (this.windowId >= SOURCE_TYPE_ENCHANT_OUTPUT && this.windowId <= SOURCE_TYPE_ENCHANT_INPUT) {
                Inventory inv = player.getWindowById(Player.ENCHANT_WINDOW_ID);
                if (!(inv instanceof EnchantInventory)) {
                    player.getServer().getLogger().debug("Player " + player.getName() + " has no open enchant inventory");
                    return null;
                }
                EnchantInventory enchant = (EnchantInventory) inv;
                switch(this.windowId) {
                    case SOURCE_TYPE_ENCHANT_INPUT:
                        this.inventorySlot = 0;
                        Item local = enchant.getItem(0);
                        if (local.equals(this.newItem, true, false)) {
                            enchant.setItem(0, this.newItem);
                        }
                        break;
                    case SOURCE_TYPE_ENCHANT_MATERIAL:
                        this.inventorySlot = 1;
                        break;
                    case SOURCE_TYPE_ENCHANT_OUTPUT:
                        enchant.sendSlot(0, player);
                        // ignore?
                        return null;
                }
                return new SlotChangeAction(enchant, this.inventorySlot, this.oldItem, this.newItem);
            }
            // TODO: more stuff
            player.getServer().getLogger().debug("Player " + player.getName() + " has no open container with window ID " + this.windowId);
            return null;
        default:
            player.getServer().getLogger().debug("Unknown inventory source type " + this.sourceType);
            return null;
    }
}
Also used : Item(cn.nukkit.item.Item) EnchantInventory(cn.nukkit.inventory.EnchantInventory) AnvilInventory(cn.nukkit.inventory.AnvilInventory) EnchantInventory(cn.nukkit.inventory.EnchantInventory) AnvilInventory(cn.nukkit.inventory.AnvilInventory) Inventory(cn.nukkit.inventory.Inventory)

Aggregations

Inventory (cn.nukkit.inventory.Inventory)3 Item (cn.nukkit.item.Item)3 EntityItem (cn.nukkit.entity.item.EntityItem)2 HopperInventory (cn.nukkit.inventory.HopperInventory)2 InventoryHolder (cn.nukkit.inventory.InventoryHolder)2 Entity (cn.nukkit.entity.Entity)1 AnvilInventory (cn.nukkit.inventory.AnvilInventory)1 EnchantInventory (cn.nukkit.inventory.EnchantInventory)1