use of net.aufdemrand.denizencore.objects.Duration 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);
}
}
use of net.aufdemrand.denizencore.objects.Duration 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!");
}
}
use of net.aufdemrand.denizencore.objects.Duration in project Denizen-For-Bukkit by DenizenScript.
the class EngageCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
Duration duration = scriptEntry.getdObject("duration");
dNPC npc = ((BukkitScriptEntryData) scriptEntry.entryData).getNPC();
// Report to dB
dB.report(scriptEntry, getName(), npc.debug() + duration.debug());
if (duration.getSecondsAsInt() > 0) {
setEngaged(npc.getCitizen(), duration.getSecondsAsInt());
} else {
setEngaged(npc.getCitizen(), true);
}
}
use of net.aufdemrand.denizencore.objects.Duration in project Denizen-For-Bukkit by DenizenScript.
the class DebugSubmit method run.
@Override
public void run() {
BufferedReader in = null;
try {
// Open a connection to the paste server
URL url = new URL("http://one.denizenscript.com/paste");
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
uc.setDoInput(true);
uc.setDoOutput(true);
uc.setConnectTimeout(10000);
uc.connect();
// Safely connected at this point
// Build a list of plugins
StringBuilder pluginlist = new StringBuilder();
int newlineLength = 0;
int pluginCount = Bukkit.getPluginManager().getPlugins().length;
for (Plugin pl : Bukkit.getPluginManager().getPlugins()) {
String temp = ((char) 0x01) + (pl.isEnabled() ? "2" : "4") + pl.getName() + ": " + pl.getDescription().getVersion() + ", ";
pluginlist.append(temp);
newlineLength += temp.length();
if (newlineLength > 80) {
newlineLength = 0;
pluginlist.append("\n");
}
}
// Build a list of worlds
StringBuilder worldlist = new StringBuilder();
newlineLength = 0;
int worldCount = Bukkit.getWorlds().size();
for (World w : Bukkit.getWorlds()) {
String temp = w.getName() + ", ";
worldlist.append(temp);
newlineLength += temp.length();
if (newlineLength > 80) {
newlineLength = 0;
worldlist.append("\n");
}
}
// Build a list of players
StringBuilder playerlist = new StringBuilder();
newlineLength = 0;
int playerCount = Bukkit.getOnlinePlayers().size();
for (Player pla : Bukkit.getOnlinePlayers()) {
String temp = pla.getDisplayName().replace(ChatColor.COLOR_CHAR, (char) 0x01) + ((char) 0x01) + "7(" + pla.getName() + "), ";
playerlist.append(temp);
newlineLength += temp.length();
if (newlineLength > 80) {
newlineLength = 0;
playerlist.append("\n");
}
}
// Prevent errors if the debug was submitted by the server
if (playerlist.length() < 2) {
playerlist.append("No Online Players, ");
}
// Create the final message pack and upload it
uc.getOutputStream().write(("postid=pastetext&pastetype=log" + "&response=micro&v=100&pastetitle=Denizen+Debug+Logs+From+" + URLEncoder.encode(Bukkit.getServer().getMotd().replace(ChatColor.COLOR_CHAR, (char) 0x01)) + "&pastecontents=" + URLEncoder.encode("Java Version: " + System.getProperty("java.version") + "\nUp-time: " + new Duration((System.currentTimeMillis() - Denizen.startTime) / 50).formatted() + "\nCraftBukkit Version: " + Bukkit.getServer().getVersion() + "\nDenizen Version: Core: " + DenizenCore.VERSION + ", CraftBukkit: " + DenizenAPI.getCurrentInstance().getImplementationVersion() + "\nActive Plugins (" + pluginCount + "): " + pluginlist.substring(0, pluginlist.length() - 2) + "\nLoaded Worlds (" + worldCount + "): " + worldlist.substring(0, worldlist.length() - 2) + "\nOnline Players (" + playerCount + "): " + playerlist.substring(0, playerlist.length() - 2) + "\nOffline Players: " + (dPlayer.getAllPlayers().size() - playerCount) + "\nMode: " + (Bukkit.getServer().getOnlineMode() ? "online" : "offline") + "\n\n") + recording).getBytes("UTF-8"));
// Wait for a response from the server
in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
// Record the response
Result = in.readLine();
// Close the connection
in.close();
} catch (Exception e) {
dB.echoError(e);
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e) {
dB.echoError(e);
}
}
}
use of net.aufdemrand.denizencore.objects.Duration in project Denizen-For-Bukkit by DenizenScript.
the class SwitchCommand method execute.
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
final dList interactLocations = scriptEntry.getdObject("locations");
long duration = ((Duration) scriptEntry.getObject("duration")).getTicks();
final SwitchState switchState = SwitchState.valueOf(scriptEntry.getElement("switchstate").asString());
final Player player = ((BukkitScriptEntryData) scriptEntry.entryData).hasPlayer() ? ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity() : null;
// Switch the Block
dB.report(scriptEntry, getName(), interactLocations.debug() + aH.debugObj("duration", duration + "t") + aH.debugObj("switchstate", switchState.name()));
for (final dLocation interactLocation : interactLocations.filter(dLocation.class)) {
switchBlock(scriptEntry, interactLocation, switchState, player);
// If duration set, schedule a delayed task.
if (duration > 0) {
// If this block already had a delayed task, cancel it.
if (taskMap.containsKey(interactLocation)) {
try {
DenizenAPI.getCurrentInstance().getServer().getScheduler().cancelTask(taskMap.get(interactLocation));
} catch (Exception e) {
}
}
dB.log("Setting delayed task 'SWITCH' for " + interactLocation.identify());
// Store new delayed task ID, for checking against, then schedule new delayed task.
taskMap.put(interactLocation, DenizenAPI.getCurrentInstance().getServer().getScheduler().scheduleSyncDelayedTask(DenizenAPI.getCurrentInstance(), new Runnable() {
public void run() {
switchBlock(scriptEntry, interactLocation, SwitchState.TOGGLE, player);
}
}, duration));
}
}
}
Aggregations