Java Code Examples for org.bukkit.entity.LivingEntity#getHealth()

The following examples show how to use org.bukkit.entity.LivingEntity#getHealth() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReceivedDamageEvent.java    From StackMob-3 with GNU General Public License v3.0 6 votes vote down vote up
@EventHandler
public void onDamageReceived(EntityDamageEvent event) {
    if(event.getEntity() instanceof LivingEntity){
        if(StackTools.hasValidStackData(event.getEntity())){
            LivingEntity entity = (LivingEntity) event.getEntity();
            if(sm.getCustomConfig().getBoolean("kill-step-damage.enabled")){
                double healthAfter = entity.getHealth() - event.getFinalDamage();
                if(healthAfter <= 0){
                    entity.setMetadata(GlobalValues.LEFTOVER_DAMAGE, new FixedMetadataValue(sm, Math.abs(healthAfter)));
                }
            }

            if(!sm.getCustomConfig().getStringList("multiply-damage-received.cause-blacklist")
                    .contains(event.getCause().toString())) {
                if(event.getCause() == EntityDamageEvent.DamageCause.ENTITY_ATTACK){
                    return;
                }
                int stackSize = StackTools.getSize(entity);
                double extraDamage = event.getDamage() + ((event.getDamage() * (stackSize - 1) * 0.25));
                event.setDamage(extraDamage);
            }
        }
    }
}
 
Example 2
Source File: HealEffect.java    From Civs with GNU General Public License v3.0 6 votes vote down vote up
public boolean meetsRequirement() {
    Object target = getTarget();
    Entity origin = getOrigin();
    if (!(target instanceof LivingEntity)) {
        if (!this.silent && origin instanceof Player) {
            ((Player) origin).sendMessage(ChatColor.RED + Civs.getPrefix() + " target cant't be healed.");
        }
        return false;
    }
    LivingEntity livingEntity = (LivingEntity) target;
    if (livingEntity.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue() - livingEntity.getHealth() < this.heal) {
        if (!this.silent && origin instanceof Player) {
            origin.sendMessage(ChatColor.RED + Civs.getPrefix() + " already has enough health.");
        }
        return false;
    }
    return true;
}
 
Example 3
Source File: PlaceholderReplacer.java    From CombatLogX with GNU General Public License v3.0 6 votes vote down vote up
public static String getEnemyHearts(ICombatLogX plugin, Player player) {
    ICombatManager manager = plugin.getCombatManager();
    LivingEntity entity = manager.getEnemy(player);
    if(entity == null) return plugin.getLanguageMessageColored("placeholders.unknown-enemy");

    double health = entity.getHealth();
    double hearts = Math.ceil(health / 2.0D);
    long heartsLong = Double.valueOf(hearts).longValue();
    
    StringBuilder builder = new StringBuilder("&4");
    String heartSymbol = "\u2764";
    while(heartsLong > 0) {
        builder.append(heartSymbol);
        heartsLong--;
    }

    return MessageUtil.color(builder.toString());
}
 
Example 4
Source File: Healing.java    From ce with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void effect(Event e, ItemStack item, int level) {
	if(e instanceof EntityDamageByEntityEvent) {
	EntityDamageByEntityEvent event = (EntityDamageByEntityEvent) e;
	LivingEntity target = (LivingEntity) event.getEntity();
	

	double newHealth = target.getHealth()+event.getDamage() + level;
	
	if(newHealth >= target.getMaxHealth())
		newHealth = target.getMaxHealth();
	target.setHealth(newHealth);
	event.setDamage(0);
	target.getWorld().playEffect(target.getLocation(), Effect.POTION_BREAK, 10);
	}
}
 
Example 5
Source File: PlaceholderReplacer.java    From CombatLogX with GNU General Public License v3.0 5 votes vote down vote up
public static String getEnemyHealth(ICombatLogX plugin, Player player) {
    ICombatManager manager = plugin.getCombatManager();
    LivingEntity entity = manager.getEnemy(player);
    if(entity == null) return plugin.getLanguageMessageColored("placeholders.unknown-enemy");

    double health = entity.getHealth();
    DecimalFormat format = new DecimalFormat("0.00");
    return format.format(health);
}
 
Example 6
Source File: PlaceholderReplacer.java    From CombatLogX with GNU General Public License v3.0 5 votes vote down vote up
public static String getEnemyHealthRounded(ICombatLogX plugin, Player player) {
    ICombatManager manager = plugin.getCombatManager();
    LivingEntity entity = manager.getEnemy(player);
    if(entity == null) return plugin.getLanguageMessageColored("placeholders.unknown-enemy");

    double health = entity.getHealth();
    long rounded = Math.round(health);
    return Long.toString(rounded);
}
 
