Skip to content

Commit d047b52

Browse files
committed
allow polytone output to specific files
1 parent d8a2535 commit d047b52

File tree

3 files changed

+115
-28
lines changed

3 files changed

+115
-28
lines changed

src/emit/polytoneTabs.ts

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

9-
type AddOptions =
10-
| {
11-
after: IdInput<ItemId>
12-
}
13-
| {
14-
before: IdInput<ItemId>
15-
}
9+
interface TabOptions {
10+
file?: IdInput
11+
}
12+
13+
interface TabOptionsWithAfter extends TabOptions {
14+
after: IdInput<ItemId>
15+
}
16+
17+
interface TabOptionsWithBefore extends TabOptions {
18+
before: IdInput<ItemId>
19+
}
20+
21+
type AddOptions = TabOptionsWithAfter | TabOptionsWithBefore | TabOptions
1622

1723
export interface PolytoneTabs {
18-
remove(tab: IdInput<CreativeModeTabId> | IdInput<CreativeModeTabId>[], items: IdInput<ItemId>[]): void
24+
remove(
25+
tab: IdInput<CreativeModeTabId> | IdInput<CreativeModeTabId>[],
26+
items: IdInput<ItemId>[],
27+
options?: TabOptions
28+
): void
1929
add(
2030
tab: IdInput<CreativeModeTabId> | IdInput<CreativeModeTabId>[],
2131
items: IdInput<ItemId>[],
@@ -54,16 +64,25 @@ function mergeModifiers(a: PolytoneTabModifier, b: PolytoneTabModifier): Polyton
5464
}
5565
}
5666

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+
function translateOptions(options: AddOptions = {}): Partial<AdditionEntry> {
68+
if ('before' in options) {
69+
return {
70+
before: true,
71+
predicate: {
72+
items: [encodeId(options.before)],
73+
type: 'items_match',
74+
},
75+
}
76+
} else if ('after' in options) {
77+
return {
78+
before: false,
79+
predicate: {
80+
items: [encodeId(options.after)],
81+
type: 'items_match',
82+
},
83+
}
84+
} else {
85+
return {}
6786
}
6887
}
6988

@@ -88,16 +107,32 @@ export default class PolytoneTabsEmitter implements PolytoneTabs, ClearableEmitt
88107
await Promise.all([this.entries.emit(acceptor), this.tabs.emit(acceptor)])
89108
}
90109

91-
remove(tab: IdInput<CreativeModeTabId> | IdInput<CreativeModeTabId>[], items: IdInput<ItemId>[]) {
92-
this.forEach(tab, {
93-
removals: [{ type: 'items_match', items: items.map(encodeId) }],
94-
})
110+
remove(
111+
tab: IdInput<CreativeModeTabId> | IdInput<CreativeModeTabId>[],
112+
items: IdInput<ItemId>[],
113+
options: TabOptions = {}
114+
) {
115+
this.forEach(
116+
tab,
117+
{
118+
removals: [{ type: 'items_match', items: items.map(encodeId) }],
119+
},
120+
options
121+
)
95122
}
96123

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-
})
124+
add(
125+
tab: IdInput<CreativeModeTabId> | IdInput<CreativeModeTabId>[],
126+
items: IdInput<ItemId>[],
127+
options: AddOptions = {}
128+
) {
129+
this.forEach(
130+
tab,
131+
{
132+
additions: [{ items: items.map(encodeId), ...translateOptions(options) }],
133+
},
134+
options
135+
)
101136
}
102137

103138
create(id: IdInput) {
@@ -109,14 +144,16 @@ export default class PolytoneTabsEmitter implements PolytoneTabs, ClearableEmitt
109144

110145
private forEach(
111146
tab: IdInput<CreativeModeTabId> | IdInput<CreativeModeTabId>[],
112-
modifier: Omit<PolytoneTabModifier, 'targets'>
147+
modifier: Omit<PolytoneTabModifier, 'targets'>,
148+
options: TabOptions
113149
) {
114150
arrayOrSelf(tab).forEach(target => {
151+
const file = options.file ?? target
115152
const entry: PolytoneTabModifier = {
116153
targets: [encodeId(target)],
117154
...modifier,
118155
}
119-
this.entries.merge(target, entry, mergeModifiers)
156+
this.entries.merge(file, entry, mergeModifiers)
120157
})
121158
}
122159
}

test/__snapshots__/polytoneTabs.test.ts.snap

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ exports[`create removal entries > resolves filter correctly > removal modifier 2
2424
"
2525
`;
2626

27+
exports[`create removal entries > uses custom file name > removal modifier with custom file name 1`] = `
28+
"{
29+
"targets": ["something:test_tab"],
30+
"removals": [{ "type": "items_match", "items": ["minecraft:diamond"] }]
31+
}
32+
"
33+
`;
34+
2735
exports[`creates addition entries > adds after predicate > addition modifier with after predicate 1`] = `
2836
"{
2937
"targets": ["example:tab"],
@@ -67,3 +75,11 @@ exports[`creates addition entries > emits per tab key > addition modifier 2 1`]
6775
}
6876
"
6977
`;
78+
79+
exports[`creates addition entries > uses custom file name > removal modifier with custom file name 1`] = `
80+
"{
81+
"targets": ["something:test_tab"],
82+
"additions": [{ "items": ["minecraft:diamond"] }]
83+
}
84+
"
85+
`;

test/polytoneTabs.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,27 @@ describe('creates addition entries', () => {
4747
'addition modifier with after predicate'
4848
)
4949
})
50+
51+
it('uses custom file name', async () => {
52+
const acceptor = createTestAcceptor()
53+
54+
loader.tabs.add('something:test_tab', ['minecraft:diamond'], { file: 'other:id' })
55+
56+
await loader.emit(acceptor)
57+
58+
expect(acceptor.at('assets/something/polytone/creative_tab_modifiers/test_tab.json')).toBeNull()
59+
expect(acceptor.at('assets/other/polytone/creative_tab_modifiers/id.json')).toMatchSnapshot(
60+
'removal modifier with custom file name'
61+
)
62+
})
63+
64+
it('fails trying to merge modifiers with different targets', () => {
65+
loader.tabs.add('something:test_tab', ['minecraft:diamond'], { file: 'other:id' })
66+
67+
expect(() =>
68+
loader.tabs.add('something:another_tab', ['minecraft:diamond_chestplate'], { file: 'other:id' })
69+
).toThrow('trying to merge modifiers with different targets')
70+
})
5071
})
5172

5273
describe('create removal entries', () => {
@@ -67,6 +88,19 @@ describe('create removal entries', () => {
6788
'removal modifier 1'
6889
)
6990
})
91+
92+
it('uses custom file name', async () => {
93+
const acceptor = createTestAcceptor()
94+
95+
loader.tabs.remove('something:test_tab', ['minecraft:diamond'], { file: 'other:id' })
96+
97+
await loader.emit(acceptor)
98+
99+
expect(acceptor.at('assets/something/polytone/creative_tab_modifiers/test_tab.json')).toBeNull()
100+
expect(acceptor.at('assets/other/polytone/creative_tab_modifiers/id.json')).toMatchSnapshot(
101+
'removal modifier with custom file name'
102+
)
103+
})
70104
})
71105

72106
describe('create new tabs', () => {

0 commit comments

Comments
 (0)