feat: improve mcbot command

This commit is contained in:
2024-10-18 11:38:45 +08:00
parent 8999e8912a
commit 838d95391e
6 files changed
+111 -82

No files matched your search

@@ -1,55 +0,0 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/10/17
*/
package cn.rtast.fancybot.util.mcbot
import org.geysermc.mcprotocollib.network.Session
import org.geysermc.mcprotocollib.network.event.session.SessionAdapter
import org.geysermc.mcprotocollib.network.packet.Packet
import org.geysermc.mcprotocollib.network.tcp.TcpClientSession
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundLoginPacket
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.entity.player.ClientboundPlayerPositionPacket
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.ServerboundChatPacket
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.level.ServerboundAcceptTeleportationPacket
import java.time.Instant
import java.util.BitSet
class MCBot(
private val content: String = "FancyBot喵~",
private val continueSendMessage: Boolean = false,
private val sendDelay: Long = 2000L,
private val client: TcpClientSession
) : Runnable {
private fun sendMessage() =
client.send(ServerboundChatPacket(content, Instant.now().toEpochMilli(), 0L, null, 0, BitSet()))
override fun run() {
client.addListener(object : SessionAdapter() {
override fun packetReceived(session: Session, packet: Packet) {
if (packet is ClientboundLoginPacket) {
if (continueSendMessage) {
while (true) {
try {
sendMessage()
Thread.sleep(sendDelay)
} catch (_: InterruptedException) {
Thread.currentThread().interrupt()
}
}
} else {
sendMessage()
}
} else if (packet is ClientboundPlayerPositionPacket) {
val p = packet
client.send(ServerboundAcceptTeleportationPacket(p.teleportId));
}
}
})
client.connect()
}
}
@@ -7,17 +7,72 @@
package cn.rtast.fancybot.util.mcbot
import cn.rtast.fancybot.util.Logger
import org.geysermc.mcprotocollib.network.Session
import org.geysermc.mcprotocollib.network.event.session.SessionAdapter
import org.geysermc.mcprotocollib.network.packet.Packet
import org.geysermc.mcprotocollib.network.tcp.TcpClientSession
import org.geysermc.mcprotocollib.protocol.MinecraftProtocol
import org.geysermc.mcprotocollib.protocol.data.game.ClientCommand
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundPlayerChatPacket
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundSystemChatPacket
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.entity.player.ClientboundPlayerCombatKillPacket
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.entity.player.ClientboundPlayerPositionPacket
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.ServerboundChatCommandPacket
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.ServerboundChatPacket
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.ServerboundClientCommandPacket
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.level.ServerboundAcceptTeleportationPacket
import java.time.Instant
import java.util.BitSet
class MCClient(
private val serverHost: String,
private val serverPort: Int,
private val botName: String
) {
fun createClient(): TcpClientSession {
private lateinit var client: TcpClientSession
private val logger = Logger.getLogger<MCClient>()
fun createClient() {
val protocol = MinecraftProtocol(botName)
val client = TcpClientSession(serverHost, serverPort, protocol)
return client
client = TcpClientSession(serverHost, serverPort, protocol)
}
fun runBot(): MCClient {
client.addListener(object : SessionAdapter() {
override fun packetReceived(session: Session, packet: Packet) {
if (packet is ClientboundPlayerPositionPacket) {
client.send(ServerboundAcceptTeleportationPacket(packet.teleportId));
}
if (packet is ClientboundSystemChatPacket) {
logger.trace("系统 >>> {}", packet.content)
}
if (packet is ClientboundPlayerChatPacket) {
logger.trace("{} >>> {}", packet.name, packet.content)
}
if (packet is ClientboundPlayerCombatKillPacket) {
logger.info("Bot已死亡自动复活中")
client.send(ServerboundClientCommandPacket(ClientCommand.RESPAWN))
}
}
})
client.connect()
return this
}
private fun sendMessage(content: String) =
client.send(ServerboundChatPacket(content, Instant.now().toEpochMilli(), 0L, null, 0, BitSet()))
private fun executeCommand(command: String) = client.send(ServerboundChatCommandPacket(command.substring(1)))
fun sendChat(text: String) {
if (text.startsWith("/")) {
this.executeCommand(text)
} else {
this.sendMessage(text)
}
}
fun disconnect() = client.disconnect("Bye~")
}
@@ -0,0 +1,25 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/10/18
*/
package cn.rtast.fancybot.util.misc
import org.xbill.DNS.Lookup
import org.xbill.DNS.SRVRecord
import org.xbill.DNS.Type
fun String.resolveMinecraftSrv(): Pair<String, Int> {
val srvDomain = "_minecraft._tcp.$this"
val lookup = Lookup(srvDomain, Type.SRV)
val result = lookup.run()
if (result != null && result.isNotEmpty()) {
val srvRecord = result[0] as SRVRecord
val target = srvRecord.target.toString().trimEnd('.')
val port = srvRecord.port
return Pair(target, port)
}
return Pair(this, 25565)
}
@@ -10,7 +10,7 @@ package cn.rtast.fancybot.util.str
import kotlin.random.Random
fun generateRandomString(): String {
val allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"
val allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
val nameLength = Random.nextInt(3, 17)
return (1..nameLength)
.map { allowedChars.random() }