clues: Fix three step cryptic clue getLocations NPE

Prior to this commit, ThreeStepCrypticClue simply returned a
concatenation of the active step locations without filtering the mapped
locations in any way. This could lead to NPEs in the plugin as some
cryptic clues have null locations for steps which have no specific
location or have a variable location. This commit addresses this by
making CrypticClue's location field `@Nullable`, filtering null
locations from ThreeStepCrypticClue's getLocations stream, and by adding
a test to ensure ThreeStepCrypticClue's getLocations method cannot yield
any null entries in its return value.
This commit is contained in:
Jordan Atwood
2021-02-23 23:54:32 -08:00
committed by Adam
parent c271580e74
commit 70dc7f56bc
3 changed files with 21 additions and 1 deletions

View File

@@ -330,6 +330,7 @@ public class CrypticClue extends ClueScroll implements TextClueScroll, NpcClueSc
private final String text;
private final String npc;
private final int objectId;
@Nullable
private final WorldPoint location;
private final String solution;
@Nullable
@@ -371,7 +372,7 @@ public class CrypticClue extends ClueScroll implements TextClueScroll, NpcClueSc
this(text, npc, objectId, location, solution, null);
}
private CrypticClue(String text, String npc, int objectId, WorldPoint location, String solution, @Nullable String questionText)
private CrypticClue(String text, String npc, int objectId, @Nullable WorldPoint location, String solution, @Nullable String questionText)
{
this.text = text;
this.npc = npc;

View File

@@ -30,6 +30,7 @@ import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import net.runelite.api.InventoryID;
@@ -167,6 +168,7 @@ public class ThreeStepCrypticClue extends ClueScroll implements TextClueScroll,
return clueSteps.stream()
.filter(s -> !s.getValue())
.map(s -> s.getKey().getLocation())
.filter(Objects::nonNull)
.toArray(WorldPoint[]::new);
}

View File

@@ -24,6 +24,10 @@
*/
package net.runelite.client.plugins.cluescrolls.clues;
import com.google.common.base.Joiner;
import net.runelite.api.coords.WorldPoint;
import net.runelite.client.util.Text;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import org.junit.Test;
@@ -34,4 +38,17 @@ public class ThreeStepCrypticClueTest
{
assertNull(ThreeStepCrypticClue.forText("", ""));
}
@Test
public void nonNullLocations()
{
final String clueText = Joiner.on("<br><br>").join(CrypticClue.CLUES.stream().map(CrypticClue::getText).toArray());
final ThreeStepCrypticClue clue = ThreeStepCrypticClue.forText(Text.sanitizeMultilineText(clueText).toLowerCase(), clueText);
assertNotNull(clue);
for (final WorldPoint location : clue.getLocations())
{
assertNotNull(location);
}
}
}