use of com.laytonsmith.abstraction.MCItemStack in project CommandHelper by EngineHub.
the class ObjectGenerator method recipe.
public Construct recipe(MCRecipe r, Target t) {
if (r == null) {
return CNull.NULL;
}
CArray ret = CArray.GetAssociativeArray(t);
ret.set("type", new CString(r.getRecipeType().name(), t), t);
ret.set("result", item(r.getResult(), t), t);
if (r instanceof MCFurnaceRecipe) {
ret.set("input", item(((MCFurnaceRecipe) r).getInput(), t), t);
} else if (r instanceof MCShapelessRecipe) {
MCShapelessRecipe shapeless = (MCShapelessRecipe) r;
CArray il = new CArray(t);
for (MCItemStack i : shapeless.getIngredients()) {
il.push(item(i, t), t);
}
ret.set("ingredients", il, t);
if (shapeless.getKey() != null) {
ret.set("key", shapeless.getKey(), t);
}
} else if (r instanceof MCShapedRecipe) {
MCShapedRecipe shaped = (MCShapedRecipe) r;
CArray shape = new CArray(t);
for (String line : shaped.getShape()) {
shape.push(new CString(line, t), t);
}
CArray imap = CArray.GetAssociativeArray(t);
for (Map.Entry<Character, MCItemStack> entry : shaped.getIngredientMap().entrySet()) {
imap.set(entry.getKey().toString(), item(entry.getValue(), t), t);
}
ret.set("shape", shape, t);
ret.set("ingredients", imap, t);
if (shaped.getKey() != null) {
ret.set("key", shaped.getKey(), t);
}
}
return ret;
}
use of com.laytonsmith.abstraction.MCItemStack 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.abstraction.MCItemStack in project CommandHelper by EngineHub.
the class ObjectGenerator method itemMeta.
public MCItemMeta itemMeta(Construct c, MCMaterial mat, Target t) throws ConfigRuntimeException {
MCItemFactory itemFactory = Static.getServer().getItemFactory();
if (itemFactory == null) {
throw new CRENotFoundException("Could not find the internal MCItemFactory object (are you running in cmdline mode?)", t);
}
MCItemMeta meta = itemFactory.getItemMeta(mat);
if (c instanceof CNull) {
return meta;
}
CArray ma;
if (c instanceof CArray) {
ma = (CArray) c;
try {
if (ma.containsKey("display")) {
Construct dni = ma.get("display", t);
if (!(dni instanceof CNull)) {
meta.setDisplayName(dni.val());
}
}
if (ma.containsKey("lore")) {
Construct li = ma.get("lore", t);
if (li instanceof CNull) {
// do nothing
} else if (li instanceof CString) {
List<String> ll = new ArrayList<>();
ll.add(li.val());
meta.setLore(ll);
} else if (li instanceof CArray) {
CArray la = (CArray) li;
List<String> ll = new ArrayList<>();
for (int j = 0; j < la.size(); j++) {
ll.add(la.get(j, t).val());
}
meta.setLore(ll);
} else {
throw new CREFormatException("Lore was expected to be an array or a string.", t);
}
}
if (ma.containsKey("enchants")) {
Construct enchants = ma.get("enchants", t);
if (enchants instanceof CArray) {
for (Map.Entry<MCEnchantment, Integer> ench : enchants((CArray) enchants, t).entrySet()) {
meta.addEnchant(ench.getKey(), ench.getValue(), true);
}
} else {
throw new CREFormatException("Enchants field was expected to be an array of Enchantment arrays", t);
}
}
if (ma.containsKey("repair") && !(ma.get("repair", t) instanceof CNull)) {
meta.setRepairCost(Static.getInt32(ma.get("repair", t), t));
}
// Version specific ItemMeta
if (Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_8)) {
if (ma.containsKey("flags")) {
Construct flags = ma.get("flags", t);
if (flags instanceof CArray) {
CArray flagArray = (CArray) flags;
for (int i = 0; i < flagArray.size(); i++) {
Construct flag = flagArray.get(i, t);
meta.addItemFlags(MCItemFlag.valueOf(flag.getValue().toUpperCase()));
}
} else {
throw new CREFormatException("Itemflags was expected to be an array of flags.", t);
}
}
if (Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_11)) {
if (ma.containsKey("unbreakable")) {
meta.setUnbreakable(Static.getBoolean(ma.get("unbreakable", t), t));
}
}
}
// Specific ItemMeta
if (meta instanceof MCBlockStateMeta) {
MCBlockStateMeta bsm = (MCBlockStateMeta) meta;
MCBlockState bs = bsm.getBlockState();
if (bs instanceof MCShulkerBox) {
if (ma.containsKey("inventory")) {
MCShulkerBox box = (MCShulkerBox) bs;
MCInventory inv = box.getInventory();
Construct csbm = ma.get("inventory", t);
if (csbm instanceof CArray) {
CArray cinv = (CArray) csbm;
for (String key : cinv.stringKeySet()) {
try {
int index = Integer.parseInt(key);
if (index < 0 || index >= inv.getSize()) {
ConfigRuntimeException.DoWarning("Out of range value (" + index + ") found" + " in ShulkerBox inventory array, so ignoring.");
}
MCItemStack is = ObjectGenerator.GetGenerator().item(cinv.get(key, t), t);
inv.setItem(index, is);
} catch (NumberFormatException ex) {
ConfigRuntimeException.DoWarning("Expecting integer value for key in ShulkerBox" + " inventory array, but \"" + key + "\" was found. Ignoring.");
}
}
bsm.setBlockState(bs);
} else if (!(csbm instanceof CNull)) {
throw new CREFormatException("ShulkerBox inventory expected to be an array or null.", t);
}
}
} else if (bs instanceof MCBanner) {
MCBanner banner = (MCBanner) bs;
if (ma.containsKey("basecolor")) {
banner.setBaseColor(MCDyeColor.valueOf(ma.get("basecolor", t).val().toUpperCase()));
}
if (ma.containsKey("patterns")) {
CArray array = ArgumentValidation.getArray(ma.get("patterns", t), t);
for (String key : array.stringKeySet()) {
CArray pattern = ArgumentValidation.getArray(array.get(key, t), t);
MCPatternShape shape = MCPatternShape.valueOf(pattern.get("shape", t).val().toUpperCase());
MCDyeColor color = MCDyeColor.valueOf(pattern.get("color", t).val().toUpperCase());
banner.addPattern(StaticLayer.GetConvertor().GetPattern(color, shape));
}
}
banner.update();
bsm.setBlockState(banner);
} else if (bs instanceof MCCreatureSpawner) {
if (ma.containsKey("spawntype")) {
MCCreatureSpawner mccs = (MCCreatureSpawner) bs;
MCEntityType type = MCEntityType.valueOf(ma.get("spawntype", t).val().toUpperCase());
mccs.setSpawnedType(type);
bsm.setBlockState(bs);
}
}
} else if (meta instanceof MCFireworkEffectMeta) {
MCFireworkEffectMeta femeta = (MCFireworkEffectMeta) meta;
if (ma.containsKey("effect")) {
Construct cfem = ma.get("effect", t);
if (cfem instanceof CArray) {
femeta.setEffect(fireworkEffect((CArray) cfem, t));
} else if (!(cfem instanceof CNull)) {
throw new CREFormatException("FireworkCharge effect was expected to be an array or null.", t);
}
}
} else if (meta instanceof MCFireworkMeta) {
MCFireworkMeta fmeta = (MCFireworkMeta) meta;
if (ma.containsKey("firework")) {
Construct construct = ma.get("firework", t);
if (construct instanceof CArray) {
CArray firework = (CArray) construct;
if (firework.containsKey("strength")) {
fmeta.setStrength(Static.getInt32(firework.get("strength", t), t));
}
if (firework.containsKey("effects")) {
// New style (supports multiple effects)
Construct effects = firework.get("effects", t);
if (effects instanceof CArray) {
for (Construct effect : ((CArray) effects).asList()) {
if (effect instanceof CArray) {
fmeta.addEffect(fireworkEffect((CArray) effect, t));
} else {
throw new CREFormatException("Firework effect was expected to be an array.", t);
}
}
} else {
throw new CREFormatException("Firework effects was expected to be an array.", t);
}
} else {
// Old style (supports only one effect)
fmeta.addEffect(fireworkEffect(firework, t));
}
} else {
throw new CREFormatException("Firework was expected to be an array.", t);
}
}
} else if (meta instanceof MCLeatherArmorMeta) {
if (ma.containsKey("color")) {
Construct ci = ma.get("color", t);
if (ci instanceof CNull) {
// nothing
} else if (ci instanceof CArray) {
((MCLeatherArmorMeta) meta).setColor(color((CArray) ci, t));
} else {
throw new CREFormatException("Color was expected to be an array.", t);
}
}
} else if (meta instanceof MCBookMeta) {
if (ma.containsKey("title")) {
Construct title = ma.get("title", t);
if (!(title instanceof CNull)) {
((MCBookMeta) meta).setTitle(title.val());
}
}
if (ma.containsKey("author")) {
Construct author = ma.get("author", t);
if (!(author instanceof CNull)) {
((MCBookMeta) meta).setAuthor(author.val());
}
}
if (ma.containsKey("pages")) {
Construct pages = ma.get("pages", t);
if (pages instanceof CNull) {
// nothing
} else if (pages instanceof CArray) {
CArray pa = (CArray) pages;
List<String> pl = new ArrayList<>();
for (int j = 0; j < pa.size(); j++) {
pl.add(pa.get(j, t).val());
}
((MCBookMeta) meta).setPages(pl);
} else {
throw new CREFormatException("Pages field was expected to be an array.", t);
}
}
} else if (meta instanceof MCSkullMeta) {
if (ma.containsKey("owneruuid")) {
Construct id = ma.get("owneruuid", t);
if (!(id instanceof CNull)) {
((MCSkullMeta) meta).setOwningPlayer(Static.getServer().getOfflinePlayer(Static.GetUUID(id, t)));
}
} else if (ma.containsKey("owner")) {
Construct owner = ma.get("owner", t);
if (!(owner instanceof CNull)) {
((MCSkullMeta) meta).setOwner(owner.val());
}
}
} else if (meta instanceof MCEnchantmentStorageMeta) {
if (ma.containsKey("stored")) {
Construct stored = ma.get("stored", t);
if (stored instanceof CNull) {
// Still doing nothing
} else if (stored instanceof CArray) {
for (Map.Entry<MCEnchantment, Integer> ench : enchants((CArray) stored, t).entrySet()) {
((MCEnchantmentStorageMeta) meta).addStoredEnchant(ench.getKey(), ench.getValue(), true);
}
} else {
throw new CREFormatException("Stored field was expected to be an array of Enchantment arrays", t);
}
}
} else if (meta instanceof MCPotionMeta) {
if (ma.containsKey("potions")) {
Construct effects = ma.get("potions", t);
if (effects instanceof CArray) {
for (MCLivingEntity.MCEffect e : potions((CArray) effects, t)) {
((MCPotionMeta) meta).addCustomEffect(e.getPotionID(), e.getStrength(), e.getTicksRemaining(), e.isAmbient(), true, t);
}
} else {
throw new CREFormatException("Effects was expected to be an array of potion arrays.", t);
}
}
if (Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_9)) {
if (ma.containsKey("base")) {
Construct potiondata = ma.get("base", t);
if (potiondata instanceof CArray) {
CArray pd = (CArray) potiondata;
((MCPotionMeta) meta).setBasePotionData(potionData((CArray) potiondata, t));
}
}
} else if (ma.containsKey("main")) {
((MCPotionMeta) meta).setMainEffect(Static.getInt32(ma.get("main", t), t));
}
} else if (meta instanceof MCBannerMeta) {
if (ma.containsKey("basecolor")) {
((MCBannerMeta) meta).setBaseColor(MCDyeColor.valueOf(ma.get("basecolor", t).val().toUpperCase()));
}
if (ma.containsKey("patterns")) {
CArray array = ArgumentValidation.getArray(ma.get("patterns", t), t);
for (String key : array.stringKeySet()) {
CArray pattern = ArgumentValidation.getArray(array.get(key, t), t);
MCPatternShape shape = MCPatternShape.valueOf(pattern.get("shape", t).val().toUpperCase());
MCDyeColor color = MCDyeColor.valueOf(pattern.get("color", t).val().toUpperCase());
((MCBannerMeta) meta).addPattern(StaticLayer.GetConvertor().GetPattern(color, shape));
}
}
} else if (meta instanceof MCSpawnEggMeta) {
if (ma.containsKey("spawntype")) {
Construct spawntype = ma.get("spawntype", t);
if (spawntype instanceof CString) {
((MCSpawnEggMeta) meta).setSpawnedType(MCEntityType.valueOf(spawntype.val().toUpperCase()));
}
}
} else if (meta instanceof MCMapMeta && Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_11)) {
if (ma.containsKey("color")) {
Construct ci = ma.get("color", t);
if (ci instanceof CArray) {
((MCMapMeta) meta).setColor(color((CArray) ci, t));
} else if (!(ci instanceof CNull)) {
throw new CREFormatException("Color was expected to be an array.", t);
}
}
}
} catch (Exception ex) {
throw new CREFormatException(ex.getMessage(), t, ex);
}
} else {
throw new CREFormatException("An array was expected but recieved " + c + " instead.", t);
}
return meta;
}
use of com.laytonsmith.abstraction.MCItemStack in project CommandHelper by EngineHub.
the class ObjectGenerator method recipe.
public MCRecipe recipe(Construct c, Target t) {
if (!(c instanceof CArray)) {
throw new CRECastException("Expected array but recieved " + c.getCType().name(), t);
}
CArray recipe = (CArray) c;
String recipeKey = null;
if (recipe.containsKey("key")) {
recipeKey = recipe.get("key", t).val();
}
MCRecipeType recipeType;
try {
recipeType = MCRecipeType.valueOf(recipe.get("type", t).val());
} catch (IllegalArgumentException e) {
throw new CREFormatException("Invalid recipe type.", t);
}
MCItemStack result = item(recipe.get("result", t), t);
MCRecipe ret;
try {
ret = StaticLayer.GetNewRecipe(recipeKey, recipeType, result);
} catch (IllegalArgumentException ex) {
throw new CREFormatException(ex.getMessage(), t);
}
switch(recipeType) {
case SHAPED:
CArray shaped = Static.getArray(recipe.get("shape", t), t);
;
String[] shape = new String[(int) shaped.size()];
if (shaped.size() < 1 || shaped.size() > 3 || shaped.inAssociativeMode()) {
throw new CREFormatException("Shape array is invalid.", t);
}
int i = 0;
for (Construct row : shaped.asList()) {
if (row instanceof CString && row.val().length() >= 1 && row.val().length() <= 3) {
shape[i] = row.val();
i++;
} else {
throw new CREFormatException("Shape array is invalid.", t);
}
}
((MCShapedRecipe) ret).setShape(shape);
CArray shapedIngredients = Static.getArray(recipe.get("ingredients", t), t);
if (!shapedIngredients.inAssociativeMode()) {
throw new CREFormatException("Ingredients array is invalid.", t);
}
for (String key : shapedIngredients.stringKeySet()) {
MCItemStack is;
Construct ingredient = shapedIngredients.get(key, t);
if (ingredient instanceof CString) {
CString item = (CString) ingredient;
if (item.val().contains(":")) {
String[] split = item.val().split(":");
is = StaticLayer.GetItemStack(Integer.valueOf(split[0]), Integer.valueOf(split[1]), 1);
} else {
is = StaticLayer.GetItemStack(Integer.valueOf(item.val()), 1);
}
} else if (ingredient instanceof CInt) {
is = StaticLayer.GetItemStack(Static.getInt32(ingredient, t), 1);
} else if (ingredient instanceof CArray) {
is = item(ingredient, t);
} else if (ingredient instanceof CNull) {
is = StaticLayer.GetItemStack(0, 0);
} else {
throw new CREFormatException("Item was not found", t);
}
((MCShapedRecipe) ret).setIngredient(key.charAt(0), is);
}
return ret;
case SHAPELESS:
CArray ingredients = Static.getArray(recipe.get("ingredients", t), t);
if (ingredients.inAssociativeMode()) {
throw new CREFormatException("Ingredients array is invalid.", t);
}
for (Construct ingredient : ingredients.asList()) {
MCItemStack is;
if (ingredient instanceof CString) {
if (ingredient.val().contains(":")) {
String[] split = ingredient.val().split(":");
is = StaticLayer.GetItemStack(Integer.valueOf(split[0]), Integer.valueOf(split[1]), 1);
} else {
is = StaticLayer.GetItemStack(Integer.valueOf(ingredient.val()), 1);
}
} else if (ingredient instanceof CArray) {
is = item(ingredient, t);
} else {
throw new CREFormatException("Item was not found", t);
}
((MCShapelessRecipe) ret).addIngredient(is);
}
return ret;
case FURNACE:
CArray is = Static.getArray(recipe.get("input", t), t);
((MCFurnaceRecipe) ret).setInput(item(is, t));
return ret;
default:
throw new CREFormatException("Could not find valid recipe type.", t);
}
}
use of com.laytonsmith.abstraction.MCItemStack in project CommandHelper by EngineHub.
the class BukkitBlockListener method onBlockBreak.
@EventHandler(priority = EventPriority.LOWEST)
public void onBlockBreak(BlockBreakEvent e) {
if (ignorebreak) {
return;
}
BukkitBlockEvents.BukkitMCBlockBreakEvent bbe = new BukkitBlockEvents.BukkitMCBlockBreakEvent(e);
EventUtils.TriggerListener(Driver.BLOCK_BREAK, "block_break", bbe);
if (bbe.isModified() && !e.isCancelled()) {
e.setCancelled(true);
// If we've modified the drops, create a new event for other plugins (eg. block loggers, region protection)
BlockBreakEvent chevent = new BlockBreakEvent(e.getBlock(), e.getPlayer());
chevent.setExpToDrop(bbe.getExpToDrop());
PluginManager manager = Bukkit.getServer().getPluginManager();
ignorebreak = true;
try {
manager.callEvent(chevent);
} finally {
ignorebreak = false;
}
if (!chevent.isCancelled()) {
Block block = chevent.getBlock();
block.setType(Material.AIR);
Location loc = block.getLocation();
loc.add(0.5, 0.5, 0.5);
for (MCItemStack item : bbe.getDrops()) {
block.getWorld().dropItemNaturally(loc, (ItemStack) item.getHandle());
}
int amt = chevent.getExpToDrop();
if (amt > 0) {
ExperienceOrb exp = (ExperienceOrb) block.getWorld().spawnEntity(loc, EntityType.EXPERIENCE_ORB);
exp.setExperience(amt);
}
}
}
}
Aggregations