Skip to content

Commit

Permalink
Add support for multiple crafting recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Jul 29, 2019
1 parent dd22d9b commit 232f4c2
Showing 1 changed file with 49 additions and 38 deletions.
87 changes: 49 additions & 38 deletions lib/infobook/appendix/InfoBookAppendixHandlerCraftingRecipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,69 +43,80 @@ export class InfoBookAppendixHandlerCraftingRecipe implements IInfoBookAppendixH
}

public createAppendix(data: any): IInfoAppendix {
const index = data.$.index || 0;
let index = data.$.index || 0;
// const meta = data.$.meta || 0;
// const count = data.$.count || 1;
const outputName = data._;
let recipes;
let recipes: IRecipe[];
if (data.$.predefined) {
const predefined = this.recipePredefineds[outputName];
if (!predefined) {
throw new Error(`Could not find any predefined recipe for ${outputName}`);
}
recipes = [predefined];
} else {
recipes = this.registry[outputName] || this.registryTagged['crafting_recipe:' + outputName];
recipes = this.registry[outputName];
if (!recipes) {
recipes = this.registryTagged['crafting_recipe:' + outputName];
index = -1;
}
}
if (!recipes) {
throw new Error(`Could not find any recipe for ${outputName}`);
}
if (index >= recipes.length) {
throw new Error(`Could not find recipe ${index} for ${outputName} that only has ${recipes.length} recipes.`);
if (index > -1) {
if (index >= recipes.length) {
throw new Error(`Could not find recipe ${index} for ${outputName} that only has ${recipes.length} recipes.`);
}
recipes = [recipes[index]];
}
const recipe = recipes[index];

return {
getName: (context) => this.resourceHandler.getTranslation('tile.workbench.name', context.language),
toHtml: (context: ISerializeContext, fileWriter: IFileWriter, serializer: HtmlInfoBookSerializer) => {
// Prepare input array
const inputs = "|".repeat(9).split("|").map(() => []);
return recipes.map((recipe) => this.serializeRecipe(recipe, context, fileWriter, serializer)).join('<hr />');
},
};
}

// Define custom dimensions for shapeless recipes
if (!recipe.width || !recipe.height) {
recipe.width = recipe.height = Math.sqrt(recipe.input.length);
}
protected serializeRecipe(recipe: IRecipe, context: ISerializeContext,
fileWriter: IFileWriter, serializer: HtmlInfoBookSerializer) {
// Prepare input array
const inputs = "|".repeat(9).split("|").map(() => []);

// Format items in grid
for (let x = 0; x < 3; x++) {
for (let y = 0; y < 3; y++) {
let items: IItem[];
if (x < recipe.width && y < recipe.height) {
const inputIndex = y * recipe.width + x;
items = recipe.input[inputIndex] || [];
} else {
items = [];
}
if (!items.length) {
items.push({ item: 'minecraft:air', data: 0 });
}
const outputIndex = y * 3 + x;
for (const item of items) {
inputs[outputIndex].push(serializer.createItemDisplay(this.resourceHandler, context,
fileWriter, item, true));
}
}
// Define custom dimensions for shapeless recipes
if (!recipe.width || !recipe.height) {
recipe.width = recipe.height = Math.sqrt(recipe.input.length);
}

// Format items in grid
for (let x = 0; x < 3; x++) {
for (let y = 0; y < 3; y++) {
let items: IItem[];
if (x < recipe.width && y < recipe.height) {
const inputIndex = y * recipe.width + x;
items = recipe.input[inputIndex] || [];
} else {
items = [];
}
if (!items.length) {
items.push({ item: 'minecraft:air', data: 0 });
}
const outputIndex = y * 3 + x;
for (const item of items) {
inputs[outputIndex].push(serializer.createItemDisplay(this.resourceHandler, context,
fileWriter, item, true));
}
}
}

const output = serializer.createItemDisplay(this.resourceHandler, context,
fileWriter, recipe.output, true);
const output = serializer.createItemDisplay(this.resourceHandler, context,
fileWriter, recipe.output, true);

const appendixIcon = serializer.createItemDisplay(this.resourceHandler, context,
fileWriter, { item: 'minecraft:crafting_table', data: 0 }, false);
const appendixIcon = serializer.createItemDisplay(this.resourceHandler, context,
fileWriter, { item: 'minecraft:crafting_table', data: 0 }, false);

return this.templateCraftingRecipe({ inputs, output, appendixIcon });
},
};
return this.templateCraftingRecipe({ inputs, output, appendixIcon });
}

}
Expand Down

0 comments on commit 232f4c2

Please sign in to comment.