-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from MrGraversen/feature/player-list
Feature/player list
- Loading branch information
Showing
8 changed files
with
142 additions
and
15 deletions.
There are no files selected for viewing
19 changes: 18 additions & 1 deletion
19
src/main/java/io/graversen/minecraft/rcon/commands/PlayerListCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/io/graversen/minecraft/rcon/query/playerlist/PlayerNames.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.graversen.minecraft.rcon.query.playerlist; | ||
|
||
import java.util.List; | ||
|
||
public class PlayerNames { | ||
private final List<String> playerNames; | ||
|
||
PlayerNames(List<String> playerNames) { | ||
this.playerNames = playerNames; | ||
} | ||
|
||
public List<String> getPlayerNames() { | ||
return playerNames; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/io/graversen/minecraft/rcon/query/playerlist/PlayerNamesMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<PlayerNames> { | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/test/java/io/graversen/minecraft/rcon/commands/PlayerUuidsCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/test/java/io/graversen/minecraft/rcon/query/playerlist/PlayerNamesMapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters