-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c110234
commit 249b211
Showing
12 changed files
with
170 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
export interface IItem { | ||
item: string; | ||
data: number; | ||
count?: number; | ||
nbt?: string; | ||
} |
77 changes: 77 additions & 0 deletions
77
lib/infobook/appendix/InfoBookAppendixHandlerAbstractRecipe.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import * as fs from "fs"; | ||
import {ResourceHandler} from "../../resource/ResourceHandler"; | ||
import {HtmlInfoBookSerializer, ISerializeContext} from "../../serialize/HtmlInfoBookSerializer"; | ||
import {IFileWriter} from "../IFileWriter"; | ||
import {IInfoAppendix} from "../IInfoAppendix"; | ||
import {IInfoBookAppendixHandler} from "./IInfoBookAppendixHandler"; | ||
import {join} from "path"; | ||
|
||
/** | ||
* Handles abstract recipe appendices. | ||
*/ | ||
export abstract class InfoBookAppendixHandlerAbstractRecipe<R extends IRecipe> implements IInfoBookAppendixHandler { | ||
|
||
protected readonly id: string; | ||
protected readonly resourceHandler: ResourceHandler; | ||
protected readonly registry: IRecipeRegistryIndexed<R>; | ||
|
||
constructor(id: string, resourceHandler: ResourceHandler, registriesPath: string, recipeOverrides: any) { | ||
this.id = id; | ||
this.resourceHandler = resourceHandler; | ||
this.registry = InfoBookAppendixHandlerAbstractRecipe.indexRegistry(JSON.parse(fs.readFileSync(join(registriesPath, id.replace(/:/g, '__') + '.json'), "utf8"))); | ||
if (recipeOverrides) { | ||
this.registry = { ... this.registry, ...(recipeOverrides[id] || {}) }; | ||
} | ||
} | ||
|
||
public static indexRegistry<R extends IRecipe>(registryRead: IRecipeRegistryRead<R>) : IRecipeRegistryIndexed<R> { | ||
const index: IRecipeRegistryIndexed<R> = {}; | ||
for (const recipe of registryRead.recipes) { | ||
index[recipe.id] = recipe; | ||
} | ||
return index; | ||
} | ||
|
||
public createAppendix(data: any): IInfoAppendix { | ||
const recipeId = data._; | ||
let recipes: R[] = []; | ||
if (recipeId.includes('*')) { | ||
const recipeRegex = new RegExp(recipeId); | ||
for (const [k, v] of Object.entries(this.registry)) { | ||
if (recipeRegex.exec(k)) { | ||
recipes.push(v); | ||
} | ||
} | ||
} else if (this.registry[recipeId]) { | ||
recipes = [ this.registry[recipeId] ]; | ||
} | ||
if (recipes.length === 0) { | ||
throw new Error(`Could not find ${this.id} recipe for ${recipeId}`); | ||
} | ||
|
||
return { | ||
getName: (context) => this.resourceHandler.getTranslation(this.getRecipeNameUnlocalized(), context.language), | ||
toHtml: (context: ISerializeContext, fileWriter: IFileWriter, serializer: HtmlInfoBookSerializer) => { | ||
return recipes.map((recipe) => this.serializeRecipe(recipe, context, fileWriter, serializer)).join('<hr />'); | ||
}, | ||
}; | ||
} | ||
|
||
protected abstract getRecipeNameUnlocalized(): string; | ||
|
||
protected abstract serializeRecipe(recipe: R, context: ISerializeContext, | ||
fileWriter: IFileWriter, serializer: HtmlInfoBookSerializer): string; | ||
|
||
} | ||
|
||
export interface IRecipeRegistryRead<R extends IRecipe> { | ||
recipes: [R]; | ||
} | ||
|
||
export interface IRecipeRegistryIndexed<R extends IRecipe> { | ||
[id: string]: R; | ||
} | ||
|
||
export interface IRecipe { | ||
id: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 0 additions & 63 deletions
63
lib/infobook/appendix/InfoBookAppendixHandlerFurnaceRecipe.ts
This file was deleted.
Oops, something went wrong.
44 changes: 44 additions & 0 deletions
44
lib/infobook/appendix/InfoBookAppendixHandlerSmeltingRecipe.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import {compileFile as compilePug, compileTemplate} from "pug"; | ||
import {ResourceHandler} from "../../resource/ResourceHandler"; | ||
import {HtmlInfoBookSerializer, ISerializeContext} from "../../serialize/HtmlInfoBookSerializer"; | ||
import {IFileWriter} from "../IFileWriter"; | ||
import {IItem} from "../IItem"; | ||
import { | ||
InfoBookAppendixHandlerAbstractRecipe, | ||
IRecipe, | ||
} from "./InfoBookAppendixHandlerAbstractRecipe"; | ||
|
||
/** | ||
* Handles furnace recipe appendices. | ||
*/ | ||
export class InfoBookAppendixHandlerSmeltingRecipe extends InfoBookAppendixHandlerAbstractRecipe<IRecipeSmelting> { | ||
|
||
private readonly templateFurnaceRecipe: compileTemplate; | ||
|
||
constructor(resourceHandler: ResourceHandler, registriesPath: string, recipeOverrides: any) { | ||
super('minecraft:smelting', resourceHandler, registriesPath, recipeOverrides); | ||
this.templateFurnaceRecipe = compilePug(__dirname + '/../../../template/appendix/furnace_recipe.pug'); | ||
} | ||
|
||
protected getRecipeNameUnlocalized(): string { | ||
return 'block.minecraft.furnace'; | ||
} | ||
|
||
protected serializeRecipe(recipe: IRecipeSmelting, context: ISerializeContext, fileWriter: IFileWriter, serializer: HtmlInfoBookSerializer): string { | ||
const input = recipe.input.map((item) => serializer.createItemDisplay(this.resourceHandler, context, | ||
fileWriter, item, true)); | ||
const output = serializer.createItemDisplay(this.resourceHandler, context, | ||
fileWriter, recipe.output, true); | ||
|
||
const appendixIcon = serializer.createItemDisplay(this.resourceHandler, context, | ||
fileWriter, { item: 'minecraft:furnace' }, false); | ||
|
||
return this.templateFurnaceRecipe({ input, output, appendixIcon }); | ||
} | ||
|
||
} | ||
|
||
export interface IRecipeSmelting extends IRecipe { | ||
input: IItem[]; | ||
output: IItem; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.