Config is now sorted

This commit is contained in:
GregF 2026-06-09 05:35:52 +00:00 committed by Ryan
parent 7ef00582a5
commit 2c5eeaeef6
2 changed files with 59 additions and 21 deletions

View file

@ -4,7 +4,7 @@ plugins {
}
group 'org.example'
version '1.3'
version '1.4'
repositories {
repositories {

View file

@ -107,14 +107,9 @@ enum class Editors(val data: EditorData) {
override fun save() {
val array = JSONArray()
TableData.npcConfigs.forEach {
array.add(it)
}
val manager = ScriptEngineManager()
val scriptEngine = manager.getEngineByName("JavaScript")
scriptEngine.put("jsonString", array.toJSONString())
scriptEngine.eval("result = JSON.stringify(JSON.parse(jsonString), null, 2)")
val prettyPrintedJson = scriptEngine["result"] as String
TableData.npcConfigs.forEach { array.add(it) }
val prettyPrintedJson = generateJSON(array)
try {
FileWriter(EditorConstants.CONFIG_PATH + File.separator + fileName).use { file ->
@ -147,14 +142,9 @@ enum class Editors(val data: EditorData) {
override fun save() {
val array = JSONArray()
TableData.itemConfigs.forEach {
array.add(it)
}
val manager = ScriptEngineManager()
val scriptEngine = manager.getEngineByName("JavaScript")
scriptEngine.put("jsonString", array.toJSONString())
scriptEngine.eval("result = JSON.stringify(JSON.parse(jsonString), null, 2)")
val prettyPrintedJson = scriptEngine["result"] as String
TableData.itemConfigs.forEach { array.add(it) }
val prettyPrintedJson = generateJSON(array)
try {
FileWriter(EditorConstants.CONFIG_PATH + File.separator + fileName).use { file ->
@ -179,12 +169,15 @@ enum class Editors(val data: EditorData) {
override fun save() {
val array = JSONArray()
TableData.objConfigs.forEach {
array.add(it)
}
TableData.objConfigs.forEach { array.add(it) }
val sortedList = array
.map { it as JSONObject }
.sortedBy { (it["ids"] as String) }
val sortedArray = JSONArray()
sortedList.forEach { sortedArray.add(it) }
val manager = ScriptEngineManager()
val scriptEngine = manager.getEngineByName("JavaScript")
scriptEngine.put("jsonString", array.toJSONString())
scriptEngine.put("jsonString", sortedArray.toJSONString())
scriptEngine.eval("result = JSON.stringify(JSON.parse(jsonString), null, 2)")
val prettyPrintedJson = scriptEngine["result"] as String
@ -421,3 +414,48 @@ private fun saveTable(table: WeightBasedTable): JSONArray{
}
return arr
}
/**
* Sorts and generates a string from an unsorted JSONArray.
* Currently this is only used by NPC_Config or item_config as it assumes each entry has 1 ID.
* Most other tables have some collection of IDs
* ID name and examine will be first if present then everything else will be alphabetical
* If other things wish to be sorted add them to the when statement
* @param array the JSONArray from npc_config or item_config
* @return The string ready to write out
*/
private fun generateJSON(array: JSONArray): String{
val sortedArray = array
.map{ it as JSONObject }
// Sort IDs
.sortedBy { (it["id"] as String).toIntOrNull() ?: Int.MAX_VALUE }
.map { itemJSONObj ->
val sortedKeys = itemJSONObj.keys
.asSequence()
.map { it as String}
.sortedWith(compareBy<String> { key ->
when (key) {
"id" -> 0
"name" -> 1
"examine" -> 2
else -> 3
}
}.thenBy { it })
.toList()
val orderedItem = LinkedHashMap<String, Any?>()
for (key in sortedKeys){
orderedItem[key] = itemJSONObj[key]
}
orderedItem
}
val sortedJSONArray = JSONArray()
sortedArray.forEach { sortedJSONArray.add(it) }
val manager = ScriptEngineManager()
val scriptEngine = manager.getEngineByName("JavaScript")
scriptEngine.put("jsonString", sortedJSONArray.toJSONString())
scriptEngine.eval("result = JSON.stringify(JSON.parse(jsonString), null, 2)")
val prettyPrintedJson = scriptEngine["result"] as String
return prettyPrintedJson
}