Skip to content

Commit bb8f7a2

Browse files
committed
✨ Add ObjectProperty class to moddingTools.ts
1 parent fd8e4ab commit bb8f7a2

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/util/moddingTools.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,35 @@ export function createPropertySubscribable<Value = any>(object: any, key: string
260260
// ) {
261261
// //
262262
// }
263+
264+
/**
265+
* A wrapper for the Blockbench.Property class that deep-clones the property value when copying or merging.
266+
*/
267+
export class ObjectProperty extends Property<'object'> {
268+
constructor(target: any, name: string, options: PropertyOptions) {
269+
super(target, 'object', name, options)
270+
}
271+
272+
copy(instance: any, target: any) {
273+
console.log('Copying', this.name)
274+
if (instance[this.name] == undefined) {
275+
target[this.name] = instance[this.name]
276+
} else {
277+
target[this.name] = JSON.parse(JSON.stringify(instance[this.name]))
278+
}
279+
}
280+
281+
merge(instance: any, data: any) {
282+
console.log('Merging', this.name, instance[this.name], data[this.name])
283+
if (data[this.name] == undefined) {
284+
instance[this.name] = this.default
285+
} else {
286+
instance[this.name] = JSON.parse(JSON.stringify(data[this.name]))
287+
}
288+
}
289+
}
290+
291+
export const fixClassPropertyInheritance: ClassDecorator = target => {
292+
target.properties = { ...target.properties }
293+
return target
294+
}

0 commit comments

Comments
 (0)