Search in sources :

Example 1 with PlayerInteractEvent

use of cn.nukkit.event.player.PlayerInteractEvent in project Nukkit by Nukkit.

the class Entity method fall.

public void fall(float fallDistance) {
    float damage = (float) Math.floor(fallDistance - 3 - (this.hasEffect(Effect.JUMP) ? this.getEffect(Effect.JUMP).getAmplifier() + 1 : 0));
    if (damage > 0) {
        this.attack(new EntityDamageEvent(this, DamageCause.FALL, damage));
    }
    if (fallDistance > 0.75) {
        Block down = this.level.getBlock(this.floor().down());
        if (down.getId() == Item.FARMLAND) {
            Event ev;
            if (this instanceof Player) {
                ev = new PlayerInteractEvent((Player) this, null, down, null, Action.PHYSICAL);
            } else {
                ev = new EntityInteractEvent(this, down);
            }
            this.server.getPluginManager().callEvent(ev);
            if (ev.isCancelled()) {
                return;
            }
            this.level.setBlock(down, new BlockDirt(), false, true);
        }
    }
}
Also used : Player(cn.nukkit.Player) PlayerInteractEvent(cn.nukkit.event.player.PlayerInteractEvent) Block(cn.nukkit.block.Block) PlayerInteractEvent(cn.nukkit.event.player.PlayerInteractEvent) Event(cn.nukkit.event.Event) PlayerTeleportEvent(cn.nukkit.event.player.PlayerTeleportEvent) BlockDirt(cn.nukkit.block.BlockDirt)

Example 2 with PlayerInteractEvent

use of cn.nukkit.event.player.PlayerInteractEvent in project Nukkit by Nukkit.

the class BlockPressurePlateBase method onEntityCollide.

@Override
public void onEntityCollide(Entity entity) {
    int power = getRedstonePower();
    if (power == 0) {
        Event ev;
        if (entity instanceof Player) {
            ev = new PlayerInteractEvent((Player) entity, null, this, null, Action.PHYSICAL);
        } else {
            ev = new EntityInteractEvent(entity, this);
        }
        this.level.getServer().getPluginManager().callEvent(ev);
        if (!ev.isCancelled()) {
            updateState(power);
        }
    }
}
Also used : Player(cn.nukkit.Player) PlayerInteractEvent(cn.nukkit.event.player.PlayerInteractEvent) PlayerInteractEvent(cn.nukkit.event.player.PlayerInteractEvent) Event(cn.nukkit.event.Event) EntityInteractEvent(cn.nukkit.event.entity.EntityInteractEvent) BlockRedstoneEvent(cn.nukkit.event.block.BlockRedstoneEvent) EntityInteractEvent(cn.nukkit.event.entity.EntityInteractEvent)

Example 3 with PlayerInteractEvent

use of cn.nukkit.event.player.PlayerInteractEvent 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

Player (cn.nukkit.Player)3 PlayerInteractEvent (cn.nukkit.event.player.PlayerInteractEvent)3 Block (cn.nukkit.block.Block)2 Event (cn.nukkit.event.Event)2 BlockAir (cn.nukkit.block.BlockAir)1 BlockDirt (cn.nukkit.block.BlockDirt)1 BlockEntity (cn.nukkit.blockentity.BlockEntity)1 Entity (cn.nukkit.entity.Entity)1 EntityItem (cn.nukkit.entity.item.EntityItem)1 EntityArrow (cn.nukkit.entity.projectile.EntityArrow)1 BlockPlaceEvent (cn.nukkit.event.block.BlockPlaceEvent)1 BlockRedstoneEvent (cn.nukkit.event.block.BlockRedstoneEvent)1 EntityInteractEvent (cn.nukkit.event.entity.EntityInteractEvent)1 PlayerTeleportEvent (cn.nukkit.event.player.PlayerTeleportEvent)1 Item (cn.nukkit.item.Item)1 ItemBlock (cn.nukkit.item.ItemBlock)1