Search in sources :

Example 6 with Projectile

use of org.bukkit.entity.Projectile in project Denizen-For-Bukkit by DenizenScript.

the class AssignmentTrait method onHit.

// <--[action]
// @Actions
// hit
// hit on <entity>
//
// @Triggers when the NPC hits an enemy.
//
// @Context
// None
//
// -->
// <--[action]
// @Actions
// kill
// kill of <entity>
//
// @Triggers when the NPC kills an enemy.
//
// @Context
// None
//
// -->
// Listen for this NPC's hits on entities
@EventHandler(priority = EventPriority.MONITOR)
public void onHit(EntityDamageByEntityEvent event) {
    // Check if the damager is this NPC
    if (event.getDamager() != npc.getEntity()) {
        // projectile shot by this NPC, in which case we want to continue
        if (event.getDamager() instanceof Projectile) {
            if (((Projectile) event.getDamager()).getShooter() != npc.getEntity()) {
                return;
            }
        } else {
            return;
        }
    }
    dPlayer player = null;
    // Check if the entity hit by this NPC is a player
    if (event.getEntity() instanceof Player) {
        player = dPlayer.mirrorBukkitPlayer((Player) event.getEntity());
    }
    // TODO: Context containing the entity hit
    DenizenAPI.getDenizenNPC(npc).action("hit", player);
    DenizenAPI.getDenizenNPC(npc).action("hit on " + event.getEntityType().name(), player);
    if (event.getEntity() instanceof LivingEntity) {
        if (((LivingEntity) event.getEntity()).getHealth() - event.getFinalDamage() <= 0) {
            DenizenAPI.getDenizenNPC(npc).action("kill", player);
            DenizenAPI.getDenizenNPC(npc).action("kill of " + event.getEntityType().name(), player);
        }
    }
// All done!
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) net.aufdemrand.denizen.objects.dPlayer(net.aufdemrand.denizen.objects.dPlayer) Player(org.bukkit.entity.Player) net.aufdemrand.denizen.objects.dPlayer(net.aufdemrand.denizen.objects.dPlayer) Projectile(org.bukkit.entity.Projectile) EventHandler(org.bukkit.event.EventHandler)

Example 7 with Projectile

use of org.bukkit.entity.Projectile in project Denizen-For-Bukkit by DenizenScript.

the class HealthTrait method onDamage.

