use of am2.api.events.SpellRecipeItemsEvent in project ArsMagica2 by Mithion.
the class SpellRecipeManager method RegisterRecipe.
public void RegisterRecipe(ISpellPart part) {
ArrayList<Object> recipeItems = new ArrayList<Object>();
Object[] recipe = part.getRecipeItems();
SpellRecipeItemsEvent event = new SpellRecipeItemsEvent(SkillManager.instance.getSkillName(part), SkillManager.instance.getShiftedPartID(part), recipe);
MinecraftForge.EVENT_BUS.post(event);
recipe = event.recipeItems;
if (recipe == null) {
LogHelper.info("Component %s has been registered with no craftable recipe - is this intentional? If so, return a 0-length array for recipe.", SkillManager.instance.getDisplayName(part));
return;
}
if (recipe.length == 0) {
return;
}
for (int i = 0; i < recipe.length; ++i) {
Object o = recipe[i];
if (o instanceof Item) {
recipeItems.add(new ItemStack((Item) o));
} else if (o instanceof Block) {
recipeItems.add(new ItemStack((Block) o));
} else if (o instanceof String) {
if (((String) o).toLowerCase().startsWith("e:")) {
if (i == recipe.length - 1 || !(recipe[i + 1] instanceof Integer)) {
LogHelper.warn("Error registering recipe. Power must be declared in Integer pairs (type flags, quantity)).");
return;
}
int[] ids = ParseEssenceIDs((String) o);
int flag = 0;
for (int f : ids) {
flag |= f;
}
int quantity = (Integer) recipe[++i];
ItemStack stack = new ItemStack(ItemsCommonProxy.essence, quantity, ItemEssence.META_MAX + flag);
recipeItems.add(stack);
} else {
recipeItems.add(o);
}
} else {
recipeItems.add(o);
}
}
recipes.put(recipeItems, part);
}
use of am2.api.events.SpellRecipeItemsEvent in project ArsMagica2 by Mithion.
the class TileEntityInscriptionTable method writeRecipeAndDataToBook.
public ItemStack writeRecipeAndDataToBook(ItemStack bookstack, EntityPlayer player, String title) {
if (bookstack.getItem() == Items.written_book && this.currentRecipe != null) {
if (!currentRecipeIsValid().valid)
return bookstack;
if (!bookstack.hasTagCompound())
bookstack.setTagCompound(new NBTTagCompound());
else if (//don't overwrite a completed spell
bookstack.getTagCompound().getBoolean("spellFinalized"))
return bookstack;
LinkedHashMap<String, Integer> materialsList = new LinkedHashMap<String, Integer>();
materialsList.put(ItemsCommonProxy.rune.getItemStackDisplayName(new ItemStack(ItemsCommonProxy.rune, 1, ItemsCommonProxy.rune.META_BLANK)), 1);
ArrayList<ItemStack> componentRecipeList = new ArrayList<ItemStack>();
int count = 0;
ArrayList<ISpellPart> allRecipeItems = new ArrayList<ISpellPart>();
for (ArrayList<ISpellPart> shapeGroup : shapeGroups) {
if (shapeGroup == null || shapeGroup.size() == 0)
continue;
allRecipeItems.addAll(shapeGroup);
}
allRecipeItems.addAll(currentRecipe);
for (ISpellPart part : allRecipeItems) {
if (part == null) {
LogHelper.error("Unable to write recipe to book. Recipe part is null!");
return bookstack;
}
Object[] recipeItems = part.getRecipeItems();
SpellRecipeItemsEvent event = new SpellRecipeItemsEvent(SkillManager.instance.getSkillName(part), SkillManager.instance.getShiftedPartID(part), recipeItems);
MinecraftForge.EVENT_BUS.post(event);
recipeItems = event.recipeItems;
if (recipeItems == null) {
LogHelper.error("Unable to write recipe to book. Recipe items are null for part %d!", part.getID());
return bookstack;
}
for (int i = 0; i < recipeItems.length; ++i) {
Object o = recipeItems[i];
String materialkey = "";
int qty = 1;
ItemStack recipeStack = null;
if (o instanceof ItemStack) {
materialkey = ((ItemStack) o).getDisplayName();
recipeStack = (ItemStack) o;
} else if (o instanceof Item) {
recipeStack = new ItemStack((Item) o);
materialkey = ((Item) o).getItemStackDisplayName(new ItemStack((Item) o));
} else if (o instanceof Block) {
recipeStack = new ItemStack((Block) o);
materialkey = ((Block) o).getLocalizedName();
} else if (o instanceof String) {
if (((String) o).startsWith("P:")) {
String s = ((String) o).substring(2);
int pfx = SpellRecipeManager.parsePotionMeta(s);
recipeStack = new ItemStack(Items.potionitem, 1, pfx);
materialkey = recipeStack.getDisplayName();
} else if (((String) o).startsWith("E:")) {
int[] ids = SpellRecipeManager.ParseEssenceIDs((String) o);
materialkey = "Essence (";
for (int powerID : ids) {
PowerTypes type = PowerTypes.getByID(powerID);
materialkey += type.name() + "/";
}
if (materialkey.equals("Essence (")) {
++i;
continue;
}
o = recipeItems[++i];
if (materialkey.startsWith("Essence (")) {
materialkey = materialkey.substring(0, materialkey.lastIndexOf("/")) + ")";
qty = (Integer) o;
int flag = 0;
for (int f : ids) {
flag |= f;
}
recipeStack = new ItemStack(ItemsCommonProxy.essence, qty, ItemsCommonProxy.essence.META_MAX + flag);
}
} else {
ArrayList<ItemStack> ores = OreDictionary.getOres((String) o);
recipeStack = ores.size() > 0 ? ores.get(1) : null;
materialkey = (String) o;
}
}
if (materialsList.containsKey(materialkey)) {
int old = materialsList.get(materialkey);
old += qty;
materialsList.put(materialkey, old);
} else {
materialsList.put(materialkey, qty);
}
if (recipeStack != null)
componentRecipeList.add(recipeStack);
}
}
materialsList.put(ItemsCommonProxy.spellParchment.getItemStackDisplayName(new ItemStack(ItemsCommonProxy.spellParchment)), 1);
StringBuilder sb = new StringBuilder();
int sgCount = 0;
int[][] shapeGroupCombos = new int[shapeGroups.size()][];
for (ArrayList<ISpellPart> shapeGroup : shapeGroups) {
sb.append("Shape Group " + ++sgCount + "\n\n");
Iterator<ISpellPart> it = shapeGroup.iterator();
shapeGroupCombos[sgCount - 1] = SpellPartListToStringBuilder(it, sb, " -");
sb.append("\n");
}
sb.append("Combination:\n\n");
Iterator<ISpellPart> it = currentRecipe.iterator();
ArrayList<Integer> outputCombo = new ArrayList<Integer>();
int[] outputData = SpellPartListToStringBuilder(it, sb, null);
ArrayList<NBTTagString> pages = Story.splitStoryPartIntoPages(sb.toString());
sb = new StringBuilder();
sb.append("\n\nMaterials List:\n\n");
for (String s : materialsList.keySet()) {
sb.append(materialsList.get(s) + " x " + s + "\n");
}
pages.addAll(Story.splitStoryPartIntoPages(sb.toString()));
sb = new StringBuilder();
sb.append("Affinity Breakdown:\n\n");
it = currentRecipe.iterator();
HashMap<Affinity, Integer> affinityData = new HashMap<Affinity, Integer>();
int cpCount = 0;
while (it.hasNext()) {
ISpellPart part = it.next();
if (part instanceof ISpellComponent) {
EnumSet<Affinity> aff = ((ISpellComponent) part).getAffinity();
for (Affinity affinity : aff) {
int qty = 1;
if (affinityData.containsKey(affinity)) {
qty = 1 + affinityData.get(affinity);
}
affinityData.put(affinity, qty);
}
cpCount++;
}
}
ValueComparator vc = new ValueComparator(affinityData);
TreeMap<Affinity, Integer> sorted = new TreeMap<Affinity, Integer>(vc);
sorted.putAll(affinityData);
for (Affinity aff : sorted.keySet()) {
float pct = (float) sorted.get(aff) / (float) cpCount * 100f;
sb.append(String.format("%s: %.2f%%", aff.toString(), pct));
sb.append("\n");
}
pages.addAll(Story.splitStoryPartIntoPages(sb.toString()));
Story.WritePartToNBT(bookstack.stackTagCompound, pages);
bookstack = Story.finalizeStory(bookstack, title, player.getCommandSenderName());
int[] recipeData = new int[componentRecipeList.size() * 3];
int idx = 0;
for (ItemStack stack : componentRecipeList) {
recipeData[idx++] = Item.getIdFromItem(stack.getItem());
recipeData[idx++] = stack.stackSize;
recipeData[idx++] = stack.getItemDamage();
}
bookstack.stackTagCompound.setIntArray("spell_combo", recipeData);
bookstack.stackTagCompound.setIntArray("output_combo", outputData);
bookstack.stackTagCompound.setInteger("numShapeGroups", shapeGroupCombos.length);
int index = 0;
for (int[] sgArray : shapeGroupCombos) {
bookstack.stackTagCompound.setIntArray("shapeGroupCombo_" + index++, sgArray);
}
bookstack.stackTagCompound.setString("spell_mod_version", AMCore.instance.getVersion());
if (currentSpellName.equals(""))
currentSpellName = "Spell Recipe";
bookstack.setStackDisplayName(currentSpellName);
this.currentRecipe.clear();
for (ArrayList<ISpellPart> list : shapeGroups) list.clear();
currentSpellName = "";
bookstack.stackTagCompound.setBoolean("spellFinalized", true);
worldObj.playSound(xCoord, yCoord, zCoord, "arsmagica2:misc.inscriptiontable.takebook", 1.0f, 1.0f, true);
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}
return bookstack;
}
use of am2.api.events.SpellRecipeItemsEvent in project ArsMagica2 by Mithion.
the class GuiArcaneCompendium method getAndAnalyzeRecipe.
private void getAndAnalyzeRecipe() {
if (renderStack == null)
return;
if (renderStack.getItem() == ItemsCommonProxy.essence || renderStack.getItem() == ItemsCommonProxy.deficitCrystal) {
RecipeArsMagica essenceRecipe = RecipesEssenceRefiner.essenceRefinement().recipeFor(renderStack);
if (essenceRecipe != null) {
craftingComponents = essenceRecipe.getRecipeItems();
recipeHeight = 2;
} else {
craftingComponents = null;
}
} else if (renderStack.getItem() instanceof ItemSpellPart) {
ISkillTreeEntry part = SkillManager.instance.getSkill(this.entryName);
if (part == null)
return;
ArrayList<Object> recipe = new ArrayList<Object>();
if (part instanceof ISpellPart) {
Object[] recipeItems = ((ISpellPart) part).getRecipeItems();
SpellRecipeItemsEvent event = new SpellRecipeItemsEvent(SkillManager.instance.getSkillName(part), SkillManager.instance.getShiftedPartID(part), recipeItems);
MinecraftForge.EVENT_BUS.post(event);
recipeItems = event.recipeItems;
if (recipeItems != null) {
for (int i = 0; i < recipeItems.length; ++i) {
Object o = recipeItems[i];
boolean matches = false;
if (o instanceof ItemStack) {
recipe.add(o);
} else if (o instanceof Item) {
recipe.add(new ItemStack((Item) o));
} else if (o instanceof Block) {
recipe.add(new ItemStack((Block) o));
} else if (o instanceof String) {
if (((String) o).startsWith("P:")) {
//potion
String s = ((String) o).substring(2);
int pfx = SpellRecipeManager.parsePotionMeta(s);
recipe.add(new ItemStack(Items.potionitem, 1, pfx));
} else if (((String) o).startsWith("E:")) {
//essence
String s = ((String) o);
try {
int[] types = SpellRecipeManager.ParseEssenceIDs(s);
int type = 0;
for (int t : types) type |= t;
int amount = (Integer) recipeItems[++i];
recipe.add(new ItemStack(ItemsCommonProxy.essence, amount, ItemsCommonProxy.essence.META_MAX + type));
} catch (Throwable t) {
continue;
}
} else {
recipe.add(OreDictionary.getOres((String) o));
}
}
}
}
}
craftingComponents = recipe.toArray();
} else {
IRecipe recipe = RecipeUtilities.getRecipeFor(renderStack);
if (recipe != null) {
renderStack = recipe.getRecipeOutput();
if (recipe instanceof ShapedRecipes) {
recipeWidth = ((ShapedRecipes) recipe).recipeWidth;
recipeHeight = ((ShapedRecipes) recipe).recipeHeight;
craftingComponents = ((ShapedRecipes) recipe).recipeItems;
} else if (recipe instanceof ShapedOreRecipe) {
recipeWidth = ReflectionHelper.getPrivateValue(ShapedOreRecipe.class, ((ShapedOreRecipe) recipe), "width");
recipeHeight = ReflectionHelper.getPrivateValue(ShapedOreRecipe.class, ((ShapedOreRecipe) recipe), "height");
craftingComponents = ((ShapedOreRecipe) recipe).getInput();
} else if (recipe instanceof ShapelessRecipes) {
recipeWidth = ((ShapelessRecipes) recipe).getRecipeSize();
recipeHeight = -1;
craftingComponents = ((ShapelessRecipes) recipe).recipeItems.toArray();
} else if (recipe instanceof ShapelessOreRecipe) {
recipeWidth = ((ShapelessOreRecipe) recipe).getRecipeSize();
recipeHeight = -1;
craftingComponents = ((ShapelessOreRecipe) recipe).getInput().toArray();
} else {
craftingComponents = null;
}
} else {
craftingComponents = null;
}
}
}
Aggregations