Protobuf 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 representations.
Integration
public static UUID toJavaUuid(JsonObject obj) {
long high = Long.parseUnsignedLong(obj.get("high64").getAsString());
long low = Long.parseUnsignedLong(obj.get("low64").getAsString());
return new UUID(high, low);
}
public static long toJavaTimestamp(JsonObject timestampObject) {
JsonObject packetInfo = timestampObject.getAsJsonObject("packet_info");
String iso = packetInfo.get("instantiation_time").getAsString();
return Instant.parse(iso).toEpochMilli();
}
public static Uuid createUuidProto(UUID object) {
return Uuid.newBuilder()
.setHigh64(object.getMostSignificantBits())
.setLow64(object.getLeastSignificantBits())
.build();
}
public static com.lunarclient.apollo.common.v1.Color createColorProto(Color object) {
return com.lunarclient.apollo.common.v1.Color.newBuilder()
.setColor(object.getRGB())
.build();
}
public static com.google.protobuf.Duration createDurationProto(Duration object) {
return com.google.protobuf.Duration.newBuilder()
.setSeconds(object.getSeconds())
.setNanos(object.getNano())
.build();
}
public static Cuboid2D createCuboid2DProto(double minX, double minZ, double maxX, double maxZ) {
return Cuboid2D.newBuilder()
.setMinX(minX)
.setMinZ(minZ)
.setMaxX(maxX)
.setMaxZ(maxZ)
.build();
}
public static EntityId createEntityIdProto(int id, UUID uuid) {
return EntityId.newBuilder()
.setEntityId(id)
.setEntityUuid(ProtobufUtil.createUuidProto(uuid))
.build();
}Location-related methods
public static com.lunarclient.apollo.common.v1.Location createLocationProto(Location location) {
return com.lunarclient.apollo.common.v1.Location.newBuilder()
.setWorld(location.getWorld().getName())
.setX(location.getX())
.setY(location.getY())
.setZ(location.getZ())
.build();
}
public static BlockLocation createBlockLocationProto(Location location) {
return BlockLocation.newBuilder()
.setWorld(location.getWorld().getName())
.setX(location.getBlockX())
.setY(location.getBlockY())
.setZ(location.getBlockZ())
.build();
}
public static Location toBukkitLocation(JsonObject message) {
return new Location(
Bukkit.getWorld(message.get("world").getAsString()),
message.get("x").getAsDouble(),
message.get("y").getAsDouble(),
message.get("z").getAsDouble()
);
}
public static Location toBukkitPlayerLocation(JsonObject message) {
Location location = JsonUtil.toBukkitLocation(message.getAsJsonObject("location"));
location.setYaw(message.get("yaw").getAsFloat());
location.setPitch(message.get("pitch").getAsFloat());
return location;
}Icon-related methods
public static ItemStackIcon createItemStackIconProto(@Nullable String itemName, int itemId, int customModelData) {
ItemStackIcon.Builder iconBuilder = ItemStackIcon.newBuilder()
.setItemId(itemId)
.setCustomModelData(customModelData);
if (itemName != null) {
iconBuilder.setItemName(itemName);
}
return iconBuilder.build();
}
public static ResourceLocationIcon createResourceLocationIconProto(String resourceLocation) {
return ResourceLocationIcon.newBuilder()
.setResourceLocation(resourceLocation)
.build();
}
public static SimpleResourceLocationIcon createSimpleResourceLocationIconProto(String resourceLocation, int size) {
return SimpleResourceLocationIcon.newBuilder()
.setResourceLocation(resourceLocation)
.setSize(size)
.build();
}
public static AdvancedResourceLocationIcon createAdvancedResourceLocationIconProto(String resourceLocation, float width, float height,
float minU, float maxU, float minV, float maxV) {
return AdvancedResourceLocationIcon.newBuilder()
.setResourceLocation(resourceLocation)
.setWidth(width)
.setHeight(height)
.setMinU(minU)
.setMaxU(maxU)
.setMinV(minV)
.setMaxV(maxV)
.build();
}