// <--[action]
// @Actions
// death
// death by entity
// death by <entity>
// death by block
// death by <cause>
//
// @Triggers when the NPC dies. (Requires Health Trait)
//
// @Context
// <context.killer> returns the entity that killed the NPC (if any)
// <context.shooter> returns the shooter of the killing projectile (if any)
//
// -->
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onDamage(EntityDamageEvent event) {
    // Check if the event pertains to this NPC
    if (event.getEntity() != npc.getEntity() || dying) {
        return;
    }
    // Make sure this is a killing blow
    if (this.getHealth() - event.getFinalDamage() > 0) {
        return;
    }
    dying = true;
    player = null;
    // Save entityId for EntityDeath event
    entityId = npc.getEntity().getEntityId();
    String deathCause = CoreUtilities.toLowerCase(event.getCause().toString()).replace('_', ' ');
    Map<String, dObject> context = new HashMap<String, dObject>();
    context.put("damage", new Element(event.getDamage()));
    context.put("death_cause", new Element(deathCause));
    // Check if the entity has been killed by another entity
    if (event instanceof EntityDamageByEntityEvent) {
        Entity killerEntity = ((EntityDamageByEntityEvent) event).getDamager();
        context.put("killer", new dEntity(killerEntity));
        // that player to the action's ScriptEntry
        if (killerEntity instanceof Player) {
            player = dPlayer.mirrorBukkitPlayer((Player) killerEntity);
        } else // account as well
        if (killerEntity instanceof Projectile) {
            ProjectileSource shooter = ((Projectile) killerEntity).getShooter();
            if (shooter != null && shooter instanceof LivingEntity) {
                context.put("shooter", new dEntity((LivingEntity) shooter));
                if (shooter instanceof Player) {
                    player = dPlayer.mirrorBukkitPlayer((Player) shooter);
                }
                DenizenAPI.getDenizenNPC(npc).action("death by " + ((LivingEntity) shooter).getType().toString(), player, context);
            }
        // TODO: Handle other shooter source thingy types
        }
        DenizenAPI.getDenizenNPC(npc).action("death by entity", player, context);
        DenizenAPI.getDenizenNPC(npc).action("death by " + killerEntity.getType().toString(), player, context);
    } else // If not, check if the entity has been killed by a block
    if (event instanceof EntityDamageByBlockEvent) {
        DenizenAPI.getDenizenNPC(npc).action("death by block", player, context);
    // TODO:
    // The line of code below should work, but a Bukkit bug makes the damager
    // return null. Uncomment it once the bug is fixed.
    // DenizenAPI.getDenizenNPC(npc).action("death by " +
    // ((EntityDamageByBlockEvent) event).getDamager().getType().name(), null);
    }
    DenizenAPI.getDenizenNPC(npc).action("death", player, context);
    DenizenAPI.getDenizenNPC(npc).action("death by " + deathCause, player, context);
    // NPC's entity still exists before proceeding
    if (npc.getEntity() == null) {
        return;
    }
    loc = dLocation.valueOf(// TODO: debug option?
    TagManager.tag(// TODO: debug option?
    respawnLocation, new BukkitTagContext(null, DenizenAPI.getDenizenNPC(npc), false, null, true, null)));
    if (loc == null) {
        loc = npc.getEntity().getLocation();
    }
    if (animatedeath) {
        // Cancel navigation to keep the NPC from damaging players
        // while the death animation is being carried out.
        npc.getNavigator().cancelNavigation();
    // Reset health now to avoid the death from happening instantly
    //setHealth();
    // Play animation (TODO)
    // playDeathAnimation(npc.getEntity());
    }
    die();
    if (respawn && (Duration.valueOf(respawnDelay).getTicks() > 0)) {
        Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(DenizenAPI.getCurrentInstance(), new Runnable() {

            public void run() {
                if (CitizensAPI.getNPCRegistry().getById(npc.getId()) == null || npc.isSpawned()) {
                    return;
                } else {
                    npc.spawn(loc);
                }
            }
        }, (Duration.valueOf(respawnDelay).getTicks()));
    }
}
Also used : net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) net.aufdemrand.denizen.objects.dPlayer(net.aufdemrand.denizen.objects.dPlayer) HashMap(java.util.HashMap) EntityDamageByBlockEvent(org.bukkit.event.entity.EntityDamageByBlockEvent) Element(net.aufdemrand.denizencore.objects.Element) Projectile(org.bukkit.entity.Projectile) LivingEntity(org.bukkit.entity.LivingEntity) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) BukkitTagContext(net.aufdemrand.denizen.tags.BukkitTagContext) net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) net.aufdemrand.denizencore.objects.dObject(net.aufdemrand.denizencore.objects.dObject) ProjectileSource(org.bukkit.projectiles.ProjectileSource) EventHandler(org.bukkit.event.EventHandler)

Example 8 with Projectile

use of org.bukkit.entity.Projectile in project Towny by ElgarL.

the class CombatUtil method preventDamageCall.

/**
	 * Tests the attacker against defender to see if we need to cancel
	 * the damage event due to world PvP, Plot PvP or Friendly Fire settings.
	 * Only allow a Wolves owner to cause it damage, and residents with destroy
	 * permissions to damage passive animals and villagers while in a town.
	 * 
	 * @param attacker
	 * @param defender
	 * @return true if we should cancel.
	 */
public static boolean preventDamageCall(Towny plugin, Entity attacker, Entity defender) {
    try {
        TownyWorld world = TownyUniverse.getDataSource().getWorld(defender.getWorld().getName());
        // World using Towny
        if (!world.isUsingTowny())
            return false;
        Player a = null;
        Player b = null;
        /*
			 * Find the shooter if this is a projectile.
			 */
        if (attacker instanceof Projectile) {
            Projectile projectile = (Projectile) attacker;
            Object source = projectile.getShooter();
            if (source instanceof Entity) {
                attacker = (Entity) source;
            } else {
                // TODO: prevent damage from dispensers
                return false;
            }
        }
        if (attacker instanceof Player)
            a = (Player) attacker;
        if (defender instanceof Player)
            b = (Player) defender;
        // Allow players to injure themselves
        if (a == b)
            return false;
        return preventDamageCall(plugin, world, attacker, defender, a, b);
    } catch (Exception e) {
    // Failed to fetch world
    }
    return false;
}
Also used : Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) Projectile(org.bukkit.entity.Projectile)

Example 9 with Projectile

use of org.bukkit.entity.Projectile in project Towny by ElgarL.

the class TownyEntityListener method onEntityDamageByEntityEvent.

