Java Code Examples for org.bukkit.event.entity.EntityDamageEvent#getCause()

The following examples show how to use org.bukkit.event.entity.EntityDamageEvent#getCause() . 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: ArrowBlockerListener.java    From BedWars with GNU Lesser General Public License v3.0 6 votes vote down vote up
@EventHandler(priority = EventPriority.HIGH)
public void onDamage(EntityDamageEvent event) {
    Entity entity = event.getEntity();
    if (!(entity instanceof Player)) {
        return;
    }

    Player player = ((Player) entity).getPlayer();

    if (!Main.isPlayerInGame(player)) {
        return;
    }

    GamePlayer gPlayer = Main.getPlayerGameProfile(player);
    Game game = gPlayer.getGame();

    if (gPlayer.isSpectator) {
        return;
    }

    ArrowBlocker arrowBlocker = (ArrowBlocker) game.getFirstActivedSpecialItemOfPlayer(player, ArrowBlocker.class);
    if (arrowBlocker != null && event.getCause() == EntityDamageEvent.DamageCause.PROJECTILE) {
        event.setCancelled(true);
    }
}
 
Example 2
Source File: SentinelTrait.java    From Sentinel with MIT License 6 votes vote down vote up
/**
 * Called when this sentinel gets hurt.
 */
public void whenImHurt(EntityDamageEvent event) {
    if (SentinelPlugin.debugMe) {
        debug("I'm hurt! By " + event.getCause().name() + " for " + event.getFinalDamage() + " hp");
    }
    switch (event.getCause()) {
        case FIRE:
        case FIRE_TICK:
        case LAVA:
        case MELTING:
            if (ticksSinceLastBurn <= 20) {
                event.setDamage(0);
                event.setCancelled(true);
                return;
            }
            ticksSinceLastBurn = 0;
            break;
    }
}
 
Example 3
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 4
Source File: DisableDamage.java    From CardinalPGM with MIT License 6 votes vote down vote up
@EventHandler
public void onEntityDamage(EntityDamageEvent event) {
    if (damageTypes.contains(event.getCause()) && (event.getCause() != DamageCause.BLOCK_EXPLOSION || event.getCause() != DamageCause.ENTITY_EXPLOSION)) {
        event.setCancelled(true);
    } else if (event.getCause() == DamageCause.BLOCK_EXPLOSION || event.getCause() == DamageCause.ENTITY_EXPLOSION) {
        if (event instanceof EntityDamageByEntityEvent) {
            if (event.getEntity() instanceof Player && TntTracker.getWhoPlaced(((EntityDamageByEntityEvent) event).getDamager()) != null) {
                Player player = (Player) event.getEntity();
                UUID source = TntTracker.getWhoPlaced(((EntityDamageByEntityEvent) event).getDamager());
                Match match = GameHandler.getGameHandler().getMatch();
                if (Bukkit.getOfflinePlayer(source).isOnline()) {
                    if (Bukkit.getPlayer(source).equals(player)) {
                        event.setCancelled(!blockExplosionSelf);
                    } else if (Teams.getTeamByPlayer(Bukkit.getPlayer(source)) == Teams.getTeamByPlayer(player)) {
                        event.setCancelled(!blockExplosionAlly);
                    } else if (Teams.getTeamByPlayer(Bukkit.getPlayer(source)) != Teams.getTeamByPlayer(player)) {
                        event.setCancelled(!blockExplosionEnemy);
                    } else {
                        event.setCancelled(!blockExplosionOther);
                    }
                }
            }
        } else if (damageTypes.contains(DamageCause.BLOCK_EXPLOSION)) event.setCancelled(true);
    }
}
 