Example 7
Source File: EliteMobScanner.java    From EliteMobs with GNU General Public License v3.0 4 votes vote down vote up
public static void scanValidAggressiveLivingEntity(LivingEntity livingEntity) {

                if (!EntityTracker.isNaturalEntity(livingEntity) &&
                        !ConfigValues.mobCombatSettingsConfig.getBoolean(MobCombatSettingsConfig.STACK_AGGRESSIVE_SPAWNER_MOBS))
                    return;
                if (EntityTracker.isNaturalEntity(livingEntity) &&
                        !ConfigValues.mobCombatSettingsConfig.getBoolean(MobCombatSettingsConfig.STACK_AGGRESSIVE_NATURAL_MOBS))
                    return;

                for (Entity secondEntity : livingEntity.getNearbyEntities(aggressiveRange, aggressiveRange, aggressiveRange)) {

                    if (!secondEntity.getType().equals(livingEntity.getType())) continue;
                    if (!livingEntity.isValid() || !secondEntity.isValid()) continue;

                    if (!EntityTracker.isNaturalEntity(secondEntity) &&
                            !ConfigValues.mobCombatSettingsConfig.getBoolean(MobCombatSettingsConfig.STACK_AGGRESSIVE_SPAWNER_MOBS))
                        return;
                    if (EntityTracker.isNaturalEntity(secondEntity) &&
                            !ConfigValues.mobCombatSettingsConfig.getBoolean(MobCombatSettingsConfig.STACK_AGGRESSIVE_NATURAL_MOBS))
                        return;


                    EliteMobEntity eliteMobEntity1 = null;
                    EliteMobEntity eliteMobEntity2 = null;

                    int level1 = 1;
                    int level2 = 1;

                    if (EntityTracker.isEliteMob(livingEntity)) {
                        eliteMobEntity1 = EntityTracker.getEliteMobEntity(livingEntity);
                        if (!eliteMobEntity1.canStack()) continue;
                        level1 = eliteMobEntity1.getLevel();
                    }

                    if (EntityTracker.isEliteMob(secondEntity)) {
                        eliteMobEntity2 = EntityTracker.getEliteMobEntity(secondEntity);
                        if (!eliteMobEntity2.canStack()) continue;
                        level2 = eliteMobEntity2.getLevel();
                    }

                    int newLevel = level1 + level2;

                    if (newLevel > ConfigValues.mobCombatSettingsConfig.getInt(MobCombatSettingsConfig.ELITEMOB_STACKING_CAP))
                        return;

                    double healthPercentage = (livingEntity.getHealth() / livingEntity.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue() +
                            ((LivingEntity) secondEntity).getHealth() / ((LivingEntity) secondEntity).getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue()) / 2;

                    /*
                    This is a bit of a dirty hack
                    It won't register the entity twice, but it will overwrite the existing method
                    */
                    eliteMobEntity1 = new EliteMobEntity(livingEntity, newLevel, healthPercentage, CreatureSpawnEvent.SpawnReason.CUSTOM);
                    secondEntity.remove();

                }

    }
 
Example 8
Source File: BossSpecialAttackDamage.java    From EliteMobs with GNU General Public License v3.0 4 votes vote down vote up
public static boolean dealSpecialDamage(LivingEntity damager, LivingEntity damagee, double damage) {

        if (damagee.isInvulnerable() || damagee.getHealth() <= 0) return false;

        if (damagee instanceof Player) {

            if (!(((Player) damagee).getGameMode().equals(GameMode.SURVIVAL) ||
                    ((Player) damagee).getGameMode().equals(GameMode.ADVENTURE))) return false;

        }

        damagee.damage(damage);
        damagee.setNoDamageTicks(0);

        if (damagee instanceof Player && damagee.getHealth() <= 0) {

            PlayerDeathMessageByEliteMob.intializeDeathMessage((Player) damagee, damager);

        }

        return true;

    }
 
