Skip to content

Commit

Permalink
working esp order command
Browse files Browse the repository at this point in the history
  • Loading branch information
ManApart committed Jan 5, 2024
1 parent 900abe2 commit f4d5fda
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
28 changes: 18 additions & 10 deletions src/main/kotlin/commands/Esps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ fun esp(args: List<String>) {
subCommand == "sooner" && amount != null -> setModOrder(
toolData.mods,
index,
nextPluginIndex(mods, index, -amount)
nextPluginLoadOrder(mods, index, -amount)
)

subCommand == "later" && amount != null -> setModOrder(
toolData.mods,
index,
nextPluginIndex(mods, index, amount)
nextPluginLoadOrder(mods, index, amount)
)

subCommand == "sooner" -> setModOrder(toolData.mods, index, nextPluginIndex(mods, index, -1))
subCommand == "later" -> setModOrder(toolData.mods, index, nextPluginIndex(mods, index, 1))
subCommand == "sooner" -> setModOrder(toolData.mods, index, nextPluginLoadOrder(mods, index, -1))
subCommand == "later" -> setModOrder(toolData.mods, index, nextPluginLoadOrder(mods, index, 1))
else -> println("Unknown subCommand: ")
}
display(getModsWithPlugins())
Expand All @@ -69,25 +69,33 @@ private fun getModsWithPlugins(): Map<Mod, List<File>> {
.filter { (_, files) -> files.isNotEmpty() }
}

private typealias NewModIndex = Int
private fun nextPluginLoadOrder(mods: Map<Mod, List<File>>, modIndexToShift: Int, pluginShiftAmount: Int): Int {
return nextPlugin(mods, modIndexToShift, pluginShiftAmount)?.loadOrder ?: 0
}

private fun nextPluginIndex(mods: Map<Mod, List<File>>, modIndexToShift: Int, pluginShiftAmount: Int): Int {
return nextPlugin(mods, modIndexToShift, pluginShiftAmount)?.index ?: -1
}

private fun nextPluginIndex(mods: Map<Mod, List<File>>, modIndexToShift: Int, pluginShiftAmount: Int): NewModIndex {
private fun nextPlugin(mods: Map<Mod, List<File>>, modIndexToShift: Int, pluginShiftAmount: Int): Mod? {
val sortedMods = mods.keys.toList().sortedBy { it.loadOrder }
val i = sortedMods.firstOrNull { it.index == modIndexToShift }?.let { sortedMods.indexOf(it) } ?: -1
val mod = sortedMods.getOrNull(i + pluginShiftAmount)
return mod?.index ?: -1
return sortedMods.getOrNull(i + pluginShiftAmount)
}

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

clearConsole()
val columns = listOf(
Column("Id", 7),
Column("Load", 7, true),
Column("Index", 7, true),
Column("Esp", 30),
Column("Esp", espWidth),
Column("Mod", 22),
)
val data = mods.flatMap { (mod, files) -> files.map { mod to it } }.map { (mod, file) ->
val data = modFiles.map { (mod, file) ->
with(mod) {
val idClean = id?.toString() ?: "?"
mapOf(
Expand Down
8 changes: 7 additions & 1 deletion src/main/kotlin/commands/Order.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import Mod
import red
import save
import toolData

Expand Down Expand Up @@ -58,7 +59,12 @@ fun parseArgs(args: List<String>): Args? {

fun setModOrder(mods: List<Mod>, modIndex: Int, position: Int) {
if (position < 0) return
val mod = mods.getOrNull(modIndex) ?: return
val mod = mods.getOrNull(modIndex)
if (mod == null){
println(red("No mod found at $modIndex"))
return
}
println("Setting mod $modIndex at position ${mod.loadOrder} to position $position")
val oldOrder = mod.loadOrder
mods.filter { it.loadOrder > oldOrder }.forEach { it.loadOrder -= 1 }
mods.filter { it.loadOrder >= position }.forEach { it.loadOrder += 1 }
Expand Down

0 comments on commit f4d5fda

Please sign in to comment.