Search in sources :

Example 1 with LivingEntity

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

the class EquipCommand method execute.

@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    Map<String, dItem> equipment = (Map<String, dItem>) scriptEntry.getObject("equipment");
    List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
    // Report to dB
    dB.report(scriptEntry, getName(), aH.debugObj("entities", entities.toString()) + aH.debugObj("equipment", equipment.toString()));
    for (dEntity entity : entities) {
        if (entity.isGeneric()) {
            dB.echoError(scriptEntry.getResidingQueue(), "Cannot equip generic entity " + entity.identify() + "!");
        } else if (entity.isCitizensNPC()) {
            dNPC npc = entity.getDenizenNPC();
            if (npc != null) {
                Equipment trait = npc.getEquipmentTrait();
                if (equipment.get("hand") != null) {
                    trait.set(0, equipment.get("hand").getItemStack());
                }
                if (equipment.get("head") != null) {
                    trait.set(1, equipment.get("head").getItemStack());
                }
                if (equipment.get("chest") != null) {
                    trait.set(2, equipment.get("chest").getItemStack());
                }
                if (equipment.get("legs") != null) {
                    trait.set(3, equipment.get("legs").getItemStack());
                }
                if (equipment.get("boots") != null) {
                    trait.set(4, equipment.get("boots").getItemStack());
                }
                if (equipment.get("offhand") != null) {
                    trait.set(5, equipment.get("offhand").getItemStack());
                }
                if (npc.isSpawned()) {
                    LivingEntity livingEntity = npc.getLivingEntity();
                    if (livingEntity.getType() == EntityType.HORSE) {
                        if (equipment.get("saddle") != null) {
                            ((Horse) livingEntity).getInventory().setSaddle(equipment.get("saddle").getItemStack());
                        }
                        if (equipment.get("horse_armor") != null) {
                            ((Horse) livingEntity).getInventory().setArmor(equipment.get("horse_armor").getItemStack());
                        }
                    } else if (livingEntity.getType() == EntityType.PIG) {
                        if (equipment.get("saddle") != null) {
                            dItem saddle = equipment.get("saddle");
                            if (saddle.getItemStack().getType() == Material.SADDLE) {
                                ((Pig) livingEntity).setSaddle(true);
                            } else {
                                ((Pig) livingEntity).setSaddle(false);
                            }
                        }
                    }
                }
            }
        } else {
            LivingEntity livingEntity = entity.getLivingEntity();
            if (livingEntity != null) {
                if (livingEntity.getType() == EntityType.HORSE) {
                    if (equipment.get("saddle") != null) {
                        ((Horse) livingEntity).getInventory().setSaddle(equipment.get("saddle").getItemStack());
                    }
                    if (equipment.get("horse_armor") != null) {
                        ((Horse) livingEntity).getInventory().setArmor(equipment.get("horse_armor").getItemStack());
                    }
                } else if (livingEntity.getType() == EntityType.PIG) {
                    if (equipment.get("saddle") != null) {
                        dItem saddle = equipment.get("saddle");
                        if (saddle.getItemStack().getType() == Material.SADDLE) {
                            ((Pig) livingEntity).setSaddle(true);
                        } else {
                            ((Pig) livingEntity).setSaddle(false);
                        }
                    }
                } else {
                    if (equipment.get("hand") != null) {
                        NMSHandler.getInstance().getEntityHelper().setItemInHand(livingEntity, equipment.get("hand").getItemStack());
                    }
                    if (equipment.get("head") != null) {
                        livingEntity.getEquipment().setHelmet(equipment.get("head").getItemStack());
                    }
                    if (equipment.get("chest") != null) {
                        livingEntity.getEquipment().setChestplate(equipment.get("chest").getItemStack());
                    }
                    if (equipment.get("legs") != null) {
                        livingEntity.getEquipment().setLeggings(equipment.get("legs").getItemStack());
                    }
                    if (equipment.get("boots") != null) {
                        livingEntity.getEquipment().setBoots(equipment.get("boots").getItemStack());
                    }
                    if (equipment.get("offhand") != null) {
                        NMSHandler.getInstance().getEntityHelper().setItemInOffHand(livingEntity, equipment.get("offhand").getItemStack());
                    }
                }
            }
        }
    }
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) net.aufdemrand.denizen.objects.dItem(net.aufdemrand.denizen.objects.dItem) net.aufdemrand.denizen.objects.dNPC(net.aufdemrand.denizen.objects.dNPC) Equipment(net.citizensnpcs.api.trait.trait.Equipment) net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) Horse(org.bukkit.entity.Horse) List(java.util.List) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) HashMap(java.util.HashMap) Map(java.util.Map) Pig(org.bukkit.entity.Pig)

