use of com.laytonsmith.core.constructs.CArray in project CommandHelper by EngineHub.
the class Regex method getPattern.
private static Pattern getPattern(Construct c, Target t) throws ConfigRuntimeException {
String regex = "";
int flags = 0;
String sflags = "";
if (c instanceof CArray) {
CArray ca = (CArray) c;
regex = ca.get(0, t).val();
sflags = ca.get(1, t).val();
for (int i = 0; i < sflags.length(); i++) {
if (sflags.toLowerCase().charAt(i) == 'i') {
flags |= java.util.regex.Pattern.CASE_INSENSITIVE;
} else if (sflags.toLowerCase().charAt(i) == 'm') {
flags |= java.util.regex.Pattern.MULTILINE;
} else if (sflags.toLowerCase().charAt(i) == 's') {
flags |= java.util.regex.Pattern.DOTALL;
} else {
throw new CREFormatException("Unrecognized flag: " + sflags.toLowerCase().charAt(i), t);
}
}
} else {
regex = c.val();
}
try {
return Pattern.compile(regex, flags);
} catch (PatternSyntaxException e) {
throw new CREFormatException(e.getMessage(), t);
}
}
use of com.laytonsmith.core.constructs.CArray in project CommandHelper by EngineHub.
the class Scoreboards method getTeam.
static CArray getTeam(MCTeam team, Target t) {
CArray to = CArray.GetAssociativeArray(t);
to.set("name", new CString(team.getName(), t), t);
to.set("displayname", new CString(team.getDisplayName(), t), t);
to.set("prefix", new CString(team.getPrefix(), t), t);
to.set("suffix", new CString(team.getSuffix(), t), t);
to.set("size", new CInt(team.getSize(), t), t);
CArray ops = CArray.GetAssociativeArray(t);
ops.set("friendlyfire", CBoolean.get(team.allowFriendlyFire()), t);
ops.set("friendlyinvisibles", CBoolean.get(team.canSeeFriendlyInvisibles()), t);
if (Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_8)) {
ops.set("nametagvisibility", new CString(team.getNameTagVisibility().name(), t), t);
}
if (Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_9)) {
ops.set("collisionrule", new CString(team.getOption(MCOption.COLLISION_RULE).name(), t), t);
ops.set("deathmessagevisibility", new CString(team.getOption(MCOption.DEATH_MESSAGE_VISIBILITY).name(), t), t);
}
to.set("options", ops, t);
CArray pl = new CArray(t);
for (String entry : team.getEntries()) {
pl.push(new CString(entry, t), t);
}
to.set("players", pl, t);
return to;
}
use of com.laytonsmith.core.constructs.CArray in project CommandHelper by EngineHub.
the class Web method getCookieJar.
private static CookieJar getCookieJar(CArray cookieJar, Target t) {
CookieJar ret = new CookieJar();
for (String key : cookieJar.stringKeySet()) {
CArray cookie = Static.getArray(cookieJar.get(key, t), t);
String name;
String value;
String domain;
String path;
long expiration = 0;
boolean httpOnly = false;
boolean secureOnly = false;
if (cookie.containsKey("name") && cookie.containsKey("value") && cookie.containsKey("domain") && cookie.containsKey("path")) {
name = cookie.get("name", t).val();
value = cookie.get("value", t).val();
domain = cookie.get("domain", t).val();
path = cookie.get("path", t).val();
} else {
throw new CREFormatException("The name, value, domain, and path keys are required" + " in all cookies.", t);
}
if (cookie.containsKey("expiration")) {
expiration = Static.getInt(cookie.get("expiration", t), t);
}
if (cookie.containsKey("httpOnly")) {
httpOnly = Static.getBoolean(cookie.get("httpOnly", t), t);
}
if (cookie.containsKey("secureOnly")) {
secureOnly = Static.getBoolean(cookie.get("secureOnly", t), t);
}
Cookie c = new Cookie(name, value, domain, path, expiration, httpOnly, secureOnly);
ret.addCookie(c);
}
return ret;
}
use of com.laytonsmith.core.constructs.CArray in project CommandHelper by EngineHub.
the class MObject method set.
/**
* Sets the field to the given parameter. If the field is a non-dynamic property, it is actually set in the object
* (and converted properly), otherwise it is simply added to the dynamic field list.
*
* @param field
* @param value
* @param t
*/
public final void set(String field, Construct value, Target t) {
String alias = alias(field);
if (alias != null) {
field = alias;
}
for (Field f : this.getClass().getFields()) {
if (f.isAnnotationPresent(nofield.class)) {
// Skip this one
continue;
}
if (f.getName().equals(field)) {
// This is it, so let's set it, (converting if necessary) then break
Object val;
Class fType = f.getType();
if (value instanceof CNull) {
// TODO
// Easy case
val = null;
} else {
if (fType == byte.class) {
val = Static.getInt8(value, t);
} else if (fType == short.class) {
val = Static.getInt16(value, t);
} else if (fType == int.class) {
val = Static.getInt32(value, t);
} else if (fType == long.class) {
val = Static.getInt(value, t);
} else if (fType == char.class) {
if (value.val().length() == 0) {
val = null;
} else {
val = value.val().charAt(0);
}
} else if (fType == boolean.class) {
val = Static.getBoolean(value, t);
} else if (fType == float.class) {
val = Static.getDouble32(value, t);
} else if (fType == double.class) {
val = Static.getDouble(value, t);
} else if (fType == MMap.class) {
CArray ca = Static.getArray(value, t);
MMap m = new MMap();
for (String key : ca.stringKeySet()) {
m.put(key, ca.get(key, t));
}
val = m;
} else if (fType == MList.class) {
CArray ca = Static.getArray(value, t);
MList m = new MList();
if (ca.inAssociativeMode()) {
throw new CRECastException("Expected non-associative array, but an associative array was found instead.", t);
}
for (int i = 0; i < ca.size(); i++) {
m.add(ca.get(i, t));
}
val = m;
} else if (Construct.class.isAssignableFrom(fType)) {
val = value;
} else if (MObject.class.isAssignableFrom(fType)) {
CArray ca = Static.getArray(value, t);
val = MObject.Construct(fType, ca);
} else {
// Programming error.
throw new Error(this.getClass().getName() + " contained the public field " + f.getName() + " of type " + fType.getName() + ", which is an unsupported field type.");
}
}
try {
// val is now set correctly, guaranteed.
f.set(this, val);
// These exceptions cannot happen.
} catch (IllegalArgumentException | IllegalAccessException ex) {
throw new Error(ex);
}
}
}
// Always put the dynamic parameter, regardless
fields.put(field, value);
}
use of com.laytonsmith.core.constructs.CArray in project CommandHelper by EngineHub.
the class BukkitMCCommand method handleCustomCommand.
@Override
public boolean handleCustomCommand(MCCommandSender sender, String label, String[] args) {
if (Commands.onCommand.containsKey(cmd.getName().toLowerCase())) {
Target t = Target.UNKNOWN;
CArray cargs = new CArray(t);
for (String arg : args) {
cargs.push(new CString(arg, t), t);
}
CClosure closure = Commands.onCommand.get(cmd.getName().toLowerCase());
CommandHelperEnvironment cEnv = closure.getEnv().getEnv(CommandHelperEnvironment.class);
cEnv.SetCommandSender(sender);
cEnv.SetCommand("/" + label + StringUtils.Join(args, " "));
try {
closure.execute(new CString(label, t), new CString(sender.getName(), t), cargs, // reserved for an obgen style command array
new CArray(t));
} catch (FunctionReturnException e) {
Construct fret = e.getReturn();
if (fret instanceof CBoolean) {
return ((CBoolean) fret).getBoolean();
}
} catch (ConfigRuntimeException cre) {
cre.setEnv(closure.getEnv());
ConfigRuntimeException.HandleUncaughtException(cre, closure.getEnv());
}
return true;
} else {
return false;
}
}
Aggregations