Search in sources :

Example 81 with EventHandler

use of org.bukkit.event.EventHandler 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 82 with EventHandler

use of org.bukkit.event.EventHandler in project Denizen-For-Bukkit by DenizenScript.

the class HungerTrait method onMove.

// <--[action]
// @Actions
// exhausted
//
// @Triggers when the NPC is exhausted (Requires the Hunger trait)
//
// @Context
// None
//
// -->
/**
     * Listens for the NPC to move so hunger-loss can be calculated.
     * Cuts down on processing since loss is only calculated when moving.
     * Also checks for exhaustion, if enabled. If a NPC is exhausted, that is,
     * if currenthunger >= maxhunger, the NPC cannot move and a
     * NPCExhaustedEvent and 'On Exhausted:' action will fire.
     */
@EventHandler
public void onMove(NavigationBeginEvent event) {
    // TODO: Check if NPC == this NPC?
    if (allowexhaustion) {
        if (isStarving()) {
            // Create NPCExhaustedEvent, give chance for outside plugins to cancel.
            ExhaustedNPCEvent e = new ExhaustedNPCEvent(npc);
            Bukkit.getServer().getPluginManager().callEvent(e);
            // If still exhausted, cancel navigation and fire 'On Exhausted:' action
            if (!e.isCancelled()) {
                npc.getNavigator().cancelNavigation();
                DenizenAPI.getDenizenNPC(npc).action("exhausted", null);
                // No need to progress any further.
                return;
            }
        }
    }
    location = npc.getEntity().getLocation();
    listening = true;
}
Also used : ExhaustedNPCEvent(net.aufdemrand.denizen.events.bukkit.ExhaustedNPCEvent) EventHandler(org.bukkit.event.EventHandler)

Example 83 with EventHandler

use of org.bukkit.event.EventHandler in project Denizen-For-Bukkit by DenizenScript.

the class PushableTrait method NPCCompleteDestination.

// <--[action]
// @Actions
// push return
//
// @Triggers when the NPC returns to its center after being pushed by a player.
//
// @Context
// None
//
// -->
/**
     * Fires a 'On Push Return:' action upon return after being pushed.
     */
@EventHandler
public void NPCCompleteDestination(NavigationCompleteEvent event) {
    if (event.getNPC() == npc && pushed) {
        Entity npcEntity = npc.getEntity();
        Location location = npcEntity.getLocation();
        location.setYaw(returnLocation.getYaw());
        location.setPitch(returnLocation.getPitch());
        NMS.setHeadYaw(npcEntity, returnLocation.getYaw());
        pushed = false;
        // Push Return action
        DenizenAPI.getDenizenNPC(npc).action("push return", null);
    }
}
Also used : Entity(org.bukkit.entity.Entity) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Example 84 with EventHandler

use of org.bukkit.event.EventHandler in project Denizen-For-Bukkit by DenizenScript.

the class KillListenerInstance method listen.