Example 2 with LivingEntity

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

the class FlyCommand method execute.

@SuppressWarnings("unchecked")
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
    // Get objects
    dLocation origin = (dLocation) scriptEntry.getObject("origin");
    List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
    final List<dLocation> destinations = scriptEntry.hasObject("destinations") ? (List<dLocation>) scriptEntry.getObject("destinations") : new ArrayList<dLocation>();
    // Set freeflight to true only if there are no destinations
    final boolean freeflight = destinations.size() < 1;
    dEntity controller = (dEntity) scriptEntry.getObject("controller");
    // If freeflight is on, we need to do some checks
    if (freeflight) {
        // flying entities, so try to find a player in the entity list
        if (controller == null) {
            for (dEntity entity : entities) {
                if (entity.isPlayer()) {
                    // the controller
                    if (entities.get(entities.size() - 1) != entity) {
                        controller = entity;
                        dB.report(scriptEntry, getName(), "Flight control defaulting to " + controller);
                        break;
                    }
                }
            }
            // If the controller is still null, we cannot continue
            if (controller == null) {
                dB.report(scriptEntry, getName(), "There is no one to control the flight's path!");
                return;
            }
        } else // Else, if the controller was set, we need to make sure
        // it is among the flying entities, and add it if it is not
        {
            boolean found = false;
            for (dEntity entity : entities) {
                if (entity.identify().equals(controller.identify())) {
                    found = true;
                    break;
                }
            }
            // Add the controller to the entity list
            if (!found) {
                dB.report(scriptEntry, getName(), "Adding controller " + controller + " to flying entities.");
                entities.add(0, controller);
            }
        }
    }
    final double speed = ((Element) scriptEntry.getObject("speed")).asDouble();
    final float rotationThreshold = ((Element) scriptEntry.getObject("rotationThreshold")).asFloat();
    boolean cancel = scriptEntry.hasObject("cancel");
    // Report to dB
    dB.report(scriptEntry, getName(), (cancel ? aH.debugObj("cancel", cancel) : "") + aH.debugObj("origin", origin) + aH.debugObj("entities", entities.toString()) + aH.debugObj("speed", speed) + aH.debugObj("rotation threshold degrees", rotationThreshold) + (freeflight ? aH.debugObj("controller", controller) : aH.debugObj("destinations", destinations.toString())));
    // Mount or dismount all of the entities
    if (!cancel) {
        // Go through all the entities, spawning/teleporting them
        for (dEntity entity : entities) {
            entity.spawnAt(origin);
        }
        Position.mount(Conversion.convertEntities(entities));
    } else {
        Position.dismount(Conversion.convertEntities(entities));
        // Go no further if we are dismounting entities
        return;
    }
    // Get the last entity on the list
    final Entity entity = entities.get(entities.size() - 1).getBukkitEntity();
    final LivingEntity finalController = controller != null ? controller.getLivingEntity() : null;
    BukkitRunnable task = new BukkitRunnable() {

        Location location = null;

        Boolean flying = true;

        public void run() {
            if (freeflight) {
                if (!entity.isEmpty() && finalController.isInsideVehicle()) {
                    location = finalController.getEyeLocation().add(finalController.getEyeLocation().getDirection().multiply(30));
                } else {
                    flying = false;
                }
            } else {
                if (destinations.size() > 0) {
                    location = destinations.get(0);
                } else {
                    flying = false;
                }
            }
            if (flying && entity.isValid()) {
                // when it really needs to
                if (!NMSHandler.getInstance().getEntityHelper().isFacingLocation(entity, location, rotationThreshold)) {
                    NMSHandler.getInstance().getEntityHelper().faceLocation(entity, location);
                }
                Vector v1 = entity.getLocation().toVector();
                Vector v2 = location.toVector();
                Vector v3 = v2.clone().subtract(v1).normalize().multiply(speed);
                entity.setVelocity(v3);
                if (!freeflight) {
                    if (Math.abs(v2.getX() - v1.getX()) < 2 && Math.abs(v2.getY() - v1.getY()) < 2 && Math.abs(v2.getZ() - v1.getZ()) < 2) {
                        destinations.remove(0);
                    }
                }
            } else {
                flying = false;
                this.cancel();
            }
        }
    };
    task.runTaskTimer(DenizenAPI.getCurrentInstance(), 0, 3);
}
Also used : Entity(org.bukkit.entity.Entity) net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) LivingEntity(org.bukkit.entity.LivingEntity) Element(net.aufdemrand.denizencore.objects.Element) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) LivingEntity(org.bukkit.entity.LivingEntity) net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) ArrayList(java.util.ArrayList) List(java.util.List) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) net.aufdemrand.denizen.objects.dLocation(net.aufdemrand.denizen.objects.dLocation) Vector(org.bukkit.util.Vector) Location(org.bukkit.Location) net.aufdemrand.denizen.objects.dLocation(net.aufdemrand.denizen.objects.dLocation)

