Exemplo n.º 1
0
  @EventHandler(priority = EventPriority.HIGH)
  public void onEntityDamage(EntityDamageEvent event_) {
    // If the event is cancelled, then we want to return.
    if (event_.isCancelled() == true) return;

    // Block damage in creative world.
    WorldConfig wm = new WorldConfig();

    if (!wm.getIsRpg(event_.getEntity().getWorld().getName())) return;

    // Store event.
    event = event_;

    // Set defaults.
    setGlobalDefaults();

    // Always set event damage to 0.
    event.setDamage(0);

    boolean dealEnviromentalDamage = false;

    // For entity damage events we want to handle the attck normally.
    if (event instanceof EntityDamageByEntityEvent) {
      Entity damager = ((EntityDamageByEntityEvent) event).getDamager();

      if (damager.getType() == EntityType.WITHER) dealEnviromentalDamage = true;
      else if (damager.getType() == EntityType.WITHER_SKULL) dealEnviromentalDamage = true;
      else if (damager.getType() == EntityType.SPLASH_POTION) dealEnviromentalDamage = true;
      else if (damager.getType() == EntityType.PRIMED_TNT) dealEnviromentalDamage = true;
      else entityAttack((EntityDamageByEntityEvent) event, -1);
    } else dealEnviromentalDamage = true;

    // Else we want to deal the damage to the player/mob as though not from an entity.
    if (dealEnviromentalDamage == true) {
      // If the defender is a player, then we attacker player defender.
      if (event.getEntity() instanceof Player) {
        Player player = (Player) event.getEntity();

        // Prevent damage done to already dead players
        if (FC_Rpg.rpgEntityManager.getRpgPlayer(player).playerConfig.getIsActive() == true) {
          if (FC_Rpg.rpgEntityManager.getRpgPlayer(player).getIsAlive() == false) return;
        }

        // Prepare the defender.
        prepareDefender(player);

        double damage = getEnviromentalDamage(player);

        if (damage == 0) return;

        // Attack the player with enviromental damage.
        edm.attackPlayerDefender(
            rpgDefender, null, null, damage, damageType, event.getCause().toString());
      } else {
        // If the event entity is a livingentity and the damage isn't fall damage, then nuke.
        if (event.getCause() != DamageCause.FALL && event.getEntity() instanceof LivingEntity)
          edm.nukeMob((LivingEntity) event.getEntity());
      }
    }
  }
