Example usage for net.minecraftforge.common ForgeHooks onLivingAttack

List of usage examples for net.minecraftforge.common ForgeHooks onLivingAttack

Introduction

In this page you can find the example usage for net.minecraftforge.common ForgeHooks onLivingAttack.

Prototype

public static boolean onLivingAttack(LivingEntity entity, DamageSource src, float amount) 

Source Link

Usage

From source file:buildcraft.robotics.EntityRobot.java

License:Minecraft Mod Public

@Override
public boolean attackEntityFrom(DamageSource source, float f) {
    // Ignore hits from mobs or when docked.
    Entity src = source.getSourceOfDamage();
    if (src != null && !(src instanceof EntityFallingBlock) && !(src instanceof IMob)
            && currentDockingStation == null) {
        if (ForgeHooks.onLivingAttack(this, source, f)) {
            return false;
        }/*from  www .  j  av  a2s  .  c  om*/

        if (!worldObj.isRemote) {
            hurtTime = maxHurtTime = 10;

            int mul = 2600;
            for (ItemStack s : wearables) {
                if (s.getItem() instanceof ItemArmor) {
                    mul = mul * 2 / (2 + ((ItemArmor) s.getItem()).damageReduceAmount);
                } else {
                    mul *= 0.7;
                }
            }

            int energy = Math.round(f * mul);
            if (battery.getEnergyStored() - energy > 0) {
                battery.setEnergy(battery.getEnergyStored() - energy);
                return true;
            } else {
                onRobotHit(true);
            }
        }
        return true;
    }
    return false;
}

From source file:com.blogspot.jabelarminecraft.magicbeans.entities.EntityGiant.java

License:Open Source License

/**
 * Called when the entity is attacked./*from  w w  w . j  a  v a 2s.  c  om*/
 */
@Override
public boolean attackEntityFrom(DamageSource parDamageSource, float parDamageAmount) {
    damageSource = parDamageSource;
    damageAmount = parDamageAmount;
    entityAttackedBy = damageSource.getEntity();

    //        // DEBUG
    //       System.out.println("EntityGiant attackEntityFrom()");

    if (!ForgeHooks.onLivingAttack(this, damageSource, damageAmount)) // in 1.8 this hook returns opposite of 1.7.10
    {
        // DEBUG
        System.out.println("LivingAttackEvent must have been canceled");
        return false;
    }

    // DEBUG
    System.out.println("OnLivingAttack event was not canceled");

    if (conditionsPreventDamage(damageSource)) {
        //            // DEBUG
        //           System.out.println("There are conditions preventing damage");

        return false;
    }

    wasDamageDoneOutsideResistancePeriod = processDamage(damageSource, damageAmount);

    updateEntityState();

    // process death
    if (getHealth() <= 0.0F) {
        onDeath(damageSource);
        if (entityAttackedBy instanceof EntityPlayer) {
            ((EntityPlayer) entityAttackedBy).addStat(MagicBeans.achievementGiantSlayer, 1);
        }
    }

    playHurtOrDeathSound();

    return wasDamageDoneOutsideResistancePeriod;
}

From source file:com.blogspot.jabelarminecraft.wildanimals.entities.herdanimals.EntityHerdAnimal.java

License:Open Source License

/**
 * Called when the entity is attacked.//  ww  w  .  jav  a2  s  .  c  o m
 */
