Search in sources :

Example 11 with Element

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

the class SpawnCommand method execute.

@SuppressWarnings("unchecked")
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
    // Get objects
    List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
    dLocation location = (dLocation) scriptEntry.getObject("location");
    dEntity target = (dEntity) scriptEntry.getObject("target");
    Element spread = scriptEntry.getElement("spread");
    boolean persistent = scriptEntry.hasObject("persistent");
    // Report to dB
    dB.report(scriptEntry, getName(), aH.debugObj("entities", entities.toString()) + location.debug() + (spread != null ? spread.debug() : "") + (target != null ? target.debug() : "") + (persistent ? aH.debugObj("persistent", "true") : ""));
    // Keep a dList of entities that can be called using <entry[name].spawned_entities>
    // later in the script queue
    dList entityList = new dList();
    for (dEntity entity : entities) {
        Location loc = location.clone();
        if (spread != null) {
            loc.add(CoreUtilities.getRandom().nextInt(spread.asInt() * 2) - spread.asInt(), 0, CoreUtilities.getRandom().nextInt(spread.asInt() * 2) - spread.asInt());
        }
        entity.spawnAt(loc);
        // Only add to entityList after the entities have been
        // spawned, otherwise you'll get something like "e@skeleton"
        // instead of "e@57" on it
        entityList.add(entity.toString());
        if (persistent && entity.isLivingEntity()) {
            entity.getLivingEntity().setRemoveWhenFarAway(false);
        }
        if (target != null && target.isLivingEntity()) {
            entity.target(target.getLivingEntity());
        }
    }
    // Add entities to context so that the specific entities created/spawned
    // can be fetched.
    scriptEntry.addObject("spawned_entities", entityList);
}
Also used : 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) List(java.util.List) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) net.aufdemrand.denizen.objects.dLocation(net.aufdemrand.denizen.objects.dLocation) Location(org.bukkit.Location) net.aufdemrand.denizen.objects.dLocation(net.aufdemrand.denizen.objects.dLocation)

Example 12 with Element

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

