Ejemplo n.º 1
0
  @SubscribeEvent
  public void onAttackEntity(AttackEntityEvent event) {
    if (event.entityLiving.worldObj.isRemote) return;

    EntityLivingBase attacker = event.entityLiving;
    EntityPlayer player = event.entityPlayer;
    Entity target = event.target;
    ItemStack stack = attacker.getEquipmentInSlot(0);
    if (stack != null && stack.getItem().onLeftClickEntity(stack, player, target)) return;

    if (target.canAttackWithItem()) {
      if (!target.hitByEntity(target)) {
        float damageAmount = TFC_MobData.STEVE_DAMAGE;
        if (stack != null) {
          damageAmount =
              (float)
                  player
                      .getEntityAttribute(SharedMonsterAttributes.attackDamage)
                      .getAttributeValue();
          // player.addChatMessage("Damage: " + i);
          if (damageAmount == 1.0f) {
            damageAmount = TFC_MobData.STEVE_DAMAGE;
            // i = player.inventory.getCurrentItem().getItem().getDamageVsEntity(target,
            // player.inventory.getCurrentItem());
          }
        }

        if (player.isPotionActive(Potion.damageBoost))
          damageAmount += 3 << player.getActivePotionEffect(Potion.damageBoost).getAmplifier();

        if (player.isPotionActive(Potion.weakness))
          damageAmount -= 2 << player.getActivePotionEffect(Potion.weakness).getAmplifier();

        int knockback = 0;
        float enchantmentDamage = 0;

        if (target instanceof EntityLiving) {
          enchantmentDamage =
              EnchantmentHelper.getEnchantmentModifierLiving(player, (EntityLiving) target);
          knockback += EnchantmentHelper.getKnockbackModifier(player, (EntityLiving) target);
        }

        if (player.isSprinting()) ++knockback;

        if (damageAmount > 0 || enchantmentDamage > 0) {
          boolean criticalHit =
              player.fallDistance > 0.0F
                  && !player.onGround
                  && !player.isOnLadder()
                  && !player.isInWater()
                  && !player.isPotionActive(Potion.blindness)
                  && player.ridingEntity == null
                  && target instanceof EntityLiving;

          if (criticalHit && damageAmount > 0)
            damageAmount += event.entity.worldObj.rand.nextInt((int) (damageAmount / 2 + 2));

          damageAmount += enchantmentDamage;
          boolean onFire = false;
          int fireAspect = EnchantmentHelper.getFireAspectModifier(player);

          if (target instanceof EntityLiving && fireAspect > 0 && !target.isBurning()) {
            onFire = true;
            target.setFire(1);
          }

          boolean entityAttacked =
              target.attackEntityFrom(DamageSource.causePlayerDamage(player), damageAmount);

          if (entityAttacked) {
            if (knockback > 0) {
              target.addVelocity(
                  -MathHelper.sin(player.rotationYaw * (float) Math.PI / 180.0F) * knockback * 0.5F,
                  0.1D,
                  MathHelper.cos(player.rotationYaw * (float) Math.PI / 180.0F) * knockback * 0.5F);
              player.motionX *= 0.6D;
              player.motionZ *= 0.6D;
              player.setSprinting(false);
            }

            if (criticalHit) player.onCriticalHit(target);

            if (enchantmentDamage > 0) player.onEnchantmentCritical(target);

            if (damageAmount >= 18) player.triggerAchievement(AchievementList.overkill);

            player.setLastAttacker(target);

            if (target instanceof EntityLiving)
              target.attackEntityFrom(DamageSource.causeThornsDamage(attacker), damageAmount);
          }

          ItemStack itemstack = player.getCurrentEquippedItem();
          Object object = target;

          if (target instanceof EntityDragonPart) {
            IEntityMultiPart ientitymultipart = ((EntityDragonPart) target).entityDragonObj;
            if (ientitymultipart instanceof EntityLiving) object = ientitymultipart;
          }

          if (itemstack != null && object instanceof EntityLiving) {
            itemstack.hitEntity((EntityLiving) object, player);
            if (itemstack.stackSize <= 0) player.destroyCurrentEquippedItem();
          }

          if (target instanceof EntityLivingBase) {
            player.addStat(StatList.damageDealtStat, Math.round(damageAmount * 10.0f));
            if (fireAspect > 0 && entityAttacked) target.setFire(fireAspect * 4);
            else if (onFire) target.extinguish();
          }

          player.addExhaustion(0.3F);
        }
      }
    }
    event.setCanceled(true);
  }
  /**
   * @author gabizou - April 8th, 2016
   * @reason Rewrites the attackTargetEntityWithCurrentItem to throw an {@link AttackEntityEvent}
   *     prior to the ensuing {@link DamageEntityEvent}. This should cover all cases where players
   *     are attacking entities and those entities override {@link
   *     EntityLivingBase#attackEntityFrom(DamageSource, float)} and effectively bypass our damage
   *     event hooks.
   * @param targetEntity The target entity
   */
  @Overwrite
  public void attackTargetEntityWithCurrentItem(net.minecraft.entity.Entity targetEntity) {
    // Sponge Start - Add SpongeImpl hook to override in forge as necessary
    if (!SpongeImplHooks.checkAttackEntity((EntityPlayer) (Object) this, targetEntity)) {
      return;
    }
    // Sponge End
    if (targetEntity.canAttackWithItem()) {
      if (!targetEntity.hitByEntity((EntityPlayer) (Object) this)) {
        // Sponge Start - Prepare our event values
        // float baseDamage =
        // this.getEntityAttribute(SharedMonsterAttributes.attackDamage).getAttributeValue();
        final double originalBaseDamage =
            this.getEntityAttribute(SharedMonsterAttributes.attackDamage).getAttributeValue();
        float baseDamage = (float) originalBaseDamage;
        // Sponge End
        int knockbackModifier = 0;
        float enchantmentModifierAmount = 0.0F;

        // Sponge Start - gather the attack modifiers
        final List<Tuple<DamageModifier, Function<? super Double, Double>>> originalFunctions =
            new ArrayList<>();

        final EnumCreatureAttribute creatureAttribute =
            targetEntity instanceof EntityLivingBase
                ? ((EntityLivingBase) targetEntity).getCreatureAttribute()
                : EnumCreatureAttribute.UNDEFINED;
        final List<Tuple<DamageModifier, Function<? super Double, Double>>>
            enchantmentModifierFunctions =
                DamageEventHandler.createAttackEnchamntmentFunction(
                    this.getHeldItem(), creatureAttribute);
        // if (targetEntity instanceof EntityLivingBase) {
        //     enchantmentModifierAmount =
        // EnchantmentHelper.getModifierForCreature(this.getHeldItem(), creatureAttribute);
        // } else {
        //     enchantmentModifierAmount =
        // EnchantmentHelper.getModifierForCreature(this.getHeldItem(),
        // EnumCreatureAttribute.UNDEFINED);
        // }
        enchantmentModifierAmount =
            (float)
                enchantmentModifierFunctions
                    .stream()
                    .map(Tuple::getSecond)
                    .mapToDouble(function -> function.apply(originalBaseDamage))
                    .sum();
        originalFunctions.addAll(enchantmentModifierFunctions);
        // Sponge End

        knockbackModifier =
            knockbackModifier
                + EnchantmentHelper.getKnockbackModifier((EntityPlayer) (Object) this);

        if (this.isSprinting()) {
          ++knockbackModifier;
        }

        if (baseDamage > 0.0F || enchantmentModifierAmount > 0.0F) {
          boolean fallingCriticalHit =
              this.fallDistance > 0.0F
                  && !this.onGround
                  && !this.isOnLadder()
                  && !this.isInWater()
                  && !this.isPotionActive(Potion.blindness)
                  && this.ridingEntity == null
                  && targetEntity instanceof EntityLivingBase;

          if (fallingCriticalHit && baseDamage > 0.0F) {
            // Sponge - Add the function for critical attacking
            originalFunctions.add(
                DamageEventHandler.provideCriticalAttackTuple((EntityPlayer) (Object) this));
            // baseDamage *= 1.5F; Sponge - remove since it's handled in the event
          }

          // baseDamage = baseDamage + enchantmentModifierAmount; // Sponge - remove since it is
          // delegated through the event.
          boolean targetLitOnFire = false;
          int fireAspectLevel =
              EnchantmentHelper.getFireAspectModifier((EntityPlayer) (Object) this);

          if (targetEntity instanceof EntityLivingBase
              && fireAspectLevel > 0
              && !targetEntity.isBurning()) {
            targetLitOnFire = true;
            targetEntity.setFire(1);
          }

          double targetMotionX = targetEntity.motionX;
          double targetMotionY = targetEntity.motionY;
          double targetMotionZ = targetEntity.motionZ;

          // Sponge Start - Create the event and throw it
          final DamageSource damageSource =
              DamageSource.causePlayerDamage((EntityPlayer) (Object) this);
          final AttackEntityEvent event =
              SpongeEventFactory.createAttackEntityEvent(
                  Cause.source(damageSource).build(),
                  originalFunctions,
                  EntityUtil.fromNative(targetEntity),
                  knockbackModifier,
                  originalBaseDamage);
          SpongeImpl.postEvent(event);
          if (event.isCancelled()) {
            if (targetLitOnFire) {
              targetEntity.extinguish();
            }
            return;
          }
          baseDamage = (float) event.getFinalOutputDamage();
          knockbackModifier = event.getKnockbackModifier();
          boolean attackSucceded =
              targetEntity.attackEntityFrom(damageSource, (float) event.getFinalOutputDamage());
          // Sponge End
          if (attackSucceded) {
            if (knockbackModifier > 0) {
              targetEntity.addVelocity(
                  (double)
                      (-MathHelper.sin(this.rotationYaw * (float) Math.PI / 180.0F)
                          * (float) knockbackModifier
                          * 0.5F),
                  0.1D,
                  (double)
                      (MathHelper.cos(this.rotationYaw * (float) Math.PI / 180.0F)
                          * (float) knockbackModifier
                          * 0.5F));
              this.motionX *= 0.6D;
              this.motionZ *= 0.6D;
              this.setSprinting(false);
            }

            if (targetEntity instanceof EntityPlayerMP && targetEntity.velocityChanged) {
              ((EntityPlayerMP) targetEntity)
                  .playerNetServerHandler.sendPacket(new S12PacketEntityVelocity(targetEntity));
              targetEntity.velocityChanged = false;
              targetEntity.motionX = targetMotionX;
              targetEntity.motionY = targetMotionY;
              targetEntity.motionZ = targetMotionZ;
            }

            if (fallingCriticalHit) {
              this.onCriticalHit(targetEntity);
            }

            if (enchantmentModifierAmount > 0.0F) {
              this.onEnchantmentCritical(targetEntity);
            }

            if (baseDamage >= 18.0F) {
              this.triggerAchievement(AchievementList.overkill);
            }

            this.setLastAttacker(targetEntity);

            if (targetEntity instanceof EntityLivingBase) {
              EnchantmentHelper.applyThornEnchantments(
                  (EntityLivingBase) targetEntity, (EntityPlayer) (Object) this);
            }

            EnchantmentHelper.applyArthropodEnchantments(
                (EntityPlayer) (Object) this, targetEntity);
            ItemStack itemstack = this.getCurrentEquippedItem();
            net.minecraft.entity.Entity entity = targetEntity;

            if (targetEntity instanceof EntityDragonPart) {
              IEntityMultiPart ientitymultipart = ((EntityDragonPart) targetEntity).entityDragonObj;

              if (ientitymultipart instanceof EntityLivingBase) {
                entity = (EntityLivingBase) ientitymultipart;
              }
            }

            if (itemstack != null && entity instanceof EntityLivingBase) {
              itemstack.hitEntity((EntityLivingBase) entity, (EntityPlayer) (Object) this);

              if (itemstack.stackSize <= 0) {
                this.destroyCurrentEquippedItem();
              }
            }

            if (targetEntity instanceof EntityLivingBase) {
              this.addStat(StatList.damageDealtStat, Math.round(baseDamage * 10.0F));

              if (fireAspectLevel > 0) {
                targetEntity.setFire(fireAspectLevel * 4);
              }
            }

            this.addExhaustion(0.3F);
          } else if (targetLitOnFire) {
            targetEntity.extinguish();
          }
        }
      }
    }
  }
