Search in sources :

Example 61 with ElementTag

use of com.denizenscript.denizencore.objects.core.ElementTag in project Denizen-For-Bukkit by DenizenScript.

the class CastCommand method parseArgs.

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
    for (Argument arg : scriptEntry) {
        if (!scriptEntry.hasObject("remove") && arg.matches("remove", "cancel")) {
            scriptEntry.addObject("remove", new ElementTag(true));
        } else if (!scriptEntry.hasObject("ambient") && arg.matches("no_ambient")) {
            scriptEntry.addObject("ambient", new ElementTag(false));
        } else if (!scriptEntry.hasObject("show_particles") && arg.matches("hide_particles")) {
            scriptEntry.addObject("show_particles", new ElementTag(false));
        } else if (!scriptEntry.hasObject("show_icon") && arg.matches("no_icon")) {
            scriptEntry.addObject("show_icon", new ElementTag(false));
        } else if (!scriptEntry.hasObject("duration") && arg.matchesPrefix("duration", "d") && arg.matchesArgumentType(DurationTag.class)) {
            scriptEntry.addObject("duration", arg.asType(DurationTag.class));
        } else if (!scriptEntry.hasObject("amplifier") && arg.matchesPrefix("power", "p", "amplifier", "a") && arg.matchesFloat()) {
            scriptEntry.addObject("amplifier", arg.asElement());
        } else if (!scriptEntry.hasObject("effect") && PotionEffectType.getByName(arg.asElement().asString()) != null) {
            scriptEntry.addObject("effect", PotionEffectType.getByName(arg.asElement().asString()));
        } else if (!scriptEntry.hasObject("entities") && arg.matchesArgumentList(EntityTag.class)) {
            scriptEntry.addObject("entities", arg.asType(ListTag.class).filter(EntityTag.class, scriptEntry));
        } else {
            arg.reportUnhandled();
        }
    }
    if (!scriptEntry.hasObject("entities")) {
        scriptEntry.defaultObject("entities", Utilities.entryDefaultEntityList(scriptEntry, true));
    }
    if (!scriptEntry.hasObject("effect")) {
        throw new InvalidArgumentsException("Must specify a valid PotionType!");
    }
    scriptEntry.defaultObject("duration", new DurationTag(60));
    scriptEntry.defaultObject("amplifier", new ElementTag(1));
    scriptEntry.defaultObject("remove", new ElementTag(false));
    scriptEntry.defaultObject("show_particles", new ElementTag(true));
    scriptEntry.defaultObject("ambient", new ElementTag(true));
    scriptEntry.defaultObject("show_icon", new ElementTag(true));
}
Also used : ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException)

Example 62 with ElementTag

use of com.denizenscript.denizencore.objects.core.ElementTag in project Denizen-For-Bukkit by DenizenScript.

