64 lines
2.4 KiB
JavaScript
64 lines
2.4 KiB
JavaScript
import { system, world } from "@minecraft/server";
|
|
import utils from "utils";
|
|
import { websocket } from "@minecraft/server-net";
|
|
import { InitPlayer } from "entities/player";
|
|
import { interval, roomCode } from "properties";
|
|
class LagoonAddons {
|
|
constructor() {
|
|
this.timer = 0;
|
|
world.afterEvents.worldLoad.subscribe(() => system.run(() => this.initEarlyExecution()));
|
|
}
|
|
async initEarlyExecution() {
|
|
this.ws = await websocket.connect('wss://lagoon.under-scape.com/ws/rooms/' + roomCode + '/plugins/proximity');
|
|
this.ws.afterEvents.message.subscribe((event) => {
|
|
try {
|
|
const payload = JSON.parse(event.message);
|
|
const type = payload.type;
|
|
const data = payload.data;
|
|
if (type == 'status' && data == 'connected !') {
|
|
utils.logStaff('§aLagoon connected !');
|
|
console.log('Lagoon connected !');
|
|
}
|
|
}
|
|
catch (error) {
|
|
utils.logStaff('ERROR With Payload : ' + event.message);
|
|
console.log('ERROR With Payload : ' + event.message);
|
|
}
|
|
});
|
|
this.ws.afterEvents.close.subscribe((event) => {
|
|
utils.logStaff(`§cConnexion à lagoon fermé ! (${event})`);
|
|
console.log(`Connexion à lagoon fermé ! (${event})`);
|
|
});
|
|
world.getAllPlayers().forEach((p) => InitPlayer(p));
|
|
world.afterEvents.playerSpawn.subscribe((e) => {
|
|
system.runTimeout(() => {
|
|
InitPlayer(e.player);
|
|
}, 20 * 3);
|
|
});
|
|
system.runInterval(() => this.clock(), 1);
|
|
utils.logStaff('§aLagoon loaded !');
|
|
console.log('Lagoon loaded !');
|
|
}
|
|
async clock() {
|
|
this.timer++;
|
|
// 1 seconde
|
|
if (this.timer % interval === 0) {
|
|
if (this.ws.isOpen) {
|
|
const players = world.getAllPlayers().map((p) => {
|
|
return {
|
|
...p.location,
|
|
code: p.lagoonCode,
|
|
muted: p.hasTag('muted')
|
|
};
|
|
});
|
|
this.ws.send(JSON.stringify({
|
|
type: 'positions',
|
|
uuid: utils.makeid(100),
|
|
data: players
|
|
}));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
new LagoonAddons();
|