Search in sources :

Example 1 with MCMaterial

use of com.laytonsmith.abstraction.blocks.MCMaterial in project CommandHelper by EngineHub.

the class ObjectGenerator method item.

/**
 * Gets an MCItemStack from a given item "object". Supports both the old and new formats currently
 *
 * @param i
 * @param t
 * @return An abstract item stack
 */
public MCItemStack item(Construct i, Target t) {
    if (i instanceof CNull) {
        return EmptyItem();
    }
    if (!(i instanceof CArray)) {
        throw new CREFormatException("Expected an array!", t);
    }
    CArray item = (CArray) i;
    MCMaterial mat;
    int data = 0;
    int qty = 1;
    if (item.containsKey("qty")) {
        qty = Static.getInt32(item.get("qty", t), t);
        if (qty <= 0) {
            return EmptyItem();
        }
    }
    if (item.containsKey("name")) {
        mat = StaticLayer.GetConvertor().GetMaterial(item.get("name", t).val());
    } else if (item.containsKey("type")) {
        Construct type = item.get("type", t);
        if (type instanceof CString) {
            int seperatorIndex = type.val().indexOf(':');
            if (seperatorIndex != -1) {
                try {
                    data = Integer.parseInt(type.val().substring(seperatorIndex + 1));
                } catch (NumberFormatException e) {
                    throw new CRERangeException("The item data \"" + type.val().substring(seperatorIndex + 1) + "\" is not a valid integer.", t);
                }
                type = new CString(type.val().substring(0, seperatorIndex), t);
            }
        }
        mat = StaticLayer.GetConvertor().getMaterial(Static.getInt32(type, t));
    } else {
        throw new CREFormatException("Could not find item name!", t);
    }
    if (mat == null) {
        throw new CRENotFoundException("A material could not be found based on the given name.", t);
    }
    if (mat.getType() == 0) {
        return EmptyItem();
    }
    if (item.containsKey("data")) {
        data = Static.getInt32(item.get("data", t), t);
    }
    MCItemMeta meta = null;
    if (item.containsKey("meta")) {
        meta = itemMeta(item.get("meta", t), mat, t);
    }
    // Create itemstack
    MCItemStack ret = StaticLayer.GetItemStack(mat, data, qty);
    if (meta != null) {
        ret.setItemMeta(meta);
    }
    // Fallback to enchants in item array if not in meta
    if (item.containsKey("enchants")) {
        try {
            Map<MCEnchantment, Integer> enchants = enchants((CArray) item.get("enchants", t), t);
            for (Map.Entry<MCEnchantment, Integer> entry : enchants.entrySet()) {
                ret.addUnsafeEnchantment(entry.getKey(), entry.getValue());
            }
        } catch (ClassCastException ex) {
            throw new CREFormatException("Enchants must be an array of enchantment arrays.", t);
        }
    }
    return ret;
}
Also used : CRENotFoundException(com.laytonsmith.core.exceptions.CRE.CRENotFoundException) CArray(com.laytonsmith.core.constructs.CArray) MCItemMeta(com.laytonsmith.abstraction.MCItemMeta) CString(com.laytonsmith.core.constructs.CString) MCMaterial(com.laytonsmith.abstraction.blocks.MCMaterial) MCEnchantment(com.laytonsmith.abstraction.MCEnchantment) MCItemStack(com.laytonsmith.abstraction.MCItemStack) Construct(com.laytonsmith.core.constructs.Construct) CREFormatException(com.laytonsmith.core.exceptions.CRE.CREFormatException) Map(java.util.Map) HashMap(java.util.HashMap) CRERangeException(com.laytonsmith.core.exceptions.CRE.CRERangeException) CNull(com.laytonsmith.core.constructs.CNull)

Example 2 with MCMaterial

use of com.laytonsmith.abstraction.blocks.MCMaterial in project CommandHelper by EngineHub.

the class ObjectGenerator method item.

