Skip to content

Commit

Permalink
add carpet freezing status query
Browse files Browse the repository at this point in the history
  • Loading branch information
Eyre-S committed Jul 20, 2024
1 parent 3dc98ea commit 9b786df
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 1 deletion.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ dependencies {
modImplementation "com.kotori316:scalable-cats-force-fabric:3.0.0:dev"
modImplementation "net.fabricmc:fabric-language-kotlin:1.11.0+kotlin.2.0.0"

// modCompileOnly "carpet:fabric-carpet:1.20-1.4.112+v230608"
modImplementation "carpet:fabric-carpet:1.20-1.4.112+v230608"

implementation include("com.github.pengrad:java-telegram-bot-api:7.7.0")
implementation include("cc.sukazyo:morny-system-lib:2.0.0-alpha20-SNAPSHOT")
implementation include("cc.sukazyo:da4a:0.2.0-SNAPSHOT")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cc.sukazyo.minecraft_telegram

import cc.sukazyo.minecraft_telegram.bot.{Bot, BotConfig}
import cc.sukazyo.minecraft_telegram.config.{Config, ConfigManager}
import cc.sukazyo.minecraft_telegram.sub_modules.carpet.CarpetExtension
import cc.sukazyo.minecraft_telegram.utils.Log4jExtension.*
import cc.sukazyo.restools.ResourcePackage
import net.fabricmc.api.ModInitializer
Expand Down Expand Up @@ -58,6 +59,8 @@ object ModMinecraftTelegram extends ModInitializer {
ServerLifecycleEvents.SERVER_STARTING `register` this.ServerStartingCallback
ServerLifecycleEvents.SERVER_STOPPING `register` this.ServerStoppingCallback

CarpetExtension().init()

}

}
7 changes: 6 additions & 1 deletion src/main/scala/cc/sukazyo/minecraft_telegram/bot/Bot.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class Bot (config: BotConfig)(using logger: Logger) {
val eventManager: UpdateManager = UpdateManager()
val queryManager: InlineQueryManager = InlineQueryManager()

val startEpochMillis: Long = System.currentTimeMillis()

private object actionRunner extends ActionRunner

logger `info` s"Logged in to bot successfully : bot @${bot_user.username}[${bot_user.id}]"
Expand All @@ -34,13 +36,15 @@ class Bot (config: BotConfig)(using logger: Logger) {
ServerLifecycleEvents.SERVER_STOPPED `register` i.ServerStopped

eventManager += BotIgnoringOutdatedMessage()

BotLifecycleEvents.BOT_INITIALIZING.invoker.onBotInitializing(this)

eventManager += OnMinecraftCommandExecute()
eventManager += OnTelegram2Minecraft()
eventManager += queryManager.QueryEventListener

queryManager += ListUsers()

val startEpochMillis: Long = System.currentTimeMillis()
this.start()

def start (): Unit = {
Expand All @@ -51,6 +55,7 @@ class Bot (config: BotConfig)(using logger: Logger) {

def shutdown (): Unit = {
account.removeGetUpdatesListener()
BotLifecycleEvents.BOT_SHUTTING_DOWN.invoker.onBotShuttingDown(this)
actionRunner.setDisabled()
logger `info` "Stopped Telegram bot"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cc.sukazyo.minecraft_telegram.bot

import net.fabricmc.fabric.api.event.{Event, EventFactory}

object BotLifecycleEvents {

trait BotInitializing:
def onBotInitializing (bot: Bot): Unit
val BOT_INITIALIZING: Event[BotInitializing] = EventFactory.createArrayBacked(
classOf[BotInitializing],
cbs => bot => cbs.foreach(_.onBotInitializing(bot))
)

trait BotShuttingDown:
def onBotShuttingDown (bot: Bot): Unit
val BOT_SHUTTING_DOWN: Event[BotShuttingDown] = EventFactory.createArrayBacked(
classOf[BotShuttingDown],
cbs => bot => cbs.foreach(_.onBotShuttingDown(bot))
)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package cc.sukazyo.minecraft_telegram.sub_modules.carpet

import cc.sukazyo.minecraft_telegram.utils.ConditionalLoading

class CarpetExtension extends ConditionalLoading {
override val conditionMod = "carpet"
override val loadClass = "cc.sukazyo.minecraft_telegram.sub_modules.carpet.CarpetExtensionImpl"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cc.sukazyo.minecraft_telegram.sub_modules.carpet

import cc.sukazyo.minecraft_telegram.ModMinecraftTelegram
import cc.sukazyo.minecraft_telegram.bot.{Bot, BotLifecycleEvents}
import cc.sukazyo.minecraft_telegram.utils.ConditionalLoading

object CarpetExtensionImpl extends ConditionalLoading.Impl {

def init (): Unit = {

ModMinecraftTelegram.logger.info("Carpet is loaded, enabling telegram connector extensions for carpet.")
BotLifecycleEvents.BOT_INITIALIZING.register(BotExtension)
BotLifecycleEvents.BOT_SHUTTING_DOWN.register(BotExtension)

}

private object BotExtension extends BotLifecycleEvents.BotInitializing with BotLifecycleEvents.BotShuttingDown {

override def onBotInitializing (bot: Bot): Unit = {
bot.queryManager += QueryFreezeStatus()
}

override def onBotShuttingDown (bot: Bot): Unit = {}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cc.sukazyo.minecraft_telegram.sub_modules.carpet

import carpet.fakes.MinecraftServerInterface
import cc.sukazyo.cono.morny.system.telegram_api.inline_query.{InlineQueryUnit, ITelegramQuery}
import cc.sukazyo.minecraft_telegram.connector.Predef
import com.pengrad.telegrambot.model.Update
import com.pengrad.telegrambot.model.request.InlineQueryResultArticle

class QueryFreezeStatus extends ITelegramQuery with Predef {

override def query (event: Update): List[InlineQueryUnit[?]] | Null = {
import event.inlineQuery

// todo: there may be a config to enable/disable
if inlineQuery.query.nonEmpty then return null

val trm = minecraftServer.asInstanceOf[MinecraftServerInterface].getTickRateManager
val runningStatus =
if trm.gameIsPaused then
if trm.deeplyFrozen then
"DEEP FROZEN"
else "FROZEN"
else "running"

List(
InlineQueryUnit(
InlineQueryResultArticle(
s"[mc-tg/carpet/freezing/${System.currentTimeMillis}]",
s"The world is $runningStatus.",
s"The world is $runningStatus.",
)
).cacheTime(1).isPersonal(false)
)

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cc.sukazyo.minecraft_telegram.utils

import net.fabricmc.loader.api.FabricLoader
import java.lang.reflect.InvocationTargetException

object ConditionalLoading {
trait Impl:
def init(): Unit
}

trait ConditionalLoading {

val conditionMod: String
val loadClass: String

def init (): AnyRef = {
if (FabricLoader.getInstance.isModLoaded(this.conditionMod))
try
Class.forName(loadClass).getMethod("init").invoke(null)
catch case e: (ClassNotFoundException | InvocationTargetException | NoSuchMethodException | IllegalAccessException) =>
throw new RuntimeException(e)
null
}

}

0 comments on commit 9b786df

Please sign in to comment.