2022/05/25

Notes on "mojang-net" and "mojang-minecraft-server-admin"

I'm not going to try to organize this post lol

So these libraries are amazing. They allow for web access.

Due to security concerns, these are currently exclusive to only BDS. And the Preview BDS just showed up today :D


This is a simple example:

BP

import { world } from 'mojang-minecraft'; import { http, HttpHeader, HttpRequest, HttpRequestMethod } from 'mojang-net'; import { secrets, variables } from 'mojang-minecraft-server-admin'; world.events.chat.subscribe(evd => {     const { message, sender } = evd;     const request = new HttpRequest(variables.get('host'))         .setBody(JSON.stringify({             name: sender.name,             message         }))         .setHeaders([             new HttpHeader('auth', secrets.get('host'))         ])         .setMethod(HttpRequestMethod.POST);     http.request(request).catch(err => console.warn(err)); });

NodeJS Server Script

const port = 49152;
const http = require('http');
const server = http.createServer();
server.listen(port);
server.on('request', (req, res) => {
    res.setHeader('Content-Type', 'text/plain');
    console.log(req.headers);
    let body = [];
    req.on('data', d => body.push(d)).on('end', async () => {
        try { body = JSON.parse(Buffer.concat(body).toString()); } catch {}
        console.log(body);
        res.end('');
    });
    console.log('Received request');
});

console.log('Server Ready');

Variables can be used to store data in a way that's more easily accessible to the server admin.

Secrets store information that shouldn't be obtained anywhere. As of now, I've only been able to use secrets.get() in an HTTP Header. Used for things like authentication to a server.


In a BDS server, variables.json and secrets.json are located in config/default. I haven't checked if any custom subfolders beneath config work. They're just a normal key/value pair, and the get method takes the key as the argument. Objects are supported, as well as anything other JSON data type.


Update 2022/06/07: The subfolders can be the module UUID (not header), which essentially limits configured variables and secrets to only that specific add-on

No comments:

Post a Comment