Search in sources :

Example 1 with EntityItem

use of cn.nukkit.entity.item.EntityItem 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 EntityItem

use of cn.nukkit.entity.item.EntityItem in project Nukkit by Nukkit.

the class Level method dropItem.

public void dropItem(Vector3 source, Item item, Vector3 motion, boolean dropAround, int delay) {
    if (motion == null) {
        if (dropAround) {
            float f = ThreadLocalRandom.current().nextFloat() * 0.5f;
            float f1 = ThreadLocalRandom.current().nextFloat() * ((float) Math.PI * 2);
            motion = new Vector3(-MathHelper.sin(f1) * f, 0.20000000298023224, MathHelper.cos(f1) * f);
        } else {
            motion = new Vector3(new java.util.Random().nextDouble() * 0.2 - 0.1, 0.2, new java.util.Random().nextDouble() * 0.2 - 0.1);
        }
    }
    CompoundTag itemTag = NBTIO.putItemHelper(item);
    itemTag.setName("Item");
    if (item.getId() > 0 && item.getCount() > 0) {
        EntityItem itemEntity = new EntityItem(this.getChunk((int) source.getX() >> 4, (int) source.getZ() >> 4, true), new CompoundTag().putList(new ListTag<DoubleTag>("Pos").add(new DoubleTag("", source.getX())).add(new DoubleTag("", source.getY())).add(new DoubleTag("", source.getZ()))).putList(new ListTag<DoubleTag>("Motion").add(new DoubleTag("", motion.x)).add(new DoubleTag("", motion.y)).add(new DoubleTag("", motion.z))).putList(new ListTag<FloatTag>("Rotation").add(new FloatTag("", new java.util.Random().nextFloat() * 360)).add(new FloatTag("", 0))).putShort("Health", 5).putCompound("Item", itemTag).putShort("PickupDelay", delay));
        itemEntity.spawnToAll();
    }
}
Also used : java.util(java.util) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) EntityItem(cn.nukkit.entity.item.EntityItem)

Example 3 with EntityItem

use of cn.nukkit.entity.item.EntityItem in project Nukkit by Nukkit.

the class Level method useItemOn.

public Item useItemOn(Vector3 vector, Item item, BlockFace face, float fx, float fy, float fz, Player player, boolean playSound) {
    Block target = this.getBlock(vector);
    Block block = target.getSide(face);
    if (block.y > 255 || block.y < 0) {
        return null;
    }
    if (target.getId() == Item.AIR) {
        return null;
    }
    if (player != null) {
        PlayerInteractEvent ev = new PlayerInteractEvent(player, item, target, face, target.getId() == 0 ? Action.RIGHT_CLICK_AIR : Action.RIGHT_CLICK_BLOCK);
        if (player.getGamemode() > 2) {
            ev.setCancelled();
        }
        int distance = this.server.getSpawnRadius();
        if (!player.isOp() && distance > -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()) {
            target.onUpdate(BLOCK_UPDATE_TOUCH);
            if ((!player.isSneaking() || player.getInventory().getItemInHand().isNull()) && target.canBeActivated() && target.onActivate(item, player)) {
                return item;
            }
            if (item.canBeActivated() && item.onActivate(this, player, block, target, face, fx, fy, fz)) {
                if (item.getCount() <= 0) {
                    item = new ItemBlock(new BlockAir(), 0, 0);
                    return item;
                }
            }
        } else {
            return null;
        }
    } else if (target.canBeActivated() && target.onActivate(item, null)) {
        return item;
    }
    Block hand;
    if (item.canBePlaced()) {
        hand = item.getBlock();
        hand.position(block);
    } else {
        return null;
    }
    if (!(block.canBeReplaced() || (hand.getId() == Item.SLAB && block.getId() == Item.SLAB))) {
        return null;
    }
    if (target.canBeReplaced()) {
        block = target;
        hand.position(block);
    }
    if (!hand.canPassThrough() && hand.getBoundingBox() != null) {
        Entity[] entities = this.getCollidingEntities(hand.getBoundingBox());
        int realCount = 0;
        for (Entity e : entities) {
            if (e instanceof EntityArrow || e instanceof EntityItem || (e instanceof Player && ((Player) e).isSpectator())) {
                continue;
            }
            ++realCount;
        }
        if (player != null) {
            Vector3 diff = player.getNextPosition().subtract(player.getPosition());
            if (diff.lengthSquared() > 0.00001) {
                AxisAlignedBB bb = player.getBoundingBox().getOffsetBoundingBox(diff.x, diff.y, diff.z);
                if (hand.getBoundingBox().intersectsWith(bb)) {
                    ++realCount;
                }
            }
        }
        if (realCount > 0) {
            // Entity in block
            return null;
        }
    }
    Tag tag = item.getNamedTagEntry("CanPlaceOn");
    if (tag instanceof ListTag) {
        boolean canPlace = 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()) {
                    canPlace = true;
                    break;
                }
            }
        }
        if (!canPlace) {
            return null;
        }
    }
    if (player != null) {
        BlockPlaceEvent event = new BlockPlaceEvent(player, hand, block, target, item);
        int distance = this.server.getSpawnRadius();
        if (!player.isOp() && distance > -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) {
                event.setCancelled();
            }
        }
        this.server.getPluginManager().callEvent(event);
        if (event.isCancelled()) {
            return null;
        }
    }
    if (!hand.place(item, block, target, face, fx, fy, fz, player)) {
        return null;
    }
    if (player != null) {
        if (!player.isCreative()) {
            item.setCount(item.getCount() - 1);
        }
    }
    if (playSound) {
        this.addLevelSoundEvent(hand, LevelSoundEventPacket.SOUND_PLACE, 1, item.getId(), false);
    }
    if (item.getCount() <= 0) {
        item = new ItemBlock(new BlockAir(), 0, 0);
    }
    return item;
}
Also used : BlockAir(cn.nukkit.block.BlockAir) BlockEntity(cn.nukkit.blockentity.BlockEntity) Entity(cn.nukkit.entity.Entity) EntityArrow(cn.nukkit.entity.projectile.EntityArrow) Player(cn.nukkit.Player) BlockPlaceEvent(cn.nukkit.event.block.BlockPlaceEvent) PlayerInteractEvent(cn.nukkit.event.player.PlayerInteractEvent) ItemBlock(cn.nukkit.item.ItemBlock) EntityItem(cn.nukkit.entity.item.EntityItem) Item(cn.nukkit.item.Item) ItemBlock(cn.nukkit.item.ItemBlock) Block(cn.nukkit.block.Block) EntityItem(cn.nukkit.entity.item.EntityItem)

Aggregations

EntityItem (cn.nukkit.entity.item.EntityItem)3 Entity (cn.nukkit.entity.Entity)2 Item (cn.nukkit.item.Item)2 Player (cn.nukkit.Player)1 Block (cn.nukkit.block.Block)1 BlockAir (cn.nukkit.block.BlockAir)1 BlockEntity (cn.nukkit.blockentity.BlockEntity)1 EntityArrow (cn.nukkit.entity.projectile.EntityArrow)1 BlockPlaceEvent (cn.nukkit.event.block.BlockPlaceEvent)1 PlayerInteractEvent (cn.nukkit.event.player.PlayerInteractEvent)1 HopperInventory (cn.nukkit.inventory.HopperInventory)1 Inventory (cn.nukkit.inventory.Inventory)1 InventoryHolder (cn.nukkit.inventory.InventoryHolder)1 ItemBlock (cn.nukkit.item.ItemBlock)1 java.util (java.util)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1