Search in sources :

Example 76 with EntityTag

use of com.denizenscript.denizen.objects.EntityTag in project Denizen-For-Bukkit by DenizenScript.

the class EquipCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    Map<String, ItemTag> equipment = (Map<String, ItemTag>) scriptEntry.getObject("equipment");
    List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), db("entities", entities), db("equipment", equipment));
    }
    for (EntityTag entity : entities) {
        if (entity.isGeneric()) {
            Debug.echoError(scriptEntry, "Cannot equip generic entity " + entity.identify() + "!");
        } else if (entity.isCitizensNPC()) {
            NPCTag 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();
                    // TODO: Citizens API for this blob?
                    if (livingEntity instanceof AbstractHorse) {
                        if (equipment.get("saddle") != null) {
                            ((AbstractHorse) livingEntity).getInventory().setSaddle(equipment.get("saddle").getItemStack());
                        }
                        if (equipment.get("horse_armor") != null) {
                            if (((AbstractHorse) livingEntity).getInventory() instanceof HorseInventory) {
                                ((HorseInventory) ((AbstractHorse) livingEntity).getInventory()).setArmor(equipment.get("horse_armor").getItemStack());
                            }
                        }
                    } else if (livingEntity instanceof Steerable) {
                        if (equipment.get("saddle") != null) {
                            ItemTag saddle = equipment.get("saddle");
                            if (saddle.getBukkitMaterial() == Material.SADDLE) {
                                ((Steerable) livingEntity).setSaddle(true);
                            } else {
                                ((Steerable) livingEntity).setSaddle(false);
                            }
                        }
                    }
                }
            }
        } else {
            LivingEntity livingEntity = entity.getLivingEntity();
            if (livingEntity != null) {
                if (livingEntity instanceof AbstractHorse) {
                    if (equipment.get("saddle") != null) {
                        ((AbstractHorse) livingEntity).getInventory().setSaddle(equipment.get("saddle").getItemStack());
                    }
                    if (equipment.get("horse_armor") != null) {
                        if (((AbstractHorse) livingEntity).getInventory() instanceof HorseInventory) {
                            ((HorseInventory) ((AbstractHorse) livingEntity).getInventory()).setArmor(equipment.get("horse_armor").getItemStack());
                        }
                    }
                } else if (livingEntity instanceof Steerable) {
                    if (equipment.get("saddle") != null) {
                        ItemTag saddle = equipment.get("saddle");
                        if (saddle.getBukkitMaterial() == Material.SADDLE) {
                            ((Steerable) livingEntity).setSaddle(true);
                        } else {
                            ((Steerable) livingEntity).setSaddle(false);
                        }
                    }
                } else {
                    if (equipment.get("hand") != null) {
                        livingEntity.getEquipment().setItemInMainHand(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) {
                        livingEntity.getEquipment().setItemInOffHand(equipment.get("offhand").getItemStack());
                    }
                }
            }
        }
    }
}
Also used : AbstractHorse(org.bukkit.entity.AbstractHorse) LivingEntity(org.bukkit.entity.LivingEntity) Steerable(org.bukkit.entity.Steerable) Equipment(net.citizensnpcs.api.trait.trait.Equipment) NPCTag(com.denizenscript.denizen.objects.NPCTag) EntityTag(com.denizenscript.denizen.objects.EntityTag) HorseInventory(org.bukkit.inventory.HorseInventory) ItemTag(com.denizenscript.denizen.objects.ItemTag)

Example 77 with EntityTag

use of com.denizenscript.denizen.objects.EntityTag in project Denizen-For-Bukkit by DenizenScript.

