Skip to content

Commit

Permalink
Merge pull request #3 from PssbleTrngle/codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
PssbleTrngle authored Nov 8, 2023
2 parents 1fe95da + 7e1b197 commit b8d2284
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 21 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,4 @@ dist
# webstorm
.idea

test-output
generated
test-output
File renamed without changes.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,8 @@
"prismarine-nbt": "^2.2.1",
"typescript": "^4.8.2",
"zod": "^3.22.4"
},
"resolutions": {
"@pssbletrngle/data-modifier": "link:."
}
}
1 change: 1 addition & 0 deletions playground/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
generated
5 changes: 4 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions src/common/ingredient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function createIngredient(input: unknown, lookup?: RegistryLookup): Ingre

export type Predicate<T> = (value: T, logger?: Logger) => boolean
export type CommonTest<T> = RegExp | Predicate<T> | T
export type IngredientTest = CommonTest<Ingredient> | NormalizedId<ItemId>
export type IngredientTest = CommonTest<Ingredient> | ItemId | `#${string}`

function resolveIdIngredientTest(
test: NormalizedId | RegExp,
Expand Down Expand Up @@ -117,7 +117,8 @@ export function resolveIngredientTest(
lookup: RegistryLookup | undefined
): Predicate<IngredientInput> {
if (typeof test === 'string') {
return resolveIngredientTest({ item: test }, tags, lookup)
if (!test.startsWith('#')) return resolveIngredientTest({ item: test }, tags, lookup)
return resolveIngredientTest({ tag: test.substring(1) }, tags, lookup)
}

if (test instanceof RegExp) {
Expand Down
19 changes: 12 additions & 7 deletions src/common/result.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
import zod from 'zod'
import { IllegalShapeError } from '../error.js'
import RegistryLookup from '../loader/registry/index.js'
import { ZodType } from 'zod/lib/types'
import { BlockId, FluidId, ItemId } from '@pssbletrngle/data-modifier/generated'

export const ItemStackSchema = zod.object({
item: zod.string() as ZodType<ItemId>,
item: zod.string(),
count: zod.number().int().optional(),
chance: zod.number().optional(),
})

export type ItemStack = zod.infer<typeof ItemStackSchema>
export type ItemStack = Omit<zod.infer<typeof ItemStackSchema>, 'item'> & {
item: ItemId
}

export const FluidStackSchema = zod.object({
fluid: zod.string() as ZodType<FluidId>,
fluid: zod.string(),
amount: zod.number().optional(),
chance: zod.number().optional(),
})

export type FluidStack = zod.infer<typeof FluidStackSchema>
export type FluidStack = Omit<zod.infer<typeof FluidStackSchema>, 'fluid'> & {
fluid: FluidId
}

export const BlockSchema = zod.object({
block: zod.string() as ZodType<BlockId>,
block: zod.string(),
})

export type Block = zod.infer<typeof BlockSchema>
export type Block = Omit<zod.infer<typeof BlockSchema>, 'block'> & {
block: BlockId
}

export type Result = ItemStack | FluidStack | Block
export type ResultInput = Result | ItemId
Expand Down
6 changes: 3 additions & 3 deletions src/emit/blacklist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { uniq } from 'lodash-es'
import { IllegalShapeError } from '../error.js'

export interface BlacklistRules {
hide(input: IngredientInput): void
hide(...inputs: IngredientInput[]): void
}

export default class BlacklistEmitter implements BlacklistRules {
private hidden: IngredientInput[] = []

hide(input: IngredientInput) {
this.hidden.push(input)
hide(...inputs: IngredientInput[]) {
this.hidden.push(...inputs)
}

private resolveIds(input: IngredientInput): string[] {
Expand Down
8 changes: 4 additions & 4 deletions src/emit/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { TagDefinition, TagEntry } from '../schema/tag.js'
import { createId, encodeId, Id, NormalizedId, TagInput } from '../common/id.js'
import Registry from '../common/registry.js'
import { resolveIDTest } from '../common/predicates.js'
import { BlockId, FluidId, InferIds, ItemId, RegistryId } from '@pssbletrngle/data-modifier/generated'
import { InferIds, RegistryId } from '@pssbletrngle/data-modifier/generated'

export interface TagRules {
add<T extends RegistryId>(registry: T, id: TagInput, value: TagEntry<InferIds<T>>): void
Expand All @@ -16,9 +16,9 @@ export interface TagRules {

scoped<T extends RegistryId>(key: T): ScopedTagRules<T>

blocks: ScopedTagRules<BlockId>
items: ScopedTagRules<ItemId>
fluids: ScopedTagRules<FluidId>
blocks: ScopedTagRules<'minecraft:block'>
items: ScopedTagRules<'minecraft:item'>
fluids: ScopedTagRules<'minecraft:fluid'>
}

interface ScopedTagRules<T extends RegistryId> {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export { TagRules } from './emit/tags.js'
export { RecipeRules, EMPTY_RECIPE } from './emit/recipe.js'
export { LootRules, EMPTY_LOOT_TABLE } from './emit/loot.js'
export { LootItemInput } from './parser/lootTable.js'
export { IllegalShapeError } from './error.js'
export { UnknownRegistryEntry, IllegalShapeError } from './error.js'
export {
IngredientInput,
Predicate,
Expand Down
11 changes: 10 additions & 1 deletion src/loader/pack.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Acceptor, IResolver, ResolverInfo } from '@pssbletrngle/pack-resolver'
import match from 'minimatch'
import { IngredientTest } from '../common/ingredient.js'
import { createIngredient, IngredientInput, IngredientTest } from '../common/ingredient.js'
import BlacklistEmitter, { BlacklistRules } from '../emit/blacklist.js'
import LangEmitter, { LangRules } from '../emit/lang.js'
import LootTableEmitter, { LootRules } from '../emit/loot.js'
Expand All @@ -15,6 +15,7 @@ import RegistryDumpLoader from './registry/dump.js'
import RegistryLookup from './registry/index.js'
import TagsLoader from './tags.js'
import EmptyRegistryLookup from './registry/empty.js'
import { createResult, ResultInput } from '../common/result.js'

export default class PackLoader implements Loader {
constructor(private readonly logger: Logger) {}
Expand Down Expand Up @@ -78,6 +79,14 @@ export default class PackLoader implements Loader {
return this.activeRegistryLookup
}

createResult(input: ResultInput) {
return createResult(input, this.activeRegistryLookup)
}

createIngredient(input: IngredientInput) {
return createIngredient(input, this.activeRegistryLookup)
}

resolveIngredientTest(test: IngredientTest) {
return this.recipeEmitter.resolveIngredientTest(test)
}
Expand Down

0 comments on commit b8d2284

Please sign in to comment.