Example 9
Source File: IndicatorListener.java    From HoloAPI with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onDamage(EntityDamageEvent event) {

    // Check if damage indicators are enabled
    if (!Settings.INDICATOR_ENABLE.getValue("damage")) {
        return;
    }

    // Don't show damage indicators for void damage
    if (event.getCause() == EntityDamageEvent.DamageCause.VOID) {
        return;
    }

    // Make sure that indicators are enabled for this entity type
    if (event.getEntity().getType() == EntityType.PLAYER) {
        if (!Settings.INDICATOR_SHOW_FOR_PLAYERS.getValue("damage")) {
            return;
        }
    } else if (event.getEntity() instanceof LivingEntity) {
        if (!Settings.INDICATOR_SHOW_FOR_MOBS.getValue("damage")) {
            return;
        }
    } else {
        return; // We only show indicators for players and mobs.
    }

    final LivingEntity entity = (LivingEntity) event.getEntity();
    if (entity.getNoDamageTicks() > entity.getMaximumNoDamageTicks() / 2.0F) {
        return;
    }


    String damagePrefix = Settings.INDICATOR_FORMAT.getValue("damage", "default");

    // Get our DamageCause-specific damagePrefix, if any
    if (SUPPORTED_DAMAGE_TYPES.contains(event.getCause())) {
        String type = event.getCause().toString().toLowerCase();
        if (event.getCause() == EntityDamageEvent.DamageCause.LAVA) {
            type = "fire";
        }
        damagePrefix = Settings.INDICATOR_FORMAT.getValue("damage", type);
        if (!Settings.INDICATOR_ENABLE_TYPE.getValue("damage", type)) {
            return; // This type of indicator is disabled
        }
    }


    // Build the message prefix and suffix (i.e. the portions without the damage)
    final String indicatorPrefix = damagePrefix + "-";
    final String indicatorSuffix = " " + HEART_CHARACTER;

    final double healthBefore = entity.getHealth();
    Bukkit.getScheduler().runTask(HoloAPI.getCore(), new Runnable() {
        @Override
        public void run() {
            double damageTaken = healthBefore - entity.getHealth();
            if (damageTaken > 0) {
                // Round to the nearest .5
                damageTaken = Math.round(damageTaken * 2.0D) / 2.0D;

                String text = indicatorPrefix + DAMAGE_FORMAT.format(damageTaken) + indicatorSuffix;
                Location loc = entity.getLocation();
                loc.setY(loc.getY() + Settings.INDICATOR_Y_OFFSET.getValue("damage"));
                HoloAPI.getManager().createSimpleHologram(loc, Settings.INDICATOR_TIME_VISIBLE.getValue("damage"), true, text);
            }
        }
    });
}
 
Example 10
Source File: HealthDisplay.java    From EliteMobs with GNU General Public License v3.0 2 votes vote down vote up
public static void displayHealth(LivingEntity livingEntity, double damage) {

        if (!ConfigValues.mobCombatSettingsConfig.getBoolean(MobCombatSettingsConfig.DISPLAY_HEALTH_ON_HIT)) return;

        int maxHealth = (int) livingEntity.getMaxHealth();
        int currentHealth = (int) (livingEntity.getHealth() - damage);

        Location entityLocation = new Location(livingEntity.getWorld(), livingEntity.getLocation().getX(),
                livingEntity.getLocation().getY() + livingEntity.getEyeHeight() + 0.5, livingEntity.getLocation().getZ());

        /*
        Dirty fix: armorstands don't render invisibly on their first tick, so it gets moved elsewhere temporarily
         */
        ArmorStand armorStand = (ArmorStand) entityLocation.getWorld().spawnEntity(entityLocation.add(new Vector(0, -50, 0)), EntityType.ARMOR_STAND);

        armorStand.setVisible(false);
        armorStand.setMarker(true);
        armorStand.setCustomName(setHealthColor(currentHealth, maxHealth) + "" + currentHealth + "/" + maxHealth);
        armorStand.setGravity(false);
        EntityTracker.registerArmorStands(armorStand);
        armorStand.setCustomNameVisible(false);


        new BukkitRunnable() {

            int taskTimer = 0;

            @Override
            public void run() {

                Location newLocation = new Location(livingEntity.getWorld(), livingEntity.getLocation().getX(),
                        livingEntity.getLocation().getY() + livingEntity.getEyeHeight() + 0.5, livingEntity.getLocation().getZ());

                armorStand.teleport(newLocation);

                if (taskTimer == 1)
                    armorStand.setCustomNameVisible(true);

                taskTimer++;

                if (taskTimer > 15) {

                    EntityTracker.unregisterArmorStand(armorStand);
                    cancel();

                }

            }

        }.runTaskTimer(Bukkit.getPluginManager().getPlugin(MetadataHandler.ELITE_MOBS), 0, 1L);

    }