Search in sources :

Example 16 with CInt

use of com.laytonsmith.core.constructs.CInt in project CommandHelper by EngineHub.

the class ObjectGenerator method potions.

public CArray potions(List<MCLivingEntity.MCEffect> effectList, Target t) {
    CArray ea = new CArray(t);
    for (MCLivingEntity.MCEffect eff : effectList) {
        CArray effect = CArray.GetAssociativeArray(t);
        effect.set("id", new CInt(eff.getPotionID(), t), t);
        effect.set("strength", new CInt(eff.getStrength(), t), t);
        effect.set("seconds", new CDouble(eff.getTicksRemaining() / 20.0, t), t);
        effect.set("ambient", CBoolean.get(eff.isAmbient()), t);
        effect.set("particles", CBoolean.get(eff.hasParticles()), t);
        ea.push(effect, t);
    }
    return ea;
}
Also used : CInt(com.laytonsmith.core.constructs.CInt) CArray(com.laytonsmith.core.constructs.CArray) CDouble(com.laytonsmith.core.constructs.CDouble) MCLivingEntity(com.laytonsmith.abstraction.MCLivingEntity)

Example 17 with CInt

use of com.laytonsmith.core.constructs.CInt in project CommandHelper by EngineHub.

the class ObjectGenerator method color.

/**
 * Returns a CArray given an MCColor. It will be in the format array(r: 0, g: 0, b: 0)
 *
 * @param color
 * @param t
 * @return
 */
public CArray color(MCColor color, Target t) {
    CArray ca = CArray.GetAssociativeArray(t);
    ca.set("r", new CInt(color.getRed(), t), t);
    ca.set("g", new CInt(color.getGreen(), t), t);
    ca.set("b", new CInt(color.getBlue(), t), t);
    return ca;
}
Also used : CInt(com.laytonsmith.core.constructs.CInt) CArray(com.laytonsmith.core.constructs.CArray)

Example 18 with CInt

use of com.laytonsmith.core.constructs.CInt in project CommandHelper by EngineHub.

the class Static method getMSObject.

/**
 * Given a java object, returns a MethodScript object.
 *
 * @param object
 * @param t
 * @return
 */
