org.bukkit.entity.Damageable Java Examples

The following examples show how to use org.bukkit.entity.Damageable. 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: EffKill.java    From Skript with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void execute(final Event e) {
	for (Entity entity : entities.getArray(e)) {

		if (entity instanceof EnderDragonPart) {
			entity = ((EnderDragonPart) entity).getParent();
		}

		if (entity instanceof Damageable) {
			final boolean creative = entity instanceof Player && ((Player) entity).getGameMode() == GameMode.CREATIVE;
			if (creative) // Set player to survival before applying damage
				((Player) entity).setGameMode(GameMode.SURVIVAL);

			HealthUtils.damage((Damageable) entity, HealthUtils.getMaxHealth((Damageable) entity) * 100); // just to make sure that it really dies >:)

			if (creative) // Set creative player back to creative
				((Player) entity).setGameMode(GameMode.CREATIVE);
		}

		// if everything done so far has failed to kill this thing
		// We also don't want to remove a player as this would remove the player's data from the server.
		if (entity.isValid() && !(entity instanceof Player))
			entity.remove();
		
	}
}
 
Example #2
Source File: HealingShovel.java    From ce with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public boolean effect(Event event, Player player) {
    EntityDamageByEntityEvent e = (EntityDamageByEntityEvent) event;
    if (e.getDamager() == player && e.getEntity() instanceof Player) {
        Player damaged = (Player) e.getEntity();
        e.setDamage(0);
        EffectManager.playSound(damaged.getLocation(), "ENTITY_GENERIC_DRINK", 0.5f, 1f);
        EffectManager.playSound(damaged.getLocation(), "BLOCK_ANVIL_LAND", 0.5f, 2f);
        short currentDur = player.getItemInHand().getDurability();

        if (((Damageable) damaged).getHealth() + Heal <= ((Damageable) damaged).getMaxHealth()) {
            damaged.setHealth(((Damageable) damaged).getHealth() + Heal);
        } else {
            damaged.setHealth(((Damageable) damaged).getMaxHealth());
        }

        if (currentDur + Heal < player.getItemInHand().getType().getMaxDurability()) {
            player.getItemInHand().setDurability((short) (currentDur + Heal));
        } else {
            player.setItemInHand(new ItemStack(Material.AIR, 1));
            EffectManager.playSound(player.getLocation(), "ENTITY_ITEM_BREAK", 0.1f, 0.3f);
        }
        return true;
    }
    return false;
}
 
Example #3
Source File: Bandage.java    From ce with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void heal(final Player p) {
	p.sendMessage(ChatColor.GREEN + "The bandage covers your wounds.");
	p.setMetadata("ce." + getOriginalName(), new FixedMetadataValue(main, ChatColor.RED + "You are already using a bandage!"));
	if(p.hasMetadata("ce.bleed")) 
		p.removeMetadata("ce.bleed", main);
	new BukkitRunnable() {
		int localCounter = TotalHealTime;
		@Override
		public void run() {
			if(!p.isDead() && localCounter != 0) {
				if(((Damageable) p).getHealth() == ((Damageable) p).getMaxHealth() && StopAtFullHealth) {
					p.sendMessage(ChatColor.GREEN + "Your wounds have fully recovered.");
					this.cancel();
				}
				if(((Damageable) p).getHealth() + healBursts <= ((Damageable) p).getMaxHealth())
					p.setHealth(((Damageable) p).getHealth() + healBursts);
				else
					p.setHealth(((Damageable) p).getMaxHealth());
			localCounter--;
			} else {
				p.sendMessage(ChatColor.GREEN + "The bandage has recovered some of your wounds.");
				this.cancel();
			}
		}
	}.runTaskTimer(main, 0l, 20l);
}
 
