feat: add max player tip

This commit is contained in:
2024-10-18 17:38:04 +08:00
parent f5d6a3acb7
commit b0d295d97f
2 files changed
+26 -5

No files matched your search

@@ -602,16 +602,29 @@ class MCBotCommand : BaseCommand() {
private val userClientMap = mutableMapOf<Long, MutableList<MCClient>>() private val userClientMap = mutableMapOf<Long, MutableList<MCClient>>()
private fun getPlayerList(host: String, port: Int): Pair<Int, Int> {
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<String>) { override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
val clients = mutableListOf<MCClient>() val clients = mutableListOf<MCClient>()
val action = args.first() val action = args.first()
when (action) { when (action) {
"start" -> { "start" -> {
val executor = Executors.newFixedThreadPool(100)
val address = args[1] val address = args[1]
val host = address.split(":").first() val host = address.split(":").first()
val port = address.split(":").last().toInt() 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 { (1..count).forEach {
val client = MCClient(host, port, generateRandomString()) val client = MCClient(host, port, generateRandomString())
.also { it.createClient() } .also { it.createClient() }
@@ -14,6 +14,9 @@ import org.geysermc.mcprotocollib.network.packet.Packet
import org.geysermc.mcprotocollib.network.tcp.TcpClientSession import org.geysermc.mcprotocollib.network.tcp.TcpClientSession
import org.geysermc.mcprotocollib.protocol.MinecraftProtocol import org.geysermc.mcprotocollib.protocol.MinecraftProtocol
import org.geysermc.mcprotocollib.protocol.data.game.ClientCommand 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.ClientboundPlayerChatPacket
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundSystemChatPacket 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.ClientboundPlayerCombatKillPacket
@@ -33,16 +36,21 @@ class MCClient(
private lateinit var client: TcpClientSession private lateinit var client: TcpClientSession
private val logger = Logger.getLogger<MCClient>() private val logger = Logger.getLogger<MCClient>()
fun createClient() { fun createClient(): TcpClientSession {
val protocol = MinecraftProtocol(botName) val protocol = MinecraftProtocol(botName)
client = TcpClientSession(serverHost, serverPort, protocol) client = TcpClientSession(serverHost, serverPort, protocol)
return client
} }
fun runBot(): MCClient { fun runBot(): MCClient {
client.addListener(object : SessionAdapter() { client.addListener(object : SessionAdapter() {
override fun packetReceived(session: Session, packet: Packet) { 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) { if (packet is ClientboundPlayerPositionPacket) {
client.send(ServerboundAcceptTeleportationPacket(packet.teleportId)); session.send(ServerboundAcceptTeleportationPacket(packet.teleportId));
} }
if (packet is ClientboundSystemChatPacket) { if (packet is ClientboundSystemChatPacket) {
logger.trace("系统 >>> {}", packet.content) logger.trace("系统 >>> {}", packet.content)
@@ -52,7 +60,7 @@ class MCClient(
} }
if (packet is ClientboundPlayerCombatKillPacket) { if (packet is ClientboundPlayerCombatKillPacket) {
logger.info("Bot已死亡自动复活中") logger.info("Bot已死亡自动复活中")
client.send(ServerboundClientCommandPacket(ClientCommand.RESPAWN)) session.send(ServerboundClientCommandPacket(ClientCommand.RESPAWN))
} }
} }
}) })