the class FakeEquipCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    NetworkInterceptHelper.enable();
    EquipmentOverride equipment = (EquipmentOverride) scriptEntry.getObject("equipment");
    List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
    List<PlayerTag> playersFor = (List<PlayerTag>) scriptEntry.getObject("for");
    ElementTag reset = scriptEntry.getElement("reset");
    DurationTag duration = scriptEntry.getObjectTag("duration");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), db("entities", entities), db("equipment", equipment), reset, duration, db("for", playersFor));
    }
    boolean isReset = reset != null && reset.asBoolean();
    for (PlayerTag player : playersFor) {
        HashMap<UUID, EquipmentOverride> playersMap = overrides.computeIfAbsent(player.getUUID(), (k) -> new HashMap<>());
        for (EntityTag entity : entities) {
            if (entity.isGeneric()) {
                Debug.echoError(scriptEntry, "Cannot equip generic entity " + entity.identify() + "!");
                continue;
            }
            LivingEntity livingEntity = entity.getLivingEntity();
            if (livingEntity == null) {
                Debug.echoError(scriptEntry, "Cannot equip invalid/non-living entity " + entity.identify() + "!");
                continue;
            }
            EquipmentOverride entityData;
            if (isReset) {
                entityData = playersMap.remove(entity.getUUID());
                if (playersMap.isEmpty()) {
                    overrides.remove(player.getUUID());
                }
            } else {
                entityData = playersMap.computeIfAbsent(entity.getUUID(), (k) -> new EquipmentOverride());
                entityData.copyFrom(equipment);
            }
            if (entityData != null) {
                if (entityData.cancelTask != null && !entityData.cancelTask.isCancelled()) {
                    entityData.cancelTask.cancel();
                    entityData.cancelTask = null;
                }
                if (duration != null && duration.getTicks() > 0) {
                    entityData.cancelTask = new BukkitRunnable() {

                        @Override
                        public void run() {
                            entityData.cancelTask = null;
                            HashMap<UUID, EquipmentOverride> playersMap = overrides.get(player.getUUID());
                            if (playersMap != null) {
                                if (playersMap.remove(entity.getUUID()) != null) {
                                    if (playersMap.isEmpty()) {
                                        overrides.remove(player.getUUID());
                                    }
                                    NMSHandler.getPacketHelper().resetEquipment(player.getPlayerEntity(), livingEntity);
                                }
                            }
                        }
                    }.runTaskLater(Denizen.getInstance(), duration.getTicks());
                }
            }
            NMSHandler.getPacketHelper().resetEquipment(player.getPlayerEntity(), livingEntity);
        }
    }
}
Also used : ListTag(com.denizenscript.denizencore.objects.core.ListTag) NetworkInterceptHelper(com.denizenscript.denizen.utilities.packets.NetworkInterceptHelper) Utilities(com.denizenscript.denizen.utilities.Utilities) java.util(java.util) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) NMSHandler(com.denizenscript.denizen.nms.NMSHandler) ItemTag(com.denizenscript.denizen.objects.ItemTag) Player(org.bukkit.entity.Player) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException) LivingEntity(org.bukkit.entity.LivingEntity) Argument(com.denizenscript.denizencore.objects.Argument) Denizen(com.denizenscript.denizen.Denizen) BukkitTask(org.bukkit.scheduler.BukkitTask) EntityTag(com.denizenscript.denizen.objects.EntityTag) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) Debug(com.denizenscript.denizen.utilities.debugging.Debug) AbstractCommand(com.denizenscript.denizencore.scripts.commands.AbstractCommand) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag) LivingEntity(org.bukkit.entity.LivingEntity) EntityTag(com.denizenscript.denizen.objects.EntityTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Example 63 with ElementTag

use of com.denizenscript.denizencore.objects.core.ElementTag in project Denizen-For-Bukkit by DenizenScript.

the class FlyCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) {
    LocationTag origin = scriptEntry.getObjectTag("origin");
    List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
    final List<LocationTag> destinations = scriptEntry.hasObject("destinations") ? (List<LocationTag>) scriptEntry.getObject("destinations") : new ArrayList<>();
    // Set freeflight to true only if there are no destinations
    final boolean freeflight = destinations.size() < 1;
    EntityTag controller = scriptEntry.getObjectTag("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 (EntityTag entity : entities) {
                if (entity.isPlayer()) {
                    // the controller
                    if (entities.get(entities.size() - 1) != entity) {
                        controller = entity;
                        if (scriptEntry.dbCallShouldDebug()) {
                            Debug.echoDebug(scriptEntry, "Flight control defaulting to " + controller);
                        }
                        break;
                    }
                }
            }
            // If the controller is still null, we cannot continue
            if (controller == null) {
                if (scriptEntry.dbCallShouldDebug()) {
                    Debug.echoDebug(scriptEntry, "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 (EntityTag entity : entities) {
                if (entity.identify().equals(controller.identify())) {
                    found = true;
                    break;
                }
            }
            // Add the controller to the entity list
            if (!found) {
                if (scriptEntry.dbCallShouldDebug()) {
                    Debug.echoDebug(scriptEntry, "Adding controller " + controller + " to flying entities.");
                }
                entities.add(0, controller);
            }
        }
    }
    final double speed = ((ElementTag) scriptEntry.getObject("speed")).asDouble();
    final float rotationThreshold = ((ElementTag) scriptEntry.getObject("rotation_threshold")).asFloat();
    boolean cancel = scriptEntry.hasObject("cancel");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), (cancel ? db("cancel", true) : ""), origin, db("entities", entities), db("speed", speed), db("rotation threshold degrees", rotationThreshold), (freeflight ? controller : db("destinations", destinations)));
    }
    if (!cancel) {
        for (EntityTag entity : entities) {
            entity.spawnAt(origin);
        }
        Position.mount(Conversion.convertEntities(entities));
    } else {
        Position.dismount(Conversion.convertEntities(entities));
        return;
    }
    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) {
                // flying where the controller is looking
                if (!entity.isEmpty() && finalController.isInsideVehicle()) {
                    location = finalController.getEyeLocation().add(finalController.getEyeLocation().getDirection().multiply(30));
                } else {
                    flying = false;
                }
            } else {
                // as there are destinations left
                if (destinations.size() > 0) {
                    location = destinations.get(0);
                } else {
                    flying = false;
                }
            }
            if (flying && entity.isValid()) {
                // when it really needs to
                if (!NMSHandler.getEntityHelper().isFacingLocation(entity, location, rotationThreshold)) {
                    NMSHandler.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);
                // to be the case
                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(Denizen.getInstance(), 0, 3);
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) LocationTag(com.denizenscript.denizen.objects.LocationTag) LivingEntity(org.bukkit.entity.LivingEntity) EntityTag(com.denizenscript.denizen.objects.EntityTag) ArrayList(java.util.ArrayList) List(java.util.List) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) Vector(org.bukkit.util.Vector) Location(org.bukkit.Location)

