Java Code Examples for org.bukkit.event.entity.EntityDamageByEntityEvent#getEntity()

The following examples show how to use org.bukkit.event.entity.EntityDamageByEntityEvent#getEntity() . 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: IslandGuard.java    From askyblock with GNU General Public License v2.0 7 votes vote down vote up
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onItemFrameDamage(final EntityDamageByEntityEvent e) {
    // Check world
    if (!inWorld(e.getEntity()) || !(e.getEntity() instanceof ItemFrame)) {
        return;
    }
    if (e.getDamager() instanceof Projectile) {
        if (DEBUG)
            plugin.getLogger().info("DEBUG: Projectile damage to itemframe");
        // Find out who fired the arrow
        Projectile p = (Projectile) e.getDamager();
        if (DEBUG)
            plugin.getLogger().info("DEBUG: Shooter is " + p.getShooter().toString());
        if (p.getShooter() instanceof Skeleton || p.getShooter() instanceof Golem) {
            if (DEBUG)
                plugin.getLogger().info("DEBUG: Shooter is mob");
            if (!Settings.allowMobDamageToItemFrames) {
                if (DEBUG)
                    plugin.getLogger().info("DEBUG: Damage not allowed, cancelling");
                e.setCancelled(true);
            }
        }
    }
}
 
Example 2
Source File: ListenerLogger.java    From CombatLogX with GNU General Public License v3.0 6 votes vote down vote up
@EventHandler(priority=EventPriority.MONITOR)
public void onCombat(EntityDamageByEntityEvent e) {
    if(isDisabled("log-entity-damage-event")) return;
    String format = getLoggerFormat("entity-damage-event-format");
    if(format == null) return;

    String wasCancelled = Boolean.toString(e.isCancelled());

    Entity damaged = e.getEntity();
    String damagedType = damaged.getType().name();
    String damagedName = getEntityName(damaged);

    Entity damager = e.getDamager();
    String damagerType = damager.getType().name();
    String damagerName = getEntityName(damager);

    String message = format.replace("{damaged_type}", damagedType)
            .replace("{damaged_name}", damagedName).replace("{damager_type}", damagerType)
            .replace("{damager_name}", damagerName).replace("{was_cancelled}", wasCancelled);
    appendLog(message);
}
 
Example 3
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 4
Source File: ProjectileMatchModule.java    From PGM with GNU Affero General Public License v3.0 6 votes vote down vote up
@EventHandler
public void onProjectileHurtEvent(EntityDamageByEntityEvent event) {
  if (!(event.getEntity() instanceof LivingEntity)) return;
  LivingEntity damagedEntity = (LivingEntity) event.getEntity();

  ProjectileDefinition projectileDefinition =
      ProjectileMatchModule.getProjectileDefinition(event.getDamager());

  if (projectileDefinition != null) {
    if (!projectileDefinition.potion.isEmpty()) {
      damagedEntity.addPotionEffects(projectileDefinition.potion);
    }

    if (projectileDefinition.damage != null) {
      event.setDamage(projectileDefinition.damage);
    }
  }
}
 
Example 5
Source File: FlagSpectate.java    From HeavySpleef with GNU General Public License v3.0 6 votes vote down vote up
@EventHandler(priority = EventPriority.HIGH)
public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
    Entity entity = event.getEntity();
    if (!isEntitySpectating(entity)) {
        return;
    }

    boolean enablePvp = config.getSpectateSection().isEnablePvp();
    if (!enablePvp) {
        event.setCancelled(true);
        return;
    }

    Entity damager = event.getDamager();
    if (!(damager instanceof Player)) {
        event.setCancelled(true);
        return;
    }

    event.setCancelled(false);
}
 
Example 6
Source File: NoEntryListener.java    From CombatLogX with GNU General Public License v3.0 6 votes vote down vote up
@EventHandler(priority=EventPriority.HIGHEST)
public void onCancelPVP(EntityDamageByEntityEvent e) {
    if(!e.isCancelled()) return;
    if(this.expansion.getNoEntryHandler().getNoEntryMode() != NoEntryMode.VULNERABLE) return;

    Entity entity = e.getEntity();
    if(!(entity instanceof Player)) return;

    ICombatLogX plugin = this.expansion.getPlugin();
    ICombatManager manager = plugin.getCombatManager();

    Player player = (Player) entity;
    if(!manager.isInCombat(player)) return;

    LivingEntity enemy = manager.getEnemy(player);
    if(enemy == null) return;

    e.setCancelled(false);
    this.expansion.sendNoEntryMessage(player, enemy);
}
 
