Tide is a Kotlin Codecs library inspired by Mojang's DataFixerUpper that allows for serializing both network and custom objects via use of transcoders (Json, NBT, etc.)
It is purpose built for the DockyardMC project which is reimplementation of
the Minecraft server protocol so some primitive types like Strings
, Lists
and Maps
are not using the
standard format, but they are following the Minecraft protocol's implementation
repositories {
maven("https://mvn.devos.one/releases")
}
dependencies {
implementation("io.github.dockyardmc:tide:<version>")
}
You can create a codec like this:
data class Player(val username: String, val uuid: UUID) {
companion object {
// Network serialization into netty ByteBuf
val STREAM_CODEC = StreamCodec.of(
StreamCodec.STRING, Player::username,
StreamCodec.UUID, Player::uuid,
::Player
)
// Custom serialization into JSON, NBT, etc.
val CODEC = StructCodec.of(
"username", Codec.STRING, Player::username,
"uuid", Codec.UUID, Player::uuid,
::Player
)
}
}
And then you can either use the STREAM_CODEC
to write to network buffer:
fun writeToNetwork() {
val buffer = Unpooled.buffer()
val player = Player("LukynkaCZE", UUID.fromString("0c9151e4-7083-418d-a29c-bbc58f7c741b"))
Player.STREAM_CODEC.write(buffer, player)
}
or use the CODEC
to write to custom format:
fun writeJson() {
val player = Player("LukynkaCZE", UUID.randomUUID())
val json = Player.CODEC.encode(JsonTranscoder, player)
}
You can include other codecs inside a codec to create more complex types:
data class Bus(val model: String, val driver: Player, val passengers: List<Player>) {
companion object {
val STREAM_CODEC = StreamCodec.of(
StreamCodec.STRING, Bus::model,
Player.STREAM_CODEC, Bus::driver,
Player.STREAM_CODEC.list(), Bus::passengers,
::Bus
)
val CODEC = StructCodec.of(
"model", Codec.STRING, Bus::model,
"driver", Player.CODEC, Bus::driver,
"passengers", Player.CODEC.list(), Bus::passengers,
::Bus
)
}
}
You can also easily implement your own Transcoders by implementing the Transcoder
interface