Search in sources :

Example 11 with CREFormatException

use of com.laytonsmith.core.exceptions.CRE.CREFormatException 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 12 with CREFormatException

use of com.laytonsmith.core.exceptions.CRE.CREFormatException in project CommandHelper by EngineHub.

the class ObjectGenerator method fireworkEffect.

public MCFireworkEffect fireworkEffect(CArray fe, Target t) {
    MCFireworkBuilder builder = StaticLayer.GetConvertor().GetFireworkBuilder();
    if (fe.containsKey("flicker")) {
        builder.setFlicker(Static.getBoolean(fe.get("flicker", t), t));
    }
    if (fe.containsKey("trail")) {
        builder.setTrail(Static.getBoolean(fe.get("trail", t), t));
    }
    if (fe.containsKey("colors")) {
        Construct colors = fe.get("colors", t);
        if (colors instanceof CArray) {
            CArray ccolors = (CArray) colors;
            if (ccolors.size() == 0) {
                builder.addColor(MCColor.WHITE);
            } else {
                for (Construct color : ccolors.asList()) {
                    MCColor mccolor;
                    if (color instanceof CString) {
                        mccolor = StaticLayer.GetConvertor().GetColor(color.val(), t);
                    } else if (color instanceof CArray) {
                        mccolor = color((CArray) color, t);
                    } else if (color instanceof CInt && ccolors.size() == 3) {
                        // Appears to be a single color
                        builder.addColor(color(ccolors, t));
                        break;
                    } else {
                        throw new CREFormatException("Expecting individual color to be an array or string, but found " + color.typeof(), t);
                    }
                    builder.addColor(mccolor);
                }
            }
        } else if (colors instanceof CString) {
            String[] split = colors.val().split("\\|");
            if (split.length == 0) {
                builder.addColor(MCColor.WHITE);
            } else {
                for (String s : split) {
                    builder.addColor(StaticLayer.GetConvertor().GetColor(s, t));
                }
            }
        } else {
            throw new CREFormatException("Expecting an array or string for colors parameter, but found " + colors.typeof(), t);
        }
    } else {
        builder.addColor(MCColor.WHITE);
    }
    if (fe.containsKey("fade")) {
        Construct colors = fe.get("fade", t);
        if (colors instanceof CArray) {
            CArray ccolors = (CArray) colors;
            for (Construct color : ccolors.asList()) {
                MCColor mccolor;
                if (color instanceof CArray) {
                    mccolor = color((CArray) color, t);
                } else if (color instanceof CString) {
                    mccolor = StaticLayer.GetConvertor().GetColor(color.val(), t);
                } else if (color instanceof CInt && ccolors.size() == 3) {
                    // Appears to be a single color
                    builder.addFadeColor(color(ccolors, t));
                    break;
                } else {
                    throw new CREFormatException("Expecting individual color to be an array or string, but found " + color.typeof(), t);
                }
                builder.addFadeColor(mccolor);
            }
        } else if (colors instanceof CString) {
            String[] split = colors.val().split("\\|");
            for (String s : split) {
                builder.addFadeColor(StaticLayer.GetConvertor().GetColor(s, t));
            }
        } else {
            throw new CREFormatException("Expecting an array or string for fade parameter, but found " + colors.typeof(), t);
        }
    }
    if (fe.containsKey("type")) {
        try {
            builder.setType(MCFireworkType.valueOf(fe.get("type", t).val().toUpperCase()));
        } catch (IllegalArgumentException ex) {
            throw new CREFormatException(ex.getMessage(), t, ex);
        }
    }
    return builder.build();
}
Also used : CInt(com.laytonsmith.core.constructs.CInt) MCColor(com.laytonsmith.abstraction.MCColor) CArray(com.laytonsmith.core.constructs.CArray) Construct(com.laytonsmith.core.constructs.Construct) MCFireworkBuilder(com.laytonsmith.abstraction.MCFireworkBuilder) CString(com.laytonsmith.core.constructs.CString) CREFormatException(com.laytonsmith.core.exceptions.CRE.CREFormatException) CString(com.laytonsmith.core.constructs.CString)

Example 13 with CREFormatException

use of com.laytonsmith.core.exceptions.CRE.CREFormatException in project CommandHelper by EngineHub.

the class ObjectGenerator method location.

