Search in sources :

Example 21 with Element

use of net.aufdemrand.denizencore.objects.Element in project Denizen-For-Bukkit by DenizenScript.

the class FlagCommand method parseArgs.

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
    boolean specified_target = false;
    for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
        // specified amount of time
        if (!scriptEntry.hasObject("duration") && arg.matchesPrefix("duration", "d") && arg.matchesArgumentType(Duration.class)) {
            scriptEntry.addObject("duration", arg.asType(Duration.class));
        } else // Also allow attached dObjects to be specified...
        if (!scriptEntry.hasObject("flag_target") && arg.matches("npc", "denizen")) {
            specified_target = true;
            scriptEntry.addObject("flag_target", ((BukkitScriptEntryData) scriptEntry.entryData).getNPC());
        } else if (!scriptEntry.hasObject("flag_target") && arg.matches("global", "server")) {
            specified_target = true;
            scriptEntry.addObject("flag_target", Element.SERVER);
        } else if (!scriptEntry.hasObject("flag_target") && arg.matches("player")) {
            specified_target = true;
            scriptEntry.addObject("flag_target", ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer());
        } else // as the name of the flag..
        if (!scriptEntry.hasObject("flag_target") && arg.startsWith("n@") && !arg.hasPrefix()) {
            if (// TODO: Optimize
            dNPC.valueOf(arg.getValue()) == null) {
                throw new InvalidArgumentsException("Invalid NPC target.");
            }
            specified_target = true;
            scriptEntry.addObject("flag_target", arg.asType(dNPC.class));
        } else if (!scriptEntry.hasObject("flag_target") && arg.startsWith("p@") && !arg.hasPrefix()) {
            if (// TODO: Optimize
            dPlayer.valueOf(arg.getValue()) == null) {
                throw new InvalidArgumentsException("Invalid Player target.");
            }
            specified_target = true;
            scriptEntry.addObject("flag_target", arg.asType(dPlayer.class));
        } else if (!scriptEntry.hasObject("flag_target") && !arg.hasPrefix()) {
            if (// TODO: Optimize
            dEntity.valueOf(arg.getValue()) == null) {
                throw new InvalidArgumentsException("Invalid Entity target.");
            }
            specified_target = true;
            scriptEntry.addObject("flag_target", arg.asType(dEntity.class));
        } else // Check if setting a boolean
        if (!scriptEntry.hasObject("flag_name") && arg.raw_value.split(":", 3).length == 1) {
            scriptEntry.addObject("action", FlagManager.Action.SET_BOOLEAN);
            scriptEntry.addObject("value", Element.TRUE);
            scriptEntry.addObject("flag_name", arg.asElement());
        } else // Check for flag_name:value/action
        if (!scriptEntry.hasObject("flag_name") && arg.raw_value.split(":", 3).length == 2) {
            String[] flagArgs = arg.raw_value.split(":", 2);
            scriptEntry.addObject("flag_name", new Element(flagArgs[0].toUpperCase()));
            if (flagArgs[1].equals("++") || flagArgs[1].equals("+")) {
                scriptEntry.addObject("action", FlagManager.Action.INCREASE);
                scriptEntry.addObject("value", new Element(1));
            } else if (flagArgs[1].equals("--") || flagArgs[1].equals("-")) {
                scriptEntry.addObject("action", FlagManager.Action.DECREASE);
                scriptEntry.addObject("value", new Element(1));
            } else if (flagArgs[1].equals("!")) {
                scriptEntry.addObject("action", FlagManager.Action.DELETE);
                scriptEntry.addObject("value", Element.FALSE);
            } else if (flagArgs[1].equals("<-")) {
                scriptEntry.addObject("action", FlagManager.Action.REMOVE);
                scriptEntry.addObject("value", Element.FALSE);
            } else {
                // No ACTION, we're just setting a value...
                scriptEntry.addObject("action", FlagManager.Action.SET_VALUE);
                scriptEntry.addObject("value", new Element(flagArgs[1]));
            }
        } else // Check for flag_name:action:value
        if (!scriptEntry.hasObject("flag_name") && arg.raw_value.split(":", 3).length == 3) {
            String[] flagArgs = arg.raw_value.split(":", 3);
            scriptEntry.addObject("flag_name", new Element(flagArgs[0].toUpperCase()));
            if (flagArgs[1].equals("->")) {
                scriptEntry.addObject("action", FlagManager.Action.INSERT);
            } else if (flagArgs[1].equals("<-")) {
                scriptEntry.addObject("action", FlagManager.Action.REMOVE);
            } else if (flagArgs[1].equals("||") || flagArgs[1].equals("|")) {
                scriptEntry.addObject("action", FlagManager.Action.SPLIT);
            } else if (flagArgs[1].equals("++") || flagArgs[1].equals("+")) {
                scriptEntry.addObject("action", FlagManager.Action.INCREASE);
            } else if (flagArgs[1].equals("--") || flagArgs[1].equals("-")) {
                scriptEntry.addObject("action", FlagManager.Action.DECREASE);
            } else if (flagArgs[1].equals("**") || flagArgs[1].equals("*")) {
                scriptEntry.addObject("action", FlagManager.Action.MULTIPLY);
            } else if (flagArgs[1].equals("//") || flagArgs[1].equals("/")) {
                scriptEntry.addObject("action", FlagManager.Action.DIVIDE);
            } else {
                scriptEntry.addObject("action", FlagManager.Action.SET_VALUE);
                scriptEntry.addObject("value", new Element(arg.raw_value.split(":", 2)[1]));
                continue;
            }
            scriptEntry.addObject("value", new Element(flagArgs[2]));
        } else {
            arg.reportUnhandled();
        }
    }
    // Set defaults
    if (!specified_target) {
        scriptEntry.defaultObject("flag_target", ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer());
    }
    // Check required arguments
    if (!scriptEntry.hasObject("action")) {
        throw new InvalidArgumentsException("Must specify a flag action or value.");
    }
    if (!scriptEntry.hasObject("flag_target")) {
        throw new InvalidArgumentsException("Must specify a flag target!");
    }
}
Also used : net.aufdemrand.denizen.objects.dNPC(net.aufdemrand.denizen.objects.dNPC) BukkitScriptEntryData(net.aufdemrand.denizen.BukkitScriptEntryData) net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) net.aufdemrand.denizencore.objects.aH(net.aufdemrand.denizencore.objects.aH) Element(net.aufdemrand.denizencore.objects.Element) Duration(net.aufdemrand.denizencore.objects.Duration) InvalidArgumentsException(net.aufdemrand.denizencore.exceptions.InvalidArgumentsException)