the class FollowCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ElementTag stop = scriptEntry.getElement("stop");
    ElementTag lead = scriptEntry.getElement("lead");
    ElementTag maxRange = scriptEntry.getElement("max");
    ElementTag allowWander = scriptEntry.getElement("allow_wander");
    ElementTag speed = scriptEntry.getElement("speed");
    ElementTag noTeleport = scriptEntry.getElement("no_teleport");
    ListTag entities = scriptEntry.getObjectTag("entities");
    EntityTag target = scriptEntry.getObjectTag("target");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), Utilities.getEntryPlayer(scriptEntry), (!stop.asBoolean() ? db("Action", "FOLLOW") : db("Action", "STOP")), lead, noTeleport, maxRange, allowWander, entities, target);
    }
    for (EntityTag entity : entities.filter(EntityTag.class, scriptEntry)) {
        if (entity.isCitizensNPC()) {
            NPCTag npc = entity.getDenizenNPC();
            if (lead != null) {
                npc.getNavigator().getLocalParameters().distanceMargin(lead.asDouble());
            }
            if (speed != null) {
                npc.getNavigator().getLocalParameters().speedModifier(speed.asFloat());
            }
            if (noTeleport != null && noTeleport.asBoolean()) {
                npc.getNavigator().getLocalParameters().stuckAction(null);
            }
            if (stop.asBoolean()) {
                npc.getNavigator().cancelNavigation();
            } else {
                npc.getNavigator().setTarget(target.getBukkitEntity(), false);
            }
        } else {
            if (stop.asBoolean()) {
                NMSHandler.getEntityHelper().stopFollowing(entity.getBukkitEntity());
            } else {
                NMSHandler.getEntityHelper().follow(target.getBukkitEntity(), entity.getBukkitEntity(), speed != null ? speed.asDouble() : 0.3, lead != null ? lead.asDouble() : 5, maxRange != null ? maxRange.asDouble() : 8, allowWander.asBoolean(), noTeleport == null || !noTeleport.asBoolean());
            }
        }
    }
}
Also used : NPCTag(com.denizenscript.denizen.objects.NPCTag) EntityTag(com.denizenscript.denizen.objects.EntityTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag)

Example 78 with EntityTag

use of com.denizenscript.denizen.objects.EntityTag in project Denizen-For-Bukkit by DenizenScript.

the class HealCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
    if (entities == null) {
        return;
    }
    ElementTag amountelement = scriptEntry.getElement("amount");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), amountelement, db("entities", entities));
    }
    if (amountelement.asDouble() == -1) {
        for (EntityTag entity : entities) {
            if (entity.isLivingEntity()) {
                entity.getLivingEntity().setHealth(entity.getLivingEntity().getMaxHealth());
            }
        }
    } else {
        double amount = amountelement.asDouble();
        for (EntityTag entity : entities) {
            if (entity.getLivingEntity().getHealth() + amount < entity.getLivingEntity().getMaxHealth()) {
                entity.getLivingEntity().setHealth(entity.getLivingEntity().getHealth() + amount);
            } else {
                entity.getLivingEntity().setHealth(entity.getLivingEntity().getMaxHealth());
            }
        }
    }
}
Also used : EntityTag(com.denizenscript.denizen.objects.EntityTag) List(java.util.List) ArrayList(java.util.ArrayList) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Example 79 with EntityTag

use of com.denizenscript.denizen.objects.EntityTag in project Denizen-For-Bukkit by DenizenScript.

the class HurtCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
    EntityTag source = scriptEntry.getObjectTag("source");
    ElementTag amountElement = scriptEntry.getElement("amount");
    ElementTag cause = scriptEntry.getElement("cause");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), amountElement, db("entities", entities), cause, source);
    }
    double amount = amountElement.asDouble();
    for (EntityTag entity : entities) {
        if (entity.getLivingEntity() == null) {
            Debug.echoDebug(scriptEntry, entity + " is not a living entity!");
            continue;
        }
        EntityDamageEvent.DamageCause causeEnum = cause == null ? null : EntityDamageEvent.DamageCause.valueOf(cause.asString().toUpperCase());
        NMSHandler.getEntityHelper().damage(entity.getLivingEntity(), (float) amount, source == null ? null : source.getBukkitEntity(), causeEnum);
    }
}
Also used : EntityTag(com.denizenscript.denizen.objects.EntityTag) ArrayList(java.util.ArrayList) List(java.util.List) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent)

Example 80 with EntityTag

use of com.denizenscript.denizen.objects.EntityTag in project Denizen-For-Bukkit by DenizenScript.

the class HurtCommand method parseArgs.

