Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notify about imported dependencies #124

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/ctjs.api
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public final class com/chattriggers/ctjs/api/Config : gg/essential/vigilance/Vig
public static final fun getConsoleTheme ()I
public static final fun getConsoleWarningColor ()Ljava/awt/Color;
public static final fun getCustomTheme ()Z
public static final fun getDependenciesConfirmation ()Z
public static final fun getModuleChangelog ()Z
public static final fun getModuleImportHelp ()Z
public static final fun getOpenConsoleOnError ()Z
Expand All @@ -56,6 +57,7 @@ public final class com/chattriggers/ctjs/api/Config : gg/essential/vigilance/Vig
public static final fun setConsoleTheme (I)V
public static final fun setConsoleWarningColor (Ljava/awt/Color;)V
public static final fun setCustomTheme (Z)V
public static final fun setDependenciesConfirmation (Z)V
public static final fun setModuleChangelog (Z)V
public static final fun setModuleImportHelp (Z)V
public static final fun setOpenConsoleOnError (Z)V
Expand Down
9 changes: 9 additions & 0 deletions src/main/kotlin/com/chattriggers/ctjs/api/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ object Config : Vigilant(File(CTJS.configLocation, "ChatTriggers.toml"), sorting
)
var autoUpdateModules = true

@JvmStatic
@Property(
PropertyType.SWITCH,
name = "Show Dependencies Confirmation",
category = "General",
description = "Receive a confirmation message listing the dependencies before importing a module.",
)
var dependenciesConfirmation = false

@JvmStatic
@Property(
PropertyType.SWITCH,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.chattriggers.ctjs.api.message.TextComponent
import com.chattriggers.ctjs.internal.commands.StaticCommand.Companion.onExecute
import com.chattriggers.ctjs.engine.Console
import com.chattriggers.ctjs.engine.printTraceToConsole
import com.chattriggers.ctjs.internal.engine.module.Module
import com.chattriggers.ctjs.internal.engine.module.ModuleManager
import com.chattriggers.ctjs.internal.engine.module.ModulesGui
import com.chattriggers.ctjs.internal.listeners.ClientListener
Expand Down Expand Up @@ -37,6 +38,7 @@ import kotlin.concurrent.thread
internal object CTCommand : Initializer {
private const val idFixed = 90123 // ID for dumped chat
private var idFixedOffset = -1 // ID offset (increments)
private var pendingImport: String? = null

override fun init() {
ClientCommandRegistrationCallback.EVENT.register { dispatcher, _ ->
Expand Down Expand Up @@ -118,31 +120,57 @@ internal object CTCommand : Initializer {
private fun import(moduleName: String) {
if (ModuleManager.cachedModules.any { it.name.equals(moduleName, ignoreCase = true) }) {
ChatLib.chat("&cModule $moduleName is already installed!")
} else {
ChatLib.chat("&cImporting $moduleName...")
thread {
val (module, dependencies) = ModuleManager.importModule(moduleName)
if (module == null) {
ChatLib.chat("&cUnable to import module $moduleName")
return@thread
}
return
}

thread {
val (module, dependencies) = ModuleManager.importModule(moduleName)
if (module == null) {
ChatLib.chat("&cUnable to import module $moduleName")
return@thread
}

val allModules = listOf(module) + dependencies
val modVersion = CTJS.MOD_VERSION.toVersion()
allModules.forEach {
val version = it.targetModVersion ?: return@forEach
if (version.majorVersion < modVersion.majorVersion)
ModuleManager.tryReportOldVersion(it)
}
if (pendingImport == moduleName) {
// User has confirmed the import, proceed with the import
pendingImport = null
performImport(module, dependencies)
} else if(Config.dependenciesConfirmation) {
// User has not confirmed the import yet, fetch the dependencies and ask for confirmation
val dependencyNames = dependencies.map { it.metadata.name ?: it.name }
ChatLib.chat("&cImporting $moduleName... This will also import ${dependencyNames.joinToString(", ")}. Are you sure?")
TextComponent(Text.literal("Type `/ct import $moduleName` again or click [Import] to confirm.").styled {
it.withClickEvent(ClickEvent(ClickEvent.Action.RUN_COMMAND, "/ct import $moduleName"))
}).chat()

ChatLib.chat("&aSuccessfully imported ${module.metadata.name ?: module.name}")
if (Config.moduleImportHelp && module.metadata.helpMessage != null) {
ChatLib.chat(module.metadata.helpMessage.toString().take(383))
}
pendingImport = moduleName
} else {
ChatLib.chat("&cImporting $moduleName...")
performImport(module, dependencies)
}
}
}

private fun performImport(module: Module, dependencies: List<Module>) {
val allModules = listOf(module) + dependencies
val modVersion = CTJS.MOD_VERSION.toVersion()
allModules.forEach {
val version = it.targetModVersion ?: return@forEach
if (version.majorVersion < modVersion.majorVersion)
ModuleManager.tryReportOldVersion(it)
}

ChatLib.chat("&aSuccessfully imported ${module.metadata.name ?: module.name}")

if (dependencies.isNotEmpty()) {
val dependencyNames = dependencies.map { it.metadata.name ?: it.name }
ChatLib.chat("&eAlso imported ${dependencyNames.joinToString(", ")}")
}

if (Config.moduleImportHelp && module.metadata.helpMessage != null) {
ChatLib.chat(module.metadata.helpMessage.toString().take(383))
}
}

private fun getUsage() = """
&b&m${ChatLib.getChatBreak()}
&c/ct load &7- &oReloads all of the ChatTriggers modules.
Expand Down