Example 7
Source File: TitleRespawn.java    From CardinalPGM with MIT License 6 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onEntityDamage(EntityDamageByEntityEvent event) {
    if (!(event.getEntity() instanceof Player)) return;
    Player player = (Player) event.getEntity();
    double finalHealth = player.getHealth() - event.getFinalDamage();
    if (finalHealth > 0.01) return;
    player.setMaxHealth(20);
    player.setHealth(player.getMaxHealth());
    event.setDamage(1);
    player.setLastDamageCause(event);
    if (event.getActor() instanceof Player) {
        killPlayer(player, (Player)event.getActor(), event.getCause());
    } else if (event.getActor() instanceof Projectile && ((Projectile) event.getActor()).getShooter() instanceof Player) {
        killPlayer(player, (Player)((Projectile) event.getActor()).getShooter(), event.getCause());
    } else {
        killPlayer(player, null, event.getCause());
    }
}
 
Example 8
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 9
Source File: Shuffle.java    From ce with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void effect(Event e, ItemStack item, final int level) {
	if(e instanceof EntityDamageByEntityEvent) {
	EntityDamageByEntityEvent event = (EntityDamageByEntityEvent) e;
	Entity target = event.getEntity();
	Player p = (Player) ((Projectile) event.getDamager()).getShooter();
	
	if(target.getEntityId() == p.getEntityId())
		return;
	
	Location pLoc = p.getLocation();
	Location tLoc = target.getLocation();
	
	target.teleport(pLoc);
	p.teleport(tLoc);
	
	EffectManager.playSound(tLoc, "ENTITY_ENDERMAN_TELEPORT", 0.4f, 2f);
	EffectManager.playSound(pLoc, "ENTITY_ENDERMAN_TELEPORT", 0.4f, 2f);

	
	for(int i = 10; i>0; i--) {
		p.getWorld().playEffect(tLoc, Effect.ENDER_SIGNAL, 10);
		p.getWorld().playEffect(pLoc, Effect.ENDER_SIGNAL, 10);
	}
	
	if(target instanceof Player) {
		p.sendMessage(ChatColor.DARK_PURPLE + "You have switched positions with " + target.getName() + "!");
		target.sendMessage(ChatColor.DARK_PURPLE + "You have switched positions with " + p.getName() + "!");
	}
	
	
	}
}
 
Example 10
Source File: Wither.java    From ce with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void effect(Event e, ItemStack item, int level) {
	EntityDamageByEntityEvent event = (EntityDamageByEntityEvent) e;
	LivingEntity target = (LivingEntity) event.getEntity();

	target.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, duration * level, strength + level));

}
 
Example 11
Source File: SentinelTrait.java    From Sentinel with MIT License 5 votes vote down vote up
/**
 * Called when this sentinel attacks something, to correct damage handling.
 */
public void whenAttacksAreHappeningFromMe(EntityDamageByEntityEvent event) {
    if (event.isCancelled()) {
        return;
    }
    if (!npc.isSpawned()) {
        return;
    }
    if (SentinelPlugin.instance.alternateDamage) {
        if (canEnforce) {
            canEnforce = false;
            whenAttacksHappened(event);
            if (!event.isCancelled()) {
                ((LivingEntity) event.getEntity()).damage(event.getFinalDamage());
                if (event.getEntity() instanceof LivingEntity) {
                    weaponHelper.knockback((LivingEntity) event.getEntity());
                }
            }
            if (SentinelPlugin.debugMe) {
                debug("enforce damage value to " + event.getFinalDamage());
            }
        }
        else {
            if (SentinelPlugin.debugMe) {
                debug("refuse damage enforcement");
            }
        }
        event.setDamage(0);
        event.setCancelled(true);
        return;
    }
    event.setDamage(EntityDamageEvent.DamageModifier.BASE, getDamage(false));
}
 
Example 12
Source File: Shielded.java    From ce with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void effect(Event e, ItemStack item, int level) {
    EntityDamageByEntityEvent event = (EntityDamageByEntityEvent) e;
    Player player = (Player) event.getEntity();
    if (getAbsorptionHearts(player) <= 0) {
        setAbsorptionHearts(player, baseStrength + level * strengthPerLevel);
        generateCooldown(player, cooldown);
    }
}
 
Example 13
Source File: ArmorStandListener.java    From skRayFall with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Listener used to generate the ArmorStandDamageEvent.
 *
 * @param evt The initial EntityDamageByEntityEvent event
 *            used to generate the ArmorStandDamageEvent
 */
@EventHandler
public void onArmorStandDamage(EntityDamageByEntityEvent evt) {
    if (evt.getEntity().getType().equals(EntityType.ARMOR_STAND)
            && evt.getDamager().getType().equals(EntityType.PLAYER)) {
        ArmorStandDamageEvent event =
                new ArmorStandDamageEvent((Player) evt.getDamager(), evt.getEntity());
        Bukkit.getPluginManager().callEvent(event);
        if (event.isCancelled()) {
            evt.setCancelled(true);
            return;
        }
    }
}
 
