Skip to content

Commit 592a5bf

Browse files
committed
allow modification of tab properties
1 parent d047b52 commit 592a5bf

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

src/cli/codegen/registry.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ function idTemplate(type: string, values: string[]) {
1919
}
2020

2121
function inferRegistryTemplate(keys: IdInput[]) {
22+
if (keys.length === 0) throw new Error('no registry found')
2223
return `
2324
export type InferIds<T extends RegistryId> = {
2425
${keys.map(it => `'${encodeId(it)}': ${idType(createId(it))}Id`).join('\n')}

src/emit/polytoneTabs.ts

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ import RegistryLookup from '../loader/registry/index.js'
66
import CustomEmitter from './custom.js'
77
import { ClearableEmitter } from './index.js'
88

9+
interface TabModifications {
10+
icon?: IdInput<ItemId>
11+
can_scroll?: boolean
12+
show_title?: boolean
13+
search_bar?: boolean
14+
before_tabs?: IdInput<CreativeModeTabId>[]
15+
after_tabs?: IdInput<CreativeModeTabId>[]
16+
name?: string
17+
}
18+
919
interface TabOptions {
1020
file?: IdInput
1121
}
@@ -31,7 +41,8 @@ export interface PolytoneTabs {
3141
items: IdInput<ItemId>[],
3242
options?: AddOptions
3343
): void
34-
create(id: IdInput): CreativeModeTabId
44+
create(id: IdInput, options?: TabModifications): CreativeModeTabId
45+
modify(id: IdInput, options: TabModifications): void
3546
}
3647

3748
interface ItemsMatchPredicate {
@@ -48,16 +59,35 @@ interface AdditionEntry {
4859
predicate?: PolytonePredicate
4960
}
5061

51-
export interface PolytoneTabModifier {
62+
export interface PolytoneTabModifier extends TabModifications {
63+
icon?: NormalizedId<ItemId>
64+
before_tabs?: NormalizedId<CreativeModeTabId>[]
65+
after_tabs?: NormalizedId<CreativeModeTabId>[]
5266
targets: NormalizedId<CreativeModeTabId>[]
5367
removals?: PolytonePredicate[]
5468
additions?: AdditionEntry[]
5569
}
5670

71+
function translateModifications({
72+
icon,
73+
after_tabs,
74+
before_tabs,
75+
...modifications
76+
}: TabModifications): Omit<PolytoneTabModifier, 'targets'> {
77+
return {
78+
...modifications,
79+
before_tabs: arrayOrSelf(before_tabs).map(encodeId),
80+
after_tabs: arrayOrSelf(after_tabs).map(encodeId),
81+
icon: icon !== undefined ? encodeId(icon) : undefined,
82+
}
83+
}
84+
5785
function mergeModifiers(a: PolytoneTabModifier, b: PolytoneTabModifier): PolytoneTabModifier {
5886
const diff = difference(a.targets, b.targets)
5987
if (diff.length) throw new Error('trying to merge modifiers with different targets')
6088
return {
89+
...a,
90+
...b,
6191
targets: uniq([...a.targets, ...b.targets]),
6292
additions: [...(a.additions ?? []), ...(b.additions ?? [])],
6393
removals: [...(a.removals ?? []), ...(b.removals ?? [])],
@@ -135,13 +165,27 @@ export default class PolytoneTabsEmitter implements PolytoneTabs, ClearableEmitt
135165
)
136166
}
137167

138-
create(id: IdInput) {
168+
create(id: IdInput, modifications: TabModifications = {}) {
139169
const lookup = this.lookup()
140170
const { namespace, path } = createId(id)
141171
this.tabs.merge({ namespace, path: 'tab' }, [path], (a, b) => uniq([...a, ...b]))
172+
if (Object.keys(modifications).length) {
173+
this.modify(id, modifications)
174+
}
142175
return lookup.addCustom('minecraft:creative_mode_tab', id)
143176
}
144177

178+
modify(id: IdInput, modifications: TabModifications) {
179+
this.entries.merge(
180+
id,
181+
{
182+
...translateModifications(modifications),
183+
targets: [encodeId(id)],
184+
},
185+
mergeModifiers
186+
)
187+
}
188+
145189
private forEach(
146190
tab: IdInput<CreativeModeTabId> | IdInput<CreativeModeTabId>[],
147191
modifier: Omit<PolytoneTabModifier, 'targets'>,

0 commit comments

Comments
 (0)