Example #4
Source File: Deathbringer.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) {
	EntityDamageByEntityEvent event = (EntityDamageByEntityEvent) e;
	Player player = (Player) event.getDamager();
	Entity ent = event.getEntity();
	
	double trueDamage = strength + (level/10); 
	double currentHealth = ((Damageable) ent).getHealth();
	
	if(currentHealth > trueDamage) {
		((Damageable) ent).setHealth(currentHealth-trueDamage);
		EffectManager.playSound(player.getLocation(), "ENTITY_ENDERDRAGON_GROWL", 0.1f, 0.1f);
	}
	

}
 
Example #5
Source File: Vampire.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) {
	EntityDamageByEntityEvent event = (EntityDamageByEntityEvent) e;
	Player damager = (Player) event.getDamager();
	if (!getHasCooldown(damager)) {	
		double heal = (((Damageable) damager).getHealth() + (event.getDamage() / damageHealFraction));
		if ( heal < ((Damageable) damager).getMaxHealth()) 
			damager.setHealth(heal);
		 else 
			damager.setHealth(((Damageable) damager).getMaxHealth());
		int food = (int) (damager.getFoodLevel() + (event.getDamage() / damageHealFraction));
		if ( food < 20) 
			damager.setFoodLevel(food);
		 else 
			damager.setFoodLevel(20);
		EffectManager.playSound(damager.getLocation(), "ENTITY_PLAYER_BURP", 0.4f, 1f);
		generateCooldown(damager, cooldown);
	}
}
 
Example #6
Source File: Tools.java    From ce with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void applyBleed(final Player target, final int bleedDuration) {
    target.sendMessage(ChatColor.RED + "You are Bleeding!");
    target.setMetadata("ce.bleed", new FixedMetadataValue(Main.plugin, null));
    new BukkitRunnable() {

        int seconds = bleedDuration;

        @Override
        public void run() {
            if (seconds >= 0) {
                if (!target.isDead() && target.hasMetadata("ce.bleed")) {
                    target.damage(1 + (((Damageable) target).getHealth() / 15));
                    seconds--;
                } else {
                    target.removeMetadata("ce.bleed", Main.plugin);
                    this.cancel();
                }
            } else {
                target.removeMetadata("ce.bleed", Main.plugin);
                target.sendMessage(ChatColor.GREEN + "You have stopped Bleeding!");
                this.cancel();
            }
        }
    }.runTaskTimer(Main.plugin, 0l, 20l);

}
 
Example #7
Source File: PushbackCharger.java    From QualityArmory with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public boolean shoot(Gun g, Player p, ItemStack i) {

	Location start = p.getEyeLocation().clone();
	Vector go = p.getLocation().getDirection().normalize();
	//go.add(new Vector((Math.random() * 2 * sway) - sway, (Math.random() * 2 * sway) - sway,
	//		(Math.random() * 2 * sway) - sway));
	GunUtil.playShoot(g,  p);

	boolean lookup = (go.getY() > go.getX() && go.getY() > go.getZ());
	boolean lookdown = (-go.getY() > go.getX() && -go.getY() > go.getZ());
	
	double degreeVector = Math.atan2(go.getX(), go.getZ());
	if (degreeVector > Math.PI)
		degreeVector = 2 * Math.PI - degreeVector;
	for (Entity e : p.getNearbyEntities(g.getMaxDistance(), g.getMaxDistance(), g.getMaxDistance())) {
		double dis = e.getLocation().distance(start);
		if (e instanceof Damageable)
			if (e != p && e != p.getVehicle() && e != p.getPassenger()) {
				double degreeEntity = Math.atan2(e.getLocation().getX() - start.getX(),
						e.getLocation().getZ() - start.getZ());
				if (degreeEntity > Math.PI)
					degreeEntity = 2 * Math.PI - degreeEntity;
				
				if ((lookup && e.getLocation().getY() > start.getY()) || (lookdown && e.getLocation().getY() < start.getY()) ||
						
						
						(!lookdown&&!lookup&&Math.max(degreeEntity, degreeVector)
						- Math.min(degreeEntity, degreeVector) < (dis > 10 ? Math.PI / 4 : Math.PI / 2))) {
					
					Vector pushback = new Vector(e.getLocation().getX() - start.getX(),e.getLocation().getY() - start.getY(),
							e.getLocation().getZ() - start.getZ());
					pushback.normalize().multiply(g.getMaxDistance()/(dis));
					e.setVelocity(pushback);
				}
			}
	}
	return false;
}
 
