1
1
package org.kraftwerk28.spigot_tg_bridge
2
2
3
+ import kotlinx.coroutines.CancellationException
4
+ import kotlinx.coroutines.runInterruptible
5
+ import org.bukkit.configuration.file.YamlConfiguration
3
6
import java.io.File
7
+ import java.nio.file.FileSystems
8
+ import java.nio.file.StandardWatchEventKinds
4
9
import org.kraftwerk28.spigot_tg_bridge.Constants as C
5
10
6
- class Configuration (plugin : Plugin ) {
11
+ class Configuration (plugin : Plugin ) : YamlConfiguration() {
7
12
val isEnabled: Boolean
8
13
val logFromMCtoTG: Boolean
9
14
val telegramFormat: String
@@ -18,6 +23,7 @@ class Configuration(plugin: Plugin) {
18
23
val onlineString: String
19
24
val nobodyOnlineString: String
20
25
val enableIgnAuth: Boolean
26
+ val silentMessages: Boolean?
21
27
22
28
// Telegram bot stuff
23
29
val botToken: String
@@ -26,6 +32,8 @@ class Configuration(plugin: Plugin) {
26
32
val allowWebhook: Boolean
27
33
val webhookConfig: Map <String , Any >?
28
34
val pollTimeout: Int
35
+ val apiOrigin: String
36
+ val debugHttp: Boolean
29
37
30
38
var commands: BotCommands
31
39
@@ -37,74 +45,105 @@ class Configuration(plugin: Plugin) {
37
45
// plugin.saveResource(C.configFilename, false);
38
46
throw Exception (C .WARN .noConfigWarning)
39
47
}
40
- val pluginConfig = plugin.config
41
- pluginConfig.load(cfgFile)
42
48
43
- pluginConfig.getString(" minecraftMessageFormat" )?.let {
49
+ load(cfgFile)
50
+
51
+ if (! getBoolean(" disableConfigWatch" , false )) {
52
+ try {
53
+ val watchService = FileSystems .getDefault().newWatchService()
54
+ val cfgPath = cfgFile.parentFile.toPath()
55
+ val pathKey = cfgPath.register(watchService, StandardWatchEventKinds .ENTRY_MODIFY )
56
+ plugin.launch {
57
+ loop@ while (true ) {
58
+ try {
59
+ val watchKey = runInterruptible { watchService.take() }
60
+ val events = watchKey.pollEvents()
61
+ events.find {
62
+ it.kind() == StandardWatchEventKinds .ENTRY_MODIFY
63
+ }?.let {
64
+ plugin.reload()
65
+ }
66
+ } catch (e: Exception ) {
67
+ when (e) {
68
+ is CancellationException -> break @loop
69
+ else -> {
70
+ e.printStackTrace()
71
+ continue @loop
72
+ }
73
+ }
74
+ }
75
+ }
76
+ pathKey.cancel()
77
+ }
78
+ } catch (e: Exception ) {
79
+ plugin.logger.info(" Failed to set up watch on config file" )
80
+ }
81
+ }
82
+
83
+ getString(" minecraftMessageFormat" )?.let {
44
84
plugin.logger.warning(
45
85
"""
46
86
Config option "minecraftMessageFormat" is deprecated.
47
87
Moved it to new key "telegramFormat"
48
88
""" .trimIndent().replace(' \n ' , ' ' )
49
89
)
50
- pluginConfig. set(" telegramFormat" , it)
51
- pluginConfig. set(" minecraftMessageFormat" , null )
90
+ set(" telegramFormat" , it)
91
+ set(" minecraftMessageFormat" , null )
52
92
plugin.saveConfig()
53
93
}
54
94
55
- pluginConfig. getString(" telegramMessageFormat" )?.let {
95
+ getString(" telegramMessageFormat" )?.let {
56
96
plugin.logger.warning(
57
97
"""
58
98
Config option "telegramMessageFormat" is deprecated.
59
99
Moved it to new key "minecraftFormat"
60
100
""" .trimIndent().replace(' \n ' , ' ' )
61
101
)
62
- pluginConfig. set(" minecraftFormat" , it)
63
- pluginConfig. set(" telegramMessageFormat" , null )
102
+ set(" minecraftFormat" , it)
103
+ set(" telegramMessageFormat" , null )
64
104
plugin.saveConfig()
65
105
}
66
106
67
- pluginConfig.run {
68
- isEnabled = getBoolean(" enable" , true )
69
- serverStartMessage = getString(" serverStartMessage" )
70
- serverStopMessage = getString(" serverStopMessage" )
71
- logFromTGtoMC = getBoolean(" logFromTGtoMC" , true )
72
- logFromMCtoTG = getBoolean(" logFromMCtoTG" , true )
73
- telegramFormat = getString(
74
- " telegramFormat" ,
75
- " <i>%username%</i>: %message%" ,
76
- )!!
77
- minecraftFormat = getString(
78
- " minecraftFormat" ,
79
- " <%username%>: %message%" ,
80
- )!!
81
- // isEnabled = getBoolean("enable", true)
82
- allowedChats = getLongList(" chats" )
83
- enableIgnAuth = getBoolean(" enableIgnAuth" , false )
107
+ isEnabled = getBoolean(" enable" , true )
108
+ serverStartMessage = getString(" serverStartMessage" )
109
+ serverStopMessage = getString(" serverStopMessage" )
110
+ logFromTGtoMC = getBoolean(" logFromTGtoMC" , true )
111
+ logFromMCtoTG = getBoolean(" logFromMCtoTG" , true )
112
+ telegramFormat = getString(
113
+ " telegramFormat" ,
114
+ " <i>%username%</i>: %message%" ,
115
+ )!!
116
+ minecraftFormat = getString(
117
+ " minecraftFormat" ,
118
+ " <%username%>: %message%" ,
119
+ )!!
120
+ // isEnabled = getBoolean("enable", true)
121
+ allowedChats = getLongList(" chats" )
122
+ enableIgnAuth = getBoolean(" enableIgnAuth" , false )
84
123
85
- botToken = getString(" botToken" ) ? : throw Exception (C .WARN .noToken)
86
- allowWebhook = getBoolean(" useWebhook" , false )
87
- @Suppress(" unchecked_cast" )
88
- webhookConfig = get(" webhookConfig" ) as Map <String , Any >?
89
- pollTimeout = getInt(" pollTimeout" , 30 )
90
-
91
- logJoinLeave = getBoolean(" logJoinLeave" , false )
92
- onlineString = getString(" strings.online" , " Online" )!!
93
- nobodyOnlineString = getString(
94
- " strings.nobodyOnline" ,
95
- " Nobody online" ,
96
- )!!
97
- joinString = getString(
98
- " strings.joined" ,
99
- " <i>%username%</i> joined." ,
100
- )!!
101
- leaveString = getString(" strings.left" , " <i>%username%</i> left." )!!
102
- logDeath = getBoolean(" logPlayerDeath" , false )
103
- logPlayerAsleep = getBoolean(" logPlayerAsleep" , false )
104
- commands = BotCommands (this )
105
- }
106
- }
124
+ botToken = getString(" botToken" ) ? : throw Exception (C .WARN .noToken)
125
+ allowWebhook = getBoolean(" useWebhook" , false )
126
+ @Suppress(" unchecked_cast" )
127
+ webhookConfig = get(" webhookConfig" ) as Map <String , Any >?
128
+ pollTimeout = getInt(" pollTimeout" , 30 )
107
129
108
- companion object {
130
+ logJoinLeave = getBoolean(" logJoinLeave" , false )
131
+ onlineString = getString(" strings.online" , " Online" )!!
132
+ nobodyOnlineString = getString(
133
+ " strings.nobodyOnline" ,
134
+ " Nobody online" ,
135
+ )!!
136
+ joinString = getString(
137
+ " strings.joined" ,
138
+ " <i>%username%</i> joined." ,
139
+ )!!
140
+ leaveString = getString(" strings.left" , " <i>%username%</i> left." )!!
141
+ logDeath = getBoolean(" logPlayerDeath" , false )
142
+ logPlayerAsleep = getBoolean(" logPlayerAsleep" , false )
143
+ commands = BotCommands (this )
144
+ // NB: Setting to null, if false, because API expects either `true` or absent parameter
145
+ silentMessages = getBoolean(" silentMessages" ).let { if (! it) null else true }
146
+ apiOrigin = getString(" apiOrigin" , " https://api.telegram.org" )!!
147
+ debugHttp = getBoolean(" debugHttp" , false )
109
148
}
110
149
}
0 commit comments