the class YamlCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
    Element filename = scriptEntry.getElement("filename");
    Element key = scriptEntry.getElement("key");
    dObject value = scriptEntry.getdObject("value");
    Element split = scriptEntry.getElement("split");
    YAML_Action yaml_action = (YAML_Action) scriptEntry.getObject("yaml_action");
    Element actionElement = scriptEntry.getElement("action");
    Element idElement = scriptEntry.getElement("id");
    Element fixFormatting = scriptEntry.getElement("fix_formatting");
    YamlConfiguration yamlConfiguration;
    dB.report(scriptEntry, getName(), idElement.debug() + actionElement.debug() + (filename != null ? filename.debug() : "") + (yaml_action != null ? aH.debugObj("yaml_action", yaml_action.name()) : "") + (key != null ? key.debug() : "") + (value != null ? value.debug() : "") + (split != null ? split.debug() : "") + fixFormatting.debug());
    // Do action
    Action action = Action.valueOf(actionElement.asString().toUpperCase());
    String id = idElement.asString().toUpperCase();
    switch(action) {
        case LOAD:
            File file = new File(DenizenAPI.getCurrentInstance().getDataFolder(), filename.asString());
            if (!file.exists()) {
                dB.echoError("File cannot be found!");
                return;
            }
            try {
                FileInputStream fis = new FileInputStream(file);
                String str = ScriptHelper.convertStreamToString(fis);
                if (fixFormatting.asBoolean()) {
                    str = ScriptHelper.ClearComments("", str, false);
                }
                yamlConfiguration = YamlConfiguration.load(str);
                fis.close();
            } catch (Exception e) {
                dB.echoError(e);
                return;
            }
            if (yamls.containsKey(id)) {
                yamls.remove(id);
            }
            if (yamlConfiguration != null) {
                yamls.put(id, yamlConfiguration);
            }
            break;
        case UNLOAD:
            if (yamls.containsKey(id)) {
                yamls.remove(id);
            } else {
                dB.echoError("Unknown YAML ID '" + id + "'");
            }
            break;
        case SAVE:
            if (yamls.containsKey(id)) {
                try {
                    if (!Settings.allowStrangeYAMLSaves()) {
                        File fileObj = new File(DenizenAPI.getCurrentInstance().getDataFolder().getAbsolutePath() + "/" + filename.asString());
                        String directory = URLDecoder.decode(System.getProperty("user.dir"));
                        if (!fileObj.getCanonicalPath().startsWith(directory)) {
                            dB.echoError("Outside-the-main-folder YAML saves disabled by administrator.");
                            return;
                        }
                    }
                    File fileObj = new File(DenizenAPI.getCurrentInstance().getDataFolder().getAbsolutePath() + "/" + filename.asString());
                    fileObj.getParentFile().mkdirs();
                    if (!Utilities.isSafeFile(fileObj)) {
                        dB.echoError(scriptEntry.getResidingQueue(), "Cannot edit that file!");
                        return;
                    }
                    FileWriter fw = new FileWriter(fileObj.getAbsoluteFile());
                    BufferedWriter writer = new BufferedWriter(fw);
                    writer.write(yamls.get(id).saveToString());
                    writer.close();
                    fw.close();
                } catch (IOException e) {
                    dB.echoError(e);
                }
            } else {
                dB.echoError("Unknown YAML ID '" + id + "'");
            }
            break;
        case WRITE:
            if (yamls.containsKey(id)) {
                if (value instanceof Element) {
                    yamls.get(id).set(key.asString(), ((Element) value).asString());
                } else if (split != null && split.asBoolean()) {
                    yamls.get(id).set(key.asString(), value);
                } else {
                    yamls.get(id).set(key.asString(), value.identify());
                }
            } else {
                dB.echoError("Unknown YAML ID '" + id + "'");
            }
            break;
        case SET:
            if (yamls.containsKey(id)) {
                if (yaml_action == null || key == null || value == null) {
                    dB.echoError("Must specify a YAML action and value!");
                    return;
                }
                YamlConfiguration yaml = yamls.get(id);
                int index = -1;
                if (key.asString().contains("[")) {
                    try {
                        if (dB.verbose) {
                            dB.echoDebug(scriptEntry, "Try index: " + key.asString().split("\\[")[1].replace("]", ""));
                        }
                        index = Integer.valueOf(key.asString().split("\\[")[1].replace("]", "")) - 1;
                    } catch (Exception e) {
                        if (dB.verbose) {
                            dB.echoError(scriptEntry.getResidingQueue(), e);
                        }
                        index = -1;
                    }
                    key = Element.valueOf(key.asString().split("\\[")[0]);
                }
                String keyStr = key.asString();
                String valueStr = value.identify();
                switch(yaml_action) {
                    case INCREASE:
                        Set(yaml, index, keyStr, String.valueOf(aH.getFloatFrom(Get(yaml, index, keyStr, "0")) + aH.getFloatFrom(valueStr)));
                        break;
                    case DECREASE:
                        Set(yaml, index, keyStr, String.valueOf(aH.getFloatFrom(Get(yaml, index, keyStr, "0")) - aH.getFloatFrom(valueStr)));
                        break;
                    case MULTIPLY:
                        Set(yaml, index, keyStr, String.valueOf(aH.getFloatFrom(Get(yaml, index, keyStr, "1")) * aH.getFloatFrom(valueStr)));
                        break;
                    case DIVIDE:
                        Set(yaml, index, keyStr, String.valueOf(aH.getFloatFrom(Get(yaml, index, keyStr, "1")) / aH.getFloatFrom(valueStr)));
                        break;
                    case DELETE:
                        yaml.set(keyStr, null);
                        break;
                    case SET_VALUE:
                        Set(yaml, index, keyStr, valueStr);
                        break;
                    case INSERT:
                        {
                            List<String> list = yaml.getStringList(keyStr);
                            if (list == null) {
                                list = new ArrayList<String>();
                            }
                            list.add(valueStr);
                            yaml.set(keyStr, list);
                            break;
                        }
                    case REMOVE:
                        {
                            List<String> list = yaml.getStringList(keyStr);
                            if (list == null) {
                                if (dB.verbose) {
                                    dB.echoDebug(scriptEntry, "List null!");
                                }
                                break;
                            }
                            if (index > -1 && index < list.size()) {
                                if (dB.verbose) {
                                    dB.echoDebug(scriptEntry, "Remove ind: " + index);
                                }
                                list.remove(index);
                                yaml.set(keyStr, list);
                            } else {
                                if (dB.verbose) {
                                    dB.echoDebug(scriptEntry, "Remvoe value: " + valueStr);
                                }
                                for (int i = 0; i < list.size(); i++) {
                                    if (list.get(i).equalsIgnoreCase(valueStr)) {
                                        list.remove(i);
                                        break;
                                    }
                                }
                                yaml.set(keyStr, list);
                                break;
                            }
                            break;
                        }
                    case SPLIT:
                        {
                            List<String> list = yaml.getStringList(keyStr);
                            if (list == null) {
                                list = new ArrayList<String>();
                            }
                            list.addAll(dList.valueOf(valueStr));
                            yaml.set(keyStr, list);
                            break;
                        }
                }
            } else {
                dB.echoError("Unknown YAML ID '" + id + "'");
            }
            break;
        case CREATE:
            if (yamls.containsKey(id)) {
                yamls.remove(id);
            }
            yamlConfiguration = new YamlConfiguration();
            yamls.put(id.toUpperCase(), yamlConfiguration);
            break;
    }
}
Also used : Element(net.aufdemrand.denizencore.objects.Element) YamlConfiguration(net.aufdemrand.denizencore.utilities.YamlConfiguration) InvalidArgumentsException(net.aufdemrand.denizencore.exceptions.InvalidArgumentsException) CommandExecutionException(net.aufdemrand.denizencore.exceptions.CommandExecutionException) net.aufdemrand.denizencore.objects.dObject(net.aufdemrand.denizencore.objects.dObject) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList)