Example 3 with LivingEntity

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

the class NPCTags method navBegin.

// <--[event]
// @Events
// npc begins navigation
//
// @Warning This event may fire very rapidly.
//
// @Triggers when an NPC begins navigating.
//
// @Context
// None
//
// -->
// <--[action]
// @Actions
// begin navigation
//
// @Triggers when the NPC has received a 'walk' command,
// or is about to follow a path.
//
// @Context
// None
//
// -->
@EventHandler
public void navBegin(NavigationBeginEvent event) {
    dNPC npc = DenizenAPI.getDenizenNPC(event.getNPC());
    // Do world script event 'On NPC Completes Navigation'
    if (NPCNavigationSmartEvent.IsActive()) {
        OldEventManager.doEvents(Arrays.asList("npc begins navigation"), new BukkitScriptEntryData(null, npc), null);
    }
    if (!event.getNPC().hasTrait(AssignmentTrait.class)) {
        return;
    }
    npc.action("begin navigation", null);
    if (event.getNPC().getNavigator().getTargetType() == TargetType.ENTITY) {
        LivingEntity entity = (LivingEntity) event.getNPC().getNavigator().getEntityTarget().getTarget();
        // and that entity is not dead, trigger "on attack" command
        if (event.getNPC().getNavigator().getEntityTarget().isAggressive() && !entity.isDead()) {
            dPlayer player = null;
            // Check if the entity attacked by this NPC is a player
            if (entity instanceof Player) {
                player = dPlayer.mirrorBukkitPlayer((Player) entity);
            }
            // <--[action]
            // @Actions
            // attack
            // attack on <entity>
            //
            // @Triggers when the NPC is about to attack an enemy.
            //
            // @Context
            // None
            //
            // -->
            npc.action("attack", player);
            npc.action("attack on " + entity.getType().toString(), player);
        }
        previousLocations.put(event.getNPC().getId(), npc.getLocation());
    }
}
Also used : AssignmentTrait(net.aufdemrand.denizen.npc.traits.AssignmentTrait) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) net.aufdemrand.denizen.objects.dPlayer(net.aufdemrand.denizen.objects.dPlayer) net.aufdemrand.denizen.objects.dNPC(net.aufdemrand.denizen.objects.dNPC) BukkitScriptEntryData(net.aufdemrand.denizen.BukkitScriptEntryData) net.aufdemrand.denizen.objects.dPlayer(net.aufdemrand.denizen.objects.dPlayer) EventHandler(org.bukkit.event.EventHandler)

Example 4 with LivingEntity

use of org.bukkit.entity.LivingEntity 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 5 with LivingEntity

use of org.bukkit.entity.LivingEntity 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)

Aggregations

LivingEntity (org.bukkit.entity.LivingEntity)324 Entity (org.bukkit.entity.Entity)170 Player (org.bukkit.entity.Player)123 Location (org.bukkit.Location)72 EventHandler (org.bukkit.event.EventHandler)64 Vector (org.bukkit.util.Vector)60 ItemStack (org.bukkit.inventory.ItemStack)47 ArrayList (java.util.ArrayList)41 ISoliniaLivingEntity (com.solinia.solinia.Interfaces.ISoliniaLivingEntity)37 PotionEffect (org.bukkit.potion.PotionEffect)34 CoreStateInitException (com.solinia.solinia.Exceptions.CoreStateInitException)30 Block (org.bukkit.block.Block)24 Mage (com.elmakers.mine.bukkit.api.magic.Mage)22 Projectile (org.bukkit.entity.Projectile)20 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)18 FixedMetadataValue (org.bukkit.metadata.FixedMetadataValue)17 ISoliniaPlayer (com.solinia.solinia.Interfaces.ISoliniaPlayer)16 Target (com.elmakers.mine.bukkit.utility.Target)15 World (org.bukkit.World)14 Creature (org.bukkit.entity.Creature)14