Search in sources :

Example 1 with CriticalParticle

use of cn.nukkit.level.particle.CriticalParticle in project Nukkit by Nukkit.

the class Player method attack.

@Override
public boolean attack(EntityDamageEvent source) {
    if (!this.isAlive()) {
        return false;
    }
    if (this.isCreative() && source.getCause() != DamageCause.MAGIC && source.getCause() != DamageCause.SUICIDE && source.getCause() != DamageCause.VOID) {
        // source.setCancelled();
        return false;
    } else if (this.getAdventureSettings().get(Type.ALLOW_FLIGHT) && source.getCause() == DamageCause.FALL) {
        // source.setCancelled();
        return false;
    } else if (source.getCause() == DamageCause.FALL) {
        if (this.getLevel().getBlock(this.getPosition().floor().add(0.5, -1, 0.5)).getId() == Block.SLIME_BLOCK) {
            if (!this.isSneaking()) {
                // source.setCancelled();
                this.resetFallDistance();
                return false;
            }
        }
    }
    if (source instanceof EntityDamageByEntityEvent) {
        Entity damager = ((EntityDamageByEntityEvent) source).getDamager();
        if (damager instanceof Player) {
            ((Player) damager).getFoodData().updateFoodExpLevel(0.3);
        }
        // Critical hit
        boolean add = false;
        if (!damager.onGround) {
            NukkitRandom random = new NukkitRandom();
            for (int i = 0; i < 5; i++) {
                CriticalParticle par = new CriticalParticle(new Vector3(this.x + random.nextRange(-15, 15) / 10, this.y + random.nextRange(0, 20) / 10, this.z + random.nextRange(-15, 15) / 10));
                this.getLevel().addParticle(par);
            }
            add = true;
        }
        if (add)
            source.setDamage((float) (source.getDamage() * 1.5));
    }
    if (super.attack(source)) {
        // !source.isCancelled()
        if (this.getLastDamageCause() == source && this.spawned) {
            this.getFoodData().updateFoodExpLevel(0.3);
            EntityEventPacket pk = new EntityEventPacket();
            pk.eid = this.id;
            pk.event = EntityEventPacket.HURT_ANIMATION;
            this.dataPacket(pk);
        }
        return true;
    } else {
        return false;
    }
}
Also used : BlockEntity(cn.nukkit.blockentity.BlockEntity) EntityDamageByEntityEvent(cn.nukkit.event.entity.EntityDamageByEntityEvent) CriticalParticle(cn.nukkit.level.particle.CriticalParticle)

Aggregations

BlockEntity (cn.nukkit.blockentity.BlockEntity)1 EntityDamageByEntityEvent (cn.nukkit.event.entity.EntityDamageByEntityEvent)1 CriticalParticle (cn.nukkit.level.particle.CriticalParticle)1