diff --git a/src/main/kotlin/cn/rtast/fancybot/commands/misc/MinecraftCommand.kt b/src/main/kotlin/cn/rtast/fancybot/commands/misc/MinecraftCommand.kt index a4db570..b2b2413 100644 --- a/src/main/kotlin/cn/rtast/fancybot/commands/misc/MinecraftCommand.kt +++ b/src/main/kotlin/cn/rtast/fancybot/commands/misc/MinecraftCommand.kt @@ -602,16 +602,29 @@ class MCBotCommand : BaseCommand() { private val userClientMap = mutableMapOf>() + private fun getPlayerList(host: String, port: Int): Pair { + val response = JavaPing().ping(host, port, 8000)!! + return response.players.max to response.players.online + } + override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List) { val clients = mutableListOf() val action = args.first() when (action) { "start" -> { - val executor = Executors.newFixedThreadPool(100) val address = args[1] val host = address.split(":").first() val port = address.split(":").last().toInt() - val count = args[2].toInt() + val count = if (args[2].toInt() <= 200) { + message.replyAsync("最多只能开启200个客户端!!!") + args[2].toInt() + } else 200 + val executor = Executors.newFixedThreadPool(count) + val (maxPlayer, onlinePlayer) = this.getPlayerList(host, port) + val availableSit = maxPlayer - onlinePlayer + if (count > availableSit) { + message.replyAsync("当前服务器仅剩余${availableSit}个玩家可进入!!") + } (1..count).forEach { val client = MCClient(host, port, generateRandomString()) .also { it.createClient() } diff --git a/src/main/kotlin/cn/rtast/fancybot/util/mcbot/MCClient.kt b/src/main/kotlin/cn/rtast/fancybot/util/mcbot/MCClient.kt index d004455..5d3b517 100644 --- a/src/main/kotlin/cn/rtast/fancybot/util/mcbot/MCClient.kt +++ b/src/main/kotlin/cn/rtast/fancybot/util/mcbot/MCClient.kt @@ -14,6 +14,9 @@ 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.data.game.ResourcePackStatus +import org.geysermc.mcprotocollib.protocol.packet.common.clientbound.ClientboundResourcePackPushPacket +import org.geysermc.mcprotocollib.protocol.packet.common.serverbound.ServerboundResourcePackPacket 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 @@ -33,16 +36,21 @@ class MCClient( private lateinit var client: TcpClientSession private val logger = Logger.getLogger() - fun createClient() { + fun createClient(): TcpClientSession { val protocol = MinecraftProtocol(botName) client = TcpClientSession(serverHost, serverPort, protocol) + return client } fun runBot(): MCClient { client.addListener(object : SessionAdapter() { override fun packetReceived(session: Session, packet: Packet) { + if (packet is ClientboundResourcePackPushPacket) { + val loadedPacket = ServerboundResourcePackPacket(packet.id, ResourcePackStatus.SUCCESSFULLY_LOADED) + session.send(loadedPacket) + } if (packet is ClientboundPlayerPositionPacket) { - client.send(ServerboundAcceptTeleportationPacket(packet.teleportId)); + session.send(ServerboundAcceptTeleportationPacket(packet.teleportId)); } if (packet is ClientboundSystemChatPacket) { logger.trace("系统 >>> {}", packet.content) @@ -52,7 +60,7 @@ class MCClient( } if (packet is ClientboundPlayerCombatKillPacket) { logger.info("Bot已死亡自动复活中") - client.send(ServerboundClientCommandPacket(ClientCommand.RESPAWN)) + session.send(ServerboundClientCommandPacket(ClientCommand.RESPAWN)) } } })