diff --git a/src/main/java/io/graversen/minecraft/rcon/commands/PlayerListCommand.java b/src/main/java/io/graversen/minecraft/rcon/commands/PlayerListCommand.java index dbdb679..fff3158 100644 --- a/src/main/java/io/graversen/minecraft/rcon/commands/PlayerListCommand.java +++ b/src/main/java/io/graversen/minecraft/rcon/commands/PlayerListCommand.java @@ -1,10 +1,27 @@ package io.graversen.minecraft.rcon.commands; import io.graversen.minecraft.rcon.commands.base.ICommand; +import org.apache.commons.text.StringSubstitutor; + +import java.util.Map; public class PlayerListCommand implements ICommand { + private final boolean uuids; + + private PlayerListCommand(boolean uuids) { + this.uuids = uuids; + } + + public static PlayerListCommand uuids() { + return new PlayerListCommand(true); + } + + public static PlayerListCommand names() { + return new PlayerListCommand(false); + } + @Override public String command() { - return "list uuids"; + return StringSubstitutor.replace("list ${option}", Map.of("option", uuids ? "uuids" : "")).trim(); } } diff --git a/src/main/java/io/graversen/minecraft/rcon/query/playerlist/PlayerNames.java b/src/main/java/io/graversen/minecraft/rcon/query/playerlist/PlayerNames.java new file mode 100644 index 0000000..6f909a4 --- /dev/null +++ b/src/main/java/io/graversen/minecraft/rcon/query/playerlist/PlayerNames.java @@ -0,0 +1,15 @@ +package io.graversen.minecraft.rcon.query.playerlist; + +import java.util.List; + +public class PlayerNames { + private final List playerNames; + + PlayerNames(List playerNames) { + this.playerNames = playerNames; + } + + public List getPlayerNames() { + return playerNames; + } +} diff --git a/src/main/java/io/graversen/minecraft/rcon/query/playerlist/PlayerNamesMapper.java b/src/main/java/io/graversen/minecraft/rcon/query/playerlist/PlayerNamesMapper.java new file mode 100644 index 0000000..b45c486 --- /dev/null +++ b/src/main/java/io/graversen/minecraft/rcon/query/playerlist/PlayerNamesMapper.java @@ -0,0 +1,41 @@ +package io.graversen.minecraft.rcon.query.playerlist; + +import io.graversen.minecraft.rcon.RconResponse; +import io.graversen.minecraft.rcon.query.IRconResponseMapper; + +import java.util.Arrays; +import java.util.List; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +public class PlayerNamesMapper implements IRconResponseMapper { + static final Pattern PATTERN_INSIDE_PARENTHESIS = Pattern.compile("\\(.*\\)"); + static final Pattern PATTERN_INITIAL = Pattern.compile(":"); + static final Pattern PATTERN_PLAYERS = Pattern.compile(","); + + @Override + public PlayerNames apply(RconResponse rconResponse) { + if (rconResponse.getResponseString() != null) { + final String responseString = rconResponse.getResponseString().trim(); + final String[] players = responseString.split(PATTERN_INITIAL.pattern()); + + if (players.length == 2) { + return extractPlayerList(players[1]); + } else { + return new PlayerNames(List.of()); + } + } + + return new PlayerNames(List.of()); + + } + + private PlayerNames extractPlayerList(String playersRaw) { + final var players = Arrays.stream(playersRaw.split(PATTERN_PLAYERS.pattern())) + .map(playerRaw -> playerRaw.replaceAll(PATTERN_INSIDE_PARENTHESIS.pattern(), "")) + .map(String::trim) + .collect(Collectors.toUnmodifiableList()); + + return new PlayerNames(players); + } +} diff --git a/src/main/java/io/graversen/minecraft/rcon/query/playerlist/PlayerList.java b/src/main/java/io/graversen/minecraft/rcon/query/playerlist/PlayerUuids.java similarity index 77% rename from src/main/java/io/graversen/minecraft/rcon/query/playerlist/PlayerList.java rename to src/main/java/io/graversen/minecraft/rcon/query/playerlist/PlayerUuids.java index c2ca7d0..5339edb 100644 --- a/src/main/java/io/graversen/minecraft/rcon/query/playerlist/PlayerList.java +++ b/src/main/java/io/graversen/minecraft/rcon/query/playerlist/PlayerUuids.java @@ -2,10 +2,10 @@ import java.util.List; -public class PlayerList { +public class PlayerUuids { private final List playerUuids; - PlayerList(List playerUuids) { + PlayerUuids(List playerUuids) { this.playerUuids = 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/PlayerUuidsMapper.java similarity index 67% rename from src/main/java/io/graversen/minecraft/rcon/query/playerlist/PlayerListMapper.java rename to src/main/java/io/graversen/minecraft/rcon/query/playerlist/PlayerUuidsMapper.java index e1ff457..9885a74 100644 --- a/src/main/java/io/graversen/minecraft/rcon/query/playerlist/PlayerListMapper.java +++ b/src/main/java/io/graversen/minecraft/rcon/query/playerlist/PlayerUuidsMapper.java @@ -9,32 +9,32 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; -public class PlayerListMapper implements IRconResponseMapper { +public class PlayerUuidsMapper implements IRconResponseMapper { static final Pattern PATTERN_INITIAL = Pattern.compile(":"); static final Pattern PATTERN_PLAYERS = Pattern.compile(","); @Override - public PlayerList apply(RconResponse rconResponse) { + public PlayerUuids apply(RconResponse rconResponse) { 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]); + return extractPlayerList(players[1]); } else { - return new PlayerList(List.of()); + return new PlayerUuids(List.of()); } } - return new PlayerList(List.of()); + return new PlayerUuids(List.of()); } - private PlayerList extractPlayerUuids(String players) { - final var playerUuids = Arrays.stream(players.split(PATTERN_PLAYERS.pattern())) + private PlayerUuids extractPlayerList(String playersRaw) { + final var players = Arrays.stream(playersRaw.split(PATTERN_PLAYERS.pattern())) .map(String::trim) .map(player -> StringUtils.substringBetween(player, "(", ")")) .collect(Collectors.toUnmodifiableList()); - return new PlayerList(playerUuids); + return new PlayerUuids(players); } } diff --git a/src/test/java/io/graversen/minecraft/rcon/commands/PlayerUuidsCommandTest.java b/src/test/java/io/graversen/minecraft/rcon/commands/PlayerUuidsCommandTest.java new file mode 100644 index 0000000..0cf9d96 --- /dev/null +++ b/src/test/java/io/graversen/minecraft/rcon/commands/PlayerUuidsCommandTest.java @@ -0,0 +1,19 @@ +package io.graversen.minecraft.rcon.commands; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class PlayerUuidsCommandTest { + @Test + void playerListCommand_uuids() { + final var command = PlayerListCommand.uuids(); + assertEquals("list uuids", command.command()); + } + + @Test + void playerListCommand_names() { + final var command = PlayerListCommand.names(); + assertEquals("list", command.command()); + } +} \ No newline at end of file diff --git a/src/test/java/io/graversen/minecraft/rcon/query/playerlist/PlayerNamesMapperTest.java b/src/test/java/io/graversen/minecraft/rcon/query/playerlist/PlayerNamesMapperTest.java new file mode 100644 index 0000000..f48399e --- /dev/null +++ b/src/test/java/io/graversen/minecraft/rcon/query/playerlist/PlayerNamesMapperTest.java @@ -0,0 +1,35 @@ +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 PlayerNamesMapperTest { + private final PlayerNamesMapper playerNamesMapper = new PlayerNamesMapper(); + + @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 = playerNamesMapper.apply(testRconResponse); + + assertNotNull(playerList); + assertEquals(2, playerList.getPlayerNames().size()); + assertEquals("MrSkurk", playerList.getPlayerNames().get(0)); + assertEquals("test", playerList.getPlayerNames().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 = playerNamesMapper.apply(testRconResponse); + + assertNotNull(playerList); + assertTrue(playerList.getPlayerNames().isEmpty()); + } +} \ No newline at end of file diff --git a/src/test/java/io/graversen/minecraft/rcon/query/playerlist/PlayerListMapperTest.java b/src/test/java/io/graversen/minecraft/rcon/query/playerlist/PlayerUuidsMapperTest.java similarity index 79% rename from src/test/java/io/graversen/minecraft/rcon/query/playerlist/PlayerListMapperTest.java rename to src/test/java/io/graversen/minecraft/rcon/query/playerlist/PlayerUuidsMapperTest.java index 430f8d5..e2ffde0 100644 --- a/src/test/java/io/graversen/minecraft/rcon/query/playerlist/PlayerListMapperTest.java +++ b/src/test/java/io/graversen/minecraft/rcon/query/playerlist/PlayerUuidsMapperTest.java @@ -5,15 +5,15 @@ import static org.junit.jupiter.api.Assertions.*; -class PlayerListMapperTest { - private final PlayerListMapper playerListMapper = new PlayerListMapper(); +class PlayerUuidsMapperTest { + private final PlayerUuidsMapper playerUuidsMapper = new PlayerUuidsMapper(); @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); + final var playerList = playerUuidsMapper.apply(testRconResponse); assertNotNull(playerList); assertEquals(2, playerList.getPlayerUuids().size()); @@ -26,7 +26,7 @@ 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); + final var playerList = playerUuidsMapper.apply(testRconResponse); assertNotNull(playerList); assertTrue(playerList.getPlayerUuids().isEmpty());