From 131a123eeba9d1068dccad513cf67ba7b3034eb8 Mon Sep 17 00:00:00 2001 From: Ceikry Date: Thu, 1 Sep 2022 14:34:27 +0000 Subject: [PATCH] Fixed dialogue splitLines edge case --- Server/src/main/kotlin/api/DialUtils.kt | 9 +++++++-- Server/src/test/kotlin/APITests.kt | 10 ++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Server/src/main/kotlin/api/DialUtils.kt b/Server/src/main/kotlin/api/DialUtils.kt index d5361bd59..a24683e46 100644 --- a/Server/src/main/kotlin/api/DialUtils.kt +++ b/Server/src/main/kotlin/api/DialUtils.kt @@ -7,7 +7,7 @@ import kotlin.math.ceil * Should this not work out for any reason, you should fallback to standard npc and player methods for dialogue. */ fun splitLines(message: String, perLineLimit: Int = 54) : Array { - val lines = Array(ceil(message.length / perLineLimit.toFloat()).toInt()) {""} + var lines = Array(ceil(message.length / perLineLimit.toFloat()).toInt()) {""} //short circuit when possible because it's cheaper. if (lines.size == 1) { @@ -22,7 +22,12 @@ fun splitLines(message: String, perLineLimit: Int = 54) : Array { fun pushLine() { if (line.isEmpty()) return - lines[index++] = line.toString() + //allow array to be resized - there are specific edgecases where it becomes necessary. (Check the unit test for example) + if (lines.size == index) + lines = lines.plus(line.toString()) + else + lines[index] = line.toString() + index++ line.clear() accumulator = 0 } diff --git a/Server/src/test/kotlin/APITests.kt b/Server/src/test/kotlin/APITests.kt index 991bccc38..c4042d6cf 100644 --- a/Server/src/test/kotlin/APITests.kt +++ b/Server/src/test/kotlin/APITests.kt @@ -4,6 +4,7 @@ import core.game.node.entity.skill.slayer.Master import core.game.node.entity.skill.slayer.Tasks import org.json.simple.JSONObject import org.json.simple.parser.JSONParser +import org.junit.Assert import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Test import rs09.game.node.entity.skill.slayer.SlayerManager @@ -162,5 +163,14 @@ class APITests { lines = splitLines(testCase) Assertions.assertEquals(testCase, lines[0]) Assertions.assertEquals(1, lines.size) + + testCase = "I just told you: from the Seer. You will need to persuade him to take the time to make a forecast somehow." + lines = splitLines(testCase) + expectedLine1 = "I just told you: from the Seer. You will need to" + expectedLine2 = "persuade him to take the time to make a forecast" + expectedLine3 = "somehow." + Assertions.assertEquals(expectedLine1, lines.getOrNull(0) ?: "") + Assertions.assertEquals(expectedLine2, lines.getOrNull(1) ?: "") + Assertions.assertEquals(expectedLine3, lines.getOrNull(2) ?: "") } } \ No newline at end of file