Skip to content

Commit d436ddb

Browse files
committed
✨ Improve display entity actions
1 parent 39db659 commit d436ddb

6 files changed

Lines changed: 127 additions & 131 deletions

File tree

src/dialogs/displayEntityConfig/displayEntityConfig.ts

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export const DISPLAY_ENTITY_CONFIG_ACTION = registerDeletableHandlerPatch({
124124
'animated_java:action/paste-display-entity-config',
125125
],
126126
create() {
127-
return new Blockbench.Action(`animated_java:action/open-display-entity-config`, {
127+
const action = new Blockbench.Action(`animated_java:action/open-display-entity-config`, {
128128
icon: 'settings',
129129
name: translate('action.open_display_entity_config.name'),
130130
condition: activeProjectIsBlueprintFormat,
@@ -139,38 +139,38 @@ export const DISPLAY_ENTITY_CONFIG_ACTION = registerDeletableHandlerPatch({
139139
}
140140
},
141141
})
142-
},
143-
})
144142

145-
DISPLAY_ENTITY_CONFIG_ACTION.onCreated(action => {
146-
const copyAction = COPY_DISPLAY_ENTITY_CONFIG_ACTION.get()
147-
if (!copyAction) {
148-
console.error('Copy display entity config action not registered')
149-
return
150-
}
151-
const pasteAction = PASTE_DISPLAY_ENTITY_CONFIG_ACTION.get()
152-
if (!pasteAction) {
153-
console.error('Paste display entity config action not registered')
154-
return
155-
}
143+
const copyAction = COPY_DISPLAY_ENTITY_CONFIG_ACTION.get()
144+
if (!copyAction) {
145+
console.error('Copy display entity config action not registered')
146+
return
147+
}
148+
const pasteAction = PASTE_DISPLAY_ENTITY_CONFIG_ACTION.get()
149+
if (!pasteAction) {
150+
console.error('Paste display entity config action not registered')
151+
return
152+
}
156153

157-
Group.prototype.menu!.structure.splice(6, 0, '_')
158-
Group.prototype.menu!.addAction(action, 7)
159-
Group.prototype.menu!.addAction(copyAction, 8)
160-
Group.prototype.menu!.addAction(pasteAction, 9)
154+
Group.prototype.menu!.structure.splice(6, 0, '_')
155+
Group.prototype.menu!.addAction(action, 7)
156+
Group.prototype.menu!.addAction(copyAction, 8)
157+
Group.prototype.menu!.addAction(pasteAction, 9)
161158

162-
const displayEntityMenu = new Menu([
163-
...Outliner.control_menu_group,
164-
'_',
165-
action,
166-
copyAction,
167-
pasteAction,
168-
'_',
169-
'rename',
170-
'delete',
171-
])
159+
const displayEntityMenu = new Menu([
160+
...Outliner.control_menu_group,
161+
'_',
162+
action,
163+
copyAction,
164+
pasteAction,
165+
'_',
166+
'rename',
167+
'delete',
168+
])
172169

173-
TextDisplay.prototype.menu = displayEntityMenu
174-
VanillaItemDisplay.prototype.menu = displayEntityMenu
175-
VanillaBlockDisplay.prototype.menu = displayEntityMenu
170+
TextDisplay.prototype.menu = displayEntityMenu
171+
VanillaItemDisplay.prototype.menu = displayEntityMenu
172+
VanillaBlockDisplay.prototype.menu = displayEntityMenu
173+
174+
return action
175+
},
176176
})

src/lang/en.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ animated_java:
3939
confirm: Confirm Extraction
4040
create_text_display:
4141
title: Add Text Display
42-
create_vanilla_item_display:
42+
create_item_display:
4343
title: Add Item Display
44-
create_vanilla_block_display:
44+
create_block_display:
4545
title: Add Block Display
4646
copy_display_entity_config:
4747
name: Copy Display Entity Config

