Search in sources :

Example 11 with Duration

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

the class ZapCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
    final dScript script = (dScript) scriptEntry.getObject("script");
    Duration duration = (Duration) scriptEntry.getObject("duration");
    dB.report(scriptEntry, getName(), ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().debug() + script.debug() + (scriptEntry.hasObject("step") ? scriptEntry.getElement("step").debug() : aH.debugObj("step", "++ (inc)")) + (duration != null ? duration.debug() : ""));
    String step = scriptEntry.hasObject("step") ? scriptEntry.getElement("step").asString() : null;
    // Let's get the current step for reference.
    String currentStep = InteractScriptHelper.getCurrentStep(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer(), script.getName());
    // Special-case for backwards compatibility: ability to use ZAP to count up steps.
    if (step == null) {
        // to '1' so it can be incremented next time.
        if (aH.matchesInteger(currentStep)) {
            step = String.valueOf(aH.getIntegerFrom(currentStep) + 1);
        } else {
            step = "1";
        }
    }
    if (step.equalsIgnoreCase(currentStep)) {
        dB.echoError(scriptEntry.getResidingQueue(), "Zapping to own current step!");
        return;
    }
    // ZAP for this script is taking place.
    if (durations.containsKey(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getSaveName() + "," + script.getName())) {
        try {
            DenizenAPI.getCurrentInstance().getServer().getScheduler().cancelTask(durations.get(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getSaveName() + "," + script.getName()));
        } catch (Exception e) {
        }
    }
    // One last thing... check for duration.
    if (duration != null && duration.getSeconds() > 0) {
        // If a DURATION is specified, the currentStep should be remembered and
        // restored after the duration.
        scriptEntry.addObject("step", new Element(currentStep));
        // And let's take away the duration that was set to avoid a re-duration
        // inception-ion-ion-ion-ion... ;)
        scriptEntry.addObject("duration", Duration.ZERO);
        // Now let's add a delayed task to set it back after the duration
        // Delays are in ticks, so let's multiply our duration (which is in seconds) by 20.
        // 20 ticks per second.
        long delay = (long) (duration.getSeconds() * 20);
        // Set delayed task and put id in a map
        dB.log("Setting delayed task 'RESET ZAP' for '" + script.identify() + "'");
        durations.put(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getSaveName() + "," + script.getName(), DenizenAPI.getCurrentInstance().getServer().getScheduler().scheduleSyncDelayedTask(DenizenAPI.getCurrentInstance(), new Runnable() {

            @Override
            public void run() {
                dB.log("Running delayed task 'RESET ZAP' for '" + script.identify() + "'");
                try {
                    durations.remove(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getSaveName() + "," + script.getName().toUpperCase());
                    execute(scriptEntry);
                } catch (CommandExecutionException e) {
                    dB.echoError(scriptEntry.getResidingQueue(), "Could not run delayed task!");
                    dB.echoError(scriptEntry.getResidingQueue(), e);
                }
            }
        }, delay));
    }
    //
    // FINALLY! ZAP! Change the step in Saves... your step is now ZAPPED!
    // Fun fact: ZAP is named in homage of ZZT-OOPs ZAP command. Google it.
    //
    DenizenAPI.getCurrentInstance().getSaves().set("Players." + ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getSaveName() + ".Scripts." + script.getName().toUpperCase() + "." + "Current Step", step);
}
Also used : BukkitScriptEntryData(net.aufdemrand.denizen.BukkitScriptEntryData) net.aufdemrand.denizencore.objects.dScript(net.aufdemrand.denizencore.objects.dScript) Element(net.aufdemrand.denizencore.objects.Element) Duration(net.aufdemrand.denizencore.objects.Duration) CommandExecutionException(net.aufdemrand.denizencore.exceptions.CommandExecutionException) InvalidArgumentsException(net.aufdemrand.denizencore.exceptions.InvalidArgumentsException) CommandExecutionException(net.aufdemrand.denizencore.exceptions.CommandExecutionException)