@Override
public boolean attackEntityFrom(DamageSource par1DamageSource, float parDamageAmount) {
    // allow event cancellation
    if (ForgeHooks.onLivingAttack(this, par1DamageSource, parDamageAmount))
        return false;

    if (isEntityInvulnerable()) {
        return false; // not really "attacked" if invulnerable
    } else {
        if (worldObj.isRemote) // don't process attack on client side
        {
            return false;
        } else // on server side so process attack
        {
            entityToAttack = null;
            resetInLove();
            ;
            entityAge = 0;

            if (getHealth() <= 0.0F) // not really "attacked" if already dead
            {
                return false;
            } else if (par1DamageSource.isFireDamage() && isPotionActive(Potion.fireResistance)) // fire resistance negates fire attack
            {
                return false;
            } else {
                // process case of falling anvil
                if ((par1DamageSource == DamageSource.anvil || par1DamageSource == DamageSource.fallingBlock)
                        && getEquipmentInSlot(4) != null) {
                    getEquipmentInSlot(4).damageItem(
                            (int) (parDamageAmount * 4.0F + rand.nextFloat() * parDamageAmount * 2.0F), this);
                    parDamageAmount *= 0.75F;
                }

                limbSwingAmount = 1.5F;
                isHitWithoutResistance = true;

                // process temporary resistance to damage after last damage
                if (hurtResistantTime > maxHurtResistantTime / 2.0F) // more than half of max resistance time left
                {
                    if (parDamageAmount <= lastDamage) // resist damage that is less than the last damage
                    {
                        return false;
                    }

                    // top up the damage to the larger amount during the resistance period
                    damageEntity(par1DamageSource, parDamageAmount - lastDamage);
                    lastDamage = parDamageAmount;
                    isHitWithoutResistance = false;
                } else // no resistance so normal hit
                {
                    lastDamage = parDamageAmount;
                    prevHealth = getHealth();
                    hurtResistantTime = maxHurtResistantTime; // start the resistance period
                    damageEntity(par1DamageSource, parDamageAmount);
                    hurtTime = maxHurtTime = 10;
                    setRearing(true);
                }

                // process based on what is attacking
                attackedAtYaw = 0.0F;
                Entity entity = par1DamageSource.getEntity();
                if (entity != null) {
                    if (entity instanceof EntityLivingBase) // set revenge on any living entity that attacks
                    {
                        // DEBUG
                        System.out.println("Setting revenge target = " + entity.getClass().getSimpleName());
                        setRevengeTarget((EntityLivingBase) entity);
                        // DEBUG
                        System.out.println("Attack target = " + this.getAITarget().getClass().getSimpleName());
                    }

                    if (entity instanceof EntityPlayer) // identify attacking player or wolf with kill time determination
                    {
                        recentlyHit = 100;
                        attackingPlayer = (EntityPlayer) entity;
                    } else if (entity instanceof EntityWolf) {
                        EntityWolf entitywolf = (EntityWolf) entity;

                        if (entitywolf.isTamed()) {
                            recentlyHit = 100;
                            attackingPlayer = null;
                        }
                    }
                }

                if (isHitWithoutResistance) {
                    worldObj.setEntityState(this, (byte) 2);

                    // process knockback
                    if (par1DamageSource != DamageSource.drown) {
                        setBeenAttacked(); // checks against knockback resistance, really should be merged into knockback() method
                    }

                    if (entity != null) // if damage was done by an entity
                    {
                        double d1 = entity.posX - posX;
                        double d0;

                        for (d0 = entity.posZ - posZ; d1 * d1
                                + d0 * d0 < 1.0E-4D; d0 = (Math.random() - Math.random()) * 0.01D) {
                            d1 = (Math.random() - Math.random()) * 0.01D;
                        }

                        attackedAtYaw = (float) (Math.atan2(d0, d1) * 180.0D / Math.PI) - rotationYaw;
                        knockBack(entity, parDamageAmount, d1, d0);
                    } else // not an entity that caused damage
                    {
                        attackedAtYaw = (int) (Math.random() * 2.0D) * 180;
                    }
                }

                // play sounds for hurt or death
                // isHitWithoutResistance check helps ensure sound is played once and has time to complete
                String s;

                if (getHealth() <= 0.0F) // dead
                {
                    s = getDeathSound();

                    if (isHitWithoutResistance && s != null) {
                        playSound(s, getSoundVolume(), getSoundPitch());
                    }

                    onDeath(par1DamageSource);
                } else // hurt
                {
                    s = getHurtSound();

                    if (isHitWithoutResistance && s != null) {
                        playSound(s, getSoundVolume(), getSoundPitch());
                    }
                }

                return true;
            }
        }
    }
}