Skip to content

Commit 7724a97

Browse files
committed
Change config options & Add game listener and handler
1 parent 8dfc386 commit 7724a97

File tree

16 files changed

+277
-106
lines changed

16 files changed

+277
-106
lines changed

common/src/main/kotlin/me/regadpole/plumbot/PlumBot.kt

+8-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ object PlumBot : Plugin() {
3030
ConfigResolver.loadConfig()
3131
config = ConfigResolver.getConfigLoader()
3232
lang =ConfigResolver.getLangConf()
33-
info("Successfully load config")
33+
info("成功加载配置文件!")
3434
}
3535

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

4847
override fun onActive() {
4948
submitAsync(now = true) {
5049
when (config.getConfig().bot.mode) {
51-
"go-cqhttp" -> {
50+
"onebot" -> {
5251
bot = QQBot(this@PlumBot)
5352
bot.start()
5453
}
@@ -63,11 +62,14 @@ object PlumBot : Plugin() {
6362
}
6463
}
6564
}
65+
info("成功启用机器人服务!")
6666
}
6767

6868
override fun onDisable() {
6969
bot.shutdown()
70+
info("成功关闭机器人服务")
7071
database.close()
72+
info("成功关闭数据库链接!")
7173
}
7274

7375
fun reloadConfig() {
@@ -86,7 +88,9 @@ object PlumBot : Plugin() {
8688
fun getLangConfig(): LangConfig {
8789
return lang
8890
}
91+
8992
fun getDatabase(): Database {
9093
return database
9194
}
95+
9296
}

common/src/main/kotlin/me/regadpole/plumbot/bot/KookBot.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class KookBot(private val plugin: PlumBot): Bot {
5757
kookClient = kook
5858
kookBot = this
5959
kook.core.eventManager.registerHandlers(kook.internalPlugin, KookListener())
60-
val groups: List<String> = plugin.getConfig().getConfig().enableGroups
60+
val groups: List<String> = plugin.getConfig().getConfig().groups.enableGroups
6161
for (groupID in groups) {
6262
PlumBot.getBot().sendMsg(true, "PlumBot已启动", groupID)
6363
}
@@ -69,7 +69,7 @@ class KookBot(private val plugin: PlumBot): Bot {
6969
* Stop a bot
7070
*/
7171
override fun shutdown() {
72-
val groups: List<String> = plugin.getConfig().getConfig().enableGroups
72+
val groups: List<String> = plugin.getConfig().getConfig().groups.enableGroups
7373
for (groupID in groups) {
7474
sendMsg(true, "PlumBot已关闭", groupID)
7575
}

common/src/main/kotlin/me/regadpole/plumbot/bot/QQBot.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ class QQBot(private val plugin: PlumBot): Bot {
1717
* Start a bot
1818
*/
1919
override fun start(): Bot {
20-
onebot = if (plugin.getConfig().getConfig().bot.gocqhttp.hasAccessToken) {
21-
OneBotClient.create(BotConfig(plugin.getConfig().getConfig().bot.gocqhttp.ws, plugin.getConfig().getConfig().bot.gocqhttp.token)) //创建websocket客户端
20+
onebot = if (plugin.getConfig().getConfig().bot.onebot.token.enable) {
21+
OneBotClient.create(BotConfig(plugin.getConfig().getConfig().bot.onebot.ws, plugin.getConfig().getConfig().bot.onebot.token.token)) //创建websocket客户端
2222
.open() //连接onebot服务端
2323
.registerEvents(QQListener()) //注册事件监听器
2424
} else {
25-
OneBotClient.create(BotConfig(plugin.getConfig().getConfig().bot.gocqhttp.ws)) //创建websocket客户端
25+
OneBotClient.create(BotConfig(plugin.getConfig().getConfig().bot.onebot.ws)) //创建websocket客户端
2626
.open() //连接onebot服务端
2727
.registerEvents(QQListener()) //注册事件监听器
2828
}
29-
val groups: List<String> = plugin.getConfig().getConfig().enableGroups
29+
val groups: List<String> = plugin.getConfig().getConfig().groups.enableGroups
3030
for (groupID in groups) {
3131
sendMsg(true, groupID, "PlumBot已启动")
3232
}
@@ -38,7 +38,7 @@ class QQBot(private val plugin: PlumBot): Bot {
3838
* Stop a bot
3939
*/
4040
override fun shutdown() {
41-
val groups: List<String> = plugin.getConfig().getConfig().enableGroups
41+
val groups: List<String> = plugin.getConfig().getConfig().groups.enableGroups
4242
for (groupID in groups) {
4343
sendMsg(true, groupID, "PlumBot已关闭")
4444
}

common/src/main/kotlin/me/regadpole/plumbot/config/ConfigLoader.kt

+78-39
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package me.regadpole.plumbot.config
22

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

1413

1514
class Config(config: Configuration){
16-
val ver = config.getString("Ver")
15+
val version = config.getString("version")
1716
val lang = config.getString("lang")
18-
val prefix = config.getString("commandPrefix")
19-
val forwarding = Forwarding(config)
20-
val dieReport = config.getBoolean("dieReport")
21-
val whiteList = WhiteList(config)
22-
val cmd = config.getBoolean("cmd")
23-
val joinAndLeave = config.getBoolean("joinAndLeave")
24-
val online = config.getBoolean("online")
25-
val tps = config.getBoolean("tps")
26-
val sdc = config.getBoolean("SDC")
27-
val sdr = config.getBoolean("SDR")
28-
val database = DataBase(config)
29-
val enableGroups = config.getStringList("enableGroups")
30-
val botAdmins = config.getStringList("botAdmins")
31-
val bot = Bot(config)
17+
class Groups(config: Configuration) {
18+
val enableGroups = config.getStringList("groups.enableGroups")
19+
val botAdmins = config.getStringList("groups.botAdmins")
20+
val prefix = config.getString("groups.commandPrefix")
21+
val forwarding = Forwarding(config)
22+
class Forwarding(config: Configuration) {
23+
val message = Message(config)
24+
class Message(config: Configuration) {
25+
val enable = config.getBoolean("groups.forwarding.message.enable")
26+
val mode = config.getInt("groups.forwarding.message.mode")
27+
val prefix = config.getString("groups.forwarding.message.prefix")
28+
}
29+
val dieMessage = config.getBoolean("groups.forwarding.dieMessage")
30+
val whitelist = Whitelist(config)
31+
class Whitelist(config: Configuration) {
32+
val enable = config.getBoolean("groups.forwarding.whitelist.enable")
33+
val maxCount = config.getInt("maxCount")
34+
}
35+
val cmd = config.getBoolean("groups.forwarding.cmd")
36+
val joinAndLeave = config.getBoolean("gorups.forwarding.joinAndLeave")
37+
val online = config.getBoolean("groups.forwarding.online")
38+
val tps = config.getBoolean("groups.forwarding.tps")
39+
val sdc = config.getBoolean("groups.forwarding.SDC")
40+
val sdr = config.getBoolean("groups.forwarding.SDR")
41+
}
42+
}
43+
class DataBase(config: Configuration) {
44+
val type = config.getString("database.type")
45+
val settings = Settings(config)
46+
class Settings(config: Configuration) {
47+
val sqlite = SQLite(config)
48+
val mysql = MySQL(config)
49+
val pool = Pool(config)
50+
class SQLite(config: Configuration) {
51+
val path = config.getString("database.setting.sqlite.path")?.replace("%plugin_folder%", getDataFolder().path)
52+
}
53+
class MySQL(config: Configuration) {
54+
val host = config.getHost("database.settings.mysql.host")
55+
val port = config.getInt("database.settings.mysql.port")
56+
val database = config.getString("database.settings.mysql.database")
57+
val username = config.getString("database.settings.mysql.username")
58+
val password = config.getString("database.settings.mysql.password")
59+
val flagsURL = config.getString("database.settings.mysql.flagsURL")
60+
}
61+
class Pool(config: Configuration) {
62+
val connectionTimeout = config.getInt("database.settings.pool.connectionTimeout")
63+
val idleTimeout = config.getInt("database.settings.pool.idleTimeout")
64+
val maxLifetime = config.getInt("database.settings.pool.maxLifetime")
65+
val maximumPoolSize = config.getInt("database.settings.pool.maximumPoolSize")
66+
val keepaliveTime = config.getInt("database.settings.pool.keepaliveTime")
67+
val minimumIdle = config.getInt("database.settings.pool.minimumIdle")
68+
}
69+
}
70+
val enable = config.getBoolean("enable")
71+
val host = config.getHost("host")
72+
val port = config.getInt("port")
73+
val database = config.getString("database")
74+
val username = config.getString("username")
75+
val password = config.getString("password")
76+
val table = config.getString("table")
77+
val columns = config.getMap<String, String>("columns")
78+
val prefix = config.getString("prefix")
79+
val suffix = config.getString("suffix")
80+
}
3281
class Bot(config: Configuration){
3382
val mode = config.getString("bot.mode")
34-
val gocqhttp = Gocqhttp(config)
83+
val onebot = Onebot(config)
3584
val kook = Kook(config)
36-
class Gocqhttp(config: Configuration){
37-
val ws = config.getString("bot.go-cqhttp.ws")
38-
val token = config.getString("bot.go-cqhttp.token")
39-
val hasAccessToken = config.getBoolean("bot.go-cqhttp.hasAccessToken")
85+
class Onebot(config: Configuration){
86+
val ws = config.getString("bot.onebot.ws")
87+
val token = Token(config)
88+
class Token(config: Configuration) {
89+
val enable = config.getBoolean("bot.onebot.token.enable")
90+
val token = config.getString("bot.onebot.token.token")
91+
}
4092
}
4193
class Kook(config: Configuration) {
4294
val token = config.getString("bot.kook.token")
4395
}
4496
}
45-
class Forwarding(config: Configuration) {
46-
val enable = config.getBoolean("forwarding.enable")
47-
val mode = config.getInt("forwarding.mode")
48-
val prefix = config.getString("forwarding.prefix")
49-
}
50-
class WhiteList(config: Configuration) {
51-
val enable = config.getBoolean("whiteList.enable")
52-
val maxCount = config.getInt("whitelist.maxCount")
53-
}
54-
class DataBase(config: Configuration) {
55-
val type = config.getString("database.type")
56-
private val sqlitePath =
57-
config.getString("database.setting.sqlite.path")?.replace("%plugin_folder%", getDataFolder().path)
58-
val sqliteHost = newFile(sqlitePath!!, create = true, folder = false).getHost()
59-
val host = config.getHost("database.settings.mysql")
60-
}
97+
val groups = Groups(config)
98+
val database = DataBase(config)
99+
val bot = Bot(config)
61100
}
62101

63102
class Commands(config: Configuration) {
64-
val ver = config.getString("ver")
103+
val version = config.getString("version")
65104
val adminCommand = config.getMap<String, String>("admin")
66105
val userCommand = config.getMap<String, String>("user")
67106
}
68107

69108
class Returns(config: Configuration) {
70-
val ver = config.getString("ver")
109+
val version = config.getString("version")
71110
val adminReturn = config.getMap<String, String>("admin")
72111
val userReturn = config.getMap<String, String>("user")
73112
}

common/src/main/kotlin/me/regadpole/plumbot/config/ConfigResolver.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,23 @@ object ConfigResolver {
6868

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

71-
if ("2.0.0" != config.getConfig().ver) {
71+
if ("2.0.0" != config.getConfig().version) {
7272
plugin.javaClass.getResourceAsStream("/" + configFile.getName()).use { inputStream ->
7373
checkNotNull(inputStream)
7474
Files.copy(inputStream, configFile.toPath(), StandardCopyOption.REPLACE_EXISTING)
7575
loadConfig()
7676
return this
7777
}
7878
}
79-
if ("2.0.0" != config.getCommands().ver) {
79+
if ("2.0.0" != config.getCommands().version) {
8080
plugin.javaClass.getResourceAsStream("/" + commandsFile.getName()).use { inputStream ->
8181
checkNotNull(inputStream)
8282
Files.copy(inputStream, commandsFile.toPath(), StandardCopyOption.REPLACE_EXISTING)
8383
loadConfig()
8484
return this
8585
}
8686
}
87-
if ("2.0.0" != config.getReturns().ver) {
87+
if ("2.0.0" != config.getReturns().version) {
8888
plugin.javaClass.getResourceAsStream("/" + returnsFile.getName()).use { inputStream ->
8989
checkNotNull(inputStream)
9090
Files.copy(inputStream, returnsFile.toPath(), StandardCopyOption.REPLACE_EXISTING)

common/src/main/kotlin/me/regadpole/plumbot/database/SQLite.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import javax.sql.DataSource
88

99
class SQLite: Database {
1010

11-
private val host = PlumBot.getConfig().getConfig().database.sqliteHost
11+
private val host = PlumBot.getConfig().getConfig().database.host
1212
private val dataSource by lazy { host.createDataSource() }
1313

1414
private val table = Table("whitelist", host) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package me.regadpole.plumbot.event
2+
3+
import me.regadpole.plumbot.util.GPlayer
4+
5+
class GDeathEvent(val player: GPlayer)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package me.regadpole.plumbot.event
2+
3+
import me.regadpole.plumbot.util.GPlayer
4+
5+
abstract class GJoinEvent(val player: GPlayer) {
6+
abstract fun kick(message: String)
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package me.regadpole.plumbot.event
2+
3+
import me.regadpole.plumbot.util.GPlayer
4+
5+
class GPlayerChatEvent(val message: String, val player: GPlayer)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package me.regadpole.plumbot.event
2+
3+
import me.regadpole.plumbot.util.GPlayer
4+
5+
class GQuitEvent(val player: GPlayer)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package me.regadpole.plumbot.listener.game
2+
3+
import me.regadpole.plumbot.PlumBot
4+
import me.regadpole.plumbot.event.GDeathEvent
5+
import me.regadpole.plumbot.event.GJoinEvent
6+
import me.regadpole.plumbot.event.GPlayerChatEvent
7+
import me.regadpole.plumbot.event.GQuitEvent
8+
import taboolib.common5.util.replace
9+
10+
class GameListener {
11+
fun onChat(event: GPlayerChatEvent) {
12+
if (PlumBot.getConfig().getConfig().groups.forwarding.message.enable) {
13+
if (PlumBot.getConfig().getConfig().groups.forwarding.message.mode == 1 && !event.message.startsWith(
14+
PlumBot.getConfig().getConfig().groups.forwarding.message.prefix?: "")) {
15+
return
16+
}
17+
var message: String = PlumBot.getLangConfig().getLangConf().forwarding.toPlatform?: return
18+
message = message.replace("%server_name%", event.player.server)
19+
.replace("%world_name%", event.player.position.world)
20+
.replace("%message%", event.message)
21+
PlumBot.getConfig().getConfig().groups.enableGroups.forEach {
22+
PlumBot.getBot().sendGroupMsg(it, message)
23+
}
24+
}
25+
}
26+
27+
fun onDie(event: GDeathEvent) {
28+
if (PlumBot.getConfig().getConfig().groups.forwarding.dieMessage) {
29+
var message: String = PlumBot.getLangConfig().getLangConf().messageOnDie?: return
30+
message = message.replace("%player_name%", event.player.name)
31+
.replace("%world_name%", event.player.position.world)
32+
.replace("%pos%", event.player.position.toXYZ("/"))
33+
PlumBot.getConfig().getConfig().groups.enableGroups.forEach {
34+
PlumBot.getBot().sendGroupMsg(it, message)
35+
}
36+
}
37+
}
38+
39+
fun onJoin(event: GJoinEvent) {
40+
if (PlumBot.getDatabase().getBindByName(event.player.name) == null) {
41+
var message: String = PlumBot.getLangConfig().getLangConf().whitelist.kickServer?: return
42+
message = message.replace("%enable_groups%", PlumBot.getConfig().getConfig().groups.enableGroups.joinToString("/"))
43+
event.kick(message)
44+
message = PlumBot.getLangConfig().getLangConf().whitelist.kickPlatform?: return
45+
message = message.replace("%player_name%", event.player.name)
46+
.replace("%server_name%", event.player.server)
47+
PlumBot.getConfig().getConfig().groups.enableGroups.forEach {
48+
PlumBot.getBot().sendGroupMsg(it, message)
49+
}
50+
} else if (PlumBot.getConfig().getConfig().groups.forwarding.joinAndLeave) {
51+
var message: String = PlumBot.getLangConfig().getLangConf().playerJoinMsg?: return
52+
message = message.replace("%player_name%", event.player.name)
53+
.replace("%server_name%", event.player.server)
54+
PlumBot.getConfig().getConfig().groups.enableGroups.forEach {
55+
PlumBot.getBot().sendGroupMsg(it, message)
56+
}
57+
}
58+
}
59+
60+
fun onQuit(event: GQuitEvent) {
61+
if (PlumBot.getConfig().getConfig().groups.forwarding.joinAndLeave) {
62+
var message: String = PlumBot.getLangConfig().getLangConf().playerLeaveMsg?: return
63+
message = message.replace("%player_name%", event.player.name)
64+
.replace("%server_name%", event.player.server)
65+
PlumBot.getConfig().getConfig().groups.enableGroups.forEach {
66+
PlumBot.getBot().sendGroupMsg(it, message)
67+
}
68+
}
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package me.regadpole.plumbot.util
2+
3+
abstract class GPlayer(val name: String, var position: GPosition, val server: String, val isAdmin: Boolean) {
4+
abstract fun teleport()
5+
abstract fun sendMessage(message: String)
6+
}

0 commit comments

Comments
 (0)