Example 5
Source File: ListenerFlight.java    From CombatLogX with GNU General Public License v3.0 6 votes vote down vote up
@EventHandler(priority=EventPriority.HIGH, ignoreCancelled=true)
public void onDamage(EntityDamageEvent e) {
    EntityDamageEvent.DamageCause cause = e.getCause();
    if(cause != EntityDamageEvent.DamageCause.FALL) return;

    FileConfiguration config = this.expansion.getConfig("cheat-prevention.yml");
    if(!config.getBoolean("flight.prevent-fall-damage")) return;

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

    Player player = (Player) entity;
    UUID uuid = player.getUniqueId();
    if(!this.preventFallDamage.contains(uuid)) return;
    
    e.setCancelled(true);
    this.preventFallDamage.remove(uuid);
}
 
Example 6
Source File: FlagAnvilSpleef.java    From HeavySpleef with GNU General Public License v3.0 6 votes vote down vote up
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = false)
public void onEntityDamage(EntityDamageEvent e) {
	if (e.getCause() != DamageCause.FALLING_BLOCK) {
		return;
	}
	
	Entity damaged = e.getEntity();
	if (!(damaged instanceof Player)) {
		return;
	}
	
	SpleefPlayer player = getHeavySpleef().getSpleefPlayer(damaged);
	if (!game.isIngame(player)) {
		return;
	}
	
	e.setCancelled(false);
}
 
Example 7
Source File: GrapplingHookListener.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onFallDamage(EntityDamageEvent e) {
    if (!isEnabled()) {
        return;
    }

    if (e.getEntity() instanceof Player && e.getCause() == DamageCause.FALL && invulnerability.remove(e.getEntity().getUniqueId())) {
        e.setCancelled(true);
    }
}
 
Example 8
Source File: MobListener.java    From civcraft with GNU General Public License v2.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.NORMAL)
public void onEntityDamage(EntityDamageEvent event) {
	if (!(event.getEntity() instanceof LivingEntity)) {
		return;
	}
	
	if (!MobLib.isMobLibEntity((LivingEntity) event.getEntity())) {
		return;
	}
	
	switch (event.getCause()) {
	case SUFFOCATION:
		Location loc = event.getEntity().getLocation();
		int y = loc.getWorld().getHighestBlockAt(loc.getBlockX(), loc.getBlockZ()).getY()+4;
		loc.setY(y);
		event.getEntity().teleport(loc);
	case CONTACT:
	case FALL:
	case FIRE:
	case FIRE_TICK:
	case LAVA:
	case MELTING:
	case DROWNING:
	case FALLING_BLOCK:
	case BLOCK_EXPLOSION:
	case ENTITY_EXPLOSION:
	case LIGHTNING:
	case MAGIC:
		event.setCancelled(true);
		break;
	default:
		break;
	}
}
 
Example 9
Source File: Assassin.java    From AnnihilationPro with MIT License 5 votes vote down vote up
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void damageHandler(EntityDamageEvent event) 
{
	if(event.getEntityType() == EntityType.PLAYER)
	{
		AnniPlayer p = AnniPlayer.getPlayer(event.getEntity().getUniqueId());
		if(p != null && p.getKit().equals(this) && p.getData("Cur") != null)
		{
			if(event.getCause() == DamageCause.FALL)
					event.setCancelled(true);
			else
				endLeap((Player)event.getEntity(),p);
		}
	}
}
 
Example 10
Source File: FirelessListener.java    From UhcCore with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onPlayerDamage(EntityDamageEvent e) {
    if (e.getEntity() instanceof Player) {

        EntityDamageEvent.DamageCause cause = e.getCause();

        if (cause.equals(EntityDamageEvent.DamageCause.FIRE) || cause.equals(EntityDamageEvent.DamageCause.FIRE_TICK) || cause.equals(EntityDamageEvent.DamageCause.LAVA)) {
            e.setCancelled(true);
        }
    }
}
 
Example 11
Source File: Scout.java    From AnnihilationPro with MIT License 5 votes vote down vote up
@EventHandler(priority = EventPriority.NORMAL)
public void fallDamage(EntityDamageEvent event)
{
	if(event.getEntity().getType() == EntityType.PLAYER && event.getCause() == DamageCause.FALL)
	{
		Player p = (Player)event.getEntity();
		AnniPlayer pl = AnniPlayer.getPlayer(p.getUniqueId());
		if(pl != null && pl.getKit().equals(this))
		{
			event.setDamage(event.getDamage()/2);
		}
	}
}
 