Example 64 with ElementTag

use of com.denizenscript.denizencore.objects.core.ElementTag in project Denizen-For-Bukkit by DenizenScript.

the class HealthCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ElementTag quantity = scriptEntry.getElement("quantity");
    ElementTag action = scriptEntry.getElement("action");
    ElementTag heal = scriptEntry.getElement("heal");
    List<EntityTag> targets = (List<EntityTag>) scriptEntry.getObject("target");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), quantity, action, heal, db("target", targets));
    }
    if (quantity == null && action == null) {
        Debug.echoError(scriptEntry, "Null quantity!");
    }
    if (action == null) {
        action = new ElementTag(true);
    }
    for (EntityTag target : targets) {
        if (target.isCitizensNPC()) {
            if (action.asString().equalsIgnoreCase("true")) {
                target.getDenizenNPC().getCitizen().addTrait(HealthTrait.class);
            } else if (action.asString().equalsIgnoreCase("false")) {
                target.getDenizenNPC().getCitizen().removeTrait(HealthTrait.class);
            } else if (target.getDenizenNPC().getCitizen().hasTrait(HealthTrait.class)) {
                target.getDenizenNPC().getCitizen().removeTrait(HealthTrait.class);
            } else {
                target.getDenizenNPC().getCitizen().addTrait(HealthTrait.class);
            }
        }
        if (quantity != null) {
            if (target.isCitizensNPC()) {
                if (target.getDenizenNPC().getCitizen().hasTrait(HealthTrait.class)) {
                    HealthTrait trait = target.getDenizenNPC().getCitizen().getOrAddTrait(HealthTrait.class);
                    trait.setMaxhealth(quantity.asInt());
                    if (heal.asBoolean()) {
                        trait.setHealth(quantity.asDouble());
                    }
                } else {
                    Debug.echoError(scriptEntry, "NPC doesn't have health trait!");
                }
            } else if (target.isLivingEntity()) {
                target.getLivingEntity().setMaxHealth(quantity.asDouble());
                if (heal.asBoolean()) {
                    target.getLivingEntity().setHealth(quantity.asDouble());
                }
            } else {
                Debug.echoError(scriptEntry, "Entity '" + target.identify() + "'is not alive!");
            }
        }
    }
}
Also used : EntityTag(com.denizenscript.denizen.objects.EntityTag) List(java.util.List) HealthTrait(com.denizenscript.denizen.npc.traits.HealthTrait) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Example 65 with ElementTag

