Skip to content

Commit

Permalink
Change config options & Add game listener and handler
Browse files Browse the repository at this point in the history
  • Loading branch information
alazeprt committed Sep 17, 2024
1 parent 8dfc386 commit 7724a97
Show file tree
Hide file tree
Showing 16 changed files with 277 additions and 106 deletions.
12 changes: 8 additions & 4 deletions common/src/main/kotlin/me/regadpole/plumbot/PlumBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ object PlumBot : Plugin() {
ConfigResolver.loadConfig()
config = ConfigResolver.getConfigLoader()
lang =ConfigResolver.getLangConf()
info("Successfully load config")
info("成功加载配置文件!")
}

// 项目使用TabooLib Start Jar 创建!
Expand All @@ -41,14 +41,13 @@ object PlumBot : Plugin() {
else -> error("Unknown database type.")
}
database.initialize()
info("Loaded database")
info("Successfully running PlumBot!")
info("成功连接到数据库!")
}

override fun onActive() {
submitAsync(now = true) {
when (config.getConfig().bot.mode) {
"go-cqhttp" -> {
"onebot" -> {
bot = QQBot(this@PlumBot)
bot.start()
}
Expand All @@ -63,11 +62,14 @@ object PlumBot : Plugin() {
}
}
}
info("成功启用机器人服务!")
}

override fun onDisable() {
bot.shutdown()
info("成功关闭机器人服务")
database.close()
info("成功关闭数据库链接!")
}

fun reloadConfig() {
Expand All @@ -86,7 +88,9 @@ object PlumBot : Plugin() {
fun getLangConfig(): LangConfig {
return lang
}

fun getDatabase(): Database {
return database
}

}
4 changes: 2 additions & 2 deletions common/src/main/kotlin/me/regadpole/plumbot/bot/KookBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class KookBot(private val plugin: PlumBot): Bot {
kookClient = kook
kookBot = this
kook.core.eventManager.registerHandlers(kook.internalPlugin, KookListener())
val groups: List<String> = plugin.getConfig().getConfig().enableGroups
val groups: List<String> = plugin.getConfig().getConfig().groups.enableGroups
for (groupID in groups) {
PlumBot.getBot().sendMsg(true, "PlumBot已启动", groupID)
}
Expand All @@ -69,7 +69,7 @@ class KookBot(private val plugin: PlumBot): Bot {
* Stop a bot
*/
override fun shutdown() {
val groups: List<String> = plugin.getConfig().getConfig().enableGroups
val groups: List<String> = plugin.getConfig().getConfig().groups.enableGroups
for (groupID in groups) {
sendMsg(true, "PlumBot已关闭", groupID)
}
Expand Down
10 changes: 5 additions & 5 deletions common/src/main/kotlin/me/regadpole/plumbot/bot/QQBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ class QQBot(private val plugin: PlumBot): Bot {
* Start a bot
*/
override fun start(): Bot {
onebot = if (plugin.getConfig().getConfig().bot.gocqhttp.hasAccessToken) {
OneBotClient.create(BotConfig(plugin.getConfig().getConfig().bot.gocqhttp.ws, plugin.getConfig().getConfig().bot.gocqhttp.token)) //创建websocket客户端
onebot = if (plugin.getConfig().getConfig().bot.onebot.token.enable) {
OneBotClient.create(BotConfig(plugin.getConfig().getConfig().bot.onebot.ws, plugin.getConfig().getConfig().bot.onebot.token.token)) //创建websocket客户端
.open() //连接onebot服务端
.registerEvents(QQListener()) //注册事件监听器
} else {
OneBotClient.create(BotConfig(plugin.getConfig().getConfig().bot.gocqhttp.ws)) //创建websocket客户端
OneBotClient.create(BotConfig(plugin.getConfig().getConfig().bot.onebot.ws)) //创建websocket客户端
.open() //连接onebot服务端
.registerEvents(QQListener()) //注册事件监听器
}
val groups: List<String> = plugin.getConfig().getConfig().enableGroups
val groups: List<String> = plugin.getConfig().getConfig().groups.enableGroups
for (groupID in groups) {
sendMsg(true, groupID, "PlumBot已启动")
}
Expand All @@ -38,7 +38,7 @@ class QQBot(private val plugin: PlumBot): Bot {
* Stop a bot
*/
override fun shutdown() {
val groups: List<String> = plugin.getConfig().getConfig().enableGroups
val groups: List<String> = plugin.getConfig().getConfig().groups.enableGroups
for (groupID in groups) {
sendMsg(true, groupID, "PlumBot已关闭")
}
Expand Down
117 changes: 78 additions & 39 deletions common/src/main/kotlin/me/regadpole/plumbot/config/ConfigLoader.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package me.regadpole.plumbot.config

import taboolib.common.io.newFile
import taboolib.common.platform.function.getDataFolder
import taboolib.module.configuration.Configuration
import taboolib.module.configuration.util.getMap
Expand All @@ -13,61 +12,101 @@ class ConfigLoader(config: Configuration, commandsConfig: Configuration, returns


class Config(config: Configuration){
val ver = config.getString("Ver")
val version = config.getString("version")
val lang = config.getString("lang")
val prefix = config.getString("commandPrefix")
val forwarding = Forwarding(config)
val dieReport = config.getBoolean("dieReport")
val whiteList = WhiteList(config)
val cmd = config.getBoolean("cmd")
val joinAndLeave = config.getBoolean("joinAndLeave")
val online = config.getBoolean("online")
val tps = config.getBoolean("tps")
val sdc = config.getBoolean("SDC")
val sdr = config.getBoolean("SDR")
val database = DataBase(config)
val enableGroups = config.getStringList("enableGroups")
val botAdmins = config.getStringList("botAdmins")
val bot = Bot(config)
class Groups(config: Configuration) {
val enableGroups = config.getStringList("groups.enableGroups")
val botAdmins = config.getStringList("groups.botAdmins")
val prefix = config.getString("groups.commandPrefix")
val forwarding = Forwarding(config)
class Forwarding(config: Configuration) {
val message = Message(config)
class Message(config: Configuration) {
val enable = config.getBoolean("groups.forwarding.message.enable")
val mode = config.getInt("groups.forwarding.message.mode")
val prefix = config.getString("groups.forwarding.message.prefix")
}
val dieMessage = config.getBoolean("groups.forwarding.dieMessage")
val whitelist = Whitelist(config)
class Whitelist(config: Configuration) {
val enable = config.getBoolean("groups.forwarding.whitelist.enable")
val maxCount = config.getInt("maxCount")
}
val cmd = config.getBoolean("groups.forwarding.cmd")
val joinAndLeave = config.getBoolean("gorups.forwarding.joinAndLeave")
val online = config.getBoolean("groups.forwarding.online")
val tps = config.getBoolean("groups.forwarding.tps")
val sdc = config.getBoolean("groups.forwarding.SDC")
val sdr = config.getBoolean("groups.forwarding.SDR")
}
}
class DataBase(config: Configuration) {
val type = config.getString("database.type")
val settings = Settings(config)
class Settings(config: Configuration) {
val sqlite = SQLite(config)
val mysql = MySQL(config)
val pool = Pool(config)
class SQLite(config: Configuration) {
val path = config.getString("database.setting.sqlite.path")?.replace("%plugin_folder%", getDataFolder().path)
}
class MySQL(config: Configuration) {
val host = config.getHost("database.settings.mysql.host")
val port = config.getInt("database.settings.mysql.port")
val database = config.getString("database.settings.mysql.database")
val username = config.getString("database.settings.mysql.username")
val password = config.getString("database.settings.mysql.password")
val flagsURL = config.getString("database.settings.mysql.flagsURL")
}
class Pool(config: Configuration) {
val connectionTimeout = config.getInt("database.settings.pool.connectionTimeout")
val idleTimeout = config.getInt("database.settings.pool.idleTimeout")
val maxLifetime = config.getInt("database.settings.pool.maxLifetime")
val maximumPoolSize = config.getInt("database.settings.pool.maximumPoolSize")
val keepaliveTime = config.getInt("database.settings.pool.keepaliveTime")
val minimumIdle = config.getInt("database.settings.pool.minimumIdle")
}
}
val enable = config.getBoolean("enable")
val host = config.getHost("host")
val port = config.getInt("port")
val database = config.getString("database")
val username = config.getString("username")
val password = config.getString("password")
val table = config.getString("table")
val columns = config.getMap<String, String>("columns")
val prefix = config.getString("prefix")
val suffix = config.getString("suffix")
}
class Bot(config: Configuration){
val mode = config.getString("bot.mode")
val gocqhttp = Gocqhttp(config)
val onebot = Onebot(config)
val kook = Kook(config)
class Gocqhttp(config: Configuration){
val ws = config.getString("bot.go-cqhttp.ws")
val token = config.getString("bot.go-cqhttp.token")
val hasAccessToken = config.getBoolean("bot.go-cqhttp.hasAccessToken")
class Onebot(config: Configuration){
val ws = config.getString("bot.onebot.ws")
val token = Token(config)
class Token(config: Configuration) {
val enable = config.getBoolean("bot.onebot.token.enable")
val token = config.getString("bot.onebot.token.token")
}
}
class Kook(config: Configuration) {
val token = config.getString("bot.kook.token")
}
}
class Forwarding(config: Configuration) {
val enable = config.getBoolean("forwarding.enable")
val mode = config.getInt("forwarding.mode")
val prefix = config.getString("forwarding.prefix")
}
class WhiteList(config: Configuration) {
val enable = config.getBoolean("whiteList.enable")
val maxCount = config.getInt("whitelist.maxCount")
}
class DataBase(config: Configuration) {
val type = config.getString("database.type")
private val sqlitePath =
config.getString("database.setting.sqlite.path")?.replace("%plugin_folder%", getDataFolder().path)
val sqliteHost = newFile(sqlitePath!!, create = true, folder = false).getHost()
val host = config.getHost("database.settings.mysql")
}
val groups = Groups(config)
val database = DataBase(config)
val bot = Bot(config)
}

class Commands(config: Configuration) {
val ver = config.getString("ver")
val version = config.getString("version")
val adminCommand = config.getMap<String, String>("admin")
val userCommand = config.getMap<String, String>("user")
}

class Returns(config: Configuration) {
val ver = config.getString("ver")
val version = config.getString("version")
val adminReturn = config.getMap<String, String>("admin")
val userReturn = config.getMap<String, String>("user")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,23 @@ object ConfigResolver {

config = ConfigLoader(Configuration.loadFromFile(configFile, Type.YAML), Configuration.loadFromFile(commandsFile, Type.YAML), Configuration.loadFromFile(returnsFile, Type.YAML))

if ("2.0.0" != config.getConfig().ver) {
if ("2.0.0" != config.getConfig().version) {
plugin.javaClass.getResourceAsStream("/" + configFile.getName()).use { inputStream ->
checkNotNull(inputStream)
Files.copy(inputStream, configFile.toPath(), StandardCopyOption.REPLACE_EXISTING)
loadConfig()
return this
}
}
if ("2.0.0" != config.getCommands().ver) {
if ("2.0.0" != config.getCommands().version) {
plugin.javaClass.getResourceAsStream("/" + commandsFile.getName()).use { inputStream ->
checkNotNull(inputStream)
Files.copy(inputStream, commandsFile.toPath(), StandardCopyOption.REPLACE_EXISTING)
loadConfig()
return this
}
}
if ("2.0.0" != config.getReturns().ver) {
if ("2.0.0" != config.getReturns().version) {
plugin.javaClass.getResourceAsStream("/" + returnsFile.getName()).use { inputStream ->
checkNotNull(inputStream)
Files.copy(inputStream, returnsFile.toPath(), StandardCopyOption.REPLACE_EXISTING)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import javax.sql.DataSource

class SQLite: Database {

private val host = PlumBot.getConfig().getConfig().database.sqliteHost
private val host = PlumBot.getConfig().getConfig().database.host
private val dataSource by lazy { host.createDataSource() }

private val table = Table("whitelist", host) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package me.regadpole.plumbot.event

import me.regadpole.plumbot.util.GPlayer

class GDeathEvent(val player: GPlayer)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.regadpole.plumbot.event

import me.regadpole.plumbot.util.GPlayer

abstract class GJoinEvent(val player: GPlayer) {
abstract fun kick(message: String)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package me.regadpole.plumbot.event

import me.regadpole.plumbot.util.GPlayer

class GPlayerChatEvent(val message: String, val player: GPlayer)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package me.regadpole.plumbot.event

import me.regadpole.plumbot.util.GPlayer

class GQuitEvent(val player: GPlayer)
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package me.regadpole.plumbot.listener.game

import me.regadpole.plumbot.PlumBot
import me.regadpole.plumbot.event.GDeathEvent
import me.regadpole.plumbot.event.GJoinEvent
import me.regadpole.plumbot.event.GPlayerChatEvent
import me.regadpole.plumbot.event.GQuitEvent
import taboolib.common5.util.replace

class GameListener {
fun onChat(event: GPlayerChatEvent) {
if (PlumBot.getConfig().getConfig().groups.forwarding.message.enable) {
if (PlumBot.getConfig().getConfig().groups.forwarding.message.mode == 1 && !event.message.startsWith(
PlumBot.getConfig().getConfig().groups.forwarding.message.prefix?: "")) {
return
}
var message: String = PlumBot.getLangConfig().getLangConf().forwarding.toPlatform?: return
message = message.replace("%server_name%", event.player.server)
.replace("%world_name%", event.player.position.world)
.replace("%message%", event.message)
PlumBot.getConfig().getConfig().groups.enableGroups.forEach {
PlumBot.getBot().sendGroupMsg(it, message)
}
}
}

fun onDie(event: GDeathEvent) {
if (PlumBot.getConfig().getConfig().groups.forwarding.dieMessage) {
var message: String = PlumBot.getLangConfig().getLangConf().messageOnDie?: return
message = message.replace("%player_name%", event.player.name)
.replace("%world_name%", event.player.position.world)
.replace("%pos%", event.player.position.toXYZ("/"))
PlumBot.getConfig().getConfig().groups.enableGroups.forEach {
PlumBot.getBot().sendGroupMsg(it, message)
}
}
}

fun onJoin(event: GJoinEvent) {
if (PlumBot.getDatabase().getBindByName(event.player.name) == null) {
var message: String = PlumBot.getLangConfig().getLangConf().whitelist.kickServer?: return
message = message.replace("%enable_groups%", PlumBot.getConfig().getConfig().groups.enableGroups.joinToString("/"))
event.kick(message)
message = PlumBot.getLangConfig().getLangConf().whitelist.kickPlatform?: return
message = message.replace("%player_name%", event.player.name)
.replace("%server_name%", event.player.server)
PlumBot.getConfig().getConfig().groups.enableGroups.forEach {
PlumBot.getBot().sendGroupMsg(it, message)
}
} else if (PlumBot.getConfig().getConfig().groups.forwarding.joinAndLeave) {
var message: String = PlumBot.getLangConfig().getLangConf().playerJoinMsg?: return
message = message.replace("%player_name%", event.player.name)
.replace("%server_name%", event.player.server)
PlumBot.getConfig().getConfig().groups.enableGroups.forEach {
PlumBot.getBot().sendGroupMsg(it, message)
}
}
}

fun onQuit(event: GQuitEvent) {
if (PlumBot.getConfig().getConfig().groups.forwarding.joinAndLeave) {
var message: String = PlumBot.getLangConfig().getLangConf().playerLeaveMsg?: return
message = message.replace("%player_name%", event.player.name)
.replace("%server_name%", event.player.server)
PlumBot.getConfig().getConfig().groups.enableGroups.forEach {
PlumBot.getBot().sendGroupMsg(it, message)
}
}
}
}
6 changes: 6 additions & 0 deletions common/src/main/kotlin/me/regadpole/plumbot/util/GPlayer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package me.regadpole.plumbot.util

abstract class GPlayer(val name: String, var position: GPosition, val server: String, val isAdmin: Boolean) {
abstract fun teleport()
abstract fun sendMessage(message: String)
}
Loading

0 comments on commit 7724a97

Please sign in to comment.