Skip to content

Commit

Permalink
esp order within mod
Browse files Browse the repository at this point in the history
  • Loading branch information
ManApart committed Jun 23, 2024
1 parent d049b6e commit cb1baf0
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions src/main/kotlin/commands/Esps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import Column
import Mod
import Table
import clearConsole
import save
import toolData
import java.io.File

val espTypes = listOf("esp", "esm", "esl")

Expand All @@ -16,6 +16,7 @@ val espDescription = """
esp 1 set 4 - sets mod with index 1 to load order 4. Any plugins with a higher number for load order have their number increased
esp 1 later - sets the plugin to load after the next plugin (may shift the order number more than one as it shifts until it's after the next plugin).
esp 1 later 2 - same as above but shifts down by two plugins
esp 1 sub 2 set 0 - Used to re-order plugins within a single mod. Finds the plugin of mod 1 that is in sub load order 2 and sets it to sub load order 0.
For general mod order, see order command. The same order number is used for both commands.
""".trimIndent()

Expand All @@ -28,6 +29,7 @@ val espUsage = """
esp 1 sooner 5
esp 1 later
esp 1 set 4
esp 1 sub 1 set 0
""".trimIndent()

fun esp(args: List<String>) {
Expand All @@ -38,7 +40,7 @@ fun esp(args: List<String>) {
if (mods.isEmpty()) {
println("Found no plugins. Consider running esp refresh")
} else {
display(getModsWithPlugins())
display(mods)
}
return
}
Expand All @@ -48,10 +50,12 @@ fun esp(args: List<String>) {
if (i != null) {
toolData.byIndex(i)?.let {
it.refreshPlugins()
save()
println("Refreshed Plugins for mod ${it.description()}")
}
} else {
refreshPlugins()
save()
println("Refreshed Plugins")
}
return
Expand Down Expand Up @@ -82,6 +86,7 @@ fun esp(args: List<String>) {

subCommand == "sooner" -> setModOrder(toolData.mods, index, nextPluginLoadOrder(mods, index, -1))
subCommand == "later" -> setModOrder(toolData.mods, index, nextPluginLoadOrder(mods, index, 1))
subCommand == "sub" -> setSubOrder(index, amount ?: 0, args.last().toIntOrNull() ?: 0)
else -> println("Unknown subCommand: ")
}
display(getModsWithPlugins())
Expand Down Expand Up @@ -114,23 +119,25 @@ private fun nextPlugin(mods: Map<Mod, List<String>>, modIndexToShift: Int, plugi
}

private fun display(mods: Map<Mod, List<String>>) {
val modFiles = mods.flatMap { (mod, files) -> files.map { mod to it } }
val espWidth = modFiles.maxOf { it.second.length } + 5
val modFiles = mods.flatMap { (mod, files) -> files.mapIndexed { i, plugin -> mod to Pair(i, plugin) } }
val espWidth = modFiles.maxOf { it.second.second.length } + 5

clearConsole()
val columns = listOf(
Column("Id", 7),
Column("Load", 7, true),
Column("Load", 7),
Column("Index", 7, true),
Column("Esp", espWidth),
Column("Mod", 22),
)
val data = modFiles.map { (mod, plugin) ->
val data = modFiles.map { (mod, pluginPair) ->
with(mod) {
val (i, plugin) = pluginPair
val idClean = id?.toString() ?: "?"
val order = if (i == 0) "$loadOrder" else "$loadOrder-$i"
mapOf(
"Index" to mod.index,
"Load" to loadOrder,
"Load" to order,
"Id" to idClean,
"Esp" to plugin,
"Mod" to name,
Expand All @@ -139,3 +146,17 @@ private fun display(mods: Map<Mod, List<String>>) {
}
Table(columns, data).print()
}

private fun setSubOrder(index: Int, initialOrder: Int, newOrder: Int) {
toolData.byIndex(index)?.let { mod ->
val plugin = mod.plugins.getOrNull(initialOrder)
if (plugin == null) println("No Plugin found at $initialOrder") else {
println("${mod.description()}: Setting '$plugin' at position $initialOrder to $newOrder")
val newPlugins = mod.plugins.toMutableList()
newPlugins.remove(plugin)
newPlugins.add(newOrder, plugin)
mod.plugins = newPlugins
save()
}
}
}

0 comments on commit cb1baf0

Please sign in to comment.