Search in sources :

Example 36 with CArray

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);
    }
}
Also used : CArray(com.laytonsmith.core.constructs.CArray) CString(com.laytonsmith.core.constructs.CString) CREFormatException(com.laytonsmith.core.exceptions.CRE.CREFormatException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 37 with CArray

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;
}
Also used : CInt(com.laytonsmith.core.constructs.CInt) CArray(com.laytonsmith.core.constructs.CArray) CString(com.laytonsmith.core.constructs.CString) CString(com.laytonsmith.core.constructs.CString)

Example 38 with CArray

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;
}
Also used : Cookie(com.laytonsmith.PureUtilities.Web.Cookie) CArray(com.laytonsmith.core.constructs.CArray) CookieJar(com.laytonsmith.PureUtilities.Web.CookieJar) CString(com.laytonsmith.core.constructs.CString) CREFormatException(com.laytonsmith.core.exceptions.CRE.CREFormatException)

Example 39 with CArray

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);
}
Also used : CRECastException(com.laytonsmith.core.exceptions.CRE.CRECastException) CArray(com.laytonsmith.core.constructs.CArray) Field(java.lang.reflect.Field) CNull(com.laytonsmith.core.constructs.CNull)

Example 40 with CArray

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;
    }
}
Also used : Target(com.laytonsmith.core.constructs.Target) CClosure(com.laytonsmith.core.constructs.CClosure) CBoolean(com.laytonsmith.core.constructs.CBoolean) CArray(com.laytonsmith.core.constructs.CArray) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) Construct(com.laytonsmith.core.constructs.Construct) CString(com.laytonsmith.core.constructs.CString) FunctionReturnException(com.laytonsmith.core.exceptions.FunctionReturnException) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) CString(com.laytonsmith.core.constructs.CString)

Aggregations

CArray (com.laytonsmith.core.constructs.CArray)50 CString (com.laytonsmith.core.constructs.CString)27 Construct (com.laytonsmith.core.constructs.Construct)23 CInt (com.laytonsmith.core.constructs.CInt)17 CNull (com.laytonsmith.core.constructs.CNull)12 CREFormatException (com.laytonsmith.core.exceptions.CRE.CREFormatException)12 HashMap (java.util.HashMap)9 CDouble (com.laytonsmith.core.constructs.CDouble)8 IVariable (com.laytonsmith.core.constructs.IVariable)8 GlobalEnv (com.laytonsmith.core.environments.GlobalEnv)6 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)6 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 CBoolean (com.laytonsmith.core.constructs.CBoolean)5 Target (com.laytonsmith.core.constructs.Target)5 CRECastException (com.laytonsmith.core.exceptions.CRE.CRECastException)5 FunctionReturnException (com.laytonsmith.core.exceptions.FunctionReturnException)5 MCEnchantment (com.laytonsmith.abstraction.MCEnchantment)4 MCItemMeta (com.laytonsmith.abstraction.MCItemMeta)4 MCItemStack (com.laytonsmith.abstraction.MCItemStack)4