Example 12 with Duration

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

the class CastCommand method execute.

@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    // Fetch objects
    List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
    PotionEffectType effect = (PotionEffectType) scriptEntry.getObject("effect");
    int amplifier = scriptEntry.getElement("amplifier").asInt();
    Duration duration = (Duration) scriptEntry.getObject("duration");
    boolean remove = scriptEntry.getElement("remove").asBoolean();
    Element showParticles = scriptEntry.getElement("show_particles");
    Element ambient = scriptEntry.getElement("ambient");
    // Report to dB
    dB.report(scriptEntry, getName(), aH.debugObj("Target(s)", entities.toString()) + aH.debugObj("Effect", effect.getName()) + aH.debugObj("Amplifier", amplifier) + duration.debug() + ambient.debug() + showParticles.debug());
    boolean amb = ambient.asBoolean();
    boolean showP = showParticles.asBoolean();
    // Apply the PotionEffect to the targets!
    for (dEntity entity : entities) {
        if (entity.getLivingEntity().hasPotionEffect(effect)) {
            entity.getLivingEntity().removePotionEffect(effect);
        }
        if (remove) {
            continue;
        }
        PotionEffect potion = new PotionEffect(effect, duration.getTicksAsInt(), amplifier, amb, showP);
        if (!potion.apply(entity.getLivingEntity())) {
            dB.echoError(scriptEntry.getResidingQueue(), "Bukkit was unable to apply '" + potion.getType().getName() + "' to '" + entity.toString() + "'.");
        }
    }
}
Also used : PotionEffect(org.bukkit.potion.PotionEffect) net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) PotionEffectType(org.bukkit.potion.PotionEffectType) Element(net.aufdemrand.denizencore.objects.Element) List(java.util.List) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) Duration(net.aufdemrand.denizencore.objects.Duration)

Example 13 with Duration

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

the class QueueCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    ScriptQueue queue = (ScriptQueue) scriptEntry.getObject("queue");
    Action action = (Action) scriptEntry.getObject("action");
    Duration delay = (Duration) scriptEntry.getObject("delay");
    // Debugger
    dB.report(scriptEntry, getName(), queue.debug() + aH.debugObj("Action", action.toString()) + (action == Action.DELAY ? delay.debug() : ""));
    switch(action) {
        case CLEAR:
            queue.clear();
            return;
        case STOP:
            queue.clear();
            queue.stop();
            return;
        case PAUSE:
            if (queue instanceof Delayable) {
                ((Delayable) queue).setPaused(true);
            } else {
                queue.forceToTimed(new Duration(1L)).setPaused(true);
            }
            return;
        case RESUME:
            if (queue instanceof Delayable) {
                ((Delayable) queue).setPaused(false);
            }
            return;
        case DELAY:
            if (queue instanceof Delayable) {
                ((Delayable) queue).delayFor(delay);
            } else {
                queue.forceToTimed(delay);
            }
            return;
    }
}
Also used : Duration(net.aufdemrand.denizencore.objects.Duration) Delayable(net.aufdemrand.denizencore.scripts.queues.core.Delayable) ScriptQueue(net.aufdemrand.denizencore.scripts.queues.ScriptQueue)

Example 14 with Duration

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

the class CooldownCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    // Fetch objects
    dScript script = (dScript) scriptEntry.getObject("script");
    Duration duration = (Duration) scriptEntry.getObject("duration");
    Type type = (scriptEntry.hasObject("type") ? (Type) scriptEntry.getObject("type") : Type.PLAYER);
    // Report to dB
    dB.report(scriptEntry, getName(), aH.debugObj("Type", type.name()) + script.debug() + (type.name().equalsIgnoreCase("player") ? ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().debug() : "") + duration.debug());
    // Perform cooldown
    switch(type) {
        case PLAYER:
            setCooldown(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer(), duration, script.getName(), false);
            break;
        case GLOBAL:
            setCooldown(null, duration, script.getName(), true);
            break;
    }
}
Also used : BukkitScriptEntryData(net.aufdemrand.denizen.BukkitScriptEntryData) net.aufdemrand.denizencore.objects.dScript(net.aufdemrand.denizencore.objects.dScript) Duration(net.aufdemrand.denizencore.objects.Duration)

