From 217663eae9192777078df13255b49a97c9dd6fb9 Mon Sep 17 00:00:00 2001 From: Martin Graversen Date: Wed, 22 Jan 2020 21:08:39 +0100 Subject: [PATCH] :bug: Correct issue with unexpected string format --- .../rcon/query/playerlist/PlayerListMapper.java | 15 ++++++++++----- .../query/playerlist/PlayerListMapperTest.java | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/graversen/minecraft/rcon/query/playerlist/PlayerListMapper.java b/src/main/java/io/graversen/minecraft/rcon/query/playerlist/PlayerListMapper.java index d42e5d3..e1ff457 100644 --- a/src/main/java/io/graversen/minecraft/rcon/query/playerlist/PlayerListMapper.java +++ b/src/main/java/io/graversen/minecraft/rcon/query/playerlist/PlayerListMapper.java @@ -15,13 +15,18 @@ public class PlayerListMapper implements IRconResponseMapper { @Override public PlayerList apply(RconResponse rconResponse) { - final String[] players = rconResponse.getResponseString().split(PATTERN_INITIAL.pattern()); + if (rconResponse.getResponseString() != null) { + final String responseString = rconResponse.getResponseString().trim(); + final String[] players = responseString.split(PATTERN_INITIAL.pattern()); - if (players.length == 2) { - return extractPlayerUuids(players[1]); - } else { - return new PlayerList(List.of()); + if (players.length == 2) { + return extractPlayerUuids(players[1]); + } else { + return new PlayerList(List.of()); + } } + + return new PlayerList(List.of()); } private PlayerList extractPlayerUuids(String players) { diff --git a/src/test/java/io/graversen/minecraft/rcon/query/playerlist/PlayerListMapperTest.java b/src/test/java/io/graversen/minecraft/rcon/query/playerlist/PlayerListMapperTest.java index 6017fe1..430f8d5 100644 --- a/src/test/java/io/graversen/minecraft/rcon/query/playerlist/PlayerListMapperTest.java +++ b/src/test/java/io/graversen/minecraft/rcon/query/playerlist/PlayerListMapperTest.java @@ -24,7 +24,7 @@ void apply_playersOnline() { @Test void apply_noPlayersOnline() { final var testRconResponse = - new RconResponse(0, 0, 0, 0, "There are 0 of a max 20 players online:"); + new RconResponse(0, 0, 0, 0, "There are 0 of a max 20 players online: "); final var playerList = playerListMapper.apply(testRconResponse);