Search in sources :

Example 41 with CArray

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

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

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

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

the class ObjectGenerator method itemMeta.

public Construct itemMeta(MCItemStack is, Target t) {
    if (!is.hasItemMeta()) {
        return CNull.NULL;
    } else {
        Construct display, lore;
        CArray ma = CArray.GetAssociativeArray(t);
        MCItemMeta meta = is.getItemMeta();
        if (meta.hasDisplayName()) {
            display = new CString(meta.getDisplayName(), t);
        } else {
            display = CNull.NULL;
        }
        if (meta.hasLore()) {
            lore = new CArray(t);
            for (String l : meta.getLore()) {
                ((CArray) lore).push(new CString(l, t), t);
            }
        } else {
            lore = CNull.NULL;
        }
        ma.set("display", display, t);
        ma.set("lore", lore, t);
        ma.set("enchants", enchants(meta.getEnchants(), t), t);
        ma.set("repair", new CInt(meta.getRepairCost(), t), t);
        // Version specific ItemMeta
        if (Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_8)) {
            Set<MCItemFlag> itemFlags = meta.getItemFlags();
            CArray flagArray = new CArray(t);
            if (itemFlags.size() > 0) {
                for (MCItemFlag flag : itemFlags) {
                    flagArray.push(new CString(flag.name(), t), t);
                }
            }
            ma.set("flags", flagArray, t);
            if (Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_11)) {
                ma.set("unbreakable", CBoolean.get(meta.isUnbreakable()), t);
            }
        }
        // Specific ItemMeta
        if (meta instanceof MCBlockStateMeta) {
            MCBlockState bs = ((MCBlockStateMeta) meta).getBlockState();
            if (bs instanceof MCShulkerBox) {
                MCShulkerBox mcsb = (MCShulkerBox) bs;
                MCInventory inv = mcsb.getInventory();
                CArray box = CArray.GetAssociativeArray(t);
                for (int i = 0; i < inv.getSize(); i++) {
                    Construct item = ObjectGenerator.GetGenerator().item(inv.getItem(i), t);
                    if (!(item instanceof CNull)) {
                        box.set(i, item, t);
                    }
                }
                ma.set("inventory", box, t);
            } else if (bs instanceof MCBanner) {
                MCBanner banner = (MCBanner) bs;
                CArray patterns = new CArray(t, banner.numberOfPatterns());
                for (MCPattern p : banner.getPatterns()) {
                    CArray pattern = CArray.GetAssociativeArray(t);
                    pattern.set("shape", new CString(p.getShape().toString(), t), t);
                    pattern.set("color", new CString(p.getColor().toString(), t), t);
                    patterns.push(pattern, t);
                }
                ma.set("patterns", patterns, t);
                MCDyeColor dyeColor = banner.getBaseColor();
                if (dyeColor != null) {
                    ma.set("basecolor", new CString(dyeColor.toString(), t), t);
                }
            } else if (bs instanceof MCCreatureSpawner) {
                MCCreatureSpawner mccs = (MCCreatureSpawner) bs;
                ma.set("spawntype", mccs.getSpawnedType().name());
            }
        } else if (meta instanceof MCFireworkEffectMeta) {
            MCFireworkEffectMeta mcfem = (MCFireworkEffectMeta) meta;
            MCFireworkEffect effect = mcfem.getEffect();
            if (effect == null) {
                ma.set("effect", CNull.NULL, t);
            } else {
                ma.set("effect", fireworkEffect(effect, t), t);
            }
        } else if (meta instanceof MCFireworkMeta) {
            MCFireworkMeta mcfm = (MCFireworkMeta) meta;
            CArray firework = CArray.GetAssociativeArray(t);
            firework.set("strength", new CInt(mcfm.getStrength(), t), t);
            CArray fe = new CArray(t);
            for (MCFireworkEffect effect : mcfm.getEffects()) {
                fe.push(fireworkEffect(effect, t), t);
            }
            firework.set("effects", fe, t);
            ma.set("firework", firework, t);
        } else if (meta instanceof MCLeatherArmorMeta) {
            CArray color = color(((MCLeatherArmorMeta) meta).getColor(), t);
            ma.set("color", color, t);
        } else if (meta instanceof MCBookMeta) {
            Construct title, author, pages;
            if (((MCBookMeta) meta).hasTitle()) {
                title = new CString(((MCBookMeta) meta).getTitle(), t);
            } else {
                title = CNull.NULL;
            }
            if (((MCBookMeta) meta).hasAuthor()) {
                author = new CString(((MCBookMeta) meta).getAuthor(), t);
            } else {
                author = CNull.NULL;
            }
            if (((MCBookMeta) meta).hasPages()) {
                pages = new CArray(t);
                for (String p : ((MCBookMeta) meta).getPages()) {
                    ((CArray) pages).push(new CString(p, t), t);
                }
            } else {
                pages = CNull.NULL;
            }
            ma.set("title", title, t);
            ma.set("author", author, t);
            ma.set("pages", pages, t);
        } else if (meta instanceof MCSkullMeta) {
            if (Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_12_X)) {
                if (((MCSkullMeta) meta).hasOwner()) {
                    MCOfflinePlayer player = ((MCSkullMeta) meta).getOwningPlayer();
                    ma.set("owner", new CString(player.getName(), t), t);
                    ma.set("owneruuid", new CString(player.getUniqueID().toString(), t), t);
                } else {
                    ma.set("owner", CNull.NULL, t);
                    ma.set("owneruuid", CNull.NULL, t);
                }
            } else {
                if (((MCSkullMeta) meta).hasOwner()) {
                    ma.set("owner", new CString(((MCSkullMeta) meta).getOwner(), t), t);
                } else {
                    ma.set("owner", CNull.NULL, t);
                }
            }
        } else if (meta instanceof MCEnchantmentStorageMeta) {
            Construct stored;
            if (((MCEnchantmentStorageMeta) meta).hasStoredEnchants()) {
                stored = enchants(((MCEnchantmentStorageMeta) meta).getStoredEnchants(), t);
            } else {
                stored = CNull.NULL;
            }
            ma.set("stored", stored, t);
        } else if (meta instanceof MCPotionMeta) {
            MCPotionMeta potionmeta = (MCPotionMeta) meta;
            CArray effects = potions(potionmeta.getCustomEffects(), t);
            ma.set("potions", effects, t);
            if (Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_9)) {
                MCPotionData potiondata = potionmeta.getBasePotionData();
                if (potiondata != null) {
                    ma.set("base", potionData(potiondata, t), t);
                }
            } else if (effects.size() > 0) {
                ma.set("main", ((CArray) effects.get(0, t)).get("id", t), t);
            }
        } else if (meta instanceof MCBannerMeta) {
            MCBannerMeta bannermeta = (MCBannerMeta) meta;
            CArray patterns = new CArray(t, bannermeta.numberOfPatterns());
            for (MCPattern p : bannermeta.getPatterns()) {
                CArray pattern = CArray.GetAssociativeArray(t);
                pattern.set("shape", new CString(p.getShape().toString(), t), t);
                pattern.set("color", new CString(p.getColor().toString(), t), t);
                patterns.push(pattern, t);
            }
            ma.set("patterns", patterns, t);
            MCDyeColor dyeColor = bannermeta.getBaseColor();
            if (dyeColor != null) {
                ma.set("basecolor", new CString(dyeColor.toString(), t), t);
            }
        } else if (meta instanceof MCSpawnEggMeta) {
            MCEntityType spawntype = ((MCSpawnEggMeta) meta).getSpawnedType();
            if (spawntype == null) {
                ma.set("spawntype", CNull.NULL, t);
            } else {
                ma.set("spawntype", new CString(spawntype.name(), t), t);
            }
        } else if (meta instanceof MCMapMeta && Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_11)) {
            MCColor mapcolor = ((MCMapMeta) meta).getColor();
            Construct color;
            if (mapcolor == null) {
                color = CNull.NULL;
            } else {
                color = color(mapcolor, t);
            }
            ma.set("color", color, t);
        }
        return ma;
    }
}
Also used : MCBanner(com.laytonsmith.abstraction.blocks.MCBanner) MCEntityType(com.laytonsmith.abstraction.enums.MCEntityType) MCOfflinePlayer(com.laytonsmith.abstraction.MCOfflinePlayer) MCFireworkMeta(com.laytonsmith.abstraction.MCFireworkMeta) CArray(com.laytonsmith.core.constructs.CArray) MCLeatherArmorMeta(com.laytonsmith.abstraction.MCLeatherArmorMeta) MCSpawnEggMeta(com.laytonsmith.abstraction.MCSpawnEggMeta) CString(com.laytonsmith.core.constructs.CString) CString(com.laytonsmith.core.constructs.CString) MCPattern(com.laytonsmith.abstraction.MCPattern) MCFireworkEffect(com.laytonsmith.abstraction.MCFireworkEffect) MCPotionMeta(com.laytonsmith.abstraction.MCPotionMeta) MCMapMeta(com.laytonsmith.abstraction.MCMapMeta) MCColor(com.laytonsmith.abstraction.MCColor) MCItemFlag(com.laytonsmith.abstraction.enums.MCItemFlag) MCDyeColor(com.laytonsmith.abstraction.enums.MCDyeColor) MCBookMeta(com.laytonsmith.abstraction.MCBookMeta) MCBlockStateMeta(com.laytonsmith.abstraction.MCBlockStateMeta) MCFireworkEffectMeta(com.laytonsmith.abstraction.MCFireworkEffectMeta) MCBlockState(com.laytonsmith.abstraction.blocks.MCBlockState) MCBannerMeta(com.laytonsmith.abstraction.MCBannerMeta) MCInventory(com.laytonsmith.abstraction.MCInventory) MCSkullMeta(com.laytonsmith.abstraction.MCSkullMeta) MCItemMeta(com.laytonsmith.abstraction.MCItemMeta) MCPotionData(com.laytonsmith.abstraction.MCPotionData) MCEnchantmentStorageMeta(com.laytonsmith.abstraction.MCEnchantmentStorageMeta) CInt(com.laytonsmith.core.constructs.CInt) MCCreatureSpawner(com.laytonsmith.abstraction.MCCreatureSpawner) Construct(com.laytonsmith.core.constructs.Construct) MCShulkerBox(com.laytonsmith.abstraction.blocks.MCShulkerBox) CNull(com.laytonsmith.core.constructs.CNull)

Example 45 with CArray

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

the class ObjectGenerator method potionData.

public CArray potionData(MCPotionData mcpd, Target t) {
    CArray base = CArray.GetAssociativeArray(t);
    base.set("type", mcpd.getType().name(), t);
    base.set("extended", CBoolean.get(mcpd.isExtended()), t);
    base.set("upgraded", CBoolean.get(mcpd.isUpgraded()), t);
    return base;
}
Also used : CArray(com.laytonsmith.core.constructs.CArray)

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