@@ -6,6 +6,16 @@ import RegistryLookup from '../loader/registry/index.js'
66import CustomEmitter from './custom.js'
77import { 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+
919interface 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
3748interface 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+
5785function 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