Skip to content

Commit

Permalink
Update to MC 1.16
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Feb 2, 2021
1 parent c110234 commit 249b211
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 205 deletions.
11 changes: 5 additions & 6 deletions bin/generate-infobook-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
InfoBookAppendixHandlerAdvancementRewards,
} from "../lib/infobook/appendix/InfoBookAppendixHandlerAdvancementRewards";
import {InfoBookAppendixHandlerCraftingRecipe} from "../lib/infobook/appendix/InfoBookAppendixHandlerCraftingRecipe";
import {InfoBookAppendixHandlerFurnaceRecipe} from "../lib/infobook/appendix/InfoBookAppendixHandlerFurnaceRecipe";
import {InfoBookAppendixHandlerSmeltingRecipe} from "../lib/infobook/appendix/InfoBookAppendixHandlerSmeltingRecipe";
import {InfoBookAppendixHandlerImage} from "../lib/infobook/appendix/InfoBookAppendixHandlerImage";
import {InfoBookAppendixHandlerKeybinding} from "../lib/infobook/appendix/InfoBookAppendixHandlerKeybinding";
import {IInfoBook} from "../lib/infobook/IInfoBook";
Expand Down Expand Up @@ -67,11 +67,10 @@ async function create() {
const infoBookInitializer = new InfoBookInitializer(config);
infoBookInitializer.registerAppendixHandler('advancement_rewards',
new InfoBookAppendixHandlerAdvancementRewards(resourceLoader.getResourceHandler()));
infoBookInitializer.registerAppendixHandler('crafting_recipe',
new InfoBookAppendixHandlerCraftingRecipe(resourceLoader.getResourceHandler(),
'registries', config.recipeOverrides, config.recipePredefineds));
infoBookInitializer.registerAppendixHandler('furnace_recipe',
new InfoBookAppendixHandlerFurnaceRecipe(resourceLoader.getResourceHandler(), 'registries'));
infoBookInitializer.registerAppendixHandler('minecraft:crafting',
new InfoBookAppendixHandlerCraftingRecipe(resourceLoader.getResourceHandler(), 'registries', config.recipeOverrides));
infoBookInitializer.registerAppendixHandler('minecraft:smelting',
new InfoBookAppendixHandlerSmeltingRecipe(resourceLoader.getResourceHandler(), 'registries', config.recipeOverrides));
infoBookInitializer.registerAppendixHandler('image',
new InfoBookAppendixHandlerImage(resourceLoader.getResourceHandler()));
infoBookInitializer.registerAppendixHandler('keybinding',
Expand Down
3 changes: 2 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
export * from "./lib/infobook/appendix/IInfoBookAppendixHandler";
export * from "./lib/infobook/appendix/InfoBookAppendixAd";
export * from "./lib/infobook/appendix/InfoBookAppendixHandlerAbstractRecipe";
export * from "./lib/infobook/appendix/InfoBookAppendixHandlerAdvancementRewards";
export * from "./lib/infobook/appendix/InfoBookAppendixHandlerCraftingRecipe";
export * from "./lib/infobook/appendix/InfoBookAppendixHandlerFurnaceRecipe";
export * from "./lib/infobook/appendix/InfoBookAppendixHandlerSmeltingRecipe";
export * from "./lib/infobook/appendix/InfoBookAppendixHandlerImage";
export * from "./lib/infobook/appendix/InfoBookAppendixHandlerKeybinding";
export * from "./lib/infobook/appendix/InfoBookAppendixTagIndex";
Expand Down
1 change: 0 additions & 1 deletion lib/infobook/IItem.ts
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 lib/infobook/appendix/InfoBookAppendixHandlerAbstractRecipe.ts
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ export class InfoBookAppendixHandlerAdvancementRewards implements IInfoBookAppen
throw new Error(`Unknown achievement reward type '${rewardTag.$.type}'`);
}
const count = rewardTag.$.amount;
const meta = rewardTag.$.meta || 0;
rewardsData.push({ item: rewardTag._, data: meta, count });
rewardsData.push({ item: rewardTag._, count });
}

