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);
}
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;
}
}
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);
}
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() + "'.");
}
}
}
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);
}
}
Aggregations