Example #8
Source File: ExplosionHandler.java    From QualityArmory with GNU General Public License v3.0 5 votes vote down vote up
public static void handleAOEExplosion(Entity shooter, Location loc, double damage, double radius) {
	for(Entity e : loc.getWorld().getNearbyEntities(loc, radius, radius, radius)) {
		if(e instanceof Damageable) {
			Damageable d = (Damageable) e;
			d.damage(damage*radius/e.getLocation().distanceSquared(loc), shooter);
		}
	}
}
 
Example #9
Source File: HealthUtils.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
/** Apply damage to an entity
 * @param e Entity to apply damage to
 * @param d Amount of hearts to damage
 */
public static void damage(final Damageable e, final double d) {
	if (d < 0) {
		heal(e, -d);
		return;
	}
	EntityDamageEvent event = new EntityDamageEvent(e, DamageCause.CUSTOM, d * 2);
	Bukkit.getPluginManager().callEvent(event);
	if (event.isCancelled()) return;
	e.damage(event.getDamage());
}
 
Example #10
Source File: HealthUtils.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
/** Heal an entity
 * @param e Entity to heal
 * @param h Amount of hearts to heal
 */
public static void heal(final Damageable e, final double h) {
	if (h < 0) {
		damage(e, -h);
		return;
	}
	setHealth(e, getHealth(e) + h);
}
 
Example #11
Source File: EffCitizenSetMaxHealth.java    From skRayFall with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void execute(Event evt) {
    NPCRegistry registry = CitizensAPI.getNPCRegistry();
    NPC npcH = registry.getById(id.getSingle(evt).intValue());
    Damageable npc = (Damageable) npcH.getEntity();
    npc.setMaxHealth(maxHealth.getSingle(evt).doubleValue());
}
 
Example #12
Source File: HealthUtils.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
/** Get the health of an entity
 * @param e Entity to get health from
 * @return The amount of hearts the entity has left
 */
public static double getHealth(final Damageable e) {
	if (e.isDead())
		return 0;
	return e.getHealth() / 2;
}
 
Example #13
Source File: HealthUtils.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
/** Get the max health an entity has
 * @param e Entity to get max health from
 * @return How many hearts the entity can have at most
 */
public static double getMaxHealth(final Damageable e) {
	AttributeInstance attributeInstance = ((Attributable) e).getAttribute(Attribute.GENERIC_MAX_HEALTH);
	assert attributeInstance != null;
	return attributeInstance.getValue() / 2;
}
 
Example #14
Source File: HealthUtils.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
/** Set the max health an entity can have
 * @param e Entity to set max health for
 * @param health How many hearts the entity can have at most
 */
public static void setMaxHealth(final Damageable e, final double health) {
	AttributeInstance attributeInstance = ((Attributable) e).getAttribute(Attribute.GENERIC_MAX_HEALTH);
	assert attributeInstance != null;
	attributeInstance.setBaseValue(health * 2);
}
 
Example #15
Source File: HealthUtils.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
public static void setDamageCause(final Damageable e, final DamageCause cause) {
	e.setLastDamageCause(new EntityDamageEvent(e, cause, 0)); // Use deprecated way too keep it compatible and create cleaner code
	// Non-deprecated way is really, really bad
}
 
Example #16
Source File: HealthUtils.java    From Skript with GNU General Public License v3.0 2 votes vote down vote up
/** Set the health of an entity
 * @param e Entity to set health for
 * @param health The amount of hearts to set
 */
public static void setHealth(final Damageable e, final double health) {
	e.setHealth(Math2.fit(0, health, getMaxHealth(e)) * 2);
}