Example 14
Source File: EntityListener.java    From Guilds with MIT License 5 votes vote down vote up
/**
 * Guild / Ally damage handlers
 *
 * @param event handles when damage is done between two players that might be in the same guild or are allies
 */
@EventHandler
public void onEntityDamage(EntityDamageByEntityEvent event) {
    if (!(event.getEntity() instanceof Player) || !(event.getDamager() instanceof Player)) return;
    Player player = (Player) event.getEntity();
    Player damager = (Player) event.getDamager();

    // Make sure that they aren't in a claim that turns off pvpv
    if (settingsManager.getProperty(GuildSettings.RESPECT_WG_PVP_FLAG)) {
        event.setCancelled(ClaimUtils.checkPvpDisabled(player));
        return;
    }

    // Check if they are the same guild
    if (guildHandler.isSameGuild(player, damager)) {
        event.setCancelled(!settingsManager.getProperty(GuildSettings.GUILD_DAMAGE));
        return;
    }

    // Get the challenge object
    GuildChallenge challenge = challengeHandler.getChallenge(player);

    // Check if they are in a challenge
    if (challenge != null) {
        // Check if the challenge has started
        if (challenge.isStarted()) {
            // Cancel the rest of the checks in case they are battling allies
            return;
        }
    }

    if (guildHandler.isAlly(player, damager)) {
        event.setCancelled(!settingsManager.getProperty(GuildSettings.ALLY_DAMAGE));
    }
}
 
Example 15
Source File: FriendlyFire.java    From CardinalPGM with MIT License 5 votes vote down vote up
@EventHandler(priority = EventPriority.LOW)
public void onEntityDamageEvent(EntityDamageByEntityEvent event) {
    if (!enabled) return;
    if (event.getDamager() instanceof Projectile && ((Projectile) event.getDamager()).getShooter() == event.getEntity()){
        event.setCancelled(true);
    }
}
 
Example 16
Source File: ListenerAttack.java    From CombatLogX with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(priority=EventPriority.MONITOR, ignoreCancelled=true)
public void onAttack(EntityDamageByEntityEvent e) {
    Entity damager = linkPet(linkProjectile(e.getDamager()));
    Entity damaged = e.getEntity();

    if (!(damaged instanceof Player)) return;
    Player attacked = (Player) damaged;

    if (!(damager instanceof Player)) return;
    Player attacker = (Player) damager;

    ICombatManager combatManager = this.plugin.getCombatManager();
    combatManager.tag(attacker, attacked, TagType.PLAYER, TagReason.ATTACKER);
    combatManager.tag(attacked, attacker, TagType.PLAYER, TagReason.ATTACKED);
}
 
Example 17
Source File: Blind.java    From ce with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void effect(Event e, ItemStack item, int level) {
    EntityDamageByEntityEvent event = (EntityDamageByEntityEvent) e;
    LivingEntity target = (LivingEntity) event.getEntity();
    EffectManager.playSound(target.getLocation(), "ENTITY_PLAYER_HURT", 1f, 0.4f);
    target.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, duration + 20 * level, 0));

}
 
Example 18
Source File: EntityDamageByEntity.java    From ZombieEscape with GNU General Public License v2.0 4 votes vote down vote up
@EventHandler
public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
    if (event.getEntity().getWorld().equals(Bukkit.getWorlds().get(0))) {
        event.setCancelled(true);
        return;
    }

    if (!(event.getEntity() instanceof Player)) {
        return;
    }

    GameArena gameArena = PLUGIN.getGameArena();

    Player damaged = (Player) event.getEntity();

    if (event.getDamager() instanceof Projectile) {
        if (gameArena.isHuman(damaged)) {
            event.setCancelled(true);
            return;
        }
    }

    if (!(event.getDamager() instanceof Player)) {
        return;
    }

    Player damager = (Player) event.getDamager();

    if (gameArena.isGameRunning()) {
        if (gameArena.isSameTeam(damaged, damager)) {
            event.setCancelled(true);
        } else if (gameArena.isHuman(damaged) && gameArena.isZombie(damager)) {
            if (checkFortify(damaged)) {
                event.setCancelled(true);
                return;
            }

            PlayerInfectedEvent pie = new PlayerInfectedEvent(damaged, InfectReason.ZOMBIE_BITE, damager, false);
            Bukkit.getPluginManager().callEvent(pie);

            if (pie.isCancelled()) {
                event.setCancelled(true);
                return;
            }

            gameArena.addZombie(damaged);

            Profile damagerProfile = PLUGIN.getProfile(damager);
            if (damagerProfile != null) {
                damagerProfile.setHumanKills(damagerProfile.getHumanKills() + 1);
            }

            damaged.getInventory().setHelmet(new ItemStack(Material.WOOL, 1, (short) 5));
            damaged.getInventory().clear();
            damaged.getInventory().setArmorContents(null);
            gameArena.giveKit(damaged);

            Messages.PLAYER_INFECTED_OTHER.broadcast(damager.getName(), damaged.getName());

            if (gameArena.shouldEnd()) {
                gameArena.endGame();
            }
        } else if (gameArena.isNotPlaying(damager) || gameArena.isNotPlaying(damaged)) {
            event.setCancelled(true);
        }
    }
}
 
