Skip to content

Commit 4af8e7d

Browse files
authored
0.1.1 (#3)
* Remove hide props * Update default format version * Fix format version type * Add animations * Add animation controllers * Add animations accessor
1 parent c153a5b commit 4af8e7d

File tree

7 files changed

+142
-9
lines changed

7 files changed

+142
-9
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { Project } from "../../core/Project.ts";
2+
import { AddonFile } from "../AddonFile.ts";
3+
import { StringOrRecord } from "../common/StringOrRecord.ts";
4+
5+
export class AnimationControllers extends AddonFile {
6+
#data: AnimationControllersSchema;
7+
constructor(fileName: string) {
8+
super(fileName);
9+
this.#data = {
10+
format_version: "1.10.0",
11+
animation_controllers: {},
12+
};
13+
14+
Project.onSave(({ writeBP }) => {
15+
writeBP(`animation_controllers/${this.fileName}.json`, this.#data);
16+
});
17+
}
18+
19+
/**
20+
* @param id The id of the animation controller, without the `controller.animation.` prefix
21+
*/
22+
add(id: string, controller?: AnimationController) {
23+
controller ??= {
24+
states: {},
25+
};
26+
return this.#data.animation_controllers[`controller.animation.${id}`] = controller;
27+
}
28+
29+
/**
30+
* @param id The id of the animation controller, without the `controller.animation.` prefix
31+
*/
32+
get(id: string) {
33+
return this.#data.animation_controllers[`controller.animation.${id}`];
34+
}
35+
36+
/**
37+
* @param id The id of the animation controller, without the `controller.animation.` prefix
38+
*/
39+
delete(id: string) {
40+
delete this.#data.animation_controllers[`controller.animation.${id}`];
41+
}
42+
}
43+
44+
export interface AnimationControllersSchema {
45+
format_version: string;
46+
animation_controllers: Record<string, AnimationController>;
47+
}
48+
49+
export interface AnimationController {
50+
initial_state?: string;
51+
states: Record<string, AnimationControllerState>;
52+
}
53+
54+
export interface AnimationControllerState {
55+
animations?: StringOrRecord[];
56+
on_entry?: string[];
57+
on_exit?: string[];
58+
transitions?: Record<string, string>[];
59+
}

lib/bedrock/animations/Animations.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { Project } from "../../core/Project.ts";
2+
import { AddonFile } from "../AddonFile.ts";
3+
4+
export class Animations extends AddonFile {
5+
#data: AnimationsSchema;
6+
/**
7+
* @param fileName File name without extension
8+
*/
9+
constructor(fileName: string) {
10+
super(fileName);
11+
this.#data = {
12+
format_version: "1.10.0",
13+
animations: {},
14+
};
15+
16+
Project.onSave(({ writeBP }) => {
17+
writeBP(`animations/${this.fileName}.json`, this.#data);
18+
});
19+
}
20+
21+
/**
22+
* @param id The id of the animation, without the `animation.` prefix
23+
*/
24+
add(id: string, animation?: Animation) {
25+
animation ??= {
26+
timeline: {},
27+
};
28+
return this.#data.animations[`animation.${id}`] = animation;
29+
}
30+
31+
/**
32+
* @param id The id of the animation, without the `animation.` prefix
33+
*/
34+
get(id: string) {
35+
return this.#data.animations[`animation.${id}`];
36+
}
37+
38+
/**
39+
* @param id The id of the animation, without the `animation.` prefix
40+
*/
41+
delete(id: string) {
42+
delete this.#data.animations[`animation.${id}`];
43+
}
44+
}
45+
46+
export interface AnimationsSchema {
47+
format_version: string;
48+
animations: Record<string, Animation>;
49+
}
50+
51+
export interface Animation {
52+
anim_time_update?: string | number;
53+
animation_length?: number;
54+
loop?: boolean;
55+
loop_delay?: string | number;
56+
start_delay?: string | number;
57+
timeline: Record<number, string[]>;
58+
}

lib/bedrock/attachable/Attachable.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ export class Attachable extends IdentifierAddonFile {
9191
this.#attachable.scripts = value;
9292
}
9393

94+
get animations() {
95+
return this.#attachable.animations;
96+
}
97+
98+
set animations(value) {
99+
this.#attachable.animations = value;
100+
}
101+
94102
addAnimation(key: string, animation: string, condition?: string | true) {
95103
addAnimation(this.#attachable, key, animation, condition);
96104
}
@@ -127,22 +135,22 @@ export class Attachable extends IdentifierAddonFile {
127135
}
128136

129137
export interface AttachableSchema {
130-
format_version: "1.10.0";
138+
format_version: string;
131139
"minecraft:attachable": {
132140
description: {
133141
identifier: string;
134142
item?: ItemIdentifier;
135143
materials?: Record<string, string>;
136144
textures?: Record<string, string>;
137145
geometry?: Record<string, string>;
138-
animations?: Record<string, string>;
139146
scripts?: {
140147
animate?: StringOrRecord[];
141148
parent_setup?: string;
142149
pre_animation?: string[];
143150
should_update_bones_and_effects_offscreen?: string | number | boolean;
144151
should_update_effects_offscreen?: string | number | boolean;
145152
};
153+
animations?: Record<string, string>;
146154
render_controllers?: StringOrRecord[];
147155
queryable_geometry?: string;
148156
};

lib/bedrock/entity/Entity.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import { IdentifierAddonFile } from "../AddonFile.ts";
33
import { EntityBehavior } from "./EntityBehavior.ts";
44
import { EntityResource } from "./EntityResource.ts";
55

6-
type HideProps = "identifier" | "fileName";
7-
86
export class Entity extends IdentifierAddonFile {
97
#behavior: EntityBehavior;
108
#resource: EntityResource;
@@ -40,11 +38,11 @@ export class Entity extends IdentifierAddonFile {
4038
this.#resource.fileName = value;
4139
}
4240

43-
get behavior(): Omit<EntityBehavior, HideProps> {
41+
get behavior(): EntityBehavior {
4442
return this.#behavior;
4543
}
4644

47-
get resource(): Omit<EntityResource, HideProps> {
45+
get resource(): EntityResource {
4846
return this.#resource;
4947
}
5048
}

lib/bedrock/entity/EntityBehavior.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class EntityBehavior extends IdentifierAddonFile {
1313
constructor(identifier: string, dir?: string) {
1414
super(identifier, dir);
1515
this.#data = {
16-
format_version: "1.16.0",
16+
format_version: "1.19.80",
1717
"minecraft:entity": {
1818
description: {
1919
identifier,

lib/bedrock/entity/EntityResource.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ export class EntityResource extends IdentifierAddonFile {
9090
this.#entity.scripts = value;
9191
}
9292

93+
get animations() {
94+
return this.#entity.animations;
95+
}
96+
97+
set animations(value) {
98+
this.#entity.animations = value;
99+
}
100+
93101
addAnimation(key: string, animation: string, condition?: string | true) {
94102
addAnimation(this.#entity, key, animation, condition);
95103
}
@@ -178,14 +186,13 @@ export class EntityResource extends IdentifierAddonFile {
178186
}
179187

180188
export interface EntityResourceSchema {
181-
format_version: "1.10.0";
189+
format_version: string;
182190
"minecraft:client_entity": {
183191
description: {
184192
identifier: string;
185193
materials?: Record<string, string>;
186194
textures?: Record<string, string>;
187195
geometry?: Record<string, string>;
188-
animations?: Record<string, string>;
189196
scripts?: {
190197
animate?: StringOrRecord[];
191198
initialize?: string[];
@@ -195,6 +202,7 @@ export interface EntityResourceSchema {
195202
should_update_effects_offscreen?: string | number | boolean;
196203
variables?: Record<string, "public">;
197204
};
205+
animations?: Record<string, string>;
198206
particle_effects?: Record<string, string>;
199207
particle_emitters?: Record<string, string>;
200208
sound_effects?: Record<string, string>;

mod.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export * from "./lib/bedrock/common/RawText.ts";
77
export * from "./lib/bedrock/common/SpellEffects.ts";
88
export * from "./lib/bedrock/common/query.ts";
99

10+
export * from "./lib/bedrock/animation_controllers/AnimationControllers.ts";
11+
export * from "./lib/bedrock/animations/Animations.ts";
1012
export * from "./lib/bedrock/attachable/Attachable.ts";
1113
export * from "./lib/bedrock/entity/Entity.ts";
1214
export * from "./lib/bedrock/entity/EntityBehavior.ts";

0 commit comments

Comments
 (0)