Search in sources :

Example 1 with InventoryPickupItemEvent

use of cn.nukkit.event.inventory.InventoryPickupItemEvent in project Nukkit by Nukkit.

the class Player method pickupEntity.

public boolean pickupEntity(Entity entity, boolean near) {
    if (!this.spawned || !this.isAlive() || !this.isOnline() || this.getGamemode() == SPECTATOR) {
        return false;
    }
    if (near) {
        if (entity instanceof EntityArrow && ((EntityArrow) entity).hadCollision) {
            ItemArrow item = new ItemArrow();
            if (this.isSurvival() && !this.inventory.canAddItem(item)) {
                return false;
            }
            InventoryPickupArrowEvent ev;
            this.server.getPluginManager().callEvent(ev = new InventoryPickupArrowEvent(this.inventory, (EntityArrow) entity));
            if (ev.isCancelled()) {
                return false;
            }
            TakeItemEntityPacket pk = new TakeItemEntityPacket();
            pk.entityId = this.getId();
            pk.target = entity.getId();
            Server.broadcastPacket(entity.getViewers().values(), pk);
            this.dataPacket(pk);
            this.inventory.addItem(item.clone());
            entity.kill();
            return true;
        } else if (entity instanceof EntityItem) {
            if (((EntityItem) entity).getPickupDelay() <= 0) {
                Item item = ((EntityItem) entity).getItem();
                if (item != null) {
                    if (this.isSurvival() && !this.inventory.canAddItem(item)) {
                        return false;
                    }
                    InventoryPickupItemEvent ev;
                    this.server.getPluginManager().callEvent(ev = new InventoryPickupItemEvent(this.inventory, (EntityItem) entity));
                    if (ev.isCancelled()) {
                        return false;
                    }
                    switch(item.getId()) {
                        case Item.WOOD:
                        case Item.WOOD2:
                            this.awardAchievement("mineWood");
                            break;
                        case Item.DIAMOND:
                            this.awardAchievement("diamond");
                            break;
                    }
                    /*switch (item.getId()) {
                            case Item.WOOD:
                                this.awardAchievement("mineWood");
                                break;
                            case Item.DIAMOND:
                                this.awardAchievement("diamond");
                                break;
                        }*/
                    TakeItemEntityPacket pk = new TakeItemEntityPacket();
                    pk.entityId = this.getId();
                    pk.target = entity.getId();
                    Server.broadcastPacket(entity.getViewers().values(), pk);
                    this.dataPacket(pk);
                    this.inventory.addItem(item.clone());
                    entity.kill();
                    return true;
                }
            }
        }
    }
    int tick = this.getServer().getTick();
    if (pickedXPOrb < tick && entity instanceof EntityXPOrb && this.boundingBox.isVectorInside(entity)) {
        EntityXPOrb xpOrb = (EntityXPOrb) entity;
        if (xpOrb.getPickupDelay() <= 0) {
            int exp = xpOrb.getExp();
            this.addExperience(exp);
            entity.kill();
            this.getLevel().addSound(this, Sound.RANDOM_ORB);
            pickedXPOrb = tick;
            return true;
        }
    }
    return false;
}
Also used : EntityArrow(cn.nukkit.entity.projectile.EntityArrow) InventoryPickupArrowEvent(cn.nukkit.event.inventory.InventoryPickupArrowEvent) InventoryPickupItemEvent(cn.nukkit.event.inventory.InventoryPickupItemEvent)

Aggregations

EntityArrow (cn.nukkit.entity.projectile.EntityArrow)1 InventoryPickupArrowEvent (cn.nukkit.event.inventory.InventoryPickupArrowEvent)1 InventoryPickupItemEvent (cn.nukkit.event.inventory.InventoryPickupItemEvent)1