@EventHandler
public void listen(EntityDeathEvent event) {
    // Only continue if the event is an event for the player that owns this listener.
    if (event.getEntity().getKiller() != player.getPlayerEntity()) {
        return;
    }
    // If REGION argument specified, check. If not in region, don't count kill!
    if (region != null) {
    //if (!WorldGuardUtilities.inRegion(player.getLocation(), region)) return;
    }
    // Same with the CUBOID argument...
    if (cuboid != null) {
        if (!cuboid.isInsideCuboid(player.getLocation())) {
            return;
        }
    }
    //
    if (type == KillType.ENTITY) {
        // Get entity killed
        dEntity ent = new dEntity(event.getEntity());
        boolean count_it = false;
        // Check targets, if any match entity killed, count_it!
        for (String target : targets) {
            if (dEntity.valueOf(target) != null) {
                if (ent.comparesTo(dEntity.valueOf(target)) == 1) {
                    count_it = true;
                }
            }
        }
        boolean right_name = false;
        for (String name : names) {
            if (ChatColor.stripColor(ent.getName()).contains(name)) {
                right_name = true;
            }
        }
        // kills_so_far
        if (count_it || targets.contains("*")) {
            if (right_name || names.contains("*")) {
                kills_so_far++;
                dB.log(player.getName() + " killed a " + ent.identify() + ". Current progress '" + kills_so_far + "/" + required + "'.");
                // Check the number of kills so far
                check();
            }
        }
    } else //
    if (type == KillType.NPC) {
        // If a NPC wasn't killed, return.
        if (!CitizensAPI.getNPCRegistry().isNPC(event.getEntity())) {
            return;
        }
        // Get the NPC killed
        dNPC npc = dNPC.mirrorCitizensNPC(CitizensAPI.getNPCRegistry().getNPC(event.getEntity()));
        boolean count_it = false;
        // Check targets, if any match entity killed, count_it!
        for (String target : targets) {
            // Check against a physical NPC object (n@7, n@18, etc.)
            if (dNPC.valueOf(target) != null) {
                if (dNPC.valueOf(target).getId() == npc.getId()) {
                    count_it = true;
                }
            }
            if (CoreUtilities.toLowerCase(npc.getName()).equals(CoreUtilities.toLowerCase(target).replace("n@", ""))) {
                count_it = true;
            }
        }
        // the kills so far.
        if (count_it || targets.contains("*")) {
            kills_so_far++;
            dB.log(player.getName() + " killed " + npc.toString() + ". Current progress '" + kills_so_far + "/" + required + "'.");
            // Check the number of kills so far
            check();
        }
    } else //
    if (type == KillType.PLAYER) {
        // Check to make sure entity is a Player, and not a NPC
        if (event.getEntityType() != EntityType.PLAYER) {
            return;
        }
        if (Depends.citizens != null && CitizensAPI.getNPCRegistry().isNPC(event.getEntity())) {
            return;
        }
        // Get player killed
        dPlayer player = dPlayer.mirrorBukkitPlayer((Player) event.getEntity());
        boolean count_it = false;
        // Check targets, if any match entity killed, count_it!
        for (String target : targets) {
            if (dPlayer.valueOf(target) != null) {
                if (dPlayer.valueOf(target).getName().equalsIgnoreCase(player.getName())) {
                    count_it = true;
                }
            }
        }
        // kills_so_far
        if (count_it || targets.contains("*")) {
            kills_so_far++;
            dB.log(player.getName() + " killed " + player.getName() + ". Current progress '" + kills_so_far + "/" + required + "'.");
            // Check the number of kills so far
            check();
        }
    } else //
    if (type == KillType.GROUP) {
        // Require the entity to be a Player
        if (event.getEntityType() == EntityType.PLAYER) // Iterate through groups on the Player
        {
            for (String group : Depends.permissions.getPlayerGroups((Player) event.getEntity())) // If a group matches, count it!
            {
                if (targets.contains(group.toUpperCase())) {
                    kills_so_far++;
                    dB.log(player.getName() + " killed " + ((Player) event.getEntity()).getName().toUpperCase() + " of group " + group + ".");
                    check();
                    break;
                }
            }
        }
    }
}
Also used : net.aufdemrand.denizen.objects.dPlayer(net.aufdemrand.denizen.objects.dPlayer) Player(org.bukkit.entity.Player) net.aufdemrand.denizen.objects.dNPC(net.aufdemrand.denizen.objects.dNPC) net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) net.aufdemrand.denizen.objects.dPlayer(net.aufdemrand.denizen.objects.dPlayer) EventHandler(org.bukkit.event.EventHandler)

Example 85 with EventHandler

use of org.bukkit.event.EventHandler in project Denizen-For-Bukkit by DenizenScript.

the class dNPCRegistry method onInventoryClick.

@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
    Inventory inventory = event.getInventory();
    if (inventory.getHolder() instanceof dNPC) {
        dNPC npc = (dNPC) inventory.getHolder();
        npc.getInventory().setContents(inventory.getContents());
        Equipment equipment = npc.getEquipmentTrait();
        for (int i = 0; i < 5; i++) {
            equipment.set(i, inventory.getItem(i));
        }
    }
}
Also used : net.aufdemrand.denizen.objects.dNPC(net.aufdemrand.denizen.objects.dNPC) Equipment(net.citizensnpcs.api.trait.trait.Equipment) Inventory(org.bukkit.inventory.Inventory) EventHandler(org.bukkit.event.EventHandler)

Aggregations

EventHandler (org.bukkit.event.EventHandler)532 Player (org.bukkit.entity.Player)185 Entity (org.bukkit.entity.Entity)70 net.aufdemrand.denizen.objects.dEntity (net.aufdemrand.denizen.objects.dEntity)67 net.aufdemrand.denizen.objects.dLocation (net.aufdemrand.denizen.objects.dLocation)62 Block (org.bukkit.block.Block)62 ItemStack (org.bukkit.inventory.ItemStack)55 Location (org.bukkit.Location)54 Island (com.wasteofplastic.acidisland.Island)42 Element (net.aufdemrand.denizencore.objects.Element)38 LivingEntity (org.bukkit.entity.LivingEntity)37 MinigamePlayer (au.com.mineauz.minigames.MinigamePlayer)31 Minigame (au.com.mineauz.minigames.minigame.Minigame)29 net.aufdemrand.denizen.objects.dItem (net.aufdemrand.denizen.objects.dItem)29 net.aufdemrand.denizencore.objects.dList (net.aufdemrand.denizencore.objects.dList)28 Projectile (org.bukkit.entity.Projectile)26 MyPetPlayer (de.Keyle.MyPet.api.player.MyPetPlayer)23 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)22 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)18 ArrayList (java.util.ArrayList)18