-
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.
common emitter logic & basic loot loader/emitter
- Loading branch information
1 parent
23aaa5e
commit 154889e
Showing
17 changed files
with
472 additions
and
140 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,34 @@ | ||
import Rule from '../rule' | ||
import { Acceptor } from '@pssbletrngle/pack-resolver' | ||
import { Recipe } from '../parser/recipe' | ||
import { toJson } from '../textHelper' | ||
import { RecipeDefinition } from '../schema/recipe' | ||
import { createId, Id, IdInput } from '../common/id' | ||
import { createId, IdInput } from '../common/id' | ||
import Registry from '../common/registry' | ||
import { LootTable } from '../loader/loot' | ||
import { PathProvider } from './index' | ||
|
||
export interface RegistryProvider<T> { | ||
forEach(consumer: (recipe: T, id: Id) => void): void | ||
} | ||
|
||
export default abstract class RuledEmitter<TEntry, TRule extends Rule<TEntry>> { | ||
export default class CustomEmitter<TEntry> { | ||
constructor(private readonly pathProvider: PathProvider) {} | ||
|
||
protected constructor(private readonly provider: RegistryProvider<TEntry>) { | ||
protected addCustom(id: IdInput, value: TEntry) { | ||
this.customEntries.set(createId(id), value) | ||
} | ||
|
||
|
||
private customEntries = new Registry<TEntry>() | ||
private rulesArray: TRule[] = [] | ||
|
||
protected get rules(): ReadonlyArray<TRule> { | ||
return this.rulesArray | ||
} | ||
|
||
clear() { | ||
this.rulesArray = [] | ||
} | ||
|
||
protected addRule(rule: TRule) { | ||
this.rulesArray.push(rule) | ||
this.customEntries.clear() | ||
} | ||
|
||
protected addCustom( | ||
id: IdInput, | ||
value: TEntry | ||
) { | ||
add(id: IdInput, value: TEntry) { | ||
this.customEntries.set(createId(id), value) | ||
} | ||
|
||
private async modify(acceptor: Acceptor) { | ||
this.provider.forEach((recipe, id) => { | ||
if (this.customRecipe.has(id)) return | ||
|
||
const path = this.recipePath(id) | ||
|
||
const rules = this.rules.filter(it => it.matches(id, recipe, this.logger)) | ||
if (rules.length === 0) return | ||
|
||
const modified = rules.reduce<Recipe | null>((previous, rule) => previous && rule.modify(previous), recipe) | ||
|
||
acceptor(path, toJson(modified?.toDefinition() ?? RecipeEmitter.EMPTY_RECIPE)) | ||
}) | ||
} | ||
|
||
private async create(acceptor: Acceptor) { | ||
this.customRecipe.forEach((recipe, id) => { | ||
const path = this.recipePath(id) | ||
acceptor(path, toJson(recipe)) | ||
async emit(acceptor: Acceptor) { | ||
this.customEntries.forEach((entry, id) => { | ||
const path = this.pathProvider(id) | ||
acceptor(path, toJson(entry)) | ||
}) | ||
} | ||
|
||
async emit(acceptor: Acceptor) { | ||
await Promise.all([this.modifyRecipes(acceptor), this.createRecipes(acceptor)]) | ||
has(id: IdInput) { | ||
return this.customEntries.has(id) | ||
} | ||
|
||
} |
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,7 @@ | ||
import { Id } from '../common/id' | ||
|
||
export interface RegistryProvider<T> { | ||
forEach(consumer: (recipe: T, id: Id) => void): void | ||
} | ||
|
||
export type PathProvider = (id: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { Logger } from '../logger' | ||
import TagsLoader from '../loader/tags' | ||
import { LootTable, LootTableSchema } from '../schema/loot' | ||
import { Acceptor } from '@pssbletrngle/pack-resolver' | ||
import RuledEmitter from './ruled' | ||
import CustomEmitter from './custom' | ||
import { Id, IdInput } from '../common/id' | ||
import LootTableRule from '../rule/lootTable' | ||
import { RegistryProvider } from './index' | ||
import { Predicate } from '../common/ingredient' | ||
|
||
export const EMPTY_LOOT_TABLE: LootTable = { | ||
type: 'minecraft:empty', | ||
pools: [], | ||
} | ||
|
||
type LootTableTest = Predicate<Id> | ||
|
||
export interface LootRules { | ||
// replace(test: IngredientTest, to: Item | ItemTag): void | ||
|
||
addLootTable(id: IdInput, value: LootTable): void | ||
|
||
removeLootTable(test: LootTableTest): void | ||
} | ||
|
||
export default class LootTableEmitter implements LootRules { | ||
private readonly custom = new CustomEmitter<LootTable>(this.lootPath) | ||
|
||
private readonly ruled = new RuledEmitter<LootTable, LootTableRule>( | ||
this.logger, | ||
this.lootTables, | ||
this.lootPath, | ||
EMPTY_LOOT_TABLE, | ||
id => this.custom.has(id) | ||
) | ||
|
||
constructor( | ||
private readonly logger: Logger, | ||
private readonly lootTables: RegistryProvider<LootTable>, | ||
private readonly tags: TagsLoader | ||
) {} | ||
|
||
private lootPath(id: Id) { | ||
return `data/${id.namespace}/loot_tables/${id.path}.json` | ||
} | ||
|
||
async emit(acceptor: Acceptor) { | ||
await Promise.all([this.ruled.emit(acceptor), this.custom.emit(acceptor)]) | ||
} | ||
|
||
addLootTable(id: IdInput, value: LootTable): void { | ||
this.custom.add(id, LootTableSchema.parse(value)) | ||
} | ||
|
||
removeLootTable(test: LootTableTest): void { | ||
this.ruled.addRule(new LootTableRule([test], () => null)) | ||
} | ||
|
||
clear() { | ||
this.custom.clear() | ||
this.ruled.clear() | ||
} | ||
} |
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,47 @@ | ||
import Rule from '../rule' | ||
import { Acceptor } from '@pssbletrngle/pack-resolver' | ||
import { toJson } from '../textHelper' | ||
import Registry from '../common/registry' | ||
import { PathProvider, RegistryProvider } from './index' | ||
import { Logger } from '../logger' | ||
import { Id } from '../common/id' | ||
|
||
export default class RuledEmitter<TEntry, TRule extends Rule<TEntry>> { | ||
constructor( | ||
private readonly logger: Logger, | ||
private readonly provider: RegistryProvider<TEntry>, | ||
private readonly pathProvider: PathProvider, | ||
private readonly emptyValue: unknown, | ||
private readonly shouldSkip: (id: Id) => boolean = () => true | ||
) {} | ||
|
||
private customEntries = new Registry<TEntry>() | ||
private rulesArray: TRule[] = [] | ||
|
||
protected get rules(): ReadonlyArray<TRule> { | ||
return this.rulesArray | ||
} | ||
|
||
clear() { | ||
this.rulesArray = [] | ||
} | ||
|
||
addRule(rule: TRule) { | ||
this.rulesArray.push(rule) | ||
} | ||
|
||
async emit(acceptor: Acceptor) { | ||
this.provider.forEach((recipe, id) => { | ||
if (this.shouldSkip(id)) return | ||
|
||
const path = this.pathProvider(id) | ||
|
||
const rules = this.rules.filter(it => it.matches(id, recipe, this.logger)) | ||
if (rules.length === 0) return | ||
|
||
const modified = rules.reduce<TEntry | null>((previous, rule) => previous && rule.modify(previous), recipe) | ||
|
||
acceptor(path, toJson(modified ?? this.emptyValue)) | ||
}) | ||
} | ||
} |
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.