LunarClient / Apollo

Next-generation Lunar Client server API
https://lunarclient.dev
MIT License
92 stars 18 forks source link

Lightweight #162

Closed ItsNature closed 2 months ago

ItsNature commented 3 months ago

Overview

Description: Our lightweight integration allows for Apollo features to be used, without the need for running the entire Apollo plugin. We will introduce you to three different methods that all achieve the same goal, while utilizing separate approaches. At the end of the day, all the methods displayed send the JSON message through the plugin messaging channel apollo:json. Each method offers different trade-offs between complexity, flexibility, and performance. Choose the method that best fits your use case and environment.

Further documentation for Lightweight can be found on this page.

Code Example:

Method 1: Using the apollo-protos & protobuf-java-util libraries.

// Create a TypeRegistry with the message types you are using.
TypeRegistry registry = TypeRegistry.newBuilder()
        .add(DisplayWaypointMessage.getDescriptor())
        .add(ConfigurableSettings.getDescriptor())
        .add(OverrideConfigurableSettingsMessage.getDescriptor())
        .build();

// Create the protobuf printer with the registry
JsonFormat.Printer printer = JsonFormat.printer().usingTypeRegistry(registry);

// Enable Module Message
OverrideConfigurableSettingsMessage enableModuleMessage = OverrideConfigurableSettingsMessage.newBuilder()
            .addConfigurableSettings(
                ConfigurableSettings.newBuilder()
                    .setApolloModule("waypoint")
                    .setEnable(true)
                    .build()
            ).build();

// Display Waypoint message
DisplayWaypointMessage waypointMessage = DisplayWaypointMessage.newBuilder()
    .setName("KoTH")
    .setLocation(
        BlockLocation.newBuilder()
            .setWorld("world")
            .setX(150)
            .setY(100)
            .setZ(-150)
            .build())
    .setColor(
        Color.newBuilder()
            .setColor(255)
            .build())
    .setPreventRemoval(true)
    .build();

// Pack the messages into Any and serialize
Any enableModuleAny = Any.pack(enableModuleMessage);
Any displayWaypointAny = Any.pack(waypointMessage);

try {
    byte[] enableModuleBytes = printer.print(enableModuleAny).getBytes();
    byte[] displayWaypointBytes = printer.print(displayWaypointAny).getBytes();

    player.sendPluginMessage(this, "apollo:json", enableModuleBytes);
    player.sendPluginMessage(this, "apollo:json", displayWaypointBytes);
} catch (InvalidProtocolBufferException e) {
    throw new RuntimeException(e);
}

Method 2: Manual JSON Object Construction

// Construct the enable module message
JsonObject configurableSetting = new JsonObject();
configurableSetting.addProperty("apolloModule", "waypoint");
configurableSetting.addProperty("enable", true);

JsonArray configurableSettingsArray = new JsonArray();
configurableSettingsArray.add(configurableSetting);

JsonObject settingsObject = new JsonObject();
settingsObject.addProperty("@type", "type.googleapis.com/lunarclient.apollo.configurable.v1.OverrideConfigurableSettingsMessage");
settingsObject.add("configurableSettings", configurableSettingsArray);

// Send data
player.sendPluginMessage(this, "apollo:json", settingsObject.toString().getBytes());

// Construct the display waypoint message
JsonObject location = new JsonObject();
location.addProperty("world", "world");
location.addProperty("x", 150);
location.addProperty("y", 100);
location.addProperty("z", -150);

JsonObject color = new JsonObject();
color.addProperty("color", 255);

JsonObject waypointObject = new JsonObject();
waypointObject.addProperty("@type", "type.googleapis.com/lunarclient.apollo.waypoint.v1.DisplayWaypointMessage");
waypointObject.addProperty("name", "KoTH");
waypointObject.add("location", location);
waypointObject.add("color", color);
waypointObject.addProperty("preventRemoval", true);

// Send data
player.sendPluginMessage(this, "apollo:json", waypointObject.toString().getBytes());

Method 3: Direct JSON String Serialization

// Construct the JSON strings
String enableModuleData = "{\"@type\":\"type.googleapis.com/lunarclient.apollo.configurable.v1.OverrideConfigurableSettingsMessage\",\"configurableSettings\":[{\"apolloModule\":\"waypoint\",\"enable\":true}]}";
String displayWaypointData = "{\"@type\":\"type.googleapis.com/lunarclient.apollo.waypoint.v1.DisplayWaypointMessage\",\"name\":\"KoTH\",\"location\":{\"world\":\"world\",\"x\":150,\"y\":100,\"z\":-150},\"color\":{\"color\":255},\"preventRemoval\":true}";

// Send data
player.sendPluginMessage(this, "apollo:json", enableModuleData.getBytes());
player.sendPluginMessage(this, "apollo:json", displayWaypointData.getBytes());

Review Request Checklist