Add Lagoon proximity chat plugin with player initialization and utilities
This commit is contained in:
8
scripts/entities/player.js
Normal file
8
scripts/entities/player.js
Normal file
@@ -0,0 +1,8 @@
|
||||
import utils from "utils";
|
||||
export function InitPlayer(p) {
|
||||
if (!p.initialized) {
|
||||
p.initialized = true;
|
||||
p.lagoonCode = utils.generateCode();
|
||||
p.sendMessage(utils.prefix + '§aVotre lagoon code : ' + p.lagoonCode + '\n§r§oMerci de ne pas divulguer ce code, il vous est propre !');
|
||||
}
|
||||
}
|
||||
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();
|
||||
1
scripts/properties.js
Normal file
1
scripts/properties.js
Normal file
@@ -0,0 +1 @@
|
||||
export const roomCode = 'despia';
|
||||
28
scripts/utils.js
Normal file
28
scripts/utils.js
Normal file
@@ -0,0 +1,28 @@
|
||||
import { world } from "@minecraft/server";
|
||||
class Utils {
|
||||
constructor() {
|
||||
this.prefix = '§e[§cLagoon§e]§c ';
|
||||
}
|
||||
getRndInteger(min, max) {
|
||||
return Math.floor(Math.random() * (max - min + 1)) + min;
|
||||
}
|
||||
getStaffPlayers() {
|
||||
return world.getPlayers({ tags: ['staff'] });
|
||||
}
|
||||
logStaff(message) {
|
||||
this.getStaffPlayers().forEach((p) => p.sendMessage(this.prefix + message));
|
||||
}
|
||||
makeid(length) {
|
||||
var result = '';
|
||||
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
||||
var charactersLength = characters.length;
|
||||
for (var i = 0; i < length; i++) {
|
||||
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
generateCode() {
|
||||
return this.makeid(5);
|
||||
}
|
||||
}
|
||||
export default new Utils();
|
||||
Reference in New Issue
Block a user