src/mods/outlinerToolbar.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/outliner/textDisplay.ts

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { registerDeletableHandlerPatch } from 'blockbench-patch-manager'
1+
import { registerDeletableHandlerPatch, registerPatch } from 'blockbench-patch-manager'
22
import { TextComponent, TextComponentParser, type TextElement } from 'book-and-quill'
33
import { observable } from 'svelte-observable-store'
44
import { PACKAGE } from '../constants'
@@ -463,7 +463,7 @@ TextDisplay.animator = TextDisplayAnimator as any
463463
export const CREATE_ACTION = registerDeletableHandlerPatch({
464464
id: `animated_java:action/create-text-display`,
465465
create() {
466-
return new Blockbench.Action(`animated_java:action/create-text-display`, {
466+
const action = new Blockbench.Action(`animated_java:action/create-text-display`, {
467467
name: translate('action.create_text_display.title'),
468468
icon: 'text_fields',
469469
category: 'animated_java',
@@ -496,36 +496,39 @@ export const CREATE_ACTION = registerDeletableHandlerPatch({
496496
return textDisplay
497497
},
498498
})
499+
500+
// @ts-expect-error - Broken BB types
501+
BarItems.add_element.side_menu.addAction(action, 3)
502+
503+
return action
499504
},
500505
})
501506

502-
const CLEANUP_CALLBACKS: Array<() => void> = []
503-
504-
CREATE_ACTION.onCreated(action => {
505-
Interface.Panels.outliner.menu.addAction(action, 3)
506-
Toolbars.outliner.add(action, 0)
507-
MenuBar.menus.edit.addAction(action, 8)
508-
509-
CLEANUP_CALLBACKS.push(
510-
EVENTS.SELECT_PROJECT.subscribe(project => {
511-
if (!activeProjectIsBlueprintFormat()) return
512-
project.textDisplays ??= []
513-
TextDisplay.all.empty()
514-
TextDisplay.all.push(...project.textDisplays)
515-
}),
516-
517-
EVENTS.UNSELECT_PROJECT.subscribe(project => {
518-
if (!activeProjectIsBlueprintFormat()) return
519-
project.textDisplays = [...TextDisplay.all]
520-
TextDisplay.all.empty()
521-
})
522-
)
523-
})
507+
registerPatch({
508+
id: `animated_java:text-display-project-sync`,
509+
510+
apply: () => {
511+
const callbacks: Array<() => void> = []
524512

525-
CREATE_ACTION.onDeleted(action => {
526-
Interface.Panels.outliner.menu.removeAction(action)
527-
Toolbars.outliner.remove(action)
528-
MenuBar.menus.edit.removeAction(action)
513+
callbacks.push(
514+
EVENTS.SELECT_PROJECT.subscribe(project => {
515+
if (!activeProjectIsBlueprintFormat()) return
516+
project.textDisplays ??= []
517+
TextDisplay.all.empty()
518+
TextDisplay.all.push(...project.textDisplays)
519+
}),
529520

530-
CLEANUP_CALLBACKS.forEach(unsub => unsub())
521+
EVENTS.UNSELECT_PROJECT.subscribe(project => {
522+
if (!activeProjectIsBlueprintFormat()) return
523+
project.textDisplays = [...TextDisplay.all]
524+
TextDisplay.all.empty()
525+
})
526+
)
527+
return { callbacks }
528+
},
529+
revert: ({ callbacks }) => {
530+
// @ts-expect-error - Broken BB types
531+
BarItems.add_element.side_menu.removeAction(CREATE_ACTION)
532+
callbacks.forEach(unsub => unsub())
533+
},
531534
})

src/outliner/vanillaBlockDisplay.ts

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { registerDeletableHandlerPatch } from 'blockbench-patch-manager'
1+
import { registerDeletableHandlerPatch, registerPatch } from 'blockbench-patch-manager'
22
import { observable } from 'svelte-observable-store'
33
import { PACKAGE } from '../constants'
44
import { activeProjectIsBlueprintFormat } from '../formats/blueprint'
@@ -377,7 +377,7 @@ VanillaBlockDisplay.animator = VanillaBlockDisplayAnimator as any
377377
export const CREATE_ACTION = registerDeletableHandlerPatch({
378378
id: `animated_java:action/create-block-display`,
379379
create() {
380-
return new Blockbench.Action(`animated_java:action/create-block-display`, {
380+
const action = new Blockbench.Action(`animated_java:action/create-block-display`, {
381381
name: translate('action.create_block_display.title'),
382382
icon: 'deployed_code',
383383
category: 'animated_java',
@@ -401,7 +401,7 @@ export const CREATE_ACTION = registerDeletableHandlerPatch({
401401
Group.first_selected?.unselect()
402402
vanillaBlockDisplay.select()
403403

404-
Undo.finishEdit('Create Vanilla Block Display', {
404+
Undo.finishEdit('Create Block Display', {
405405
outliner: true,
406406
elements: selected,
407407
selection: true,
@@ -410,36 +410,41 @@ export const CREATE_ACTION = registerDeletableHandlerPatch({
410410
return vanillaBlockDisplay
411411
},
412412
})
413+
414+
// @ts-expect-error - Broken BB types
415+
BarItems.add_element.side_menu.addAction(action, 3)
416+
417+
return action
413418
},
414419
})
415420

416-
const CLEANUP_CALLBACKS: Array<() => void> = []
421+
registerPatch({
422+
id: `animated_java:block-display-project-sync`,
417423

418-
CREATE_ACTION.onCreated(action => {
419-
Interface.Panels.outliner.menu.addAction(action, 3)
420-
Toolbars.outliner.add(action, 0)
421-
MenuBar.menus.edit.addAction(action, 8)
424+
apply: () => {
425+
const callbacks: Array<() => void> = []
422426

423-
CLEANUP_CALLBACKS.push(
424-
EVENTS.SELECT_PROJECT.subscribe(project => {
425-
project.vanillaBlockDisplays ??= []
426-
VanillaBlockDisplay.all.empty()
427-
VanillaBlockDisplay.all.push(...project.vanillaBlockDisplays)
428-
}),
427+
callbacks.push(
428+
EVENTS.SELECT_PROJECT.subscribe(project => {
429+
project.vanillaBlockDisplays ??= []
430+
VanillaBlockDisplay.all.empty()
431+
VanillaBlockDisplay.all.push(...project.vanillaBlockDisplays)
432+
}),
429433

430-
EVENTS.UNSELECT_PROJECT.subscribe(project => {
431-
project.vanillaBlockDisplays = [...VanillaBlockDisplay.all]
432-
VanillaBlockDisplay.all.empty()
433-
})
434-
)
435-
})
434+
EVENTS.UNSELECT_PROJECT.subscribe(project => {
435+
project.vanillaBlockDisplays = [...VanillaBlockDisplay.all]
436+
VanillaBlockDisplay.all.empty()
437+
})
438+
)
439+
return { callbacks }
440+
},
436441

437-
CREATE_ACTION.onDeleted(action => {
438-
Interface.Panels.outliner.menu.removeAction(action)
439-
Toolbars.outliner.remove(action)
440-
MenuBar.menus.edit.removeAction(action)
442+
revert: ({ callbacks }) => {
443+
// @ts-expect-error - Broken BB types
444+
BarItems.add_element.side_menu.removeAction(`animated_java:action/create-block-display`)
441445

442-
CLEANUP_CALLBACKS.forEach(unsub => unsub())
446+
callbacks.forEach(unsub => unsub())
447+
},
443448
})
444449

445450
// export function debugBlocks() {

src/outliner/vanillaItemDisplay.ts

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { registerDeletableHandlerPatch } from 'blockbench-patch-manager'
1+
import { registerDeletableHandlerPatch, registerPatch } from 'blockbench-patch-manager'
22
import { observable } from 'svelte-observable-store'
33
import { PACKAGE } from '../constants'
44
import { activeProjectIsBlueprintFormat } from '../formats/blueprint/index'
@@ -372,10 +372,10 @@ VanillaItemDisplayAnimator.prototype.type = VanillaItemDisplay.type
372372
VanillaItemDisplay.animator = VanillaItemDisplayAnimator as any
373373

374374
export const CREATE_ACTION = registerDeletableHandlerPatch({
375-
id: `animated_java:action/create-vanilla-item-display`,
375+
id: `animated_java:action/create-item-display`,
376376
create() {
377-
return new Blockbench.Action(`animated_java:action/create-item-display`, {
378-
name: translate('action.create_vanilla_item_display.title'),
377+
const action = new Blockbench.Action(`animated_java:action/create-item-display`, {
378+
name: translate('action.create_item_display.title'),
379379
icon: 'icecream',
380380
category: 'animated_java',
381381
condition() {
@@ -398,7 +398,7 @@ export const CREATE_ACTION = registerDeletableHandlerPatch({
398398
Group.first_selected?.unselect()
399399
vanillaItemDisplay.select()
400400

401-
Undo.finishEdit('Create Vanilla Item Display', {
401+
Undo.finishEdit('Create Item Display', {
402402
outliner: true,
403403
elements: selected,
404404
selection: true,
@@ -407,35 +407,39 @@ export const CREATE_ACTION = registerDeletableHandlerPatch({
407407
return vanillaItemDisplay
408408
},
409409
})
410+
411+
// @ts-expect-error - Broken BB types
412+
BarItems.add_element.side_menu.addAction(action, 3)
413+
414+
return action
410415
},
411416
})
412417

413-
const CLEANUP_CALLBACKS: Array<() => void> = []
418+
registerPatch({
419+
id: `animated_java:item-display-project-sync`,
414420

415-
CREATE_ACTION.onCreated(action => {
416-
Interface.Panels.outliner.menu.addAction(action, 3)
417-
Toolbars.outliner.add(action, 0)
418-
MenuBar.menus.edit.addAction(action, 8)
421+
apply: () => {
422+
const callbacks: Array<() => void> = []
419423

420-
CLEANUP_CALLBACKS.push(
421-
EVENTS.SELECT_PROJECT.subscribe(project => {
422-
project.vanillaItemDisplays ??= []
423-
VanillaItemDisplay.all.empty()
424-
VanillaItemDisplay.all.push(...project.vanillaItemDisplays)
425-
}),
424+
callbacks.push(
425+
EVENTS.SELECT_PROJECT.subscribe(project => {
426+
project.vanillaItemDisplays ??= []
427+
VanillaItemDisplay.all.empty()
428+
VanillaItemDisplay.all.push(...project.vanillaItemDisplays)
429+
}),
426430

427-
EVENTS.UNSELECT_PROJECT.subscribe(project => {
428-
project.vanillaItemDisplays = [...VanillaItemDisplay.all]
429-
VanillaItemDisplay.all.empty()
430-
})
431-
)
432-
})
431+
EVENTS.UNSELECT_PROJECT.subscribe(project => {
432+
project.vanillaItemDisplays = [...VanillaItemDisplay.all]
433+
VanillaItemDisplay.all.empty()
434+
})
435+
)
436+
return { callbacks }
437+
},
433438

434-
CREATE_ACTION.onDeleted(action => {
435-
Interface.Panels.outliner.menu.removeAction(action)
436-
Toolbars.outliner.remove(action)
437-
MenuBar.menus.edit.removeAction(action)
439+
revert: ({ callbacks }) => {
440+
// @ts-expect-error - Broken BB types
441+
BarItems.add_element.side_menu.removeAction(`animated_java:action/create-item-display`)
438442

439-
CLEANUP_CALLBACKS.forEach(unsub => unsub())
440-
CLEANUP_CALLBACKS.empty()
443+
callbacks.forEach(unsub => unsub())
444+
},
441445
})

0 commit comments

Comments
 (0)