forked from JannisX11/blockbench-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatagen_export.js
More file actions
287 lines (278 loc) · 8.92 KB
/
Copy pathdatagen_export.js
File metadata and controls
287 lines (278 loc) · 8.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
(function () {
var codec = new Codec("datagen", {
name: "Datagen",
remember: true,
extension: "java",
compile(options) {
if (options === undefined) options = {};
let clear_elements = [];
let textures_used = [];
let overflow_cubes = [];
function computeCube(s) {
if (s.export == false) return;
//Create Element
let text = ".element()";
let element = {};
element.from = s.from.slice();
element.to = s.to.slice();
if (s.inflate) {
for (let i = 0; i < 3; i++) {
element.from[i] -= s.inflate;
element.to[i] += s.inflate;
}
}
text += `.from(${element.from[0]}F, ${element.from[1]}F, ${element.from[2]}F)`;
text += `.to(${element.to[0]}F, ${element.to[1]}F, ${element.to[2]}F)`;
if (s.shade === false) {
text += `.shade(false)`;
}
let nonOrigin = !s.rotation.allEqual(0) || !s.origin.allEqual(8);
let axis = (nonOrigin ? s.rotationAxis() : s.rotation_axis) || "y";
text += `.rotation().angle(${
nonOrigin ? getAxisNumber(axis) : 0
}F).axis(Axis.${axis.toUpperCase()}).origin(${s.origin[0]}F, ${
s.origin[1]
}F, ${s.origin[2]}F)`;
if (s.rescale) {
text += `.rescale(true)`;
}
text += `.end()`;
let faces = "";
for (let face in s.faces) {
if (s.faces.hasOwnProperty(face)) {
if (s.faces[face].texture !== null) {
faces += `.face(Direction.${face.toUpperCase()})`;
if (s.faces[face].enabled !== false) {
let temp = {};
temp.uv = s.faces[face].uv.slice();
temp.uv.forEach((n, i) => {
temp.uv[i] = (n * 16) / UVEditor.getResolution(i % 2);
});
faces += `.uvs(${temp.uv[0]}F, ${temp.uv[1]}F, ${temp.uv[2]}F, ${temp.uv[3]}F)`;
}
if (s.faces[face].rotation) {
let rotString = "";
switch (s.faces[face].rotation) {
case 0:
rotString = "ZERO";
break;
case 90:
rotString = "CLOCKWISE_90";
break;
case 180:
rotString = "UPSIDE_DOWN";
break;
case 270:
rotString = "COUNTERCLOCKWISE_90";
break;
default:
rotString = "ZERO";
break;
}
faces += `.rotation(FaceRotation.${rotString})`;
}
if (s.faces[face].texture) {
let tex = s.faces[face].getTexture();
let texOut = "";
if (tex) {
texOut = "#" + tex.id;
textures_used.safePush(tex);
} else {
texOut = "#missing";
}
faces += `.texture("${texOut}")`;
}
if (s.faces[face].cullface) {
faces += `.cullface(Direction.${s.faces[
face
].cullface.toUpperCase()})`;
}
if (s.faces[face].tint >= 0) {
faces += `.tintindex(${s.faces[face].tint})`;
}
faces += `.end()`;
}
}
}
text += faces;
text += `.end()`;
function inVd(n) {
return n < -16 || n > 32;
}
if (
inVd(element.from[0]) ||
inVd(element.from[1]) ||
inVd(element.from[2]) ||
inVd(element.to[0]) ||
inVd(element.to[1]) ||
inVd(element.to[2])
) {
overflow_cubes.push(s);
}
if (Object.keys(s.faces).length) {
clear_elements.push(text);
}
}
function iterate(arr) {
let i = 0;
if (!arr || !arr.length) {
return;
}
for (i = 0; i < arr.length; i++) {
if (arr[i].type === "cube") {
computeCube(arr[i]);
} else if (arr[i].type === "group") {
iterate(arr[i].children);
}
}
}
iterate(Outliner.root);
function checkExport(key, condition) {
key = options[key];
if (key === undefined) {
return condition;
} else {
return key;
}
}
let isTexturesOnlyModel =
clear_elements.length === 0 &&
checkExport("parent", Project.parent != "");
let texturesObj = {};
textures.forEach(function (t, i) {
let link = t.javaTextureLink();
if (t.particle) {
texturesObj.particle = link;
}
if (!textures_used.includes(t) && !isTexturesOnlyModel) return;
if (t.id !== link.replace(/^#/, "")) {
texturesObj[t.id] = link;
}
});
if (
options.prevent_dialog !== true &&
overflow_cubes.length > 0 &&
settings.dialog_larger_cubes.value
) {
Blockbench.showMessageBox(
{
translateKey: "model_clipping",
icon: "settings_overscan",
message: tl("message.model_clipping.message", [
overflow_cubes.length,
]),
buttons: ["dialog.scale.select_overflow", "dialog.ok"],
confirm: 1,
cancel: 1,
},
(result) => {
if (result == 0) {
selected.splice(0, Infinity, ...overflow_cubes);
updateSelection();
}
}
);
}
if (
options.prevent_dialog !== true &&
clear_elements.length &&
["item/generated", "item/handheld"].includes(Project.parent)
) {
Blockbench.showMessageBox({
translateKey: "invalid_builtin_parent",
icon: "info",
message: tl("message.invalid_builtin_parent.message", [
Project.parent,
]),
});
Project.parent = "";
}
let text = `this.models()`;
if (checkExport("parent", Project.parent != "")) {
text += `.withExistingParent("blockbench_export", "${Project.parent.replace(
" ",
""
)}")`;
} else {
text += `.getBuilder("blockbench_export")`;
}
if (checkExport("ambientocclusion", Project.ambientocclusion === false)) {
text += ".ao(false)";
}
if (checkExport("textures", Object.keys(texturesObj).length >= 1)) {
for (let texture of Object.keys(texturesObj)) {
text += `.texture("${texture}", new ResourceLocation("${texturesObj[texture]}"))`;
}
}
if (checkExport("elements", clear_elements.length >= 1)) {
for (let element of clear_elements) {
text += element;
}
}
if (checkExport("front_gui_light", Project.front_gui_light)) {
text += ".guiLight(GuiLight.FRONT)";
}
if (checkExport("display", Object.keys(display).length >= 1)) {
let new_display = {};
let entries = 0;
for (let i in DisplayMode.slots) {
let key = DisplayMode.slots[i];
if (
DisplayMode.slots.hasOwnProperty(i) &&
display[key] &&
display[key].export
) {
new_display[key] = display[key].export();
entries++;
}
}
if (entries) {
for (let key of Object.keys(new_display)) {
let val = new_display[key];
text += `.transform(Perspective.${key
.toUpperCase()
.replace(/PERSON/g, "_PERSON")
.replace(/HAND/g, "_HAND")})`;
if (val.rotation) {
text += `.rotation(${val.rotation[0]}F, ${val.rotation[1]}F, ${val.rotation[2]}F)`;
}
if (val.translation) {
text += `.translation(${val.translation[0]}F, ${val.translation[1]}F, ${val.translation[2]}F)`;
}
if (val.scale) {
text += `.scale(${val.scale[0]}F, ${val.scale[1]}F, ${val.scale[2]}F)`;
}
text += `.end()`;
}
}
}
this.dispatchEvent("compile", { model: text, options });
return text;
},
});
Plugin.register("datagen_export", {
title: "Datagen Exporter",
icon: "code",
author: "itsmeow",
description: "Allows exporting to BlockStateProvider datagen code",
version: "1.0.0",
variant: "both",
onload() {
let action = new Action({
id: "export_datagen",
name: "Export Datagen Code",
icon: "code",
description: "Exports a model to BlockStateProvider datagen code.",
category: "file",
condition: (_) => Format.id === "java_block",
click: () => {
codec.export();
},
});
MenuBar.addAction(action, "file.export");
},
onunload() {
action.delete();
},
});
})();