diff --git a/pom.xml b/pom.xml index 22dace4..8aa692a 100644 --- a/pom.xml +++ b/pom.xml @@ -40,6 +40,13 @@ tinylog-impl 2.0.1 + + + org.junit.jupiter + junit-jupiter-engine + 5.4.0 + test + diff --git a/src/main/java/io/graversen/minecraft/rcon/query/playerlist/PlayerList.java b/src/main/java/io/graversen/minecraft/rcon/query/playerlist/PlayerList.java index 120ab6a..c2ca7d0 100644 --- a/src/main/java/io/graversen/minecraft/rcon/query/playerlist/PlayerList.java +++ b/src/main/java/io/graversen/minecraft/rcon/query/playerlist/PlayerList.java @@ -3,13 +3,13 @@ import java.util.List; public class PlayerList { - private final List playerNames; + private final List playerUuids; - PlayerList(List playerNames) { - this.playerNames = playerNames; + PlayerList(List playerUuids) { + this.playerUuids = playerUuids; } - public List getPlayerNames() { - return playerNames; + public List getPlayerUuids() { + return playerUuids; } } 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 a1367c1..d42e5d3 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 @@ -2,18 +2,34 @@ import io.graversen.minecraft.rcon.RconResponse; import io.graversen.minecraft.rcon.query.IRconResponseMapper; +import org.apache.commons.lang3.StringUtils; +import java.util.Arrays; import java.util.List; +import java.util.regex.Pattern; +import java.util.stream.Collectors; public class PlayerListMapper implements IRconResponseMapper { + static final Pattern PATTERN_INITIAL = Pattern.compile(":"); + static final Pattern PATTERN_PLAYERS = Pattern.compile(","); + @Override public PlayerList apply(RconResponse rconResponse) { - final String[] players = rconResponse.getResponseString().split(":"); + final String[] players = rconResponse.getResponseString().split(PATTERN_INITIAL.pattern()); if (players.length == 2) { - return new PlayerList(List.of(players[1].split(","))); + return extractPlayerUuids(players[1]); } else { return new PlayerList(List.of()); } } + + private PlayerList extractPlayerUuids(String players) { + final var playerUuids = Arrays.stream(players.split(PATTERN_PLAYERS.pattern())) + .map(String::trim) + .map(player -> StringUtils.substringBetween(player, "(", ")")) + .collect(Collectors.toUnmodifiableList()); + + return new PlayerList(playerUuids); + } } 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 new file mode 100644 index 0000000..6017fe1 --- /dev/null +++ b/src/test/java/io/graversen/minecraft/rcon/query/playerlist/PlayerListMapperTest.java @@ -0,0 +1,34 @@ +package io.graversen.minecraft.rcon.query.playerlist; + +import io.graversen.minecraft.rcon.RconResponse; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class PlayerListMapperTest { + private final PlayerListMapper playerListMapper = new PlayerListMapper(); + + @Test + void apply_playersOnline() { + final var testRconResponse = + new RconResponse(0, 0, 0, 0, "There are 2 of a max 20 players online: MrSkurk (ab9b6457-e657-4a9c-ace6-22a291f92035), test (bb9b6457-e657-4a9c-ace6-22a291f92035)"); + + final var playerList = playerListMapper.apply(testRconResponse); + + assertNotNull(playerList); + assertEquals(2, playerList.getPlayerUuids().size()); + assertEquals("ab9b6457-e657-4a9c-ace6-22a291f92035", playerList.getPlayerUuids().get(0)); + assertEquals("bb9b6457-e657-4a9c-ace6-22a291f92035", playerList.getPlayerUuids().get(1)); + } + + @Test + void apply_noPlayersOnline() { + final var testRconResponse = + new RconResponse(0, 0, 0, 0, "There are 0 of a max 20 players online:"); + + final var playerList = playerListMapper.apply(testRconResponse); + + assertNotNull(playerList); + assertTrue(playerList.getPlayerUuids().isEmpty()); + } +} \ No newline at end of file