Skip to content

Commit 5781f3f

Browse files
committed
🛠️ Fix #342
1 parent 83acc4f commit 5781f3f

File tree

9 files changed

+99
-104
lines changed

9 files changed

+99
-104
lines changed

src/blueprintFormat.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ export function updateRotationLock() {
673673
!!AnimatedJava.API.TextDisplay.selected.length ||
674674
!!AnimatedJava.API.VanillaItemDisplay.selected.length ||
675675
!!AnimatedJava.API.VanillaBlockDisplay.selected.length ||
676-
!!(OutlinerElement.types.camera?.selected && OutlinerElement.types.camera?.selected.length)
676+
!!(OutlinerElement.types.camera?.selected && OutlinerElement.types.camera?.selected)
677677
)
678678
BLUEPRINT_FORMAT.rotation_snap = BLUEPRINT_FORMAT.rotation_limit
679679
}

src/mods/cameraNameMod.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { isCurrentFormat } from '../blueprintFormat'
2+
import { PACKAGE } from '../constants'
3+
import { sanitizeOutlinerElementName } from '../outliner/util'
4+
import { createBlockbenchMod } from '../util/moddingTools'
5+
6+
createBlockbenchMod(
7+
`${PACKAGE.name}:cameraNameMod`,
8+
{
9+
originalRename: OutlinerElement.types.camera?.prototype.saveName,
10+
originalSanitize: OutlinerElement.types.camera?.prototype.sanitizeName,
11+
},
12+
context => {
13+
if (OutlinerElement.types.camera) {
14+
OutlinerElement.types.camera.prototype.saveName = function (
15+
this: OutlinerElement,
16+
save?: boolean
17+
) {
18+
if (isCurrentFormat()) {
19+
this.name = sanitizeOutlinerElementName(this.name, this.uuid)
20+
}
21+
return context.originalRename.call(this, save)
22+
}
23+
OutlinerElement.types.camera.prototype.sanitizeName = function (this: OutlinerElement) {
24+
if (isCurrentFormat()) {
25+
this.name = sanitizeOutlinerElementName(this.name, this.uuid)
26+
}
27+
return context.originalSanitize.call(this)
28+
}
29+
}
30+
return context
31+
},
32+
context => {
33+
if (OutlinerElement.types.camera) {
34+
OutlinerElement.types.camera.prototype.rename = context.originalRename
35+
}
36+
}
37+
)

src/mods/groupNameMod.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { isCurrentFormat } from '../blueprintFormat'
22
import { PACKAGE } from '../constants'
3-
import { toSafeFuntionName } from '../util/minecraftUtil'
3+
import { sanitizeOutlinerElementName } from '../outliner/util'
44
import { createBlockbenchMod } from '../util/moddingTools'
55

