From 539a4f6ad1e4c616f535b5deeb84c2a8a5889eb4 Mon Sep 17 00:00:00 2001 From: Omak2r3a <86030102+kerollosy@users.noreply.github.com> Date: Tue, 28 May 2024 21:59:38 +0300 Subject: [PATCH 1/4] Update CTCommand.kt --- .../com/chattriggers/ctjs/internal/commands/CTCommand.kt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/kotlin/com/chattriggers/ctjs/internal/commands/CTCommand.kt b/src/main/kotlin/com/chattriggers/ctjs/internal/commands/CTCommand.kt index 9b3cde12..c3464b70 100644 --- a/src/main/kotlin/com/chattriggers/ctjs/internal/commands/CTCommand.kt +++ b/src/main/kotlin/com/chattriggers/ctjs/internal/commands/CTCommand.kt @@ -136,6 +136,12 @@ internal object CTCommand : Initializer { } 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)) } From b44f011f9873e92b886ae5b24ee0f2df7908e5a2 Mon Sep 17 00:00:00 2001 From: Omak2r3a <86030102+kerollosy@users.noreply.github.com> Date: Tue, 28 May 2024 22:13:53 +0300 Subject: [PATCH 2/4] Add option to show dependencies confirmation before importing a module --- src/main/kotlin/com/chattriggers/ctjs/api/Config.kt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/kotlin/com/chattriggers/ctjs/api/Config.kt b/src/main/kotlin/com/chattriggers/ctjs/api/Config.kt index 29fc92f3..f57a8ee1 100644 --- a/src/main/kotlin/com/chattriggers/ctjs/api/Config.kt +++ b/src/main/kotlin/com/chattriggers/ctjs/api/Config.kt @@ -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, From 7ec60792268488ebc4e71320cc452b718a8b0c20 Mon Sep 17 00:00:00 2001 From: Omak2r3a <86030102+kerollosy@users.noreply.github.com> Date: Thu, 30 May 2024 14:00:59 +0300 Subject: [PATCH 3/4] Update CTCommand.kt --- .../ctjs/internal/commands/CTCommand.kt | 70 ++++++++++++------- 1 file changed, 46 insertions(+), 24 deletions(-) diff --git a/src/main/kotlin/com/chattriggers/ctjs/internal/commands/CTCommand.kt b/src/main/kotlin/com/chattriggers/ctjs/internal/commands/CTCommand.kt index c3464b70..6f11bfd9 100644 --- a/src/main/kotlin/com/chattriggers/ctjs/internal/commands/CTCommand.kt +++ b/src/main/kotlin/com/chattriggers/ctjs/internal/commands/CTCommand.kt @@ -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 @@ -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, _ -> @@ -118,34 +120,54 @@ 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}") + pendingImport = moduleName + } else { + ChatLib.chat("&cImporting $moduleName...") + performImport(module, dependencies) + } + } + } - if (dependencies.isNotEmpty()) { - val dependencyNames = dependencies.map { it.metadata.name ?: it.name } - ChatLib.chat("&eAlso imported ${dependencyNames.joinToString(", ")}") - } + private fun performImport(module: Module, dependencies: List) { + 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 (Config.moduleImportHelp && module.metadata.helpMessage != null) { - ChatLib.chat(module.metadata.helpMessage.toString().take(383)) - } - } + 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)) } } From 3a032773d8c6ea0b84db514b43e48ca3edcf9dfe Mon Sep 17 00:00:00 2001 From: Omak2r3a <86030102+kerollosy@users.noreply.github.com> Date: Thu, 30 May 2024 14:38:22 +0300 Subject: [PATCH 4/4] Run `:apiDump` --- api/ctjs.api | 2 ++ 1 file changed, 2 insertions(+) diff --git a/api/ctjs.api b/api/ctjs.api index d8afafd8..aa483e89 100644 --- a/api/ctjs.api +++ b/api/ctjs.api @@ -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 @@ -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