use of net.aufdemrand.denizen.flags.FlagManager.Flag 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