diff --git a/build.gradle b/build.gradle index 825a134..6d81cdc 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group 'org.example' -version '1.3' +version '1.4' repositories { repositories { diff --git a/src/main/kotlin/Editors.kt b/src/main/kotlin/Editors.kt index 9e50a7c..8c67b32 100644 --- a/src/main/kotlin/Editors.kt +++ b/src/main/kotlin/Editors.kt @@ -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 { key -> + when (key) { + "id" -> 0 + "name" -> 1 + "examine" -> 2 + else -> 3 + } + }.thenBy { it }) + .toList() + + val orderedItem = LinkedHashMap() + 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 +} \ No newline at end of file