/**
 * An Item Object consists of data about a particular item stack. Information included is: recipeType, data, qty,
 * and an array of enchantment objects (labeled enchants): erecipeType (enchantment recipeType) and elevel
 * (enchantment level). For backwards compatibility, this information is also listed in numerical slots as well as
 * associative slots. If the MCItemStack is null, or the underlying item is nonexistant (or air) CNull is returned.
 *
 * @param is
 * @return An item array or CNull
 */
public Construct item(MCItemStack is, Target t) {
    if (is == null || is.getAmount() == 0 || is.getTypeId() == 0) {
        return CNull.NULL;
    }
    MCMaterial mat = is.getType();
    CArray ret = CArray.GetAssociativeArray(t);
    ret.set("name", new CString(mat.getName(), t), t);
    ret.set("type", new CInt(mat.getType(), t), t);
    ret.set("data", new CInt(is.getDurability(), t), t);
    ret.set("qty", new CInt(is.getAmount(), t), t);
    ret.set("enchants", enchants(is.getEnchantments(), t), t);
    ret.set("meta", itemMeta(is, t), t);
    return ret;
}
Also used : MCMaterial(com.laytonsmith.abstraction.blocks.MCMaterial) CInt(com.laytonsmith.core.constructs.CInt) CArray(com.laytonsmith.core.constructs.CArray) CString(com.laytonsmith.core.constructs.CString)

Example 3 with MCMaterial

use of com.laytonsmith.abstraction.blocks.MCMaterial in project CommandHelper by EngineHub.

the class BukkitMCItemFactory method isApplicable.

@Override
public boolean isApplicable(MCItemMeta meta, MCMaterial material) {
    ItemMeta bmeta = ((BukkitMCItemMeta) meta).asItemMeta();
    Material bmat = (Material) material.getHandle();
    return f.isApplicable(bmeta, bmat);
}
Also used : MCMaterial(com.laytonsmith.abstraction.blocks.MCMaterial) Material(org.bukkit.Material) ItemMeta(org.bukkit.inventory.meta.ItemMeta) MCItemMeta(com.laytonsmith.abstraction.MCItemMeta)

Example 4 with MCMaterial

use of com.laytonsmith.abstraction.blocks.MCMaterial in project CommandHelper by EngineHub.

the class BukkitMCItemFactory method asMetaFor.

@Override
public MCItemMeta asMetaFor(MCItemMeta meta, MCMaterial material) {
    ItemMeta bmeta = ((BukkitMCItemMeta) meta).asItemMeta();
    Material bmat = (Material) material.getHandle();
    return BukkitConvertor.BukkitGetCorrectMeta(f.asMetaFor(bmeta, bmat));
}
Also used : MCMaterial(com.laytonsmith.abstraction.blocks.MCMaterial) Material(org.bukkit.Material) ItemMeta(org.bukkit.inventory.meta.ItemMeta) MCItemMeta(com.laytonsmith.abstraction.MCItemMeta)

Aggregations

MCMaterial (com.laytonsmith.abstraction.blocks.MCMaterial)4 MCItemMeta (com.laytonsmith.abstraction.MCItemMeta)3 CArray (com.laytonsmith.core.constructs.CArray)2 CString (com.laytonsmith.core.constructs.CString)2 Material (org.bukkit.Material)2 ItemMeta (org.bukkit.inventory.meta.ItemMeta)2 MCEnchantment (com.laytonsmith.abstraction.MCEnchantment)1 MCItemStack (com.laytonsmith.abstraction.MCItemStack)1 CInt (com.laytonsmith.core.constructs.CInt)1 CNull (com.laytonsmith.core.constructs.CNull)1 Construct (com.laytonsmith.core.constructs.Construct)1 CREFormatException (com.laytonsmith.core.exceptions.CRE.CREFormatException)1 CRENotFoundException (com.laytonsmith.core.exceptions.CRE.CRENotFoundException)1 CRERangeException (com.laytonsmith.core.exceptions.CRE.CRERangeException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1