Ejemplo n.º 3
0
  @Override
  public boolean onLeftClickEntity(ItemStack stack, EntityPlayer player, Entity entity) {
    if (entity.canAttackWithItem()) {
      if (!entity.hitByEntity(player)) {
        float damage = (float) getAttackDamage(stack);
        int i = 0;
        float extraDamage = 0.0F;
        if (entity instanceof EntityLivingBase) {
          extraDamage =
              EnchantmentHelper.getEnchantmentModifierLiving(player, (EntityLivingBase) entity);
          i += EnchantmentHelper.getKnockbackModifier(player, (EntityLivingBase) entity);
        }
        if (player.isSprinting()) {
          i++;
        }
        if (damage > 0.0F || extraDamage > 0.0F) {
          boolean criricalHit =
              player.fallDistance > 0.0F
                  && !player.onGround
                  && !player.isOnLadder()
                  && !player.isInWater()
                  && !player.isPotionActive(Potion.blindness)
                  && player.ridingEntity == null
                  && entity instanceof EntityLivingBase;
          if (criricalHit && damage > 0.0F) {
            damage *= 1.5F;
          }
          damage += extraDamage;
          boolean fire = false;
          int j = EnchantmentHelper.getFireAspectModifier(player);
          if (entity instanceof EntityLivingBase && j > 0 && !entity.isBurning()) {
            fire = true;
            entity.setFire(1);
          }
          boolean attack = entity.attackEntityFrom(DamageSource.causePlayerDamage(player), damage);
          if (attack) {
            if (i > 0) {
              entity.addVelocity(
                  (double)
                      (-MathHelper.sin(player.rotationYaw * (float) Math.PI / 180.0F)
                          * (float) i
                          * 0.5F),
                  0.1D,
                  (double)
                      (MathHelper.cos(player.rotationYaw * (float) Math.PI / 180.0F)
                          * (float) i
                          * 0.5F));
              player.motionX *= 0.6D;
              player.motionZ *= 0.6D;
              player.setSprinting(false);
            }
            if (criricalHit) {
              player.onCriticalHit(entity);
            }
            if (extraDamage > 0.0F) {
              player.onEnchantmentCritical(entity);
            }
            if (damage >= 18.0F) {
              player.triggerAchievement(AchievementList.overkill);
            }
            player.setLastAttacker(entity);
            if (entity instanceof EntityLivingBase) {
              EnchantmentHelper.func_151384_a((EntityLivingBase) entity, player);
            }
          }

          ItemStack itemstack = player.getCurrentEquippedItem();
          Object object = entity;

          if (entity instanceof EntityDragonPart) {
            IEntityMultiPart entityMultipart = ((EntityDragonPart) entity).entityDragonObj;
            if (entityMultipart != null && entityMultipart instanceof EntityLivingBase) {
              object = (EntityLivingBase) entityMultipart;
            }
          }
          if (itemstack != null && object instanceof EntityLivingBase) {
            itemstack.hitEntity((EntityLivingBase) object, player);
            if (itemstack.stackSize <= 0) {
              player.destroyCurrentEquippedItem();
            }
          }
          if (entity instanceof EntityLivingBase) {
            player.addStat(StatList.damageDealtStat, Math.round(damage * 10.0F));
            if (j > 0 && attack) {
              entity.setFire(j * 4);
            } else if (fire) {
              entity.extinguish();
            }
          }
          player.addExhaustion(0.3F);
        }
      }
    }
    return true;
  }