Example 13 with Element

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

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

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

the class NoteCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    String object = (String) scriptEntry.getObject("object");
    Element id = scriptEntry.getElement("id");
    Element remove = scriptEntry.getElement("remove");
    dB.report(scriptEntry, getName(), aH.debugObj("object", object) + id.debug() + remove.debug());
    if (remove.asBoolean()) {
        if (NotableManager.isSaved(id.asString())) {
            NotableManager.remove(id.asString());
            dB.echoDebug(scriptEntry, "notable '" + id.asString() + "' removed");
        } else {
            dB.echoDebug(scriptEntry, id.asString() + " is not saved");
        }
        return;
    }
    String object_type = CoreUtilities.toLowerCase(object.split("@")[0]);
    Class object_class = ObjectFetcher.getObjectClass(object_type);
    if (object_class == null) {
        dB.echoError(scriptEntry.getResidingQueue(), "Invalid object type! Could not fetch '" + object_type + "'!");
        return;
    }
    dObject arg;
    try {
        if (!ObjectFetcher.checkMatch(object_class, object)) {
            dB.echoError(scriptEntry.getResidingQueue(), "'" + object + "' is an invalid " + object_class.getSimpleName() + ".");
            return;
        }
        arg = ObjectFetcher.getObjectFrom(object_class, object);
        if (arg instanceof Notable) {
            ((Notable) arg).makeUnique(id.asString());
        }
    } catch (Exception e) {
        dB.echoError(scriptEntry.getResidingQueue(), "Uh oh! Report this to the Denizen developers! Err: NoteCommandObjectReflection");
        dB.echoError(scriptEntry.getResidingQueue(), e);
    }
}
Also used : Element(net.aufdemrand.denizencore.objects.Element) net.aufdemrand.denizencore.objects.dObject(net.aufdemrand.denizencore.objects.dObject) Notable(net.aufdemrand.denizencore.objects.notable.Notable) InvalidArgumentsException(net.aufdemrand.denizencore.exceptions.InvalidArgumentsException) CommandExecutionException(net.aufdemrand.denizencore.exceptions.CommandExecutionException)

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