Example 22 with Element

use of net.aufdemrand.denizencore.objects.Element 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 23 with Element

use of net.aufdemrand.denizencore.objects.Element in project Denizen-For-Bukkit by DenizenScript.

the class FollowCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    // Get objects
    Element stop = scriptEntry.getElement("stop");
    Element lead = scriptEntry.getElement("lead");
    Element maxRange = scriptEntry.getElement("max");
    Element allowWander = scriptEntry.getElement("allow_wander");
    Element speed = scriptEntry.getElement("speed");
    dList entities = scriptEntry.getdObject("entities");
    dEntity target = scriptEntry.getdObject("target");
    // Report to dB
    dB.report(scriptEntry, getName(), (((BukkitScriptEntryData) scriptEntry.entryData).getPlayer() != null ? ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().debug() : "") + (!stop.asBoolean() ? aH.debugObj("Action", "FOLLOW") : aH.debugObj("Action", "STOP")) + (lead != null ? lead.debug() : "") + (maxRange != null ? maxRange.debug() : "") + allowWander.debug() + entities.debug() + target.debug());
    for (dEntity entity : entities.filter(dEntity.class)) {
        if (entity.isCitizensNPC()) {
            dNPC npc = entity.getDenizenNPC();
            if (lead != null) {
                npc.getNavigator().getLocalParameters().distanceMargin(lead.asDouble());
            }
            if (speed != null) {
                npc.getNavigator().getLocalParameters().speedModifier(speed.asFloat());
            }
            if (stop.asBoolean()) {
                npc.getNavigator().cancelNavigation();
            } else {
                npc.getNavigator().setTarget(target.getBukkitEntity(), false);
            }
        } else {
            if (stop.asBoolean()) {
                NMSHandler.getInstance().getEntityHelper().stopFollowing(entity.getBukkitEntity());
            } else {
                NMSHandler.getInstance().getEntityHelper().follow(target.getBukkitEntity(), entity.getBukkitEntity(), speed != null ? speed.asDouble() : 0.3, lead != null ? lead.asDouble() : 5, maxRange != null ? maxRange.asDouble() : 8, allowWander.asBoolean());
            }
        }
    }
}
Also used : net.aufdemrand.denizen.objects.dNPC(net.aufdemrand.denizen.objects.dNPC) BukkitScriptEntryData(net.aufdemrand.denizen.BukkitScriptEntryData) net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) Element(net.aufdemrand.denizencore.objects.Element) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList)

