forked from JaylyDev/ScriptAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (60 loc) · 2 KB
/
index.js
File metadata and controls
62 lines (60 loc) · 2 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
// Script example for ScriptAPI
// Author: mrpatches123 <https://github.com/mrpatches123>
// Project: https://github.com/JaylyDev/ScriptAPI
import { system } from '@minecraft/server';
class TickEvent {
constructor() {
this.subscriptions = {};
this.lastTickDate;
this.deltaTime;
this.currentTick = 0;
this.tickCheck;
this.avgDeltaTime = [];
this.tps;
}
__checkTicks() {
if (this.tickCheck) return;
this.tickCheck = () => {
const { lastTickDate = (new Date()).getTime() } = this;
this.currentTick++;
this.deltaTime = (new Date()).getTime() - lastTickDate;
this.avgDeltaTime.push(this.deltaTime);
if (this.avgDeltaTime.length > 100) this.avgDeltaTime.shift();
const { avgDeltaTime } = this;
this.tps = Math.round((1 / (avgDeltaTime.reduce((t, c) => t + c) / avgDeltaTime.length / 1000)) * 10) / 10;
this.lastTickDate = (new Date()).getTime();
system.run(this.tickCheck);
};
system.run(this.tickCheck);
}
/**
* @method subscribe
* @param {String} key
* @param {Function} callback
*/
subscribe(key, callback) {
this.__checkTicks();
this.subscriptions[key] = () => {
const { deltaTime = 0, currentTick = 0, tps = 20 } = this;
callback({ deltaTime, currentTick, tps });
system.run(this.subscriptions[key]);
};
system.run(this.subscriptions[key]);
}
/**
* @method unsubscribe
* @param {String} key
*/
unsubscribe(key) {
console.warn(Object.keys(this.subscriptions).length);
if (Object.keys(this.subscriptions).length <= 1) {
// @ts-ignore
system.run(() => { this.__checkTicks = false; });
}
this.subscriptions[key] = () => { };
system.run(() => {
delete this.subscriptions[key];
});
}
}
export const tickEvent = new TickEvent();