Search in sources :

Example 46 with CArray

use of com.laytonsmith.core.constructs.CArray 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 47 with CArray

use of com.laytonsmith.core.constructs.CArray 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 48 with CArray

use of com.laytonsmith.core.constructs.CArray 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)

Example 49 with CArray

use of com.laytonsmith.core.constructs.CArray 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 50 with CArray

use of com.laytonsmith.core.constructs.CArray 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

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