66
createBlockbenchMod(
@@ -12,13 +12,13 @@ createBlockbenchMod(
1212
context => {
1313
Group.prototype.saveName = function (this: Group, save?: boolean) {
1414
if (isCurrentFormat()) {
15-
this.name = toSafeFuntionName(this.name)
15+
this.name = sanitizeOutlinerElementName(this.name, this.uuid)
1616
}
1717
return context.originalRename.call(this, save)
1818
}
1919
Group.prototype.sanitizeName = function (this: Group) {
2020
if (isCurrentFormat()) {
21-
this.name = toSafeFuntionName(this.name)
21+
this.name = sanitizeOutlinerElementName(this.name, this.uuid)
2222
}
2323
return context.originalSanitize.call(this)
2424
}

src/mods/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ import './saveProjectActionMod'
2727
import './saveProjectAsActionMod'
2828
import './showDefaultPoseMod'
2929
import './variantPreviewCubeFaceMod'
30+
import './cameraNameMod'

src/outliner/textDisplay.ts

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { translate } from '../util/translation'
1717
import { ResizableOutlinerElement } from './resizableOutlinerElement'
1818
import { VanillaBlockDisplay } from './vanillaBlockDisplay'
1919
import { VanillaItemDisplay } from './vanillaItemDisplay'
20+
import { sanitizeOutlinerElementName } from './util'
2021

2122
interface TextDisplayOptions {
2223
name?: string
@@ -121,37 +122,8 @@ export class TextDisplay extends ResizableOutlinerElement {
121122
}
122123

123124
public sanitizeName(): string {
124-
this.name = toSafeFuntionName(this.name)
125-
const otherNodes = [
126-
...TextDisplay.all.filter(v => v.uuid !== this.uuid),
127-
...Group.all,
128-
...VanillaBlockDisplay.all,
129-
...VanillaItemDisplay.all,
130-
]
131-
const otherNames = new Set(otherNodes.map(v => v.name))
132-
133-
if (!otherNames.has(this.name)) {
134-
return this.name
135-
}
136-
137-
let i = 1
138-
const match = this.name.match(/\d+$/)
139-
if (match) {
140-
i = parseInt(match[0])
141-
this.name = this.name.slice(0, -match[0].length)
142-
}
143-
144-
let maxTries = 10000
145-
while (maxTries-- > 0) {
146-
const newName = `${this.name}${i}`
147-
if (!otherNames.has(newName)) {
148-
this.name = newName
149-
return newName
150-
}
151-
i++
152-
}
153-
154-
throw new Error('Could not make TextDisplay name unique!')
125+
this.name = sanitizeOutlinerElementName(this.name, this.uuid)
126+
return this.name
155127
}
156128

157129
get text() {

src/outliner/util.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { toSafeFuntionName } from '../util/minecraftUtil'
2+
import { TextDisplay } from './textDisplay'
3+
import { VanillaBlockDisplay } from './vanillaBlockDisplay'
4+
import { VanillaItemDisplay } from './vanillaItemDisplay'
5+
6+
export function sanitizeOutlinerElementName(name: string, elementUUID: string): string {
7+
name = toSafeFuntionName(name)
8+
const otherNodes: OutlinerElement[] = [
9+
...VanillaBlockDisplay.all.filter(v => v.uuid !== elementUUID),
10+
...Group.all,
11+
...TextDisplay.all,
12+
...VanillaItemDisplay.all,
13+
]
14+
if (OutlinerElement.types.camera) {
15+
otherNodes.push(OutlinerElement.types.camera)
16+
}
17+
const otherNames = new Set(otherNodes.map(v => v.name))
18+
19+
if (!otherNames.has(name)) {
20+
return name
21+
}
22+
23+
let i = 1
24+
const match = name.match(/\d+$/)
25+
if (match) {
26+
i = parseInt(match[0])
27+
name = name.slice(0, -match[0].length)
28+
}
29+
30+
let maxTries = 10000
31+
while (maxTries-- > 0) {
32+
const newName = `${name}${i}`
33+
if (!otherNames.has(newName)) {
34+
name = newName
35+
return newName
36+
}
37+
i++
38+
}
39+
40+
throw new Error(`Could not make name unique for ${name} (${elementUUID})!`)
41+
}

src/outliner/vanillaBlockDisplay.ts

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { Valuable } from '../util/stores'
1313
import { translate } from '../util/translation'
1414
import { ResizableOutlinerElement } from './resizableOutlinerElement'
1515
import { TextDisplay } from './textDisplay'
16+
import { sanitizeOutlinerElementName } from './util'
1617
import { VanillaItemDisplay } from './vanillaItemDisplay'
1718

1819
const ERROR_OUTLINE_MATERIAL = Canvas.outlineMaterial.clone()
@@ -116,37 +117,8 @@ export class VanillaBlockDisplay extends ResizableOutlinerElement {
116117
}
117118

118119
public sanitizeName(): string {
119-
this.name = toSafeFuntionName(this.name)
120-
const otherNodes = [
121-
...VanillaBlockDisplay.all.filter(v => v.uuid !== this.uuid),
122-
...Group.all,
123-
...TextDisplay.all,
124-
...VanillaItemDisplay.all,
125-
]
126-
const otherNames = new Set(otherNodes.map(v => v.name))
127-
128-
if (!otherNames.has(this.name)) {
129-
return this.name
130-
}
131-
132-
let i = 1
133-
const match = this.name.match(/\d+$/)
134-
if (match) {
135-
i = parseInt(match[0])
136-
this.name = this.name.slice(0, -match[0].length)
137-
}
138-
139-
let maxTries = 10000
140-
while (maxTries-- > 0) {
141-
const newName = `${this.name}${i}`
142-
if (!otherNames.has(newName)) {
143-
this.name = newName
144-
return newName
145-
}
146-
i++
147-
}
148-
149-
throw new Error('Could not make VanillaBlockDisplay name unique!')
120+
this.name = sanitizeOutlinerElementName(this.name, this.uuid)
121+
return this.name
150122
}
151123

152124
getUndoCopy() {

src/outliner/vanillaItemDisplay.ts

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { Valuable } from '../util/stores'
1212
import { translate } from '../util/translation'
1313
import { ResizableOutlinerElement } from './resizableOutlinerElement'
1414
import { TextDisplay } from './textDisplay'
15+
import { sanitizeOutlinerElementName } from './util'
1516
import { VanillaBlockDisplay } from './vanillaBlockDisplay'
1617

1718
interface VanillaItemDisplayOptions {
@@ -125,37 +126,8 @@ export class VanillaItemDisplay extends ResizableOutlinerElement {
125126
}
126127

127128
public sanitizeName(): string {
128-
this.name = toSafeFuntionName(this.name)
129-
const otherNodes = [
130-
...VanillaItemDisplay.all.filter(v => v.uuid !== this.uuid),
131-
...Group.all,
132-
...TextDisplay.all,
133-
...VanillaBlockDisplay.all,
134-
]
135-
const otherNames = new Set(otherNodes.map(v => v.name))
136-
137-
if (!otherNames.has(this.name)) {
138-
return this.name
139-
}
140-
141-
let i = 1
142-
const match = this.name.match(/\d+$/)
143-
if (match) {
144-
i = parseInt(match[0])
145-
this.name = this.name.slice(0, -match[0].length)
146-
}
147-
148-
let maxTries = 10000
149-
while (maxTries-- > 0) {
150-
const newName = `${this.name}${i}`
151-
if (!otherNames.has(newName)) {
152-
this.name = newName
153-
return newName
154-
}
155-
i++
156-
}
157-
158-
throw new Error('Could not make VanillaItemDisplay name unique!')
129+
this.name = sanitizeOutlinerElementName(this.name, this.uuid)
130+
return this.name
159131
}
160132

161133
getUndoCopy() {

test_blueprints/block_display.ajblueprint

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,27 @@
4141
},
4242
"elements": [
4343
{
44-
"name": "block_display",
45-
"position": [-8, 0, -8],
44+
"name": "block_display1",
45+
"position": [0, 0, 0],
4646
"rotation": [0, 0, 0],
4747
"scale": [1, 1, 1],
4848
"visibility": true,
49+
"block": "minecraft:cobblestone_wall",
50+
"config": {},
4951
"item": "minecraft:diamond",
5052
"item_display": "none",
51-
"config": {},
52-
"block": "cobblestone_wall[west=none]",
5353
"text": "\"Hello World!\"",
5454
"lineWidth": 200,
5555
"backgroundColor": "#000000",
5656
"backgroundAlpha": 0.25,
5757
"align": "center",
5858
"shadow": false,
5959
"seeThrough": false,
60-
"uuid": "31411dd2-b8c3-79b4-8437-db3c8c27da55",
60+
"uuid": "31d45ef8-ad50-c3aa-10fd-184027b767da",
6161
"type": "animated_java:vanilla_block_display"
6262
}
6363
],
64-
"outliner": ["31411dd2-b8c3-79b4-8437-db3c8c27da55"],
64+
"outliner": ["31d45ef8-ad50-c3aa-10fd-184027b767da"],
6565
"textures": [
6666
{
6767
"path": "D:\\Dropbox\\Pictures\\memes\\images.png",
@@ -96,7 +96,7 @@
9696
"default": {
9797
"name": "default",
9898
"display_name": "Default",
99-
"uuid": "5730fca2-be35-b294-f099-094000c29544",
99+
"uuid": "291e925e-0d30-297a-dd08-40e0dd69ec24",
100100
"texture_map": {},
101101
"excluded_nodes": [],
102102
"is_default": true

0 commit comments

Comments
 (0)