Search in sources :

Example 1 with MCFireworkBuilder

use of com.laytonsmith.abstraction.MCFireworkBuilder 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)

Aggregations

MCColor (com.laytonsmith.abstraction.MCColor)1 MCFireworkBuilder (com.laytonsmith.abstraction.MCFireworkBuilder)1 CArray (com.laytonsmith.core.constructs.CArray)1 CInt (com.laytonsmith.core.constructs.CInt)1 CString (com.laytonsmith.core.constructs.CString)1 Construct (com.laytonsmith.core.constructs.Construct)1 CREFormatException (com.laytonsmith.core.exceptions.CRE.CREFormatException)1