public static Construct getMSObject(Object object, Target t) {
    if (object == null) {
        return CNull.NULL;
    } else if (object instanceof Boolean) {
        return CBoolean.get((boolean) object);
    } else if ((object instanceof Byte) || (object instanceof Short) || (object instanceof Integer) || (object instanceof Long)) {
        return new CInt((long) object, t);
    } else if ((object instanceof Float) || (object instanceof Double)) {
        return new CDouble((double) object, t);
    } else if (object instanceof Character) {
        return new CString((char) object, t);
    } else if (object instanceof String) {
        return new CString((String) object, t);
    } else if (object instanceof StringBuffer) {
        return new CResource<>((StringBuffer) object, new CResource.ResourceToString() {

            @Override
            public String getString(CResource res) {
                return res.getResource().toString();
            }
        }, t);
    } else if (object instanceof XMLDocument) {
        return new CResource<>((XMLDocument) object, t);
    } else if (object instanceof Construct) {
        return (Construct) object;
    } else if (object instanceof boolean[]) {
        boolean[] array = (boolean[]) object;
        CArray r = new CArray(t);
        for (boolean b : array) {
            r.push(CBoolean.get(b), t);
        }
        return r;
    } else if (object instanceof byte[]) {
        return CByteArray.wrap((byte[]) object, t);
    } else if (object instanceof char[]) {
        char[] array = (char[]) object;
        CArray r = new CArray(t);
        for (char c : array) {
            r.push(new CString(c, t), t);
        }
        return r;
    } else if (object instanceof short[]) {
        short[] array = (short[]) object;
        CArray r = new CArray(t);
        for (short s : array) {
            r.push(new CInt(s, t), t);
        }
        return r;
    } else if (object instanceof int[]) {
        int[] array = (int[]) object;
        CArray r = new CArray(t);
        for (int i : array) {
            r.push(new CInt(i, t), t);
        }
        return r;
    } else if (object instanceof long[]) {
        long[] array = (long[]) object;
        CArray r = new CArray(t);
        for (long l : array) {
            r.push(new CInt(l, t), t);
        }
        return r;
    } else if (object instanceof float[]) {
        float[] array = (float[]) object;
        CArray r = new CArray(t);
        for (float f : array) {
            r.push(new CDouble(f, t), t);
        }
        return r;
    } else if (object instanceof double[]) {
        double[] array = (double[]) object;
        CArray r = new CArray(t);
        for (double d : array) {
            r.push(new CDouble(d, t), t);
        }
        return r;
    } else if (object instanceof Object[]) {
        CArray r = new CArray(t);
        for (Object o : (Object[]) object) {
            r.push((o == object) ? r : getMSObject(o, t), t);
        }
        return r;
    } else if (object instanceof Collection) {
        return getMSObject(((Collection) object).toArray(), t);
    } else if (object instanceof Map) {
        Map map = ((Map) object);
        CArray r = new CArray(t);
        for (Object key : map.keySet()) {
            Object o = map.get(key);
            r.set(key.toString(), (o == object) ? r : getMSObject(o, t), t);
        }
        return r;
    } else {
        return new CString(object.toString(), t);
    }
}
Also used : CArray(com.laytonsmith.core.constructs.CArray) CString(com.laytonsmith.core.constructs.CString) XMLDocument(com.laytonsmith.PureUtilities.XMLDocument) CString(com.laytonsmith.core.constructs.CString) CBoolean(com.laytonsmith.core.constructs.CBoolean) CResource(com.laytonsmith.core.constructs.CResource) CDouble(com.laytonsmith.core.constructs.CDouble) CDouble(com.laytonsmith.core.constructs.CDouble) CInt(com.laytonsmith.core.constructs.CInt) Construct(com.laytonsmith.core.constructs.Construct) Collection(java.util.Collection) Map(java.util.Map) HashMap(java.util.HashMap)

Example 19 with CInt

use of com.laytonsmith.core.constructs.CInt in project CommandHelper by EngineHub.

the class Static method getNumber.

/**
 * Returns a CNumber construct (CInt or CDouble) from any java number.
 *
 * @param number The java number to convert.
 * @param t The code target.
 * @return A construct equivalent to the given java number, whose the type is the better to represent it.
 */
public static CNumber getNumber(Number number, Target t) {
    long longValue = number.longValue();
    double doubleValue = number.doubleValue();
    return longValue == doubleValue ? new CInt(longValue, t) : new CDouble(doubleValue, t);
}
Also used : CInt(com.laytonsmith.core.constructs.CInt) CDouble(com.laytonsmith.core.constructs.CDouble)

Example 20 with CInt

use of com.laytonsmith.core.constructs.CInt in project CommandHelper by EngineHub.

the class Static method getJavaObject.

/**
 * Given a MethodScript object, returns a java object.
 *
 * @param construct
 * @return
 */
