|
| 1 | +import { CreativeModeTabId, ItemId } from '@pssbletrngle/data-modifier/generated' |
| 2 | +import { Acceptor, arrayOrSelf } from '@pssbletrngle/pack-resolver' |
| 3 | +import { difference, uniq } from 'lodash-es' |
| 4 | +import { createId, encodeId, IdInput, NormalizedId } from '../common/id.js' |
| 5 | +import RegistryLookup from '../loader/registry/index.js' |
| 6 | +import CustomEmitter from './custom.js' |
| 7 | +import { ClearableEmitter } from './index.js' |
| 8 | + |
| 9 | +type AddOptions = |
| 10 | + | { |
| 11 | + after: IdInput<ItemId> |
| 12 | + } |
| 13 | + | { |
| 14 | + before: IdInput<ItemId> |
| 15 | + } |
| 16 | + |
| 17 | +export interface PolytoneTabs { |
| 18 | + remove(tab: IdInput<CreativeModeTabId> | IdInput<CreativeModeTabId>[], items: IdInput<ItemId>[]): void |
| 19 | + add( |
| 20 | + tab: IdInput<CreativeModeTabId> | IdInput<CreativeModeTabId>[], |
| 21 | + items: IdInput<ItemId>[], |
| 22 | + options?: AddOptions |
| 23 | + ): void |
| 24 | + create(id: IdInput): void |
| 25 | +} |
| 26 | + |
| 27 | +interface ItemsMatchPredicate { |
| 28 | + type: 'items_match' |
| 29 | + items: NormalizedId<ItemId>[] |
| 30 | +} |
| 31 | + |
| 32 | +type PolytonePredicate = ItemsMatchPredicate |
| 33 | + |
| 34 | +interface AdditionEntry { |
| 35 | + items: ItemId[] |
| 36 | + before?: boolean |
| 37 | + inverse?: boolean |
| 38 | + predicate?: PolytonePredicate |
| 39 | +} |
| 40 | + |
| 41 | +export interface PolytoneTabModifier { |
| 42 | + targets: NormalizedId<CreativeModeTabId>[] |
| 43 | + removals?: PolytonePredicate[] |
| 44 | + additions?: AdditionEntry[] |
| 45 | +} |
| 46 | + |
| 47 | +function mergeModifiers(a: PolytoneTabModifier, b: PolytoneTabModifier): PolytoneTabModifier { |
| 48 | + const diff = difference(a.targets, b.targets) |
| 49 | + if (diff.length) throw new Error('trying to merge modifiers with different targets') |
| 50 | + return { |
| 51 | + targets: uniq([...a.targets, ...b.targets]), |
| 52 | + additions: [...(a.additions ?? []), ...(b.additions ?? [])], |
| 53 | + removals: [...(a.removals ?? []), ...(b.removals ?? [])], |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +function translateOptions(options?: AddOptions): Partial<AdditionEntry> { |
| 58 | + if (!options) return {} |
| 59 | + const before = 'before' in options |
| 60 | + const item = before ? options.before : options.after |
| 61 | + return { |
| 62 | + before, |
| 63 | + predicate: { |
| 64 | + items: [encodeId(item)], |
| 65 | + type: 'items_match', |
| 66 | + }, |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +export default class PolytoneTabsEmitter implements PolytoneTabs, ClearableEmitter { |
| 71 | + private readonly entries = new CustomEmitter<PolytoneTabModifier>( |
| 72 | + id => `assets/${id.namespace}/polytone/creative_tab_modifiers/${id.path}.json` |
| 73 | + ) |
| 74 | + |
| 75 | + private readonly tabs = new CustomEmitter<string[]>( |
| 76 | + id => `assets/${id.namespace}/polytone/creative_tabs.csv`, |
| 77 | + it => it.join(',') |
| 78 | + ) |
| 79 | + |
| 80 | + constructor(private readonly lookup: () => RegistryLookup) {} |
| 81 | + |
| 82 | + clear(): void { |
| 83 | + this.entries.clear() |
| 84 | + this.tabs.clear() |
| 85 | + } |
| 86 | + |
| 87 | + async emit(acceptor: Acceptor) { |
| 88 | + await Promise.all([this.entries.emit(acceptor), this.tabs.emit(acceptor)]) |
| 89 | + } |
| 90 | + |
| 91 | + remove(tab: IdInput<CreativeModeTabId> | IdInput<CreativeModeTabId>[], items: IdInput<ItemId>[]) { |
| 92 | + this.forEach(tab, { |
| 93 | + removals: [{ type: 'items_match', items: items.map(encodeId) }], |
| 94 | + }) |
| 95 | + } |
| 96 | + |
| 97 | + add(tab: IdInput<CreativeModeTabId> | IdInput<CreativeModeTabId>[], items: IdInput<ItemId>[], options?: AddOptions) { |
| 98 | + this.forEach(tab, { |
| 99 | + additions: [{ items: items.map(encodeId), ...translateOptions(options) }], |
| 100 | + }) |
| 101 | + } |
| 102 | + |
| 103 | + create(id: IdInput): void { |
| 104 | + const lookup = this.lookup() |
| 105 | + lookup.addCustom('minecraft:creative_mode_tab', id) |
| 106 | + const { namespace, path } = createId(id) |
| 107 | + this.tabs.merge({ namespace, path: 'tab' }, [path], (a, b) => uniq([...a, ...b])) |
| 108 | + } |
| 109 | + |
| 110 | + private forEach( |
| 111 | + tab: IdInput<CreativeModeTabId> | IdInput<CreativeModeTabId>[], |
| 112 | + modifier: Omit<PolytoneTabModifier, 'targets'> |
| 113 | + ) { |
| 114 | + arrayOrSelf(tab).forEach(target => { |
| 115 | + const entry: PolytoneTabModifier = { |
| 116 | + targets: [encodeId(target)], |
| 117 | + ...modifier, |
| 118 | + } |
| 119 | + this.entries.merge(target, entry, mergeModifiers) |
| 120 | + }) |
| 121 | + } |
| 122 | +} |
0 commit comments