// <--[command]
// @Name Hurt
// @Syntax hurt (<#.#>) ({player}/<entity>|...) (cause:<cause>) (source:<entity>)
// @Required 0
// @Maximum 4
// @Short Hurts the player or a list of entities.
// @Synonyms Damage,Injure
// @Group entity
// 
// @Description
// Does damage to a list of entities, or to any single entity.
// 
// If no entities are specified: if there is a linked player, the command targets that. If there is no linked
// player but there is a linked NPC, the command targets the NPC. If neither is available, the command will error.
// 
// Does a specified amount of damage usually, but, if no damage is specified, does precisely 1HP worth of damage (half a heart).
// 
// Optionally, specify (source:<entity>) to make the system treat that entity as the attacker.
// 
// You may also specify a damage cause to fire a proper damage event with the given cause, only doing the damage if the event wasn't cancelled.
// Calculates the 'final damage' rather than using the raw damage input number. See <@link language damage cause> for damage causes.
// 
// Using a valid 'cause' value is best when trying to replicate natural damage, excluding it is best when trying to force the raw damage through.
// Note that using invalid or impossible causes may lead to bugs
// 
// @Tags
// <EntityTag.health>
// <EntityTag.last_damage.amount>
// <EntityTag.last_damage.cause>
// <EntityTag.last_damage.duration>
// <EntityTag.last_damage.max_duration>
// 
// @Usage
// Use to hurt the player for 1 HP.
// - hurt
// 
// @Usage
// Use to hurt the NPC for 5 HP.
// - hurt 5 <npc>
// 
// @Usage
// Use to cause the player to hurt the NPC for all its health (if unarmored).
// - hurt <npc.health> <npc> cause:CUSTOM source:<player>
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
    for (Argument arg : scriptEntry) {
        if (!scriptEntry.hasObject("amount") && (arg.matchesFloat() || arg.matchesInteger())) {
            scriptEntry.addObject("amount", arg.asElement());
        } else if (!scriptEntry.hasObject("source") && arg.matchesPrefix("source", "s") && arg.matchesArgumentType(EntityTag.class)) {
            scriptEntry.addObject("source", arg.asType(EntityTag.class));
        } else if (!scriptEntry.hasObject("entities") && arg.matchesArgumentList(EntityTag.class)) {
            scriptEntry.addObject("entities", arg.asType(ListTag.class).filter(EntityTag.class, scriptEntry));
        } else if (!scriptEntry.hasObject("cause") && arg.matchesEnum(EntityDamageEvent.DamageCause.class)) {
            scriptEntry.addObject("cause", arg.asElement());
        } else if (!scriptEntry.hasObject("source_once") && arg.matches("source_once")) {
            Deprecations.hurtSourceOne.warn(scriptEntry);
            scriptEntry.addObject("source_once", new ElementTag(true));
        } else {
            arg.reportUnhandled();
        }
    }
    if (!scriptEntry.hasObject("amount")) {
        scriptEntry.addObject("amount", new ElementTag(1.0d));
    }
    if (!scriptEntry.hasObject("entities")) {
        List<EntityTag> entities = new ArrayList<>();
        if (Utilities.getEntryPlayer(scriptEntry) != null) {
            entities.add(Utilities.getEntryPlayer(scriptEntry).getDenizenEntity());
        } else if (Utilities.getEntryNPC(scriptEntry) != null) {
            entities.add(Utilities.getEntryNPC(scriptEntry).getDenizenEntity());
        } else {
            throw new InvalidArgumentsException("No valid target entities found.");
        }
        scriptEntry.addObject("entities", entities);
    }
}
Also used : Argument(com.denizenscript.denizencore.objects.Argument) ArrayList(java.util.ArrayList) EntityTag(com.denizenscript.denizen.objects.EntityTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent)

Aggregations

EntityTag (com.denizenscript.denizen.objects.EntityTag)142 EventHandler (org.bukkit.event.EventHandler)81 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)56 LocationTag (com.denizenscript.denizen.objects.LocationTag)45 List (java.util.List)27 Entity (org.bukkit.entity.Entity)25 ItemTag (com.denizenscript.denizen.objects.ItemTag)17 ListTag (com.denizenscript.denizencore.objects.core.ListTag)17 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)15 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)12 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)11 NPCTag (com.denizenscript.denizen.objects.NPCTag)10 ArrayList (java.util.ArrayList)10 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)10 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)9 Player (org.bukkit.entity.Player)9 MaterialTag (com.denizenscript.denizen.objects.MaterialTag)8 Argument (com.denizenscript.denizencore.objects.Argument)8 LivingEntity (org.bukkit.entity.LivingEntity)8 Item (org.bukkit.entity.Item)7