Example 19
Source File: IslandGuard1_9.java    From askyblock with GNU General Public License v2.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.LOW, ignoreCancelled=true)
public void onLingeringPotionDamage(final EntityDamageByEntityEvent e) {
    if (!IslandGuard.inWorld(e.getEntity().getLocation())) {
        return;
    }
    if (e.getEntity() == null || e.getEntity().getUniqueId() == null) {
        return;
    }
    if (e.getCause().equals(DamageCause.ENTITY_ATTACK) && thrownPotions.containsKey(e.getDamager().getEntityId())) {
        UUID attacker = thrownPotions.get(e.getDamager().getEntityId());
        // Self damage
        if (attacker.equals(e.getEntity().getUniqueId())) {
            return;
        }
        Island island = plugin.getGrid().getIslandAt(e.getEntity().getLocation());
        boolean inNether = false;
        if (e.getEntity().getWorld().equals(ASkyBlock.getNetherWorld())) {
            inNether = true;
        }
        // Monsters being hurt
        if (e.getEntity() instanceof Monster || e.getEntity() instanceof Slime || e.getEntity() instanceof Squid) {
            // Normal island check
            if (island != null && island.getMembers().contains(attacker)) {
                // Members always allowed
                return;
            }
            if (actionAllowed(attacker, e.getEntity().getLocation(), SettingsFlag.HURT_MONSTERS)) {
                return;
            }
            // Not allowed
            e.setCancelled(true);
            return;
        }

        // Mobs being hurt
        if (e.getEntity() instanceof Animals || e.getEntity() instanceof IronGolem || e.getEntity() instanceof Snowman
                || e.getEntity() instanceof Villager) {
            if (island != null && (island.getIgsFlag(SettingsFlag.HURT_MOBS) || island.getMembers().contains(attacker))) {
                return;
            }
            e.setCancelled(true);
            return;
        }

        // Establish whether PVP is allowed or not.
        boolean pvp = false;
        if ((inNether && island != null && island.getIgsFlag(SettingsFlag.NETHER_PVP) || (!inNether && island != null && island.getIgsFlag(SettingsFlag.PVP)))) {
            pvp = true;
        }

        // Players being hurt PvP
        if (e.getEntity() instanceof Player) {
            if (pvp) {
            } else {
                e.setCancelled(true);
            }
        }
    }
}
 
Example 20
Source File: ModifyBowProjectileMatchModule.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void fixEntityDamage(EntityDamageByEntityEvent event) {
    Entity projectile = event.getDamager();
    if(projectile.hasMetadata("customProjectile")) {

        // If the custom projectile replaced an arrow, recreate some effects specific to arrows
        if(projectile.hasMetadata("damage")) {
            boolean critical = projectile.getMetadata("critical").get(0).asBoolean();
            int knockback = projectile.getMetadata("knockback").get(0).asInt();
            double damage = projectile.getMetadata("damage").get(0).asDouble();
            double speed = projectile.getVelocity().length();

            // Reproduce the damage calculation from nms.EntityArrow with the addition of our modifier
            int finalDamage = (int) Math.ceil(speed * damage * this.velocityMod);
            if(critical) {
                finalDamage += random.nextInt(finalDamage / 2 + 2);
            }
            event.setDamage(finalDamage);

            // Flame arrows - target burns for 5 seconds always
            if(projectile.getFireTicks() > 0) {
                event.getEntity().setFireTicks(100);
            }

            // Reproduce the knockback calculation for punch bows
            if(knockback > 0) {
                Vector projectileVelocity = projectile.getVelocity();
                double horizontalSpeed = Math.sqrt(projectileVelocity.getX() * projectileVelocity.getX() +
                                                   projectileVelocity.getZ() * projectileVelocity.getZ());
                Vector velocity = event.getEntity().getVelocity();
                velocity.setX(velocity.getX() + projectileVelocity.getX() * knockback * 0.6 / horizontalSpeed);
                velocity.setY(velocity.getY() + 0.1);
                velocity.setZ(velocity.getZ() + projectileVelocity.getZ() * knockback * 0.6 / horizontalSpeed);
                event.getEntity().setVelocity(velocity);
            }
        }

        // Apply any potion effects attached to the projectile
        if(event.getEntity() instanceof LivingEntity) {
            for(PotionEffect potionEffect : this.potionEffects) {
                ((LivingEntity) event.getEntity()).addPotionEffect(potionEffect);
            }
        }
    }
}