@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onEntityDamageByEntityEvent(EntityDamageByEntityEvent event) {
    if (plugin.isError()) {
        return;
    }
    TownyWorld townyWorld = null;
    Entity entity = event.getEntity();
    if (entity instanceof ArmorStand) {
        String damager = event.getDamager().getType().name();
        if (damager == "PRIMED_TNT" || damager == "WITHER_SKULL" || damager == "FIREBALL" || damager == "SMALL_FIREBALL" || damager == "LARGE_FIREBALL" || damager == "WITHER") {
            try {
                townyWorld = TownyUniverse.getDataSource().getWorld(entity.getWorld().getName());
            } catch (NotRegisteredException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if (!locationCanExplode(townyWorld, entity.getLocation())) {
                event.setCancelled(true);
                return;
            }
        }
        if (event.getDamager() instanceof Projectile) {
            try {
                townyWorld = TownyUniverse.getDataSource().getWorld(entity.getWorld().getName());
            } catch (NotRegisteredException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Object remover = event.getDamager();
            remover = ((Projectile) remover).getShooter();
            if (remover instanceof Monster) {
                event.setCancelled(true);
            } else if (remover instanceof Player) {
                Player player = (Player) remover;
                // Get destroy permissions (updates if none exist)
                boolean bDestroy = PlayerCacheUtil.getCachePermission(player, entity.getLocation(), 416, (byte) 0, TownyPermission.ActionType.DESTROY);
                // Allow the removal if we are permitted
                if (bDestroy)
                    return;
                /*
					 * Fetch the players cache
					 */
                PlayerCache cache = plugin.getCache(player);
                event.setCancelled(true);
            }
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) ArmorStand(org.bukkit.entity.ArmorStand) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Monster(org.bukkit.entity.Monster) PlayerCache(com.palmergames.bukkit.towny.object.PlayerCache) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) Projectile(org.bukkit.entity.Projectile) EventHandler(org.bukkit.event.EventHandler)

Example 10 with Projectile

use of org.bukkit.entity.Projectile in project Denizen-For-Bukkit by DenizenScript.

the class BukkitScriptEvent method tryEntity.

public boolean tryEntity(dEntity entity, String comparedto) {
    if (comparedto == null || comparedto.isEmpty() || entity == null) {
        return false;
    }
    Entity bEntity = entity.getBukkitEntity();
    comparedto = CoreUtilities.toLowerCase(comparedto);
    if (comparedto.equals("entity")) {
        return true;
    } else if (comparedto.equals("npc")) {
        return entity.isCitizensNPC();
    } else if (comparedto.equals("player")) {
        return entity.isPlayer();
    } else if (comparedto.equals("vehicle")) {
        return bEntity instanceof Vehicle;
    } else if (comparedto.equals("projectile")) {
        return bEntity instanceof Projectile;
    } else if (comparedto.equals("hanging")) {
        return bEntity instanceof Hanging;
    } else if (entity.getEntityScript() != null && comparedto.equals(CoreUtilities.toLowerCase(entity.getEntityScript()))) {
        return true;
    } else if (comparedto.equals(entity.getEntityType().getLowercaseName())) {
        return true;
    }
    return false;
}
Also used : Vehicle(org.bukkit.entity.Vehicle) Entity(org.bukkit.entity.Entity) Hanging(org.bukkit.entity.Hanging) Projectile(org.bukkit.entity.Projectile)

Aggregations

Projectile (org.bukkit.entity.Projectile)26 EventHandler (org.bukkit.event.EventHandler)19 Entity (org.bukkit.entity.Entity)18 Player (org.bukkit.entity.Player)14 LivingEntity (org.bukkit.entity.LivingEntity)9 PotionEffect (org.bukkit.potion.PotionEffect)6 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)4 ProjectileLaunchEvent (org.bukkit.event.entity.ProjectileLaunchEvent)4 Test (org.junit.Test)4 TownyWorld (com.palmergames.bukkit.towny.object.TownyWorld)3 EntityDamageByEntityEvent (org.bukkit.event.entity.EntityDamageByEntityEvent)3 PlayerCache (com.palmergames.bukkit.towny.object.PlayerCache)2 net.aufdemrand.denizen.objects.dEntity (net.aufdemrand.denizen.objects.dEntity)2 net.aufdemrand.denizen.objects.dPlayer (net.aufdemrand.denizen.objects.dPlayer)2 Material (org.bukkit.Material)2 Block (org.bukkit.block.Block)2 ProjectileSource (org.bukkit.projectiles.ProjectileSource)2 Vector (org.bukkit.util.Vector)2 EntityActionInfo (com.gamingmesh.jobs.actions.EntityActionInfo)1 JobsPlayer (com.gamingmesh.jobs.container.JobsPlayer)1