Search in sources :

Example 1 with InventoryHolder

use of cn.nukkit.inventory.InventoryHolder 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 InventoryHolder

use of cn.nukkit.inventory.InventoryHolder 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 InventoryHolder

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

the class Level method useBreakOn.

public Item useBreakOn(Vector3 vector, Item item, Player player, boolean createParticles) {
    if (player != null && player.getGamemode() > 1) {
        return null;
    }
    Block target = this.getBlock(vector);
    Item[] drops;
    if (item == null) {
        item = new ItemBlock(new BlockAir(), 0, 0);
    }
    if (player != null) {
        double breakTime = target.getBreakTime(item, player);
        if (player.isCreative() && breakTime > 0.15) {
            breakTime = 0.15;
        }
        if (player.hasEffect(Effect.SWIFTNESS)) {
            breakTime *= 1 - (0.2 * (player.getEffect(Effect.SWIFTNESS).getAmplifier() + 1));
        }
        if (player.hasEffect(Effect.MINING_FATIGUE)) {
            breakTime *= 1 - (0.3 * (player.getEffect(Effect.MINING_FATIGUE).getAmplifier() + 1));
        }
        Enchantment eff = item.getEnchantment(Enchantment.ID_EFFICIENCY);
        if (eff != null && eff.getLevel() > 0) {
            breakTime *= 1 - (0.3 * eff.getLevel());
        }
        breakTime -= 0.15;
        BlockBreakEvent ev = new BlockBreakEvent(player, target, item, player.isCreative(), (player.lastBreak + breakTime * 1000) > System.currentTimeMillis());
        double distance;
        if (player.isSurvival() && !target.isBreakable(item)) {
            ev.setCancelled();
        } else if (!player.isOp() && (distance = this.server.getSpawnRadius()) > -1) {
            Vector2 t = new Vector2(target.x, target.z);
            Vector2 s = new Vector2(this.getSpawnLocation().x, this.getSpawnLocation().z);
            if (!this.server.getOps().getAll().isEmpty() && t.distance(s) <= distance) {
                ev.setCancelled();
            }
        }
        this.server.getPluginManager().callEvent(ev);
        if (ev.isCancelled()) {
            return null;
        }
        if (!ev.getInstaBreak() && ev.isFastBreak()) {
            return null;
        }
        player.lastBreak = System.currentTimeMillis();
        drops = ev.getDrops();
    } else if (!target.isBreakable(item)) {
        return null;
    } else {
        drops = target.getDrops(item);
    }
    Block above = this.getBlock(new Vector3(target.x, target.y + 1, target.z));
    if (above != null) {
        if (above.getId() == Item.FIRE) {
            this.setBlock(above, new BlockAir(), true);
        }
    }
    Tag tag = item.getNamedTagEntry("CanDestroy");
    if (tag instanceof ListTag) {
        boolean canBreak = false;
        for (Tag v : ((ListTag<Tag>) tag).getAll()) {
            if (v instanceof StringTag) {
                Item entry = Item.fromString(((StringTag) v).data);
                if (entry.getId() > 0 && entry.getBlock() != null && entry.getBlock().getId() == target.getId()) {
                    canBreak = true;
                    break;
                }
            }
        }
        if (!canBreak) {
            return null;
        }
    }
    if (createParticles) {
        Map<Integer, Player> players = this.getChunkPlayers((int) target.x >> 4, (int) target.z >> 4);
        this.addParticle(new DestroyBlockParticle(target.add(0.5), target), players.values());
        if (player != null) {
            players.remove(player.getLoaderId());
        }
    }
    target.onBreak(item);
    BlockEntity blockEntity = this.getBlockEntity(target);
    if (blockEntity != null) {
        if (blockEntity instanceof InventoryHolder) {
            if (blockEntity instanceof BlockEntityChest) {
                ((BlockEntityChest) blockEntity).unpair();
            }
            for (Item chestItem : ((InventoryHolder) blockEntity).getInventory().getContents().values()) {
                this.dropItem(target, chestItem);
            }
        }
        blockEntity.close();
        this.updateComparatorOutputLevel(target);
    }
    item.useOn(target);
    if (item.isTool() && item.getDamage() >= item.getMaxDurability()) {
        item = new ItemBlock(new BlockAir(), 0, 0);
    }
    if (this.gameRules.getBoolean(GameRule.DO_TILE_DROPS)) {
        int dropExp = target.getDropExp();
        if (player != null) {
            player.addExperience(dropExp);
            if (player.isSurvival()) {
                for (int ii = 1; ii <= dropExp; ii++) {
                    this.dropExpOrb(target, 1);
                }
            }
        }
        if (player == null || player.isSurvival()) {
            for (Item drop : drops) {
                if (drop.getCount() > 0) {
                    this.dropItem(vector.add(0.5, 0.5, 0.5), drop);
                }
            }
        }
    }
    return item;
}
Also used : BlockAir(cn.nukkit.block.BlockAir) Player(cn.nukkit.Player) DestroyBlockParticle(cn.nukkit.level.particle.DestroyBlockParticle) ItemBlock(cn.nukkit.item.ItemBlock) EntityItem(cn.nukkit.entity.item.EntityItem) Item(cn.nukkit.item.Item) BlockEntityChest(cn.nukkit.blockentity.BlockEntityChest) ItemBlock(cn.nukkit.item.ItemBlock) Block(cn.nukkit.block.Block) BlockBreakEvent(cn.nukkit.event.block.BlockBreakEvent) Enchantment(cn.nukkit.item.enchantment.Enchantment) InventoryHolder(cn.nukkit.inventory.InventoryHolder) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Aggregations

EntityItem (cn.nukkit.entity.item.EntityItem)3 InventoryHolder (cn.nukkit.inventory.InventoryHolder)3 Item (cn.nukkit.item.Item)3 HopperInventory (cn.nukkit.inventory.HopperInventory)2 Inventory (cn.nukkit.inventory.Inventory)2 Player (cn.nukkit.Player)1 Block (cn.nukkit.block.Block)1 BlockAir (cn.nukkit.block.BlockAir)1 BlockEntity (cn.nukkit.blockentity.BlockEntity)1 BlockEntityChest (cn.nukkit.blockentity.BlockEntityChest)1 Entity (cn.nukkit.entity.Entity)1 BlockBreakEvent (cn.nukkit.event.block.BlockBreakEvent)1 ItemBlock (cn.nukkit.item.ItemBlock)1 Enchantment (cn.nukkit.item.enchantment.Enchantment)1 DestroyBlockParticle (cn.nukkit.level.particle.DestroyBlockParticle)1