public static Object getJavaObject(Construct construct) {
    if ((construct == null) || (construct instanceof CNull)) {
        return null;
    } else if (construct instanceof CVoid) {
        return "";
    } else if (construct instanceof CBoolean) {
        return ((CBoolean) construct).getBoolean();
    } else if (construct instanceof CInt) {
        return ((CInt) construct).getInt();
    } else if (construct instanceof CDouble) {
        return ((CDouble) construct).getDouble();
    } else if (construct instanceof CString) {
        return construct.val();
    } else if (construct instanceof CByteArray) {
        return ((CByteArray) construct).asByteArrayCopy();
    } else if (construct instanceof CResource) {
        return ((CResource) construct).getResource();
    } else if (construct instanceof CArray) {
        CArray array = (CArray) construct;
        if (array.isAssociative()) {
            HashMap<String, Object> map = new HashMap<>();
            for (Construct key : array.keySet()) {
                Construct c = array.get(key.val(), Target.UNKNOWN);
                map.put(key.val(), (c == array) ? map : getJavaObject(c));
            }
            return map;
        } else {
            Object[] a = new Object[(int) array.size()];
            boolean nullable = false;
            Class<?> clazz = null;
            for (int i = 0; i < array.size(); i++) {
                Construct c = array.get(i, Target.UNKNOWN);
                if (c == array) {
                    a[i] = a;
                } else {
                    a[i] = getJavaObject(array.get(i, Target.UNKNOWN));
                }
                if (a[i] != null) {
                    if (clazz == null) {
                        clazz = a[i].getClass();
                    } else if (!clazz.equals(Object.class)) {
                        // to test if it is possible to return something more specific than Object[]
                        Class<?> cl = a[i].getClass();
                        while (!clazz.isAssignableFrom(cl)) {
                            clazz = clazz.getSuperclass();
                        }
                    }
                } else {
                    nullable = true;
                }
            }
            if ((clazz != null) && (!clazz.equals(Object.class))) {
                if (clazz.equals(Boolean.class) && !nullable) {
                    boolean[] r = new boolean[a.length];
                    for (int i = 0; i < a.length; i++) {
                        r[i] = (boolean) a[i];
                    }
                    return r;
                }
                if (clazz.equals(Long.class) && !nullable) {
                    long[] r = new long[a.length];
                    for (int i = 0; i < a.length; i++) {
                        r[i] = (long) a[i];
                    }
                    return r;
                } else if (clazz.equals(Double.class) && !nullable) {
                    double[] r = new double[a.length];
                    for (int i = 0; i < a.length; i++) {
                        r[i] = (double) a[i];
                    }
                    return r;
                } else {
                    Object[] r = (Object[]) Array.newInstance(clazz, a.length);
                    System.arraycopy(a, 0, r, 0, a.length);
                    return r;
                }
            } else {
                return a;
            }
        }
    } else {
        return construct;
    }
}
Also used : HashMap(java.util.HashMap) CBoolean(com.laytonsmith.core.constructs.CBoolean) CArray(com.laytonsmith.core.constructs.CArray) CDouble(com.laytonsmith.core.constructs.CDouble) CString(com.laytonsmith.core.constructs.CString) CVoid(com.laytonsmith.core.constructs.CVoid) CString(com.laytonsmith.core.constructs.CString) CByteArray(com.laytonsmith.core.constructs.CByteArray) CInt(com.laytonsmith.core.constructs.CInt) Construct(com.laytonsmith.core.constructs.Construct) CBoolean(com.laytonsmith.core.constructs.CBoolean) CResource(com.laytonsmith.core.constructs.CResource) CNull(com.laytonsmith.core.constructs.CNull)

Aggregations

CInt (com.laytonsmith.core.constructs.CInt)20 CArray (com.laytonsmith.core.constructs.CArray)17 CString (com.laytonsmith.core.constructs.CString)12 Construct (com.laytonsmith.core.constructs.Construct)11 CDouble (com.laytonsmith.core.constructs.CDouble)8 CNull (com.laytonsmith.core.constructs.CNull)5 CBoolean (com.laytonsmith.core.constructs.CBoolean)4 HashMap (java.util.HashMap)4 CFunction (com.laytonsmith.core.constructs.CFunction)3 Command (com.laytonsmith.core.constructs.Command)3 IVariable (com.laytonsmith.core.constructs.IVariable)3 CRECastException (com.laytonsmith.core.exceptions.CRE.CRECastException)3 ConfigCompileException (com.laytonsmith.core.exceptions.ConfigCompileException)3 Test (org.junit.Test)3 MCColor (com.laytonsmith.abstraction.MCColor)2 CLabel (com.laytonsmith.core.constructs.CLabel)2 CResource (com.laytonsmith.core.constructs.CResource)2 Target (com.laytonsmith.core.constructs.Target)2 Variable (com.laytonsmith.core.constructs.Variable)2 CommandHelperEnvironment (com.laytonsmith.core.environments.CommandHelperEnvironment)2