use of com.denizenscript.denizencore.objects.core.ElementTag in project Denizen-For-Bukkit by DenizenScript.

the class GiveCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ElementTag unlimit_stack_size = scriptEntry.getElement("unlimit_stack_size");
    InventoryTag inventory = scriptEntry.getObjectTag("inventory");
    ElementTag quantity = scriptEntry.getElement("quantity");
    Type type = (Type) scriptEntry.getObject("type");
    ElementTag slot = scriptEntry.getElement("slot");
    Object items_object = scriptEntry.getObject("items");
    List<ItemTag> items = null;
    if (items_object != null) {
        items = (List<ItemTag>) items_object;
    }
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), db("Type", type.name()), inventory, quantity, unlimit_stack_size, db("Items", items), slot);
    }
    switch(type) {
        case MONEY:
            if (Depends.economy != null) {
                Depends.economy.depositPlayer(Utilities.getEntryPlayer(scriptEntry).getOfflinePlayer(), quantity.asDouble());
            } else {
                Debug.echoError("No economy loaded! Have you installed Vault and a compatible economy plugin?");
            }
            break;
        case EXP:
            Utilities.getEntryPlayer(scriptEntry).getPlayerEntity().giveExp(quantity.asInt());
            break;
        case ITEM:
            boolean set_quantity = scriptEntry.hasObject("set_quantity");
            boolean limited = !unlimit_stack_size.asBoolean();
            for (ItemTag item : items) {
                ItemStack is = item.getItemStack();
                if (is.getType() == Material.AIR) {
                    Debug.echoError("Cannot give air!");
                    continue;
                }
                if (set_quantity) {
                    is.setAmount(quantity.asInt());
                }
                int slotId = SlotHelper.nameToIndexFor(slot.asString(), inventory.getInventory().getHolder());
                if (slotId == -1) {
                    Debug.echoError(scriptEntry, "The input '" + slot.asString() + "' is not a valid slot!");
                    return;
                }
                List<ItemStack> leftovers = inventory.addWithLeftovers(slotId, limited, is);
                if (!leftovers.isEmpty()) {
                    Debug.echoDebug(scriptEntry, "The inventory didn't have enough space, the rest of the items have been placed on the floor.");
                    for (ItemStack leftoverItem : leftovers) {
                        inventory.getLocation().getWorld().dropItem(inventory.getLocation(), leftoverItem);
                    }
                }
            }
            break;
    }
}
Also used : ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ItemTag(com.denizenscript.denizen.objects.ItemTag) ItemStack(org.bukkit.inventory.ItemStack) InventoryTag(com.denizenscript.denizen.objects.InventoryTag)

Aggregations

ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)237 ListTag (com.denizenscript.denizencore.objects.core.ListTag)86 EntityTag (com.denizenscript.denizen.objects.EntityTag)66 LocationTag (com.denizenscript.denizen.objects.LocationTag)49 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)48 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)43 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)40 List (java.util.List)40 Argument (com.denizenscript.denizencore.objects.Argument)28 Player (org.bukkit.entity.Player)28 MapTag (com.denizenscript.denizencore.objects.core.MapTag)27 EventHandler (org.bukkit.event.EventHandler)26 NPCTag (com.denizenscript.denizen.objects.NPCTag)24 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)22 ItemTag (com.denizenscript.denizen.objects.ItemTag)19 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)18 ArrayList (java.util.ArrayList)16 Entity (org.bukkit.entity.Entity)16 MaterialTag (com.denizenscript.denizen.objects.MaterialTag)12 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)12