Example 12
Source File: MeleeSymptom.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void onAttack(EntityDamageEvent event) {
    super.onAttack(event);

    if(event.getCause() == EntityDamageEvent.DamageCause.ENTITY_ATTACK) {
        event.setDamage(modifier.apply(event.getDamage()));
    }
}
 
Example 13
Source File: PetOwnerListener.java    From SonarPet with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerDamage(EntityDamageEvent event) {
    if (event.getEntity() instanceof Player) {
        Player p = (Player) event.getEntity();
        if (event.getCause() == EntityDamageEvent.DamageCause.FALL) {
            IPet pet = EchoPet.getManager().getPet(p);
            if (pet != null && pet.isOwnerRiding()) {
                event.setCancelled(true);
            }
        }
    }
}
 
Example 14
Source File: AntiVoid.java    From HubBasics with GNU Lesser General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onVoidDamage(EntityDamageEvent event) {
    World world = event.getEntity().getWorld();
    if (event.getCause() != EntityDamageEvent.DamageCause.VOID) return;
    if (event.getEntityType() != EntityType.PLAYER) return;
    Section worldConfiguration = getWorldConfiguration(world.getName());
    if (worldConfiguration != null && isEnabledInWorld(world)) {
        HLocation location = HubBasics.getLocationManager().get(worldConfiguration.getString("Warp", null));
        if (location == null) {
            event.getEntity().teleport(event.getEntity().getWorld().getSpawnLocation());
        } else {
            location.teleport((Player) event.getEntity());
        }
    }
}
 
Example 15
Source File: RescuePlatformListener.java    From BedWars with GNU Lesser General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onFallDamage(EntityDamageEvent event) {
    Entity entity = event.getEntity();
    if (event.isCancelled() || !(entity instanceof Player)) {
        return;
    }

    Player player = ((Player) entity).getPlayer();
    if (!Main.isPlayerInGame(player)) {
        return;
    }

    GamePlayer gPlayer = Main.getPlayerGameProfile(player);
    Game game = gPlayer.getGame();
    if (gPlayer.isSpectator) {
        return;
    }

    RescuePlatform rescuePlatform = (RescuePlatform) game.getFirstActivedSpecialItemOfPlayer(player, RescuePlatform.class);
    if (rescuePlatform != null && event.getCause() == EntityDamageEvent.DamageCause.FALL) {
        Block block = player.getLocation().getBlock().getRelative(BlockFace.DOWN);
        if (block != null) {
            if (block.getType() == rescuePlatform.getMaterial()) {
                event.setCancelled(true);
            }
        }
    }
}
 
Example 16
Source File: Poisonous.java    From MineTinker with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGH)
public void onDamage(EntityDamageEvent event) {
	if (event.getCause() != EntityDamageEvent.DamageCause.POISON) return;
	if (!(event.getEntity() instanceof Player)) return;
	if (!this.effectHealsPlayer) return;

	Player player = (Player) event.getEntity();
	if (!player.hasPermission("minetinker.modifiers.poisonous.use")) {
		return;
	}

	boolean hasPoisonous = false;
	ItemStack armor = null;
	for (ItemStack stack : player.getInventory().getArmorContents()) {
		if (stack == null) continue;
		if (modManager.hasMod(stack, this)) {
			hasPoisonous = true;
			armor = stack;
			break;
		}
	}

	if (!hasPoisonous) return;

	double damage = event.getDamage();
	if (damage > 0) {
		event.setDamage(0);
		double health = player.getHealth();
		player.setHealth(Math.min(health + damage, player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue()));
		ChatWriter.logModifier(player, event, this, armor, String.format("Health(%.2f -> %.2f)", health, player.getHealth()));
	}
}
 
