Search in sources :

Example 1 with CRERangeException

use of com.laytonsmith.core.exceptions.CRE.CRERangeException in project CommandHelper by EngineHub.

the class CArray method remove.

/**
 * Removes the value at the specified key
 *
 * @param construct
 * @return
 */
public Construct remove(Construct construct) {
    String c = normalizeConstruct(construct);
    Construct ret;
    if (!associative_mode) {
        try {
            ret = array.remove(Integer.parseInt(c));
            next_index--;
        } catch (NumberFormatException e) {
            throw new CRECastException("Expecting an integer, but received \"" + c + "\" (were you expecting an associative array? This array is a normal array.)", construct.getTarget());
        } catch (IndexOutOfBoundsException e) {
            throw new CRERangeException("Cannot remove the value at '" + c + "', as no such index exists in the array", construct.getTarget());
        }
    } else {
        ret = associative_array.remove(c);
    }
    setDirty();
    return ret;
}
Also used : CRECastException(com.laytonsmith.core.exceptions.CRE.CRECastException) CRERangeException(com.laytonsmith.core.exceptions.CRE.CRERangeException)

Example 2 with CRERangeException

use of com.laytonsmith.core.exceptions.CRE.CRERangeException 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 3 with CRERangeException

use of com.laytonsmith.core.exceptions.CRE.CRERangeException in project CommandHelper by EngineHub.

the class ObjectGenerator method color.

/**
 * Returns an MCColor given a colorArray, which supports the following three format recipeTypes (in this order of
 * priority) array(r: 0, g: 0, b: 0) array(red: 0, green: 0, blue: 0) array(0, 0, 0)
 *
 * @param color
 * @param t
 * @return
 */
public MCColor color(CArray color, Target t) {
    int red;
    int green;
    int blue;
    if (color.containsKey("r")) {
        red = Static.getInt32(color.get("r", t), t);
    } else if (color.containsKey("red")) {
        red = Static.getInt32(color.get("red", t), t);
    } else {
        red = Static.getInt32(color.get(0, t), t);
    }
    if (color.containsKey("g")) {
        green = Static.getInt32(color.get("g", t), t);
    } else if (color.containsKey("green")) {
        green = Static.getInt32(color.get("green", t), t);
    } else {
        green = Static.getInt32(color.get(1, t), t);
    }
    if (color.containsKey("b")) {
        blue = Static.getInt32(color.get("b", t), t);
    } else if (color.containsKey("blue")) {
        blue = Static.getInt32(color.get("blue", t), t);
    } else {
        blue = Static.getInt32(color.get(2, t), t);
    }
    try {
        return StaticLayer.GetConvertor().GetColor(red, green, blue);
    } catch (IllegalArgumentException ex) {
        throw new CRERangeException(ex.getMessage(), t, ex);
    }
}
Also used : CRERangeException(com.laytonsmith.core.exceptions.CRE.CRERangeException)

Example 4 with CRERangeException

use of com.laytonsmith.core.exceptions.CRE.CRERangeException in project CommandHelper by EngineHub.

the class ObjectGenerator method potions.

public List<MCLivingEntity.MCEffect> potions(CArray ea, Target t) {
    List<MCLivingEntity.MCEffect> ret = new ArrayList<>();
    for (String key : ea.stringKeySet()) {
        if (ea.get(key, t) instanceof CArray) {
            CArray effect = (CArray) ea.get(key, t);
            int potionID = 0, strength = 0;
            double seconds = 30.0;
            boolean ambient = false;
            boolean particles = true;
            if (effect.containsKey("id")) {
                potionID = Static.getInt32(effect.get("id", t), t);
            } else {
                throw new CREFormatException("No potion ID was given at index " + key, t);
            }
            if (effect.containsKey("strength")) {
                strength = Static.getInt32(effect.get("strength", t), t);
            }
            if (effect.containsKey("seconds")) {
                seconds = Static.getDouble(effect.get("seconds", t), t);
                if (seconds < 0.0) {
                    throw new CRERangeException("Seconds cannot be less than 0", t);
                } else if (seconds * 20 > Integer.MAX_VALUE) {
                    throw new CRERangeException("Seconds cannot be greater than 107374182", t);
                }
            }
            if (effect.containsKey("ambient")) {
                ambient = Static.getBoolean(effect.get("ambient", t), t);
            }
            if (effect.containsKey("particles")) {
                particles = Static.getBoolean(effect.get("particles", t), t);
            }
            ret.add(new MCLivingEntity.MCEffect(potionID, strength, (int) (seconds * 20), ambient, particles));
        } else {
            throw new CREFormatException("Expected a potion array at index" + key, t);
        }
    }
    return ret;
}
Also used : ArrayList(java.util.ArrayList) CArray(com.laytonsmith.core.constructs.CArray) CString(com.laytonsmith.core.constructs.CString) CREFormatException(com.laytonsmith.core.exceptions.CRE.CREFormatException) CRERangeException(com.laytonsmith.core.exceptions.CRE.CRERangeException) MCLivingEntity(com.laytonsmith.abstraction.MCLivingEntity)

Example 5 with CRERangeException

use of com.laytonsmith.core.exceptions.CRE.CRERangeException in project CommandHelper by EngineHub.

the class BukkitMCPotionMeta method addCustomEffect.

@Override
public boolean addCustomEffect(int potionID, int strength, int ticks, boolean ambient, boolean overwrite, Target t) {
    int maxID = PotionEffectType.values().length;
    if (potionID < 1 || potionID > maxID) {
        throw new CRERangeException("Invalid effect ID, must be from 1-" + maxID, t);
    }
    PotionEffect pe = new PotionEffect(PotionEffectType.getById(potionID), ticks, strength, ambient);
    return pm.addCustomEffect(pe, overwrite);
}
Also used : PotionEffect(org.bukkit.potion.PotionEffect) CRERangeException(com.laytonsmith.core.exceptions.CRE.CRERangeException)

Aggregations

CRERangeException (com.laytonsmith.core.exceptions.CRE.CRERangeException)5 CArray (com.laytonsmith.core.constructs.CArray)2 CString (com.laytonsmith.core.constructs.CString)2 CREFormatException (com.laytonsmith.core.exceptions.CRE.CREFormatException)2 MCEnchantment (com.laytonsmith.abstraction.MCEnchantment)1 MCItemMeta (com.laytonsmith.abstraction.MCItemMeta)1 MCItemStack (com.laytonsmith.abstraction.MCItemStack)1 MCLivingEntity (com.laytonsmith.abstraction.MCLivingEntity)1 MCMaterial (com.laytonsmith.abstraction.blocks.MCMaterial)1 CNull (com.laytonsmith.core.constructs.CNull)1 Construct (com.laytonsmith.core.constructs.Construct)1 CRECastException (com.laytonsmith.core.exceptions.CRE.CRECastException)1 CRENotFoundException (com.laytonsmith.core.exceptions.CRE.CRENotFoundException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 PotionEffect (org.bukkit.potion.PotionEffect)1