/**
 * Given a Location Object, returns a MCLocation. If the optional world is not specified in the object, the world
 * provided is used instead. Location "objects" are MethodScript arrays that represent a location in game. There are
 * 4 usages: <ul> <li>(x, y, z)</li> <li>(x, y, z, world)</li> <li>(x, y, z, yaw, pitch)</li> <li>(x, y, z, world,
 * yaw, pitch)</li> </ul> In all cases, the pitch and yaw default to 0, and the world defaults to the specified
 * world. <em>More conveniently: ([world], x, y, z, [yaw, pitch])</em>
 */
public MCLocation location(Construct c, MCWorld w, Target t) {
    if (!(c instanceof CArray)) {
        throw new CREFormatException("Expecting an array, received " + c.getCType(), t);
    }
    CArray array = (CArray) c;
    MCWorld world = w;
    double x = 0;
    double y = 0;
    double z = 0;
    float yaw = 0;
    float pitch = 0;
    if (!array.inAssociativeMode()) {
        if (array.size() == 3) {
            // Just the xyz, with default yaw and pitch, and given world
            x = Static.getNumber(array.get(0, t), t);
            y = Static.getNumber(array.get(1, t), t);
            z = Static.getNumber(array.get(2, t), t);
        } else if (array.size() == 4) {
            // x, y, z, world
            x = Static.getNumber(array.get(0, t), t);
            y = Static.getNumber(array.get(1, t), t);
            z = Static.getNumber(array.get(2, t), t);
            world = Static.getServer().getWorld(array.get(3, t).val());
        } else if (array.size() == 5) {
            // x, y, z, yaw, pitch, with given world
            x = Static.getNumber(array.get(0, t), t);
            y = Static.getNumber(array.get(1, t), t);
            z = Static.getNumber(array.get(2, t), t);
            yaw = (float) Static.getNumber(array.get(3, t), t);
            pitch = (float) Static.getNumber(array.get(4, t), t);
        } else if (array.size() == 6) {
            // All have been given
            x = Static.getNumber(array.get(0, t), t);
            y = Static.getNumber(array.get(1, t), t);
            z = Static.getNumber(array.get(2, t), t);
            world = Static.getServer().getWorld(array.get(3, t).val());
            yaw = (float) Static.getNumber(array.get(4, t), t);
            pitch = (float) Static.getNumber(array.get(5, t), t);
        } else {
            throw new CREFormatException("Expecting a Location array, but the array did not meet the format specifications", t);
        }
    } else {
        if (array.containsKey("x")) {
            x = Static.getNumber(array.get("x", t), t);
        }
        if (array.containsKey("y")) {
            y = Static.getNumber(array.get("y", t), t);
        }
        if (array.containsKey("z")) {
            z = Static.getNumber(array.get("z", t), t);
        }
        if (array.containsKey("world")) {
            world = Static.getServer().getWorld(array.get("world", t).val());
        }
        if (array.containsKey("yaw")) {
            yaw = (float) Static.getDouble(array.get("yaw", t), t);
        }
        if (array.containsKey("pitch")) {
            pitch = (float) Static.getDouble(array.get("pitch", t), t);
        }
    }
    // If world is still null at this point, it's an error
    if (world == null) {
        throw new CREInvalidWorldException("The specified world doesn't exist, or no world was provided", t);
    }
    return StaticLayer.GetLocation(world, x, y, z, yaw, pitch);
}
Also used : CREInvalidWorldException(com.laytonsmith.core.exceptions.CRE.CREInvalidWorldException) CArray(com.laytonsmith.core.constructs.CArray) CREFormatException(com.laytonsmith.core.exceptions.CRE.CREFormatException) MCWorld(com.laytonsmith.abstraction.MCWorld)

Example 14 with CREFormatException

use of com.laytonsmith.core.exceptions.CRE.CREFormatException in project CommandHelper by EngineHub.

the class ObjectGenerator method vector.

/**
 * Modifies an existing vector using a given vector object. Because Vector3D is immutable, this method does not
 * actually modify the existing vector, but creates a new one.
 *
 * @param v the original vector
 * @param c the vector array
 * @param t the target
 * @return the Vector
 */
