Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
mod usage message, lazy prettified version
Browse files Browse the repository at this point in the history
  • Loading branch information
homchom committed Jan 5, 2024
1 parent 565d578 commit fa1a64c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
9 changes: 4 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ tasks {
// these properties can be used in fabric_mod_json_template.txt in Groovy template syntax
val exposedProperties = arrayOf(
"modName" to modName,
"version" to version,
"version" to modVersion,
"minecraftVersion" to minecraftVersion,
"loaderVersion" to loaderVersion,
"fabricVersion" to fabricVersion
Expand All @@ -125,7 +125,7 @@ tasks {

// evaluate fabric_mod_json_template.txt as a Groovy template
filesMatching("fabric_mod_json_template.txt") {
val metadataRegex = Regex("""\+[\d.]+?$""")
val metadataRegex = Regex("""\+.+$""")
expand(
*exposedProperties,
"metadataRegex" to metadataRegex.toPattern(),
Expand Down Expand Up @@ -174,14 +174,13 @@ modrinth {
projectId.set("recode")
versionNumber.set(modVersionWithMeta)

val match = Regex("""-(beta|alpha)(\.)?""").find(modVersion)
val match = Regex("""-(beta|alpha)\.""").find(modVersion)
if (match == null) {
versionName.set(modVersion)
versionType.set("release")
} else {
val type = match.groupValues[1]
val replacement = if (match.groups.size == 3) " $type " else " $type"
versionName.set(modVersion.replaceRange(match.range, replacement))
versionName.set(modVersion.replaceRange(match.range, " $type "))
versionType.set(type)
}

Expand Down
39 changes: 36 additions & 3 deletions src/main/java/io/github/homchom/recode/Recode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package io.github.homchom.recode

import com.google.gson.Gson
import com.google.gson.GsonBuilder
import io.github.homchom.recode.event.listenEachFrom
import io.github.homchom.recode.feature.automation.AutoCommands
import io.github.homchom.recode.feature.visual.FBuiltInResourcePacks
import io.github.homchom.recode.feature.visual.FCodeSearch
import io.github.homchom.recode.feature.visual.FSignRenderDistance
import io.github.homchom.recode.game.QuitGameEvent
import io.github.homchom.recode.hypercube.JoinDFDetector
import io.github.homchom.recode.mod.commands.CommandHandler
import io.github.homchom.recode.mod.config.Config
import io.github.homchom.recode.mod.config.internal.ConfigFile
Expand All @@ -22,8 +22,13 @@ import io.github.homchom.recode.mod.events.LegacyEventHandler
import io.github.homchom.recode.sys.hypercube.codeaction.ActionDump
import io.github.homchom.recode.sys.hypercube.templates.TemplateStorageHandler
import io.github.homchom.recode.sys.networking.websocket.SocketHandler
import io.github.homchom.recode.ui.showRecodeMessage
import io.github.homchom.recode.ui.text.literalText
import io.github.homchom.recode.util.regex.groupValue
import io.github.homchom.recode.util.regex.regex
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback
import net.fabricmc.loader.api.FabricLoader
import net.fabricmc.loader.api.ModContainer
Expand All @@ -35,12 +40,29 @@ import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors

object Recode : ModContainer {
val version: String get() = metadata.version.friendlyString
/**
* A prettified version of the mod's version. For example, "0.1.2-beta.3" is prettified to "0.1.2 beta 3".
*/
// TODO: i assume there isn't a way to share this code with the buildscript?
val version: String by lazy {
val raw = metadata.version.friendlyString
val preReleaseMatch = regex {
str('-')
val phase by group { str("alpha"); or; str("beta") }
str('.')
}.find(raw)
if (preReleaseMatch == null) raw else {
val phase = preReleaseMatch.groupValue("phase")
raw.replaceRange(preReleaseMatch.range, " $phase ")
}
}

private val container by lazy { FabricLoader.getInstance().getModContainer(MOD_ID).get() }

private var isInitialized = false

private val power = Power(onEnable = { registerTopLevelListeners() })

// initialize features TODO: replace with FeatureGroups during config refactor
init {
// Automation
Expand All @@ -64,12 +86,23 @@ object Recode : ModContainer {
LegacyRecode.onInitialize()

@OptIn(DelicateCoroutinesApi::class)
QuitGameEvent.listenEachFrom(GlobalScope) { close() }
GlobalScope.launch { power.up() }

isInitialized = true
logInfo("initialized successfully")
}

// register globally active listeners that aren't feature-related
private fun Power.registerTopLevelListeners() {
// handle close
QuitGameEvent.listenEach { close() }

// show mod usage messages
JoinDFDetector.listenEach {
showRecodeMessage(literalText("You are using recode, version $version"))
}
}

private fun close() {
logInfo("closing...")

Expand Down

0 comments on commit fa1a64c

Please sign in to comment.