Search in sources :

Example 1 with DamageType

use of org.spongepowered.api.event.cause.entity.damage.DamageType in project RedProtect by FabioZumbi12.

the class RPPlayerListener method onEntityDamageEvent.

@Listener(order = Order.FIRST, beforeModifications = true)
public void onEntityDamageEvent(DamageEntityEvent e) {
    // victim
    Entity e1 = e.getTargetEntity();
    // damager
    Entity e2 = null;
    if (e.getCause().first(IndirectEntityDamageSource.class).isPresent()) {
        e2 = e.getCause().first(IndirectEntityDamageSource.class).get().getSource();
        RedProtect.get().logger.debug("player", "RPLayerListener: Is DamageEntityEvent event. Damager " + e2.getType().getName());
    }
    Player damager = null;
    if (e2 instanceof Projectile) {
        Projectile proj = (Projectile) e2;
        if (proj.getShooter() instanceof Player) {
            damager = (Player) proj.getShooter();
        }
    } else if (e2 instanceof Player) {
        damager = (Player) e2;
    }
    Location<World> l = e1.getLocation();
    Region r = RedProtect.get().rm.getTopRegion(l);
    if (r == null) {
        return;
    }
    RedProtect.get().logger.debug("player", "RPLayerListener: Is DamageEntityEvent event. Victim " + e1.getType().getName());
    if (damager instanceof Player) {
        if (e1 instanceof Hanging && !r.canBuild(damager)) {
            RPLang.sendMessage(damager, "entitylistener.region.cantinteract");
            e.setCancelled(true);
            return;
        }
        if (e1 instanceof Player && r.flagExists("pvp") && !r.canPVP(damager)) {
            RPLang.sendMessage(damager, "entitylistener.region.cantpvp");
            e.setCancelled(true);
            return;
        }
    }
    // return if not player
    if (!(e1 instanceof Player)) {
        return;
    }
    Player play = (Player) e.getTargetEntity();
    if (RedProtect.get().tpWait.contains(play.getName())) {
        RedProtect.get().tpWait.remove(play.getName());
        RPLang.sendMessage(play, RPLang.get("cmdmanager.region.tpcancelled"));
    }
    if (r != null && !r.canPlayerDamage()) {
        e.setCancelled(true);
    }
    // execute on health
    if (r.cmdOnHealth(play)) {
        RedProtect.get().logger.debug("player", "Cmd on healt: true");
    }
    if (!r.canDeath() && play.get(Keys.HEALTH).get() <= 1) {
        e.setCancelled(true);
    }
    // deny damagecauses
    List<String> Causes = RedProtect.get().cfgs.getStringList("server-protection.deny-playerdeath-by");
    if (e.getCause().containsType(DamageSource.class) && Causes.size() > 0) {
        DamageType damagec = e.getCause().first(DamageSource.class).get().getType();
        for (String cause : Causes) {
            if (damagec.getName().equalsIgnoreCase(cause)) {
                e.setCancelled(true);
            }
        }
    }
}
Also used : Entity(org.spongepowered.api.entity.Entity) Player(org.spongepowered.api.entity.living.player.Player) DamageSource(org.spongepowered.api.event.cause.entity.damage.source.DamageSource) IndirectEntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.IndirectEntityDamageSource) Hanging(org.spongepowered.api.entity.hanging.Hanging) Region(br.net.fabiozumbi12.RedProtect.Sponge.Region) IndirectEntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.IndirectEntityDamageSource) World(org.spongepowered.api.world.World) DamageType(org.spongepowered.api.event.cause.entity.damage.DamageType) Projectile(org.spongepowered.api.entity.projectile.Projectile) Listener(org.spongepowered.api.event.Listener)

Aggregations

Region (br.net.fabiozumbi12.RedProtect.Sponge.Region)1 Entity (org.spongepowered.api.entity.Entity)1 Hanging (org.spongepowered.api.entity.hanging.Hanging)1 Player (org.spongepowered.api.entity.living.player.Player)1 Projectile (org.spongepowered.api.entity.projectile.Projectile)1 Listener (org.spongepowered.api.event.Listener)1 DamageType (org.spongepowered.api.event.cause.entity.damage.DamageType)1 DamageSource (org.spongepowered.api.event.cause.entity.damage.source.DamageSource)1 IndirectEntityDamageSource (org.spongepowered.api.event.cause.entity.damage.source.IndirectEntityDamageSource)1 World (org.spongepowered.api.world.World)1