public Vector3D vector(Vector3D v, Construct c, Target t) {
    if (c instanceof CArray) {
        CArray va = (CArray) c;
        double x = v.X();
        double y = v.Y();
        double z = v.Z();
        if (!va.isAssociative()) {
            if (va.size() == 3) {
                // 3rd dimension vector
                x = Static.getNumber(va.get(0, t), t);
                y = Static.getNumber(va.get(1, t), t);
                z = Static.getNumber(va.get(2, t), t);
            } else if (va.size() == 2) {
                // 2nd dimension vector
                x = Static.getNumber(va.get(0, t), t);
                y = Static.getNumber(va.get(1, t), t);
            } else if (va.size() == 1) {
                x = Static.getNumber(va.get(0, t), t);
            }
        } else {
            if (va.containsKey("x")) {
                x = Static.getNumber(va.get("x", t), t);
            }
            if (va.containsKey("y")) {
                y = Static.getNumber(va.get("y", t), t);
            }
            if (va.containsKey("z")) {
                z = Static.getNumber(va.get("z", t), t);
            }
        }
        return new Vector3D(x, y, z);
    } else if (c instanceof CNull) {
        // fulfilling the todo?
        return v;
    } else {
        throw new CREFormatException("Expecting an array, received " + c.getCType(), t);
    }
}
Also used : Vector3D(com.laytonsmith.PureUtilities.Vector3D) CArray(com.laytonsmith.core.constructs.CArray) CREFormatException(com.laytonsmith.core.exceptions.CRE.CREFormatException) CNull(com.laytonsmith.core.constructs.CNull)

Example 15 with CREFormatException

use of com.laytonsmith.core.exceptions.CRE.CREFormatException in project CommandHelper by EngineHub.

the class ObjectGenerator method enchants.

public Map<MCEnchantment, Integer> enchants(CArray enchantArray, Target t) {
    Map<MCEnchantment, Integer> ret = new HashMap<>();
    for (String key : enchantArray.stringKeySet()) {
        try {
            CArray ea = (CArray) enchantArray.get(key, t);
            String setype = ea.get("etype", t).val();
            MCEnchantment etype = StaticLayer.GetConvertor().GetEnchantmentByName(setype);
            int elevel = Static.getInt32(ea.get("elevel", t), t);
            if (etype == null) {
                if (setype.equals("SWEEPING")) {
                    // data from 1.11.2, changed in 1.12
                    etype = StaticLayer.GetEnchantmentByName("SWEEPING_EDGE");
                }
                if (etype == null) {
                    throw new CREEnchantmentException("Unknown enchantment type at " + key, t);
                }
            }
            ret.put(etype, elevel);
        } catch (ClassCastException cce) {
            throw new CREFormatException("Expected an array at index " + key, t);
        }
    }
    return ret;
}
Also used : MCEnchantment(com.laytonsmith.abstraction.MCEnchantment) HashMap(java.util.HashMap) CArray(com.laytonsmith.core.constructs.CArray) CREEnchantmentException(com.laytonsmith.core.exceptions.CRE.CREEnchantmentException) CString(com.laytonsmith.core.constructs.CString) CREFormatException(com.laytonsmith.core.exceptions.CRE.CREFormatException)

Aggregations

CREFormatException (com.laytonsmith.core.exceptions.CRE.CREFormatException)16 CArray (com.laytonsmith.core.constructs.CArray)12 CString (com.laytonsmith.core.constructs.CString)9 Construct (com.laytonsmith.core.constructs.Construct)6 CNull (com.laytonsmith.core.constructs.CNull)4 MCEnchantment (com.laytonsmith.abstraction.MCEnchantment)3 MCItemStack (com.laytonsmith.abstraction.MCItemStack)3 CRECastException (com.laytonsmith.core.exceptions.CRE.CRECastException)3 CRERangeException (com.laytonsmith.core.exceptions.CRE.CRERangeException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 MCEntity (com.laytonsmith.abstraction.MCEntity)2 MCInventory (com.laytonsmith.abstraction.MCInventory)2 MCItemMeta (com.laytonsmith.abstraction.MCItemMeta)2 MCLivingEntity (com.laytonsmith.abstraction.MCLivingEntity)2 MCLocation (com.laytonsmith.abstraction.MCLocation)2 CRENotFoundException (com.laytonsmith.core.exceptions.CRE.CRENotFoundException)2 Map (java.util.Map)2 Vector3D (com.laytonsmith.PureUtilities.Vector3D)1 Cookie (com.laytonsmith.PureUtilities.Web.Cookie)1