-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRelativeScenesAPI.js
More file actions
357 lines (319 loc) · 11.6 KB
/
RelativeScenesAPI.js
File metadata and controls
357 lines (319 loc) · 11.6 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/**
* <RELATIVE SCENES API>
* <VERSION: 1.0>
* <Git: https://github.com/Evanechecssss/RelativeScenesAPI>
*
* Author: ⚘'Ivan'⚘
* License: GNU General Public License v3
* Thanks:
* - McHorse for his mods
* - Creman for help with trigonometry
* - Oshi for everything
*/
var Integer = Java.type("java.lang.Integer");
var Math = Java.type("java.lang.Math");
var Vector3f = Java.type("javax.vecmath.Vector3f");
var CommandRecord = Java.type("mchorse.blockbuster.commands.CommandRecord");
var RecordUtils = Java.type("mchorse.blockbuster.recording.RecordUtils");
var BBCommonProxy = Java.type("mchorse.blockbuster.CommonProxy");
var BBClientProxy = Java.type("mchorse.blockbuster.ClientProxy");
var Scene = Java.type("mchorse.blockbuster.recording.scene.Scene");
var Replay = Java.type("mchorse.blockbuster.recording.scene.Replay");
var AffineTransform = Java.type("java.awt.geom.AffineTransform");
var Point2D = Java.type("java.awt.geom.Point2D$Double");
/**
* @param {java.lang.String} name
* @param {net.minecraft.world.World} world
* @param {java.lang.Integer} ticks
*/
function PlayScene(name, world, ticks) {
var scene = BBCommonProxy.scenes.get(name, world);
scene.startPlayback(ticks);
}
/**
* @param {java.lang.String} scene_name
* @param {java.lang.String} save_name
* @param {net.minecraft.world.World} world
*/
function DuplicateScene(scene_name, save_name, world) {
var origin = BBCommonProxy.scenes.get(scene_name, world);
var scene = new Scene();
scene.copy(origin);
scene.setId(save_name);
scene.setupIds();
//renamePrefix(save_name) <--use on old bb versions
scene.renamePrefix(origin.getId(), scene.getId(), function (id) {
return save_name;
});
for (var i = 0; i < scene.replays.size(); i++) {
var replaySource = origin.replays.get(i);
var replayDestination = scene.replays.get(i);
var counter = 0;
try {
var record = CommandRecord.getRecord(replaySource.id).clone();
if (RecordUtils.isReplayExists(replayDestination.id)) {
continue;
}
record.filename = replayDestination.id + ((counter != 0) ? "_" + Integer.toString(counter) : "");
replayDestination.id = record.filename;
record.save(RecordUtils.replayFile(record.filename));
CommonProxy.manager.records.put(record.filename, record);
} catch (e) {
e.printStackTrace();
}
}
var scene = BBCommonProxy.scenes.save(save_name, scene);
BBClientProxy.manager.reset();
}
/**
* @param {java.lang.String} scene_name
* @param {net.minecraft.world.World} world
*/
function DeleteScene(scene_name, world) {
var scene = BBCommonProxy.scenes.get(scene_name, world);
for (var i = 0; i < scene.replays.size(); i++) {
var replaySource = scene.replays.get(i);
try {
var record = CommandRecord.getRecord(replaySource.id);
RecordUtils.replayFile(record.filename).delete();
RecordUtils.unloadRecord(BBCommonProxy.manager.records.get(record.filename));
BBCommonProxy.manager.records.remove(record.filename);
} catch (e) {
e.printStackTrace();
}
}
BBCommonProxy.scenes.remove(scene_name);
}
/**
* @param {java.lang.String} origin_scene_name
* @param {java.lang.String} saved_scene_name
* @param {net.minecraft.world.World} world
* @param {java.awt.geom.AffineTransform} affine
* @param {java.lang.Integer} yaw
*/
function AffineScene(origin_scene_name, saved_scene_name, world, affine, yaw) {
yaw = yaw === undefined ? 0 : yaw;
affine = affine === undefined ? new AffineTransform() : affine;
var origin_records = new java.util.ArrayList();
var sceneCenter = new Vector3f();
var count = 0;
processScene(BBCommonProxy.scenes.get(origin_scene_name, world), origin_records, true);
if (count > 0) {
sceneCenter.scale(1.0 / count);
}
processScene(BBCommonProxy.scenes.get(saved_scene_name, world), origin_records, false);
function processScene(scene, originList, isOrigin) {
var sceneActors = scene.actors;
var emptyActors = sceneActors.isEmpty();
if (emptyActors) {
collectActors(scene);
}
var actorsList = new java.util.ArrayList(sceneActors.entrySet());
for (var i = 0; i < actorsList.size(); i++) {
var entry = actorsList.get(i);
var record = entry.getValue().record;
if (isOrigin) {
handleOriginRecord(record, originList, sceneCenter);
} else {
transformRecord(originList.get(i), record, sceneCenter, affine, yaw);
}
}
if (emptyActors) {
clearActors(scene);
}
}
function handleOriginRecord(record, originList, sceneCenter) {
var clonedRecord = record.clone();
originList.add(clonedRecord);
var firstFrame = clonedRecord.frames.get(0);
if (firstFrame) {
sceneCenter.add(new Vector3f(firstFrame.x, firstFrame.y, firstFrame.z));
count++;
}
}
function transformRecord(origin_record, target_record, sceneCenter, affine, yaw) {
if (!origin_record) return;
transformFrames(origin_record.frames, sceneCenter, affine, yaw);
transformActions(origin_record.actions, sceneCenter, yaw);
origin_record.filename = target_record.filename;
origin_record.save(RecordUtils.replayFile(origin_record.filename));
BBCommonProxy.manager.records.put(origin_record.filename, origin_record);
}
function transformFrames(frames, sceneCenter, affine, yaw) {
for (var j = 0; j < frames.size(); j++) {
var frame = frames.get(j);
var position = new Vector3f(frame.x, frame.y, frame.z);
position.sub(sceneCenter);
var point2d = new Point2D(position.x, position.z);
affine.transform(point2d, point2d);
frame.x = point2d.x + sceneCenter.x;
frame.y = position.y + sceneCenter.y;
frame.z = point2d.y + sceneCenter.z;
frame.yaw = normalizeAngle(frame.yaw + yaw);
frame.yawHead = normalizeAngle(frame.yawHead + yaw);
if (frame.hasBodyYaw) {
frame.bodyYaw = normalizeAngle(frame.bodyYaw + yaw);
}
}
}
function transformActions(actions, sceneCenter, yaw) {
for (var j = 0; j < actions.size(); j++) {
var actionList = actions.get(j);
if (actionList != null && !actionList.isEmpty()) {
for (var k = 0; k < actionList.size(); k++) {
var action = actionList.get(k);
action.changeOrigin(
yaw,
sceneCenter.x, sceneCenter.y, sceneCenter.z,
sceneCenter.x, sceneCenter.y, sceneCenter.z
);
}
}
}
}
}
/**
* @param {java.lang.String} originName
* @param {java.lang.String} saveName
* @param {net.minecraft.world.World} world
* @param {java.lang.Integer} degrees
*/
function RotateScene(originName,saveName, world, degrees)
{
var rotation = new AffineTransform();
rotation.rotate(Math.toRadians(degrees));
AffineScene(originName, saveName, world, rotation, degrees);
}
/**
* @param {java.lang.String} originName
* @param {java.lang.String} saveName
* @param {net.minecraft.world.World} world
* @param dx
* @param dz
*/
function TranslateScene(originName, saveName, world, dx, dz) {
var affine = new AffineTransform();
affine.translate(dx, dz);
AffineScene(originName, saveName, world, affine, 0);
}
//@Utils
function clearActors(scene) {
scene.actors.clear();
scene.actorsCount = 0;
}
function collectActors(scene) {
var method = Scene.class.getDeclaredMethod("collectActors", [Replay.class]);
method.setAccessible(true);
method.invoke(scene, null);
method.setAccessible(false);
}
function normalizeAngle(angle) {
angle = angle % 360;
if (angle > 180) {
angle -= 360;
} else if (angle < -180) {
angle += 360;
}
return angle;
}
function setRootPoint(root, point)
{
if (root == null)
{
root = new Vector3f(point.x, point.y, point.z);
}
return root;
}
function runThread(runnable)
{(new (Java.extend(java.lang.Thread, {run: runnable}))()).start();}
//@Deprecated
/**
* Changes the scene and saves it.
* Changes are made by accepting a function.
* You can override your function with the desired signature.
* Example: function(frames, actions, parameter){return [frames, actions];}
*
* @param {java.lang.String} scene_name
* @param {java.lang.String} save_name
* @param {net.minecraft.world.World} world
* @param {java.lang.Function} fun
* @param {java.lang.Object} params
*/
function ChangeScene(scene_name, save_name, world, fun, params) {
var origin_record;
function aply(name, origin) {
var scene = BBCommonProxy.scenes.get(name, world);
var hasntactors = scene.actors.isEmpty();
if (hasntactors) {
collectActors(scene);
}
scene.actors.entrySet().forEach(function (entry) {
if (origin == null) {
var record = entry.getValue().record;
origin_record = record.clone();
} else {
var record = entry.getValue().record;
var record_name = record.filename;
var origin_frames = origin.frames;
var origin_actions = origin.actions;
var result = fun(origin_frames, origin_actions, params);
origin.frames = result[0];
origin.actions = result[1];
origin.filename = record_name;
origin.save(RecordUtils.replayFile(origin.filename));
BBCommonProxy.manager.records.put(origin.filename, origin);
}
});
if (hasntactors) {
clearActors(scene);
}
}
aply(scene_name, null);
aply(save_name, origin_record);
}
/**
* Move the scene by a vector and rotates it by degrees
* @param {java.util.ArrayList} frames
* @param {java.util.ArrayList} actions
* @param {java.lang.Object} params {yaw: value, position: value}
* @returns Array of changed frames and actions
*/
function MoveScene(frames, actions, params) {
var root = null;
var i = 0;
var yaw = params.yaw;
var position = params.position;
frames.forEach(function (frame) {
var point = new Vector3f(frame.x, frame.y, frame.z);
if (i == 0) {
root = setRootPoint(root, point);
}
var delta = new Vector3f(point.x - root.x, point.y - root.y, point.z - root.z);
if (yaw != 0) {
var cos = Math.cos(Math.toRadians(yaw));
var sin = Math.sin(Math.toRadians(yaw));
var xx = delta.x * cos - delta.z * sin;
var zz = delta.x * sin + delta.z * cos;
delta.x = xx;
delta.z = zz;
frame.yaw += yaw;
frame.yawHead += yaw;
if (frame.hasBodyYaw) {
frame.bodyYaw += yaw;
}
}
frame.x = (position == null ? root.x : position.x) + delta.x;
frame.y = (position == null ? root.y : position.y) + delta.y;
frame.z = (position == null ? root.z : position.z) + delta.z;
i++;
});
actions.forEach(function (a_actions) {
if (a_actions == null || a_actions.isEmpty()) {
return;
}
a_actions.forEach(function (action) {
action.changeOrigin(yaw, root.x, root.y, root.z, root.x, root.y, root.z);
});
});
return [frames, actions];
}