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"?>
<project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<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 {
execute(SendMessage(it, mcMessageStr(username, text)))
}
}
public fun broadcastToTG(text: String) {
fun broadcastToTG(text: String) {
allowedChats.forEach {
execute(SendMessage(it, text))
}

View File

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

View File

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