Skip to content

Commit

Permalink
correct recipe exports & several bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
PssbleTrngle committed Oct 15, 2023
1 parent 281a1eb commit c75e278
Show file tree
Hide file tree
Showing 14 changed files with 154 additions and 52 deletions.
33 changes: 28 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,34 @@
"description": "Data Modifier",
"main": "dist/index.js",
"exports": {
".": "./dist/index.js",
"./parser": "./dist/parser/index.js",
"./parser/create": "./dist/parser/create.js",
"./parser/thermal": "./dist/parser/thermal.js",
"./parser/farmersdelight": "./dist/parser/farmersdelight.js"
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./parser": {
"types": "./dist/parser/index.d.ts",
"default": "./dist/parser/index.js"
},
"./parser/create": {
"types": "./dist/parser/create.d.ts",
"default": "./dist/parser/create.js"
},
"./parser/thermal": {
"types": "./dist/parser/thermal.d.ts",
"default": "./dist/parser/thermal.js"
},
"./parser/farmersdelight": {
"types": "./dist/parser/farmersdelight.d.ts",
"default": "./dist/parser/farmersdelight.js"
},
"./parser/adAstra": {
"types": "./dist/parser/adAstra.d.ts",
"default": "./dist/parser/adAstra.js"
},
"./parser/roots": {
"types": "./dist/parser/roots.d.ts",
"default": "./dist/parser/roots.js"
}
},
"types": "./dist/index.d.ts",
"files": [
Expand Down
10 changes: 6 additions & 4 deletions src/common/ingredient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ import { TagRegistry, TagRegistryHolder } from '../loader/tags.js'
import { createId, encodeId, Id, NormalizedId } from './id.js'
import { Logger } from '../logger.js'
import { resolveCommonTest } from './predicates.js'
import { Block, BlockSchema, Fluid, FluidSchema, Item, ItemSchema } from './result.js'
import { Block, BlockSchema, FluidStack, FluidStackSchema, ItemStack, ItemStackSchema } from './result.js'
import zod from 'zod'
import { exists } from '@pssbletrngle/pack-resolver'
import { IllegalShapeError, tryCatching } from '../error.js'

export const ItemTagSchema = zod.object({
tag: zod.string(),
count: zod.number().optional(),
})

export type ItemTag = zod.infer<typeof ItemTagSchema>

export const FluidTagSchema = zod.object({
fluidTag: zod.string(),
amount: zod.number().optional(),
})

export type FluidTag = zod.infer<typeof FluidTagSchema>
Expand All @@ -26,7 +28,7 @@ export const BlockTagSchema = zod.object({

export type BlockTag = zod.infer<typeof BlockTagSchema>

export type Ingredient = Item | ItemTag | Fluid | FluidTag | Block | BlockTag | IngredientInput[]
export type Ingredient = ItemStack | ItemTag | FluidStack | FluidTag | Block | BlockTag | IngredientInput[]
export type IngredientInput = Ingredient | string

export function createIngredient(input: unknown): Ingredient {
Expand All @@ -42,8 +44,8 @@ export function createIngredient(input: unknown): Ingredient {
}

if (typeof input === 'object') {
if ('item' in input) return ItemSchema.parse(input)
if ('fluid' in input) return FluidSchema.parse(input)
if ('item' in input) return ItemStackSchema.parse(input)
if ('fluid' in input) return FluidStackSchema.parse(input)
if ('block' in input) return BlockSchema.parse(input)

if ('tag' in input) return ItemTagSchema.parse(input)
Expand Down
25 changes: 6 additions & 19 deletions src/common/result.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,24 @@
import zod from 'zod'
import { IllegalShapeError } from '../error.js'

export const ItemSchema = zod.object({
export const ItemStackSchema = zod.object({
item: zod.string(),
count: zod.number().optional(),
chance: zod.number().optional(),
})

export type Item = zod.infer<typeof ItemSchema>

export const FluidSchema = zod.object({
export const FluidStackSchema = zod.object({
fluid: zod.string(),
amount: zod.number().optional(),
chance: zod.number().optional(),
})

export type Fluid = zod.infer<typeof FluidSchema>

export const ItemStackSchema = ItemSchema.and(
zod.object({
count: zod.number().optional(),
})
)

export type ItemStack = zod.infer<typeof ItemStackSchema>

export const FluidStackSchema = FluidSchema.and(
zod.object({
amount: zod.number().optional(),
})
)

export type FluidStack = zod.infer<typeof FluidStackSchema>

export const BlockSchema = zod.object({
block: zod.string(),
weight: zod.number().optional(),
})

export type Block = zod.infer<typeof BlockSchema>
Expand Down
15 changes: 13 additions & 2 deletions src/emit/recipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { Acceptor } from '@pssbletrngle/pack-resolver'

type RecipeTest = Readonly<{
id?: CommonTest<NormalizedId>
type?: CommonTest<NormalizedId>
namespace?: string
output?: IngredientTest
input?: IngredientTest
Expand Down Expand Up @@ -88,15 +89,17 @@ export default class RecipeEmitter implements RecipeRules {

private resolveRecipeTest(test: RecipeTest) {
const id: Predicate<Id>[] = []
const type: Predicate<Id>[] = []
const ingredient: Predicate<IngredientInput>[] = []
const result: Predicate<IngredientInput>[] = []

if (test.id) id.push(resolveIDTest(test.id))
if (test.type) type.push(resolveIDTest(test.type))
if (test.namespace) id.push(id => id.namespace === test.namespace)
if (test.output) result.push(this.resolveIngredientTest(test.output))
if (test.input) ingredient.push(this.resolveIngredientTest(test.input))

return { id, ingredient, result }
return { id, type, ingredient, result }
}

addRecipe<TDefinition extends RecipeDefinition, TRecipe extends Recipe<TDefinition>>(
Expand All @@ -110,7 +113,13 @@ export default class RecipeEmitter implements RecipeRules {
removeRecipe(test: RecipeTest) {
const recipePredicates = this.resolveRecipeTest(test)
this.ruled.addRule(
new RecipeRule(recipePredicates.id, recipePredicates.ingredient, recipePredicates.result, () => null)
new RecipeRule(
recipePredicates.id,
recipePredicates.type,
recipePredicates.ingredient,
recipePredicates.result,
() => null
)
)
}

Expand All @@ -121,6 +130,7 @@ export default class RecipeEmitter implements RecipeRules {
this.ruled.addRule(
new RecipeRule(
recipePredicates.id,
recipePredicates.type,
recipePredicates.ingredient,
[predicate, ...recipePredicates.result],
recipe => recipe.replaceResult(replacer)
Expand All @@ -135,6 +145,7 @@ export default class RecipeEmitter implements RecipeRules {
this.ruled.addRule(
new RecipeRule(
recipePredicates.id,
recipePredicates.type,
[predicate, ...recipePredicates.ingredient],
recipePredicates.result,
recipe => recipe.replaceIngredient(replacer)
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export {
} from './common/ingredient.js'
export { TagInput, Id, IdInput, NormalizedId, encodeId, createId } from './common/id.js'
export { default as Registry } from './common/registry.js'
export { Item, Block, ItemStack, Fluid, FluidStack, createResult, ResultInput, Result } from './common/result.js'
export { Block, ItemStack, FluidStack, createResult, ResultInput, Result } from './common/result.js'
export { default as createLogger, Logger, createSilentLogger, wrapLogMethods } from './logger.js'
export { RecipeDefinition, FabricCondition, ForgeCondition } from './schema/recipe.js'
export { TagEntry, TagDefinition } from './schema/tag.js'
Expand Down
20 changes: 20 additions & 0 deletions src/parser/adAstra.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export {
HammeringRecipe,
HammeringRecipeDefinition,
default as HammeringRecipeParser,
} from './recipe/adAstra/hammering.js'
export {
InputOutputRecipe,
InputOutputRecipeDefinition,
default as InputOutputRecipeRecipeParser,
} from './recipe/adAstra/inputOutput.js'
export {
NasaWorkbenchRecipe,
NasaWorkbenchRecipeDefinition,
default as NasaWorkbenchRecipeParser,
} from './recipe/adAstra/nasaWorkbench.js'
export {
SpaceStationRecipe,
SpaceStationRecipeDefinition,
default as SpaceStationRecipeParser,
} from './recipe/adAstra/spaceStation.js'
51 changes: 42 additions & 9 deletions src/parser/botania.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,42 @@
export { ApothecaryRecipeDefinition, ApothecaryRecipe, default as ApothecaryRecipeParser } from './recipe/botania/apothecary.js'
export { NbtWrapperRecipeDefinition, NbtWrapperRecipe, default as NbtWrapperRecipeParser } from './recipe/botania/nbtWrapper.js'
export { ElvenTradeRecipeDefinition, ElvenTradeRecipe, default as ElvenTradeRecipeParser } from './recipe/botania/elvenTrade.js'
export { GogWrapperRecipeDefinition, GogWrapperRecipe, default as GogWrapperRecipeParser } from './recipe/botania/gogWrapper.js'
export { ManaInfusionRecipeDefinition, ManaInfusionRecipe, default as ManaInfusionRecipeParser } from './recipe/botania/manaInfusion.js'
export { OrechidRecipeDefinition, OrechidRecipe , default as OrechidRecipeParser } from './recipe/botania/orechid.js'
export { PureDaisyRecipeDefinition, PureDaisyRecipe, default as PureDaisyRecipeParser } from './recipe/botania/pureDaisy.js'
export { TerraPlateRecipeDefinition, TerraPlateRecipe, default as TerraPlateRecipeParser } from './recipe/botania/terraPlate.js'
export { RunicAltarRecipeDefinition, RunicAltarRecipe, default as RunicAltarRecipeParser } from './recipe/botania/runicAltar.js'
export {
ApothecaryRecipeDefinition,
ApothecaryRecipe,
default as ApothecaryRecipeParser,
} from './recipe/botania/apothecary.js'
export { BrewRecipeDefinition, BrewRecipe, default as BrewRecipeParser } from './recipe/botania/brew.js'
export {
ElvenTradeRecipeDefinition,
ElvenTradeRecipe,
default as ElvenTradeRecipeParser,
} from './recipe/botania/elvenTrade.js'
export {
GogWrapperRecipeDefinition,
GogWrapperRecipe,
default as GogWrapperRecipeParser,
} from './recipe/botania/gogWrapper.js'
export {
ManaInfusionRecipeDefinition,
ManaInfusionRecipe,
default as ManaInfusionRecipeParser,
} from './recipe/botania/manaInfusion.js'
export {
NbtWrapperRecipeDefinition,
NbtWrapperRecipe,
default as NbtWrapperRecipeParser,
} from './recipe/botania/nbtWrapper.js'
export { OrechidRecipeDefinition, OrechidRecipe, default as OrechidRecipeParser } from './recipe/botania/orechid.js'
export {
PureDaisyRecipeDefinition,
PureDaisyRecipe,
default as PureDaisyRecipeParser,
} from './recipe/botania/pureDaisy.js'
export {
RunicAltarRecipeDefinition,
RunicAltarRecipe,
default as RunicAltarRecipeParser,
} from './recipe/botania/runicAltar.js'
export {
TerraPlateRecipeDefinition,
TerraPlateRecipe,
default as TerraPlateRecipeParser,
} from './recipe/botania/terraPlate.js'
4 changes: 2 additions & 2 deletions src/parser/lootTable.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { extendLootEntry, LootEntry, LootEntryBase, LootEntrySchema, LootTable } from '../schema/loot.js'
import { IllegalShapeError } from '../error.js'
import { IngredientInput, ItemTag, Predicate } from '../common/ingredient.js'
import { Item } from '../common/result.js'
import { ItemStack } from '../common/result.js'

export type LootItemInput = Item | ItemTag | LootEntry
export type LootItemInput = ItemStack | ItemTag | LootEntry

export function createLootEntry(input: LootItemInput): LootEntry {
if (input && typeof input === 'object') {
Expand Down
10 changes: 3 additions & 7 deletions src/parser/recipe/botania/orechid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ export type BlockInput =
| Readonly<{
type: 'block'
block: string
weight?: number
}>
| Readonly<{
type: 'tag'
tag: string
weight?: number
}>

export type OrechidRecipeDefinition = RecipeDefinition &
Readonly<{
input: BlockInput
output: BlockInput
result: ResultInput
biome_bonus?: number
biome_bonus_tag?: string
weight?: number
}>

export function createBlockInput(input: IngredientInput): BlockInput | null {
Expand All @@ -37,14 +37,12 @@ export function createBlockInput(input: IngredientInput): BlockInput | null {
return {
type: 'block',
block: ingredient.block,
weight: ingredient.weight,
}

if ('blockTag' in ingredient)
return {
type: 'tag',
tag: ingredient.blockTag,
weight: ingredient.weight,
}

return null
Expand All @@ -65,12 +63,10 @@ export function fromBlockInput(input: BlockInput): Block | BlockTag {
case 'block':
return {
block: encodeId(input.block),
weight: input.weight,
}
case 'tag':
return {
blockTag: encodeId(input.tag),
weight: input.weight,
}
default:
throw new IllegalShapeError(`Unknown block input type`, input)
Expand Down
4 changes: 2 additions & 2 deletions src/parser/recipe/thermal/ingredient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FluidTag, IngredientInput, ItemTagSchema } from '../../../common/ingredient.js'
import zod from 'zod'
import { ItemSchema } from '../../../common/result.js'
import { ItemStackSchema } from '../../../common/result.js'
import { omit } from 'lodash-es'
import { IllegalShapeError } from '../../../error.js'

Expand All @@ -21,7 +21,7 @@ export type ThermalIngredientInput = Exclude<IngredientInput, FluidTag> | Therma
function fromThermalList(input: ThermalItemList): IngredientInput {
return input.value.map(it => {
if (it && typeof it === 'object') {
if ('item' in it) return { ...ItemSchema.parse(it), count: input.count }
if ('item' in it) return { ...ItemStackSchema.parse(it), count: input.count }
if ('tag' in it) return { ...ItemTagSchema.parse(it), count: input.count }
}
throw new IllegalShapeError('thermal array ingredients may only be of type item/itemtag', it)
Expand Down
10 changes: 10 additions & 0 deletions src/parser/roots.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export {
RootRitualRecipe,
RootRitualRecipeDefinition,
default as RootRitualRecipeParser,
} from './recipe/roots/ritual.js'
export {
RootComponentRecipe,
RootComponentRecipeDefinition,
default as RootComponentRecipeParser,
} from './recipe/roots/component.js'
5 changes: 5 additions & 0 deletions src/parser/thermal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export { ThermalRecipeDefinition, ThermalRecipe, default as ThermalRecipeParser } from './recipe/thermal/index.js'
export {
TreeExtractionRecipe,
TreeExtractionRecipeDefinition,
default as TreeExtractionRecipeParser,
} from './recipe/thermal/treeExtraction.js'
export {
ThermalCatalystRecipeDefinition,
ThermalCatalystRecipe,
Expand Down
5 changes: 4 additions & 1 deletion src/rule/recipe.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { IngredientInput, Predicate } from '../common/ingredient.js'
import { Recipe } from '../parser/recipe/index.js'
import { encodeId, Id } from '../common/id.js'
import { createId, encodeId, Id } from '../common/id.js'
import { Logger } from '../logger.js'
import Rule, { Modifier } from './index.js'

export default class RecipeRule extends Rule<Recipe> {
constructor(
private readonly idsTests: Predicate<Id>[],
private readonly typeTests: Predicate<Id>[],
private readonly ingredientTests: Predicate<IngredientInput>[],
private readonly resultTests: Predicate<IngredientInput>[],
modifier: Modifier<Recipe>
Expand All @@ -16,8 +17,10 @@ export default class RecipeRule extends Rule<Recipe> {

matches(id: Id, recipe: Recipe, logger: Logger): boolean {
const prefixed = logger.group(encodeId(id))
const type = createId(recipe.toJSON().type)
return (
this.idsTests.every(test => test(id, prefixed)) &&
this.typeTests.every(test => test(type, prefixed)) &&
this.ingredientTests.every(test => recipe.getIngredients().some(it => test(it, prefixed))) &&
this.resultTests.every(test => recipe.getResults().some(it => test(it, prefixed)))
)
Expand Down
Loading

0 comments on commit c75e278

Please sign in to comment.