Example 24 with Element

use of net.aufdemrand.denizencore.objects.Element in project Denizen-For-Bukkit by DenizenScript.

the class HealCommand method execute.

@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
    if (entities == null) {
        return;
    }
    Element amountelement = scriptEntry.getElement("amount");
    dB.report(scriptEntry, getName(), amountelement.debug() + aH.debugObj("entities", entities));
    if (amountelement.asDouble() == -1) {
        for (dEntity entity : entities) {
            if (entity.isLivingEntity()) {
                entity.getLivingEntity().setHealth(entity.getLivingEntity().getMaxHealth());
            }
        }
    } else {
        double amount = amountelement.asDouble();
        for (dEntity 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 : net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) Element(net.aufdemrand.denizencore.objects.Element) ArrayList(java.util.ArrayList) List(java.util.List) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList)

Example 25 with Element

use of net.aufdemrand.denizencore.objects.Element in project Denizen-For-Bukkit by DenizenScript.

the class HurtCommand method execute.

@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
    dEntity source = (dEntity) scriptEntry.getObject("source");
    Element amountElement = scriptEntry.getElement("amount");
    Element cause = scriptEntry.getElement("cause");
    dB.report(scriptEntry, getName(), amountElement.debug() + aH.debugList("entities", entities) + (cause == null ? "" : cause.debug()) + (source == null ? "" : source.debug()));
    double amount = amountElement.asDouble();
    for (dEntity entity : entities) {
        if (entity.getLivingEntity() == null) {
            dB.echoDebug(scriptEntry, entity + " is not a living entity!");
            continue;
        }
        if (cause == null) {
            if (source == null) {
                entity.getLivingEntity().damage(amount);
            } else {
                entity.getLivingEntity().damage(amount, source.getBukkitEntity());
            }
        } else {
            EntityDamageEvent ede = source == null ? new EntityDamageEvent(entity.getBukkitEntity(), EntityDamageEvent.DamageCause.valueOf(cause.asString().toUpperCase()), amount) : new EntityDamageByEntityEvent(source.getBukkitEntity(), entity.getBukkitEntity(), EntityDamageEvent.DamageCause.valueOf(cause.asString().toUpperCase()), amount);
            Bukkit.getPluginManager().callEvent(ede);
            if (!ede.isCancelled()) {
                if (source == null) {
                    entity.getLivingEntity().damage(ede.getFinalDamage());
                } else {
                    entity.getLivingEntity().damage(ede.getFinalDamage(), source.getBukkitEntity());
                }
            }
        }
    }
}
Also used : EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) Element(net.aufdemrand.denizencore.objects.Element) ArrayList(java.util.ArrayList) List(java.util.List) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent)

Aggregations

Element (net.aufdemrand.denizencore.objects.Element)166 net.aufdemrand.denizencore.objects.dList (net.aufdemrand.denizencore.objects.dList)74 net.aufdemrand.denizen.objects.dEntity (net.aufdemrand.denizen.objects.dEntity)49 net.aufdemrand.denizen.objects.dLocation (net.aufdemrand.denizen.objects.dLocation)46 BukkitScriptEntryData (net.aufdemrand.denizen.BukkitScriptEntryData)38 InvalidArgumentsException (net.aufdemrand.denizencore.exceptions.InvalidArgumentsException)38 EventHandler (org.bukkit.event.EventHandler)38 List (java.util.List)29 net.aufdemrand.denizencore.objects.aH (net.aufdemrand.denizencore.objects.aH)28 net.aufdemrand.denizen.objects.dPlayer (net.aufdemrand.denizen.objects.dPlayer)27 net.aufdemrand.denizencore.objects.dObject (net.aufdemrand.denizencore.objects.dObject)21 Duration (net.aufdemrand.denizencore.objects.Duration)20 CommandExecutionException (net.aufdemrand.denizencore.exceptions.CommandExecutionException)16 ArrayList (java.util.ArrayList)14 net.aufdemrand.denizen.objects.dItem (net.aufdemrand.denizen.objects.dItem)14 net.aufdemrand.denizen.objects.dNPC (net.aufdemrand.denizen.objects.dNPC)14 Player (org.bukkit.entity.Player)11 net.aufdemrand.denizen.objects.dWorld (net.aufdemrand.denizen.objects.dWorld)10 net.aufdemrand.denizencore.objects.dScript (net.aufdemrand.denizencore.objects.dScript)10 ScriptEntry (net.aufdemrand.denizencore.scripts.ScriptEntry)9