Example 17
Source File: Withered.java    From MineTinker with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGH)
public void onDamage(EntityDamageEvent event) {
	if (event.getCause() != EntityDamageEvent.DamageCause.WITHER) return;
	if (!(event.getEntity() instanceof Player)) return;
	if (!this.effectHealsPlayer) return;

	Player player = (Player) event.getEntity();
	if (!player.hasPermission("minetinker.modifiers.withered.use")) {
		return;
	}

	boolean hasWither = false;
	ItemStack armor = null;
	for (ItemStack stack : player.getInventory().getArmorContents()) {
		if (stack == null) continue;
		if (modManager.hasMod(stack, this)) {
			hasWither = true;
			armor = stack;
			break;
		}
	}

	if (!hasWither) return;

	double damage = event.getDamage();
	if (damage > 0) {
		event.setDamage(0);
		double health = player.getHealth();
		player.setHealth(Math.min(health + damage, player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue()));
		ChatWriter.logModifier(player, event, this, armor, String.format("Health(%.2f -> %.2f)", health, player.getHealth()));
	}
}
 
Example 18
Source File: CombatLogTracker.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerDamage(EntityDamageEvent event) {
  if (event.getDamage() <= 0) return;

  if (!(event.getEntity() instanceof Player)) return;
  Player player = (Player) event.getEntity();

  if (player.getGameMode() == GameMode.CREATIVE) return;

  if (player.isDead()) return;

  if (player.getNoDamageTicks() > 0) return;

  if (getResistanceFactor(player) <= 0) return;

  switch (event.getCause()) {
    case ENTITY_EXPLOSION:
    case BLOCK_EXPLOSION:
    case CUSTOM:
    case FALL:
    case FALLING_BLOCK:
    case LIGHTNING:
    case MELTING:
    case SUICIDE:
    case THORNS:
      return; // Skip damage causes that are not particularly likely to be followed by more damage

    case FIRE:
    case FIRE_TICK:
    case LAVA:
      if (hasFireResistance(player)) return;
      break;
  }

  // Record the player's damage with a timestamp
  this.recentDamage.put(player, new Damage(Instant.now(), event));
}
 
Example 19
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 20
Source File: FallTracker.java    From PGM with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Called when a player is damaged in a way that could initiate a Fall, i.e. damage from another
 * entity that causes knockback
 */
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onAttack(final EntityDamageEvent event) {
  // Filter out damage types that don't cause knockback
  switch (event.getCause()) {
    case ENTITY_ATTACK:
    case PROJECTILE:
    case BLOCK_EXPLOSION:
    case ENTITY_EXPLOSION:
    case MAGIC:
    case CUSTOM:
      break;

    default:
      return;
  }

  MatchPlayer victim = match.getParticipant(event.getEntity());
  if (victim == null) return;

  if (this.falls.containsKey(victim)) {
    // A new fall can't be initiated if the victim is already falling
    return;
  }

  Location loc = victim.getBukkit().getLocation();
  boolean isInLava = Materials.isLava(loc);
  boolean isClimbing = Materials.isClimbable(loc);
  boolean isSwimming = Materials.isWater(loc);

  DamageInfo cause = tracker.resolveDamage(event);

  // Note the victim's situation when the attack happened
  FallInfo.From from;
  if (isClimbing) {
    from = FallInfo.From.LADDER;
  } else if (isSwimming) {
    from = FallInfo.From.WATER;
  } else {
    from = FallInfo.From.GROUND;
  }

  FallState fall = new FallState(victim, from, cause);
  this.falls.put(victim, fall);

  fall.isClimbing = isClimbing;
  fall.isSwimming = isSwimming;
  fall.isInLava = isInLava;

  // If the victim is already in the air, immediately confirm that they are falling.
  // Otherwise, the fall will be confirmed when they leave the ground, if it happens
  // within the time window.
  fall.isStarted = !fall.isSupported();

  if (!fall.isStarted) {
    this.scheduleCheckFallTimeout(fall, FallState.MAX_KNOCKBACK_TICKS);
  }

  logger.fine("Attacked " + fall);
}