Example 15 with Duration

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

the class CooldownCommand method getCooldownDuration.

/**
     * Gets the duration of a script cool-down.
     *
     * @param player     the Player to check, null if only checking Global.
     * @param scriptName the name of the script to check
     * @return a Duration of the time remaining
     */
public static Duration getCooldownDuration(dPlayer player, String scriptName) {
    // Change to UPPERCASE so there's no case-sensitivity.
    scriptName = scriptName.toUpperCase();
    Duration duration = Duration.ZERO;
    // Check current entry GLOBALLY, reset it if necessary
    if (DenizenAPI._saves().contains("Global.Scripts." + scriptName + ".Cooldown Time")) {
        if (System.currentTimeMillis() < DenizenAPI._saves().getLong("Global.Scripts." + scriptName + ".Cooldown Time")) {
            duration = new Duration((double) (DenizenAPI._saves().getLong("Global.Scripts." + scriptName + ".Cooldown Time") - System.currentTimeMillis()) / 1000);
        }
    }
    // No player specified? No need to check any further...
    if (player == null) {
        return duration;
    }
    // If no entry for the script, return true
    if (!DenizenAPI._saves().contains("Players." + player.getSaveName() + ".Scripts." + scriptName + ".Cooldown Time")) {
        return duration;
    }
    // If there is an entry, check against the time
    if (System.currentTimeMillis() <= DenizenAPI._saves().getLong("Players." + player.getSaveName() + ".Scripts." + scriptName + ".Cooldown Time")) {
        Duration player_dur = new Duration((double) (DenizenAPI._saves().getLong("Players." + player.getSaveName() + ".Scripts." + scriptName + ".Cooldown Time") - System.currentTimeMillis()) / 1000);
        if (player_dur.getSeconds() > duration.getSeconds()) {
            return player_dur;
        }
    }
    return duration;
}
Also used : Duration(net.aufdemrand.denizencore.objects.Duration)

Aggregations

Duration (net.aufdemrand.denizencore.objects.Duration)31 Element (net.aufdemrand.denizencore.objects.Element)20 net.aufdemrand.denizencore.objects.dList (net.aufdemrand.denizencore.objects.dList)12 net.aufdemrand.denizen.objects.dEntity (net.aufdemrand.denizen.objects.dEntity)11 InvalidArgumentsException (net.aufdemrand.denizencore.exceptions.InvalidArgumentsException)10 BukkitScriptEntryData (net.aufdemrand.denizen.BukkitScriptEntryData)9 net.aufdemrand.denizen.objects.dLocation (net.aufdemrand.denizen.objects.dLocation)9 List (java.util.List)7 net.aufdemrand.denizen.objects.dPlayer (net.aufdemrand.denizen.objects.dPlayer)6 CommandExecutionException (net.aufdemrand.denizencore.exceptions.CommandExecutionException)6 net.aufdemrand.denizencore.objects.aH (net.aufdemrand.denizencore.objects.aH)6 net.aufdemrand.denizen.objects.dNPC (net.aufdemrand.denizen.objects.dNPC)5 net.aufdemrand.denizencore.objects.dScript (net.aufdemrand.denizencore.objects.dScript)4 ScriptQueue (net.aufdemrand.denizencore.scripts.queues.ScriptQueue)4 Player (org.bukkit.entity.Player)3 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)3 net.aufdemrand.denizen.objects.dItem (net.aufdemrand.denizen.objects.dItem)2 net.aufdemrand.denizen.objects.dMaterial (net.aufdemrand.denizen.objects.dMaterial)2 Delayable (net.aufdemrand.denizencore.scripts.queues.core.Delayable)2 BufferedReader (java.io.BufferedReader)1