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;
}
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;
}
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);
}
}
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;
}
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);
}
Aggregations