-
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
3c9d9ae
commit 447a0dd
Showing
37 changed files
with
239 additions
and
1,887 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { encodeId, IdInput, NormalizedId, TagInput } from './id' | ||
import { TagRegistry } from '../loader/tags' | ||
import { CommonTest, Predicate } from './ingredient' | ||
|
||
export function resolveCommonTest<TEntry, TId extends string>( | ||
test: CommonTest<NormalizedId<TId>>, | ||
resolve: (value: TEntry) => NormalizedId<TId>[], | ||
tags?: TagRegistry | ||
): Predicate<TEntry> { | ||
if (typeof test === 'function') { | ||
return it => resolve(it).some(test) | ||
} else if (test instanceof RegExp) { | ||
return ingredient => { | ||
return resolve(ingredient).some(it => test.test(it)) | ||
} | ||
} else if (test.startsWith('#')) { | ||
return ingredient => { | ||
return resolve(ingredient).some(id => { | ||
if (id.startsWith('#') && test === id) return true | ||
else if (tags) return tags.contains(test as TagInput, id) ?? false | ||
else throw new Error('Cannot parse ID test without tags') | ||
}) | ||
} | ||
} else { | ||
return ingredient => { | ||
return resolve(ingredient).includes(test) | ||
} | ||
} | ||
} | ||
|
||
export function resolveIDTest<T extends NormalizedId>(test: CommonTest<T>, tags?: TagRegistry): Predicate<IdInput<T>> { | ||
return resolveCommonTest(test, it => [encodeId<T>(it)], tags) | ||
} |
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,52 @@ | ||
import zod from 'zod' | ||
import { IllegalShapeError } from '../error' | ||
|
||
export const ItemSchema = zod.object({ | ||
item: zod.string(), | ||
}) | ||
|
||
export type Item = zod.infer<typeof ItemSchema> | ||
|
||
export const FluidSchema = zod.object({ | ||
fluid: zod.string(), | ||
}) | ||
|
||
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> | ||
|
||
export type Result = ItemStack | FluidStack | Block | ||
export type ResultInput = Result | string | ||
|
||
export function createResult(input: ResultInput): Result { | ||
if (!input) throw new IllegalShapeError('result input may not be null') | ||
|
||
if (typeof input === 'string') return { item: 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) | ||
|
||
throw new IllegalShapeError(`unknown result shape`, input) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export class IllegalShapeError extends Error { | ||
constructor(message: string, readonly input?: any) { | ||
super(input ? `${message}: ${JSON.stringify(input)}` : message) | ||
} | ||
} |
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
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.