return {
Expand Down
96 changes: 16 additions & 80 deletions lib/infobook/appendix/InfoBookAppendixHandlerCraftingRecipe.ts
Original file line number Diff line number Diff line change
@@ -1,85 +1,30 @@
import * as fs from "fs";
import {join} from "path";
import {compileFile as compilePug, compileTemplate} from "pug";
import {ResourceHandler} from "../../resource/ResourceHandler";
import {HtmlInfoBookSerializer, ISerializeContext} from "../../serialize/HtmlInfoBookSerializer";
import {IFileWriter} from "../IFileWriter";
import {IInfoAppendix} from "../IInfoAppendix";
import {IItem} from "../IItem";
import {IInfoBookAppendixHandler} from "./IInfoBookAppendixHandler";
import {
InfoBookAppendixHandlerAbstractRecipe,
IRecipe,
} from "./InfoBookAppendixHandlerAbstractRecipe";

/**
* Handles crafting recipe appendices.
*/
export class InfoBookAppendixHandlerCraftingRecipe implements IInfoBookAppendixHandler {
export class InfoBookAppendixHandlerCraftingRecipe extends InfoBookAppendixHandlerAbstractRecipe<IRecipeCrafting> {

private readonly resourceHandler: ResourceHandler;
private readonly registry: IRecipeRegistry;
private readonly registryTagged: {[tag: string]: IRecipe[]};
private readonly recipePredefineds: IRecipePredefineds;
private readonly templateCraftingRecipe: compileTemplate;

constructor(resourceHandler: ResourceHandler, registriesPath: string, recipeOverrides: any,
recipePredefineds: IRecipePredefineds) {
this.resourceHandler = resourceHandler;
this.registry = JSON.parse(fs.readFileSync(join(registriesPath, 'crafting_recipe.json'), "utf8"));
if (recipeOverrides) {
this.registry = { ... this.registry, ...recipeOverrides };
}
this.recipePredefineds = recipePredefineds;
this.registryTagged = {};
for (const recipeId in this.registry) {
for (const recipe of this.registry[recipeId]) {
for (const tag of recipe.tags) {
let recipes = this.registryTagged[tag];
if (!recipes) {
recipes = this.registryTagged[tag] = [];
}
recipes.push(recipe);
}
}
}
constructor(resourceHandler: ResourceHandler, registriesPath: string, recipeOverrides: any) {
super('minecraft:crafting', resourceHandler, registriesPath, recipeOverrides);
this.templateCraftingRecipe = compilePug(__dirname + '/../../../template/appendix/crafting_recipe.pug');
}

public createAppendix(data: any): IInfoAppendix {
let index = data.$.index || 0;
// const meta = data.$.meta || 0;
// const count = data.$.count || 1;
const outputName = data._;
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];
if (!recipes) {
recipes = this.registryTagged['crafting_recipe:' + outputName];
index = -1;
}
}
if (!recipes) {
throw new Error(`Could not find any recipe for ${outputName}`);
}
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]];
}

return {
getName: (context) => this.resourceHandler.getTranslation('tile.workbench.name', context.language),
toHtml: (context: ISerializeContext, fileWriter: IFileWriter, serializer: HtmlInfoBookSerializer) => {
return recipes.map((recipe) => this.serializeRecipe(recipe, context, fileWriter, serializer)).join('<hr />');
},
};
protected getRecipeNameUnlocalized(): string {
return 'block.minecraft.crafting_table';
}

protected serializeRecipe(recipe: IRecipe, context: ISerializeContext,
protected serializeRecipe(recipe: IRecipeCrafting, context: ISerializeContext,
fileWriter: IFileWriter, serializer: HtmlInfoBookSerializer) {
// Prepare input array
const inputs = "|".repeat(9).split("|").map(() => []);
Expand All @@ -100,7 +45,7 @@ export class InfoBookAppendixHandlerCraftingRecipe implements IInfoBookAppendixH
items = [];
}
if (!items.length) {
items.push({ item: 'minecraft:air', data: 0 });
items.push({ item: 'minecraft:air' });
}
const outputIndex = y * 3 + x;
for (const item of items) {
Expand All @@ -114,25 +59,16 @@ export class InfoBookAppendixHandlerCraftingRecipe implements IInfoBookAppendixH
fileWriter, recipe.output, true);

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

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

}

export interface IRecipeRegistry {
[id: string]: [IRecipe];
}

export interface IRecipe {
export interface IRecipeCrafting extends IRecipe {
id: string;
input: IItem[][];
output: IItem;
width: number;
height: number;
tags: string[];
}

export interface IRecipePredefineds {
[id: string]: IRecipe;
width?: number;
height?: number;
}
63 changes: 0 additions & 63 deletions lib/infobook/appendix/InfoBookAppendixHandlerFurnaceRecipe.ts

This file was deleted.

44 changes: 44 additions & 0 deletions lib/infobook/appendix/InfoBookAppendixHandlerSmeltingRecipe.ts
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;
}
4 changes: 2 additions & 2 deletions lib/infobook/appendix/InfoBookAppendixTagIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ export class InfoBookAppendixTagIndex implements IInfoAppendix {

// First try localizing as item, and if that fails, as fluid
let icon: string;
const item = { item: tag, data: 0 };
const item = { item: tag };
let translationKey = this.resourceHandler.getItemTranslationKey(item);
if (translationKey) {
icon = serializer.createItemDisplay(this.resourceHandler, context, fileWriter, item, false);
} else {
const fluid = { fluid: tag.substr(tag.indexOf(':') + 1) };
const fluid = { fluid: tag };
translationKey = this.resourceHandler.getFluidTranslationKey(fluid);
icon = serializer.createFluidDisplay(this.resourceHandler, context, fileWriter, fluid, false);
}
Expand Down
Loading

0 comments on commit 249b211

Please sign in to comment.