Skip to content

Commit

Permalink
✨ PlayerNamesMapper, rename PlayerUuidsMapper
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGraversen committed Oct 4, 2020
1 parent eaf5f4e commit 9a6dd39
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 10 deletions.
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;
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import java.util.List;

public class PlayerList {
public class PlayerUuids {
private final List<String> playerUuids;

PlayerList(List<String> playerUuids) {
PlayerUuids(List<String> playerUuids) {
this.playerUuids = playerUuids;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,32 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class PlayerListMapper implements IRconResponseMapper<PlayerList> {
public class PlayerUuidsMapper implements IRconResponseMapper<PlayerUuids> {
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);
}
}

0 comments on commit 9a6dd39

Please sign in to comment.