chore: refactoring

This commit is contained in:
kraftwerk28 2020-02-09 14:39:00 +02:00
parent 72bbb0a189
commit f8e13d037c
4 changed files with 10 additions and 13 deletions

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings"> <component name="GradleSettings">
<option name="linkedExternalProjectsSettings"> <option name="linkedExternalProjectsSettings">
<GradleProjectSettings> <GradleProjectSettings>

View File

@ -45,13 +45,13 @@ class Bot(plugin: Plugin) : TelegramLongPollingBot() {
) )
} }
public fun sendMessageToTGFrom(username: String, text: String) { fun sendMessageToTGFrom(username: String, text: String) {
allowedChats.forEach { allowedChats.forEach {
execute(SendMessage(it, mcMessageStr(username, text))) execute(SendMessage(it, mcMessageStr(username, text)))
} }
} }
public fun broadcastToTG(text: String) { fun broadcastToTG(text: String) {
allowedChats.forEach { allowedChats.forEach {
execute(SendMessage(it, text)) execute(SendMessage(it, text))
} }

View File

@ -10,7 +10,7 @@ class MessageListener(plugin: Plugin) : Listener {
init { this.plugin = plugin } init { this.plugin = plugin }
@EventHandler @EventHandler
public fun onPlayerChat1(event: AsyncPlayerChatEvent) { fun onPlayerChat1(event: AsyncPlayerChatEvent) {
plugin?.tgBot?.sendMessageToTGFrom( plugin?.tgBot?.sendMessageToTGFrom(
event.player.displayName, event.player.displayName,
event.message event.message

View File

@ -4,35 +4,31 @@ import org.bukkit.plugin.java.JavaPlugin
import org.telegram.telegrambots.ApiContextInitializer import org.telegram.telegrambots.ApiContextInitializer
import org.telegram.telegrambots.meta.TelegramBotsApi import org.telegram.telegrambots.meta.TelegramBotsApi
import java.io.File import java.io.File
import java.nio.file.Paths
class Plugin : JavaPlugin() { class Plugin : JavaPlugin() {
var tgBot: Bot? = null var tgBot: Bot? = null
override fun onEnable() { override fun onEnable() {
super.onEnable()
if (!File("plugins/${name}/config.yml").exists()) { if (!File("plugins/${name}/config.yml").exists()) {
logger.warning("No config file found! Saving default one.") logger.warning("No config file found! Saving default one.")
saveDefaultConfig() saveDefaultConfig()
return return
} }
logger.info(config.getLongList("chats").toString())
ApiContextInitializer.init() ApiContextInitializer.init()
val botsApi = TelegramBotsApi() val botsApi = TelegramBotsApi()
tgBot = Bot(this) tgBot = Bot(this)
botsApi.registerBot(tgBot) botsApi.registerBot(tgBot)
server.pluginManager.registerEvents(MessageListener(this), this) server.pluginManager.registerEvents(MessageListener(this), this)
val startMsg = config.getString("serverStartMessage")
if (startMsg != null) val startMsg = config.getString("serverStartMessage", null)
tgBot?.broadcastToTG(startMsg) if (startMsg != null) tgBot?.broadcastToTG(startMsg)
logger.info("Plugin started") logger.info("Plugin started")
} }
override fun onDisable() { override fun onDisable() {
super.onDisable() val stopMsg = config.getString("serverStopMessage", null)
val stopMsg = config.getString("serverStopMessage") if (stopMsg != null) tgBot?.broadcastToTG(stopMsg)
if (stopMsg != null)
tgBot?.broadcastToTG(stopMsg)
logger.info("Plugin stopped") logger.info("Plugin stopped")
} }
} }