Add Lagoon proximity chat plugin with player initialization and utilities
This commit is contained in:
63
scripts/main.js
Normal file
63
scripts/main.js
Normal file
@@ -0,0 +1,63 @@
|
||||
import { system, world } from "@minecraft/server";
|
||||
import utils from "utils";
|
||||
import { websocket } from "@minecraft/server-net";
|
||||
import { InitPlayer } from "entities/player";
|
||||
import { 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 % 20 === 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();
|
||||
Reference in New Issue
Block a user