2.4 KiB
Creating a Client
public fun main() = runBlocking {
val client = createMinecraftClient(
"127.0.0.1", 25566, "MyBot",
generateOfflineUuid("MyBot"),
accessToken = null,
context = DefaultProtocolContext.withCustom {
socketEngine = KtorNetworkEngine()
}
)
client.launch { client.connect() }
while (true) {
delay(5.seconds)
}
}
In the example code above, a
MinecraftClientis created. This client will connect to an offline server at127.0.0.1:25565usingMyBotas the player name, and replaces the underlying TCP Socket implementation with aktor-networkbased TCP Socket. (For details on how to create a SocketEngine, please refer to Implementing TCP Socket. For details on how to create a Context, please refer to Required APIs) MinecraftClient implements CoroutineScope, and callingclient.connect()will execute the connection on a background thread. Blocking thread to prevent the application from exiting
Connecting to an Online-Mode Server
private val accessToken = "eyJraWQiOiIw..."
public fun main() = runBlocking {
val client = createMinecraftClient(
// Other parameters
username = "RTAkland",
uuid = Uuid.parse("bb033844-e68e-4909-a636-1a5d1821ddc4"),
accessToken = accessToken,
// Other parameters
)
}
In the example code above, the client will connect to the server using
RTAklandas the player name.
Get an AccessToken
Open minecraft.net, log in, press F12, and run the following code in the Console:
console.log(`; ${document.cookie}`.split('; bearer_token=').pop().split(';').shift())
Note: AccessTokens are valid for 24 hours
Listening for Packets
// Listen for specific received packets (must start with Clientbound)
client.onPacket<ClientboundLoginSuccessPacket> {
println(it)
}
// Listen for all received packets
client.on { packet, direction ->
println("$direction ->$packet")
}
// Listen for outgoing packets (must start with Serverbound)
cli.onSent<ServerboundPongConfigurationPacket> {
println(it)
}
Sending Packets
// Only packets starting with Serverbound can be sent, otherwise an UnsupportedOperationException will be thrown
client.networkChannel.sendPacket(
ServerboundChatCommandPacket(command = "say Hello from libmc")
)