-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/scala/cc/sukazyo/minecraft_telegram/bot/BotLifecycleEvents.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
) | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/main/scala/cc/sukazyo/minecraft_telegram/sub_modules/carpet/CarpetExtension.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/scala/cc/sukazyo/minecraft_telegram/sub_modules/carpet/CarpetExtensionImpl.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = {} | ||
|
||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
src/main/scala/cc/sukazyo/minecraft_telegram/sub_modules/carpet/QueryFreezeStatus.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
|
||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/scala/cc/sukazyo/minecraft_telegram/utils/ConditionalLoading.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |