Download MESH Lib — Minecraft Mods — MetaMods

MESH Lib

Active

Downloads

0

Last update

9 months ago

Versions

1.19 — 1.21.4
Server
Fabric
Libraries

MESH Lib

A library that simplifies running an HTTP server through the same port used by the Minecraft server. Compatible with Fabric and Paper frameworks.

Compiled documentation is available at this link.

Supported Versions

Support for the modification is provided starting from version 1.19 up to the latest available.

Usage

Players

Players generally won't need this library — the module is embedded in the final file by plugin and mod developers.

For Developers

Gradle

To use the library, add the repository to the repositories block:

repositories {
    // Other repositories

    maven {
        name = "OffsetMods538"
        url = "https://maven.offsetmonkey538.top/releases"
        content {
            includeGroup "top.offsetmonkey538.meshlib"
        }
    }
}

The library is applied using the Jar-In-Jar principle, meaning library files are placed inside the module and utilized from within:

dependencies {
    // For Fabric
    include modImplementation("top.offsetmonkey538.meshlib:mesh-lib-fabric:1.0.4+1.21.4")

    // For Paper
    implementation "top.offsetmonkey538.meshlib:mesh-lib-paper:1.0.4+1.21.4"
}

Always use the latest version.

Practical Usage

Let's say we need to develop a simple web server operating at the path http://server.com:25565/simple-server and responsible for returning plain text "Hello, world!".

The first step is to create an HTTP request handler; we'll create the class MyHttpHandler as an example:

public class MyHttpHandler implements HttpHandler {

    @Override
    public void handleRequest(@NotNull ChannelHandlerContext ctx, @NotNull FullHttpRequest request) throws Exception {
        // We'll add logic here
    }
}

Next, we implement the code inside the method that processes incoming HTTP requests:

public void handleRequest(@NotNull ChannelHandlerContext ctx, @NotNull FullHttpRequest request) throws Exception {
    // Write "Hello, world!" to a buffer using UTF-8 encoding
    final ByteBuf content = Unpooled.copiedBuffer("Hello, world!", StandardCharsets.UTF_8);
    // Create a response that includes the previously defined buffer
    final FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content);

    // Setting the "CONTENT_TYPE" header informs the browser that the data type is plain text in UTF-8
    response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");

    // Send the completed response and close the connection
    ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}

The final step is to register the created handler. Add the following element to your code or plugin initialization block:

@Override
public void onInitializeServer() {
    // Other functionality within the project

    HttpHandlerRegistry.INSTANCE.register("simple-server", new MyHttpHandler());
}

In the general library compatibility settings, the registered ID is treated as the routing path. It is not allowed to occupy the root path — for handling more than one subpath, you should use the request.uri() parameter rather than trying to specify a path with slashes in the ID, which simply won't work.

Project members
OffsetMonkey538

OffsetMonkey538

Developer

Created: 14 Oct 2024

ID: 62167