use of net.aufdemrand.denizencore.objects.Element in project Denizen-For-Bukkit by DenizenScript.
the class RandomCommand method execute.
@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
int possibilities = 0;
ScriptQueue queue = scriptEntry.getResidingQueue();
List<ScriptEntry> bracedCommands = null;
if (!scriptEntry.hasObject("braces")) {
possibilities = scriptEntry.getElement("possibilities").asInt();
} else {
bracedCommands = ((List<BracedData>) scriptEntry.getObject("braces")).get(0).value;
possibilities = bracedCommands.size();
}
int selected = CoreUtilities.getRandom().nextInt(possibilities);
// Try to not duplicate
if (selected == previous || selected == previous2 || selected == previous3) {
selected = CoreUtilities.getRandom().nextInt(possibilities);
}
if (selected == previous || selected == previous2 || selected == previous3) {
selected = CoreUtilities.getRandom().nextInt(possibilities);
}
previous3 = previous2;
previous2 = previous;
previous = selected;
scriptEntry.addObject("possibilities", new Element(possibilities));
scriptEntry.addObject("selected", new Element(selected));
dB.report(scriptEntry, getName(), aH.debugObj("possibilities", possibilities) + aH.debugObj("choice", selected + 1));
if (bracedCommands == null) {
ScriptEntry keeping = null;
for (int x = 0; x < possibilities; x++) {
if (x != selected) {
queue.removeEntry(0);
} else {
dB.echoDebug(scriptEntry, "...selected '" + queue.getEntry(0).getCommandName() + ": " + queue.getEntry(0).getArguments() + "'.");
keeping = queue.getEntry(0);
queue.removeEntry(0);
}
}
queue.injectEntry(keeping, 0);
} else {
queue.injectEntry(bracedCommands.get(selected).addObject("reqID", scriptEntry.getObject("reqID")), 0);
}
}
use of net.aufdemrand.denizencore.objects.Element in project Denizen-For-Bukkit by DenizenScript.
the class RepeatCommand method execute.
@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
Element stop = scriptEntry.getElement("stop");
Element next = scriptEntry.getElement("next");
Element callback = scriptEntry.getElement("callback");
Element quantity = scriptEntry.getElement("qty");
if (stop != null && stop.asBoolean()) {
// Report to dB
dB.report(scriptEntry, getName(), stop.debug());
boolean hasnext = false;
for (int i = 0; i < scriptEntry.getResidingQueue().getQueueSize(); i++) {
ScriptEntry entry = scriptEntry.getResidingQueue().getEntry(i);
List<String> args = entry.getOriginalArguments();
if (entry.getCommandName().equalsIgnoreCase("repeat") && args.size() > 0 && args.get(0).equalsIgnoreCase("\0CALLBACK")) {
hasnext = true;
break;
}
}
if (hasnext) {
while (scriptEntry.getResidingQueue().getQueueSize() > 0) {
ScriptEntry entry = scriptEntry.getResidingQueue().getEntry(0);
List<String> args = entry.getOriginalArguments();
if (entry.getCommandName().equalsIgnoreCase("repeat") && args.size() > 0 && args.get(0).equalsIgnoreCase("\0CALLBACK")) {
scriptEntry.getResidingQueue().removeEntry(0);
break;
}
scriptEntry.getResidingQueue().removeEntry(0);
}
} else {
dB.echoError("Cannot stop repeat: not in one!");
}
return;
} else if (next != null && next.asBoolean()) {
// Report to dB
dB.report(scriptEntry, getName(), next.debug());
boolean hasnext = false;
for (int i = 0; i < scriptEntry.getResidingQueue().getQueueSize(); i++) {
ScriptEntry entry = scriptEntry.getResidingQueue().getEntry(i);
List<String> args = entry.getOriginalArguments();
if (entry.getCommandName().equalsIgnoreCase("repeat") && args.size() > 0 && args.get(0).equalsIgnoreCase("\0CALLBACK")) {
hasnext = true;
break;
}
}
if (hasnext) {
while (scriptEntry.getResidingQueue().getQueueSize() > 0) {
ScriptEntry entry = scriptEntry.getResidingQueue().getEntry(0);
List<String> args = entry.getOriginalArguments();
if (entry.getCommandName().equalsIgnoreCase("repeat") && args.size() > 0 && args.get(0).equalsIgnoreCase("\0CALLBACK")) {
break;
}
scriptEntry.getResidingQueue().removeEntry(0);
}
} else {
dB.echoError("Cannot stop repeat: not in one!");
}
return;
} else if (callback != null && callback.asBoolean()) {
if (scriptEntry.getOwner() != null && (scriptEntry.getOwner().getCommandName().equalsIgnoreCase("repeat") || scriptEntry.getOwner().getBracedSet() == null || scriptEntry.getOwner().getBracedSet().size() == 0 || scriptEntry.getBracedSet().get(0).value.get(scriptEntry.getBracedSet().get(0).value.size() - 1) != scriptEntry)) {
RepeatData data = (RepeatData) scriptEntry.getOwner().getData();
data.index++;
if (data.index <= data.target) {
dB.echoDebug(scriptEntry, DebugElement.Header, "Repeat loop " + data.index);
scriptEntry.getResidingQueue().addDefinition("value", String.valueOf(data.index));
List<ScriptEntry> bracedCommands = BracedCommand.getBracedCommands(scriptEntry.getOwner()).get(0).value;
ScriptEntry callbackEntry = null;
try {
callbackEntry = new ScriptEntry("REPEAT", new String[] { "\0CALLBACK" }, (scriptEntry.getScript() != null ? scriptEntry.getScript().getContainer() : null));
callbackEntry.copyFrom(scriptEntry);
} catch (ScriptEntryCreationException e) {
dB.echoError(e);
}
callbackEntry.setOwner(scriptEntry.getOwner());
bracedCommands.add(callbackEntry);
for (int i = 0; i < bracedCommands.size(); i++) {
bracedCommands.get(i).setInstant(true);
bracedCommands.get(i).addObject("reqId", scriptEntry.getObject("reqId"));
}
scriptEntry.getResidingQueue().injectEntries(bracedCommands, 0);
}
} else {
dB.echoError("Repeat CALLBACK invalid: not a real callback!");
}
} else {
// Get objects
List<ScriptEntry> bracedCommandsList = ((List<BracedData>) scriptEntry.getObject("braces")).get(0).value;
if (bracedCommandsList == null || bracedCommandsList.isEmpty()) {
dB.echoError("Empty braces!");
return;
}
// Report to dB
dB.report(scriptEntry, getName(), quantity.debug());
int target = quantity.asInt();
if (target <= 0) {
dB.echoDebug(scriptEntry, "Zero count, not looping...");
return;
}
RepeatData datum = new RepeatData();
datum.target = target;
datum.index = 1;
scriptEntry.setData(datum);
ScriptEntry callbackEntry = null;
try {
callbackEntry = new ScriptEntry("REPEAT", new String[] { "\0CALLBACK" }, (scriptEntry.getScript() != null ? scriptEntry.getScript().getContainer() : null));
callbackEntry.copyFrom(scriptEntry);
} catch (ScriptEntryCreationException e) {
dB.echoError(e);
}
callbackEntry.setOwner(scriptEntry);
bracedCommandsList.add(callbackEntry);
scriptEntry.getResidingQueue().addDefinition("value", "1");
for (int i = 0; i < bracedCommandsList.size(); i++) {
bracedCommandsList.get(i).setInstant(true);
bracedCommandsList.get(i).addObject("reqId", scriptEntry.getObject("reqId"));
}
scriptEntry.getResidingQueue().injectEntries(bracedCommandsList, 0);
}
}
use of net.aufdemrand.denizencore.objects.Element in project Denizen-For-Bukkit by DenizenScript.
the class SQLCommand method execute.
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
Element action = scriptEntry.getElement("action");
final Element server = scriptEntry.getElement("server");
final Element username = scriptEntry.getElement("username");
final Element password = scriptEntry.getElement("password");
final Element sqlID = scriptEntry.getElement("sqlid");
final Element query = scriptEntry.getElement("query");
dB.report(scriptEntry, getName(), sqlID.debug() + action.debug() + (server != null ? server.debug() : "") + (username != null ? username.debug() : "") + (password != null ? aH.debugObj("password", "NotLogged") : "") + (query != null ? query.debug() : ""));
if (!action.asString().equalsIgnoreCase("connect") && (!action.asString().equalsIgnoreCase("query") || !scriptEntry.shouldWaitFor())) {
scriptEntry.setFinished(true);
}
try {
if (action.asString().equalsIgnoreCase("connect")) {
if (server == null) {
dB.echoError(scriptEntry.getResidingQueue(), "Must specify a server!");
return;
}
if (username == null) {
dB.echoError(scriptEntry.getResidingQueue(), "Must specify a username!");
return;
}
if (password == null) {
dB.echoError(scriptEntry.getResidingQueue(), "Must specify a password!");
return;
}
if (connections.containsKey(sqlID.asString().toUpperCase())) {
dB.echoError(scriptEntry.getResidingQueue(), "Already connected to a server with ID '" + sqlID.asString() + "'!");
return;
}
Bukkit.getScheduler().runTaskLaterAsynchronously(DenizenAPI.getCurrentInstance(), new Runnable() {
@Override
public void run() {
Connection con = null;
if (dB.verbose) {
dB.echoDebug(scriptEntry, "Connecting to " + server.asString());
}
try {
con = getConnection(username.asString(), password.asString(), server.asString());
} catch (final Exception e) {
Bukkit.getScheduler().runTaskLater(DenizenAPI.getCurrentInstance(), new Runnable() {
@Override
public void run() {
dB.echoError(scriptEntry.getResidingQueue(), "SQL Exception: " + e.getMessage());
scriptEntry.setFinished(true);
if (dB.verbose) {
dB.echoError(scriptEntry.getResidingQueue(), e);
}
}
}, 1);
}
if (dB.verbose) {
dB.echoDebug(scriptEntry, "Connection did not error");
}
final Connection conn = con;
if (con != null) {
Bukkit.getScheduler().runTaskLater(DenizenAPI.getCurrentInstance(), new Runnable() {
@Override
public void run() {
connections.put(sqlID.asString().toUpperCase(), conn);
dB.echoDebug(scriptEntry, "Successfully connected to " + server);
scriptEntry.setFinished(true);
}
}, 1);
} else {
Bukkit.getScheduler().runTaskLater(DenizenAPI.getCurrentInstance(), new Runnable() {
@Override
public void run() {
scriptEntry.setFinished(true);
if (dB.verbose) {
dB.echoDebug(scriptEntry, "Connecting errored!");
}
}
}, 1);
}
}
}, 1);
} else if (action.asString().equalsIgnoreCase("disconnect")) {
Connection con = connections.get(sqlID.asString().toUpperCase());
if (con == null) {
dB.echoError(scriptEntry.getResidingQueue(), "Not connected to server with ID '" + sqlID.asString() + "'!");
return;
}
con.close();
connections.remove(sqlID.asString().toUpperCase());
dB.echoDebug(scriptEntry, "Disconnected from '" + sqlID.asString() + "'.");
} else if (action.asString().equalsIgnoreCase("query")) {
if (query == null) {
dB.echoError(scriptEntry.getResidingQueue(), "Must specify a query!");
return;
}
final Connection con = connections.get(sqlID.asString().toUpperCase());
if (con == null) {
dB.echoError(scriptEntry.getResidingQueue(), "Not connected to server with ID '" + sqlID.asString() + "'!");
return;
}
dB.echoDebug(scriptEntry, "Running query " + query.asString());
if (scriptEntry.shouldWaitFor()) {
Bukkit.getScheduler().runTaskLaterAsynchronously(DenizenAPI.getCurrentInstance(), new Runnable() {
@Override
public void run() {
try {
Statement statement = con.createStatement();
ResultSet set = statement.executeQuery(query.asString());
ResultSetMetaData rsmd = set.getMetaData();
final int columns = rsmd.getColumnCount();
Bukkit.getScheduler().runTaskLater(DenizenAPI.getCurrentInstance(), new Runnable() {
@Override
public void run() {
dB.echoDebug(scriptEntry, "Got a query result of " + columns + " columns");
}
}, 1);
int count = 0;
dList rows = new dList();
while (set.next()) {
count++;
StringBuilder current = new StringBuilder();
for (int i = 0; i < columns; i++) {
current.append(EscapeTags.Escape(set.getString(i + 1))).append("/");
}
rows.add(current.toString());
}
scriptEntry.addObject("result", rows);
final int finalCount = count;
Bukkit.getScheduler().runTaskLater(DenizenAPI.getCurrentInstance(), new Runnable() {
@Override
public void run() {
dB.echoDebug(scriptEntry, "Got a query result of " + finalCount + " rows");
scriptEntry.setFinished(true);
}
}, 1);
} catch (final Exception e) {
Bukkit.getScheduler().runTaskLater(DenizenAPI.getCurrentInstance(), new Runnable() {
@Override
public void run() {
dB.echoError(scriptEntry.getResidingQueue(), "SQL Exception: " + e.getMessage());
scriptEntry.setFinished(true);
if (dB.verbose) {
dB.echoError(scriptEntry.getResidingQueue(), e);
}
}
}, 1);
}
}
}, 1);
} else {
Statement statement = con.createStatement();
ResultSet set = statement.executeQuery(query.asString());
ResultSetMetaData rsmd = set.getMetaData();
final int columns = rsmd.getColumnCount();
dB.echoDebug(scriptEntry, "Got a query result of " + columns + " columns");
int count = 0;
dList rows = new dList();
while (set.next()) {
count++;
StringBuilder current = new StringBuilder();
for (int i = 0; i < columns; i++) {
current.append(EscapeTags.Escape(set.getString(i + 1))).append("/");
}
rows.add(current.toString());
}
scriptEntry.addObject("result", rows);
final int finalCount = count;
dB.echoDebug(scriptEntry, "Got a query result of " + finalCount + " rows");
}
} else if (action.asString().equalsIgnoreCase("update")) {
if (query == null) {
dB.echoError(scriptEntry.getResidingQueue(), "Must specify an update query!");
return;
}
Connection con = connections.get(sqlID.asString().toUpperCase());
if (con == null) {
dB.echoError(scriptEntry.getResidingQueue(), "Not connected to server with ID '" + sqlID.asString() + "'!");
return;
}
dB.echoDebug(scriptEntry, "Running update " + query.asString());
Statement statement = con.createStatement();
int affected = statement.executeUpdate(query.asString(), Statement.RETURN_GENERATED_KEYS);
scriptEntry.addObject("affected_rows", new Element(affected));
ResultSet set = statement.getGeneratedKeys();
ResultSetMetaData rsmd = set.getMetaData();
int columns = rsmd.getColumnCount();
dB.echoDebug(scriptEntry, "Got a query result of " + columns + " columns");
dList rows = new dList();
while (set.next()) {
StringBuilder current = new StringBuilder();
for (int i = 0; i < columns; i++) {
current.append(EscapeTags.Escape(set.getString(i + 1))).append("/");
}
rows.add(current.toString());
}
scriptEntry.addObject("result", rows);
dB.echoDebug(scriptEntry, "Updated " + affected + " rows");
} else {
dB.echoError(scriptEntry.getResidingQueue(), "Unknown action '" + action.asString() + "'");
}
} catch (SQLException e) {
dB.echoError(scriptEntry.getResidingQueue(), "SQL Exception: " + e.getMessage());
if (dB.verbose) {
dB.echoError(scriptEntry.getResidingQueue(), e);
}
}
}
use of net.aufdemrand.denizencore.objects.Element in project Denizen-For-Bukkit by DenizenScript.
the class FileCopyCommand method execute.
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
Element origin = scriptEntry.getElement("origin");
Element destination = scriptEntry.getElement("destination");
Element overwrite = scriptEntry.getElement("overwrite");
dB.report(scriptEntry, getName(), origin.debug() + destination.debug() + overwrite.debug());
if (!Settings.allowFilecopy()) {
dB.echoError(scriptEntry.getResidingQueue(), "File copy disabled by server administrator.");
scriptEntry.addObject("success", new Element("false"));
return;
}
File o = new File(DenizenAPI.getCurrentInstance().getDataFolder(), origin.asString());
File d = new File(DenizenAPI.getCurrentInstance().getDataFolder(), destination.asString());
boolean ow = overwrite.asBoolean();
boolean dexists = d.exists();
boolean disdir = d.isDirectory();
if (!o.exists()) {
dB.echoError(scriptEntry.getResidingQueue(), "File copy failed, origin does not exist!");
scriptEntry.addObject("success", new Element("false"));
return;
}
if (!Utilities.isSafeFile(d)) {
dB.echoError(scriptEntry.getResidingQueue(), "Can't copy files to there!");
scriptEntry.addObject("success", new Element("false"));
return;
}
if (dexists && !disdir && !ow) {
dB.echoDebug(scriptEntry, "File copy ignored, destination file already exists!");
scriptEntry.addObject("success", new Element("false"));
return;
}
try {
if (dexists && !disdir) {
d.delete();
}
FileUtils.copyFile(o, d);
scriptEntry.addObject("success", new Element("true"));
} catch (Exception e) {
dB.echoError(scriptEntry.getResidingQueue(), e);
scriptEntry.addObject("success", new Element("false"));
return;
}
}
use of net.aufdemrand.denizencore.objects.Element in project Denizen-For-Bukkit by DenizenScript.
the class FlagCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
dObject flag_target = scriptEntry.getdObject("flag_target");
Duration duration = (Duration) scriptEntry.getObject("duration");
FlagManager.Action action = (FlagManager.Action) scriptEntry.getObject("action");
Element value = scriptEntry.getElement("value");
Element name = scriptEntry.getElement("flag_name");
int index = -1;
// Usage example: - FLAG FLAGNAME[3]:VALUE specifies an index of 3 should be set with VALUE.
if (name.asString().contains("[")) {
try {
index = Integer.valueOf(name.asString().split("\\[")[1].replace("]", ""));
} catch (Exception e) {
index = -1;
}
name = Element.valueOf(name.asString().split("\\[")[0]);
}
// Send information to debugger
dB.report(scriptEntry, getName(), name.debug() + (index > 0 ? aH.debugObj("Index", String.valueOf(index)) : "") + aH.debugUniqueObj("Action/Value", action.toString(), (value != null ? value.asString() : "null")) + (duration != null ? duration.debug() : "") + flag_target.debug());
Flag flag;
// Returns existing flag (if existing), or a new flag if not
if (flag_target instanceof Element) {
flag = DenizenAPI.getCurrentInstance().flagManager().getGlobalFlag(name.asString());
} else if (flag_target instanceof dPlayer) {
flag = DenizenAPI.getCurrentInstance().flagManager().getPlayerFlag((dPlayer) flag_target, name.asString());
} else if (flag_target instanceof dNPC) {
flag = DenizenAPI.getCurrentInstance().flagManager().getNPCFlag(((dNPC) flag_target).getId(), name.asString());
} else if (flag_target instanceof dEntity) {
flag = DenizenAPI.getCurrentInstance().flagManager().getEntityFlag((dEntity) flag_target, name.asString());
} else {
throw new CommandExecutionException("Could not fetch a flag for this entity: " + flag_target.debug());
}
// Do the action!
flag.doAction(action, value, index);
// Set flag duration
if (flag.StillValid() && duration != null && duration.getSeconds() > 0) {
flag.setExpiration(DenizenCore.currentTimeMillis + Double.valueOf(duration.getSeconds() * 1000.0).longValue());
} else if (flag.StillValid() && flag.expiration().getMillis() != 0L) {
flag.setExpiration(0L);
}
}
Aggregations