use of org.bukkit.projectiles.ProjectileSource in project Denizen-For-Bukkit by DenizenScript.
the class NPCTagBase method onDeath.
// <--[action]
// @Actions
// death
// death by entity
// death by <entity>
// death by block
// death by <cause>
//
// @Triggers when the NPC dies.
//
// @Context
// <context.killer> returns the entity that killed the NPC (if any)
// <context.shooter> returns the shooter of the killing projectile (if any)
// <context.damage> returns the last amount of damage applied (if any)
// <context.death_cause> returns the last damage cause (if any)
//
// -->
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onDeath(EntityDeathEvent deathEvent) {
NPC citizen = CitizensAPI.getNPCRegistry().getNPC(deathEvent.getEntity());
if (citizen == null || !citizen.hasTrait(AssignmentTrait.class)) {
return;
}
NPCTag npc = new NPCTag(citizen);
EntityDamageEvent event = deathEvent.getEntity().getLastDamageCause();
String deathCause = event == null ? "unknown" : CoreUtilities.toLowerCase(event.getCause().toString()).replace('_', ' ');
Map<String, ObjectTag> context = new HashMap<>();
context.put("damage", new ElementTag(event == null ? 0 : event.getDamage()));
context.put("death_cause", new ElementTag(deathCause));
PlayerTag player = null;
if (event instanceof EntityDamageByEntityEvent) {
Entity killerEntity = ((EntityDamageByEntityEvent) event).getDamager();
context.put("killer", new EntityTag(killerEntity).getDenizenObject());
if (killerEntity instanceof Player) {
player = PlayerTag.mirrorBukkitPlayer((Player) killerEntity);
} else if (killerEntity instanceof Projectile) {
ProjectileSource shooter = ((Projectile) killerEntity).getShooter();
if (shooter instanceof LivingEntity) {
context.put("shooter", new EntityTag((LivingEntity) shooter).getDenizenObject());
if (shooter instanceof Player) {
player = PlayerTag.mirrorBukkitPlayer((Player) shooter);
}
npc.action("death by " + ((LivingEntity) shooter).getType().toString(), player, context);
}
}
npc.action("death by entity", player, context);
npc.action("death by " + killerEntity.getType().toString(), player, context);
} else if (event instanceof EntityDamageByBlockEvent) {
npc.action("death by block", null, context);
}
npc.action("death", player, context);
npc.action("death by " + deathCause, player, context);
}
use of org.bukkit.projectiles.ProjectileSource in project FunnyGuilds by FunnyGuilds.
the class EntityDamage method onDamage.
@EventHandler(priority = EventPriority.HIGHEST)
public void onDamage(EntityDamageByEntityEvent event) {
Entity entity = event.getEntity();
Entity damager = event.getDamager();
if (!(entity instanceof Player)) {
return;
}
Player attacker = null;
if (damager instanceof Player) {
attacker = (Player) damager;
} else if (damager instanceof Projectile) {
ProjectileSource shooter = ((Projectile) damager).getShooter();
if (shooter instanceof Player) {
attacker = (Player) shooter;
}
}
if (attacker == null) {
return;
}
PluginConfig config = Settings.getConfig();
User victimUser = User.get((Player) event.getEntity());
User attackerUser = User.get(attacker);
if (victimUser.hasGuild() && attackerUser.hasGuild()) {
if (victimUser.getUUID().equals(attackerUser.getUUID())) {
return;
}
if (victimUser.getGuild().equals(attackerUser.getGuild())) {
if (!victimUser.getGuild().getPvP()) {
event.setCancelled(true);
return;
}
}
if (victimUser.getGuild().getAllies().contains(attackerUser.getGuild())) {
if (!config.damageAlly) {
event.setCancelled(true);
return;
}
}
}
if (config.assistEnable && !event.isCancelled()) {
victimUser.addDamage(attackerUser, event.getDamage());
}
}
use of org.bukkit.projectiles.ProjectileSource in project Sentinel by mcmonkey4eva.
the class SentinelTrait method whenAttacksHappened.
public void whenAttacksHappened(EntityDamageByEntityEvent event) {
if (!npc.isSpawned()) {
return;
}
if (event.isCancelled()) {
return;
}
if (event == damageDeDup) {
return;
}
damageDeDup = event;
Entity damager = event.getDamager();
LivingEntity projectileSource = null;
if (damager instanceof Projectile) {
ProjectileSource source = ((Projectile) damager).getShooter();
if (source instanceof LivingEntity) {
projectileSource = (LivingEntity) source;
damager = projectileSource;
}
}
boolean isMe = event.getEntity().getUniqueId().equals(getLivingEntity().getUniqueId());
if (isMe && (SentinelPlugin.instance.protectFromIgnores || protectFromIgnores)) {
if (damager instanceof LivingEntity && targetingHelper.isIgnored((LivingEntity) damager)) {
event.setCancelled(true);
return;
} else if (projectileSource != null && targetingHelper.isIgnored(projectileSource)) {
event.setCancelled(true);
return;
}
}
boolean isKilling = event.getEntity() instanceof LivingEntity && event.getFinalDamage() >= ((LivingEntity) event.getEntity()).getHealth();
boolean isFriend = getGuarding() != null && event.getEntity().getUniqueId().equals(getGuarding());
boolean attackerIsMe = damager.getUniqueId().equals(getLivingEntity().getUniqueId());
if (projectileSource != null && projectileSource.getUniqueId().equals(getLivingEntity().getUniqueId())) {
attackerIsMe = true;
}
if (isMe || isFriend) {
if (attackerIsMe) {
if (SentinelPlugin.debugMe) {
debug("Ignoring damage I did to " + (isMe ? "myself." : "my friend."));
}
event.setCancelled(true);
return;
}
if (getGuarding() != null && damager.getUniqueId().equals(getGuarding())) {
if (isMe && SentinelPlugin.instance.noGuardDamage) {
debug("Ignoring damage from the player we're guarding.");
event.setCancelled(true);
}
return;
}
if (isMe) {
stats_damageTaken += event.getFinalDamage();
if (runaway) {
debug("Ow! They hit me! Run!");
targetingHelper.addAvoid(damager.getUniqueId());
}
}
if (fightback && (damager instanceof LivingEntity) && !targetingHelper.isIgnored((LivingEntity) damager)) {
if (SentinelPlugin.debugMe) {
debug("Fighting back against attacker: " + damager.getUniqueId() + "! They hurt " + (isMe ? "me!" : "my friend!"));
}
targetingHelper.addTarget(damager.getUniqueId());
if (chasing == null) {
if (itemHelper.isRanged() ? rangedChase : closeChase) {
attackHelper.chase((LivingEntity) damager);
}
}
}
if (SentinelPlugin.debugMe && isMe) {
debug("Took damage of " + event.getFinalDamage() + " with currently remaining health " + getLivingEntity().getHealth() + (isKilling ? ". This will kill me." : "."));
}
if (isKilling && isMe && SentinelPlugin.instance.blockEvents) {
// We're going to die but we've been requested to avoid letting a death event fire.
// The solution at this point is to fake our death: remove the NPC entity from existence and trigger our internal death handling sequence.
// We also mark the damage event cancelled to reduce trouble (ensure event doesn't try to apply damage to our now-gone entity).
debug("Died! Applying death workaround (due to config setting)");
generalDeathHandler(getLivingEntity());
npc.despawn(DespawnReason.PLUGIN);
event.setCancelled(true);
return;
}
return;
}
if (attackerIsMe) {
if (safeShot && !targetingHelper.shouldTarget((LivingEntity) event.getEntity())) {
event.setCancelled(true);
return;
}
stats_damageGiven += event.getFinalDamage();
if (!enemyDrops && event.getEntity().getType() != EntityType.PLAYER) {
needsDropsClear.add(event.getEntity().getUniqueId());
if (SentinelPlugin.debugMe) {
debug("This " + event.getEntity().getType() + " with id " + event.getEntity().getUniqueId() + " is being tracked for potential drops removal.");
}
}
return;
}
}
use of org.bukkit.projectiles.ProjectileSource in project Sentinel by mcmonkey4eva.
the class SentinelTargetList method isEventTarget.
/**
* Returns whether the damager in a damage event is targeted by this list.
*/
public boolean isEventTarget(EntityDamageByEntityEvent event) {
Entity damager = event.getDamager();
if (event.getDamager() instanceof Projectile) {
ProjectileSource source = ((Projectile) event.getDamager()).getShooter();
if (source instanceof Entity) {
damager = (Entity) source;
}
}
if (CitizensAPI.getNPCRegistry().isNPC(damager)) {
return false;
}
if (damager.equals(event.getEntity())) {
// Players can accidentally hurt themselves - that's not PvP
return false;
}
for (String evt : byEvent) {
if (evt.equals("pvp") && event.getEntity() instanceof Player && damager instanceof Player && !CitizensAPI.getNPCRegistry().isNPC(event.getEntity())) {
return true;
} else if (evt.equals("pve") && !(event.getEntity() instanceof Player) && damager instanceof Player && event.getEntity() instanceof LivingEntity) {
return true;
} else if (evt.equals("eve") && !(damager instanceof Player) && !(event.getEntity() instanceof Player) && event.getEntity() instanceof LivingEntity) {
return true;
} else if (evt.equals("pvnpc") && event.getEntity() instanceof LivingEntity && damager instanceof Player && CitizensAPI.getNPCRegistry().isNPC(event.getEntity())) {
return true;
} else if (evt.equals("pvsentinel") && event.getEntity() instanceof LivingEntity && damager instanceof Player && CitizensAPI.getNPCRegistry().isNPC(event.getEntity()) && CitizensAPI.getNPCRegistry().getNPC(event.getEntity()).hasTrait(SentinelTrait.class)) {
return true;
}
if (evt.contains(":")) {
int colon = evt.indexOf(':');
String prefix = evt.substring(0, colon);
String value = evt.substring(colon + 1);
if (prefix.equals("pv")) {
SentinelTarget target = SentinelTarget.forName(value);
if (target != null) {
if (damager instanceof Player && event.getEntity() instanceof LivingEntity && target.isTarget((LivingEntity) event.getEntity())) {
return true;
}
}
} else if (prefix.equals("ev")) {
SentinelTarget target = SentinelTarget.forName(value);
if (target != null) {
if (!(damager instanceof Player) && event.getEntity() instanceof LivingEntity && target.isTarget((LivingEntity) event.getEntity())) {
return true;
}
}
}
}
}
return false;
}
Aggregations