JSON Object Util
Overview
These utilities facilitate the creation of Apollo objects, commonly used across various Apollo Modules. The utility methods are used for converting objects to and from their corresponding Protocol Buffers represented as a JSON Object.
Integration
public static JsonObject createEnableModuleObjectWithType(@NotNull String module, Map<String, Object> properties) {
JsonObject enableModuleObject = JsonPacketUtil.createEnableModuleObject(module, properties);
enableModuleObject.addProperty("@type", "type.googleapis.com/lunarclient.apollo.configurable.v1.ConfigurableSettings");
return enableModuleObject;
}
public static JsonObject createUuidObject(@NotNull UUID uuid) {
JsonObject uuidObject = new JsonObject();
uuidObject.addProperty("high64", Long.toUnsignedString(uuid.getMostSignificantBits()));
uuidObject.addProperty("low64", Long.toUnsignedString(uuid.getLeastSignificantBits()));
return uuidObject;
}
public static JsonObject createColorObject(@NotNull Color color) {
JsonObject colorObject = new JsonObject();
colorObject.addProperty("color", color.getRGB());
return colorObject;
}
public static String createDurationObject(@NotNull Duration duration) {
long seconds = duration.getSeconds();
int nanos = duration.getNano();
String durationString;
if (nanos == 0) {
durationString = seconds + "s";
} else {
durationString = String.format("%d.%09ds", seconds, nanos)
.replaceAll("0+$", "")
.replaceAll("\\.$", "");
}
return durationString;
}
public static JsonObject createCuboid2DObject(double minX, double minZ, double maxX, double maxZ) {
JsonObject cuboid2DObject = new JsonObject();
cuboid2DObject.addProperty("min_x", minX);
cuboid2DObject.addProperty("min_z", minZ);
cuboid2DObject.addProperty("max_x", maxX);
cuboid2DObject.addProperty("max_z", maxZ);
return cuboid2DObject;
}
public static JsonObject createEntityIdObject(@NotNull Entity entity) {
JsonObject entityIdObject = new JsonObject();
entityIdObject.addProperty("entity_id", entity.getEntityId());
entityIdObject.add("entity_uuid", JsonUtil.createUuidObject(entity.getUniqueId()));
return entityIdObject;
}
Location-related methods
public static JsonObject createLocationObject(@NotNull Location location) {
JsonObject locationObject = new JsonObject();
locationObject.addProperty("world", location.getWorld().getName());
locationObject.addProperty("x", location.getX());
locationObject.addProperty("y", location.getY());
locationObject.addProperty("z", location.getZ());
return locationObject;
}
public static JsonObject createBlockLocationObject(@NotNull Location location) {
JsonObject locationObject = new JsonObject();
locationObject.addProperty("world", location.getWorld().getName());
locationObject.addProperty("x", location.getBlockX());
locationObject.addProperty("y", location.getBlockY());
locationObject.addProperty("z", location.getBlockZ());
return locationObject;
}
Icon-related methods
public static JsonObject createItemStackIconObject(@Nullable String itemName, int itemId, int customModelData) {
JsonObject itemIconObject = new JsonObject();
if (itemName != null) {
itemIconObject.addProperty("item_name", itemName);
} else {
itemIconObject.addProperty("item_id", itemId);
}
itemIconObject.addProperty("custom_model_data", customModelData);
JsonObject iconObject = new JsonObject();
iconObject.add("item_stack", itemIconObject);
return iconObject;
}
public static JsonObject createSimpleResourceLocationIconObject(@NotNull String resourceLocation, int size) {
JsonObject simpleIconObject = new JsonObject();
simpleIconObject.addProperty("resource_location", resourceLocation);
simpleIconObject.addProperty("size", size);
JsonObject iconObject = new JsonObject();
iconObject.add("simple_resource_location", simpleIconObject);
return iconObject;
}
public static JsonObject createAdvancedResourceLocationIconObject(@NotNull String resourceLocation, float width, float height,
float minU, float maxU, float minV, float maxV) {
JsonObject advancedIcon = new JsonObject();
advancedIcon.addProperty("resource_location", resourceLocation);
advancedIcon.addProperty("width", width);
advancedIcon.addProperty("height", height);
advancedIcon.addProperty("min_u", minU);
advancedIcon.addProperty("max_u", maxU);
advancedIcon.addProperty("min_v", minV);
advancedIcon.addProperty("max_v", maxV);
JsonObject iconObject = new JsonObject();
iconObject.add("advanced_resource_location", advancedIcon);
return iconObject;
}