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);
}
}
}
}
Aggregations