Exemplo n.º 2
0
  // -1, false, false are defaults, attackmob and attack player are only processed when damage = -1.
  public void entityAttack(EntityDamageByEntityEvent ed, double customDamage) {
    double damage = 0;

    event = ed;

    // Reset all globals.
    setGlobalDefaults();

    // Set event damage to 0.
    ed.setDamage(0);

    LivingEntity entity;
    Entity eEntity = ed.getEntity();

    if (eEntity instanceof Arrow) {
      Arrow ca = (Arrow) eEntity;
      entity = ca.getShooter();
    } else if (eEntity instanceof LargeFireball) {
      LargeFireball cf = (LargeFireball) eEntity;
      entity = cf.getShooter();
    } else if (eEntity instanceof ExperienceOrb) {
      return;
    } else if (eEntity instanceof Item) {
      return;
    } else {
      // Set entity equal to the entity that got hit.
      try {
        entity = (LivingEntity) ed.getEntity();
      } catch (ClassCastException e) {
        FC_Rpg.plugin
            .getLogger()
            .info(
                "Failed to cast an entity to living entity, damage cancelled -> "
                    + eEntity.toString()
                    + " <- report to a FC_Rpg developer via a ticket please.");
        return;
      }
    }

    // Prepare the defender and attacker variables.
    prepareDefender(entity);

    // If a damage was passed in, then we don't want to set damage. Else, store new damage
    damage = prepareAttacker(ed);

    // When two players are fighting, then...
    boolean isPvp = false;

    // set whether in pvp or not.
    if (playerAttacker != null && playerDefender != null) isPvp = true;

    if (isPvp == true && cancelRpgDamage == true) {
      MessageLib msgLib = new MessageLib(playerAttacker);
      msgLib.standardMessage("This player can't be attacked currently.");
      return;
    }

    // Check player tiers.
    if (isPvp == true) {
      int attackerTier = getTier(rpgAttacker.playerConfig.getClassLevel());
      int defenderTier = getTier(rpgDefender.playerConfig.getClassLevel());

      if (attackerTier != defenderTier) {
        MessageLib msgLib = new MessageLib(playerAttacker);
        msgLib.infiniteMessage(
            "You can't fight this target because they are in tier ",
            defenderTier + "",
            " and you are in tier ",
            attackerTier + "",
            "!");
        return;
      }
    }

    // If we are cancelling rpg damage, then return.
    if (cancelRpgDamage == true) return;

    if (customDamage > -1) damage = customDamage;

    /**
     * *********************************************************
     *
     * <p>SET THE BASE DAMAGE BASED ON IF A PLAYER, MOB, OR ENVIROMENT CAUSED DAMAGE
     *
     * <p>*********************************************************
     */

    // We do a sword check for atttackers.
    if (rpgAttacker != null) {
      // Check the attackers sword.
      rpgAttacker.swordAttackRequirementCheck();

      // We handle spells on non-fireball player attacks.
      if (damageType != 2) {
        if (rpgAttacker.playerConfig.getAutoCast() == true) rpgAttacker.prepareSpell(false);

        double spellDamage = -1;

        // Attempt to cast a spell.
        if (rpgMobDefender != null)
          spellDamage = rpgAttacker.castSpell(rpgMobDefender.getEntity(), damage, damageType);
        else spellDamage = rpgAttacker.castSpell(rpgDefender.getPlayer(), damage, damageType);

        // If the spell failed to cast, then we want to do spells/enchantment stuff.
        if (spellDamage == -1) {
          // Cast auto spells/enchantment stuff.
          if (rpgMobDefender != null)
            spellDamage = rpgAttacker.autocastOffense(rpgMobDefender.getEntity(), damage);
          else spellDamage = rpgAttacker.autocastOffense(rpgDefender.getPlayer(), damage);
        }

        // If the spell damage isn't -1, then we want to store the spell damage.
        if (spellDamage != -1) damage = spellDamage;
      }
    }

    // Apply randomization to damage.
    damage = getRandomDamageModifier(damage);

    // We do a armor check for defenders.
    if (rpgDefender != null) {
      rpgDefender.fullArmorCheck();

      // Cast armor enchantment stuff.
      rpgDefender.autocastDefense();
    }

    if (rpgMobDefender != null) {
      // Prevent mobs from damaging friendly mobs.
      if (rpgMobAttacker != null) return;

      // If no player attacked, then we want to nuke the mob (enviromental damage).
      if (rpgAttacker == null) {
        edm.nukeMob(rpgMobDefender.getEntity());
        return;
      }

      // Attack the mob defender
      if (edm.attackMobDefender(rpgMobDefender, rpgAttacker, damage, damageType) == false) {
        MessageLib msgLib = new MessageLib(playerAttacker);
        msgLib.standardMessage("Attack Missed.");
      }

      // Make creatures forcibly attack attacker.
      if (playerAttacker != null && mobDefender != null) {
        // Set wolves to angry.
        if (mobDefender instanceof Wolf) ((Wolf) mobDefender).setAngry(true);

        if (mobDefender instanceof Creature) {
          // Change aggro
          ((Creature) mobDefender).setTarget(playerAttacker);
        }
      }
    } else if (rpgDefender != null) {
      // Attack the player Defender.
      if (rpgAttacker != null) {
        if (edm.attackPlayerDefender(
                rpgDefender,
                rpgAttacker,
                null,
                damage,
                damageType,
                rpgAttacker.getPlayer().getName().substring(0, nameTrimEnd))
            == false) {
          MessageLib msgLib = new MessageLib(playerAttacker);
          msgLib.standardMessage("Attack Missed.");
        }
      } else if (rpgMobAttacker != null)
        edm.attackPlayerDefender(
            rpgDefender,
            null,
            rpgMobAttacker,
            damage,
            damageType,
            rpgMobAttacker.getEntity().getType().toString());
    }
  }