Skip to content

Commit

Permalink
add varargs to subscribing and playerlists
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianVennen committed May 27, 2022
1 parent 10c1d1b commit 3b57130
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 53 deletions.
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ try {
for (String entry: whitelist.getEntries()) {
System.out.println(entry);
}
whitelist.add(new String[]{"example", "example2"});
whitelist.add("example", "example2");
whitelist.remove("example34");
} catch (APIException e) {
e.printStackTrace();
Expand Down Expand Up @@ -258,7 +258,7 @@ You can subscribe to multiple streams at once by passing an array to the subscri
ExarotonClient client = new ExarotonClient("example-api-token");

Server server = client.getServer("tgkm731xO7GiHt76");
server.subscribe(new String[]{"stats", "heap"});
server.subscribe("stats", "heap");
server.addStatsSubscriber(new StatsSubscriber() {
@Override
public void stats(StatsData stats) {
Expand All @@ -280,8 +280,21 @@ You can unsubscribe from one, multiple or all streams using the server.unsubscri
ExarotonClient client = new ExarotonClient("example-api-token");

Server server = client.getServer("tgkm731xO7GiHt76");
server.subscribe(new String[]{"console", "stats", "heap"});
server.subscribe("console", "stats", "heap");
server.unsubscribe("heap");
server.unsubscribe(new String[]{"stats", "console"});
server.unsubscribe("stats", "console");
server.unsubscribe(); // closes websocket connection
```

### Debugging Websocket connections
```java
ExarotonClient client = new ExarotonClient("example-api-token");

Server server = client.getServer("tgkm731xO7GiHt76");
server.subscribe();
server.getWebSocket().setErrorListener((message, throwable) -> {
System.out.println(message);
System.out.println(throwable.toString());
});
server.getWebSocket().setDebugListener(System.out::println);
```
36 changes: 14 additions & 22 deletions src/main/java/com/exaroton/api/server/PlayerList.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public class PlayerList {

/**
* create a new playerlist
* @param name playerlist name (see Server.getPlayerLists)
*
* @param name playerlist name (see Server.getPlayerLists)
* @param server exaroton server
* @param client exaroton client
*/
Expand Down Expand Up @@ -42,40 +43,31 @@ public String[] getEntries() throws APIException {

/**
* add players to list
*
* @param entries player names
* @throws APIException API error
*/
public void add(String[] entries) throws APIException {
public void add(String... entries) throws APIException {
if (entries.length == 0) {
return;
}

AddPlayerListEntriesRequest request = new AddPlayerListEntriesRequest(this.client, this.server, this.name, entries);
request.request();
}

/**
* add player to list
* @param entry player name
* @throws APIException API error
*/
public void add(String entry) throws APIException {
this.add(new String[]{entry});
}

/**
* remove players from list
*
* @param entries player names
* @throws APIException API error
*/
public void remove(String[] entries) throws APIException {
public void remove(String... entries) throws APIException {
if (entries.length == 0) {
return;
}

RemovePlayerListEntriesRequest request = new RemovePlayerListEntriesRequest(this.client, this.server, this.name, entries);
request.request();
}

/**
* remove player from list
* @param entry player name
* @throws APIException API error
*/
public void remove(String entry) throws APIException {
this.remove(new String[]{entry});
}

}
38 changes: 11 additions & 27 deletions src/main/java/com/exaroton/api/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -366,24 +366,16 @@ public void subscribe() {
}

/**
* subscribe to a single stream
* @param stream stream name
*/
public void subscribe(String stream) {
if (this.webSocket == null) {
this.subscribe();
}

this.webSocket.subscribe(stream);
}

/**
* subscribe to multiple streams at once
* subscribe to one or more streams
* @param streams stream names
*/
public void subscribe(String[] streams) {
public void subscribe(String... streams) {
for (String stream: streams) {
this.subscribe(stream);
if (this.webSocket == null) {
this.subscribe();
}

this.webSocket.subscribe(stream);
}
}

Expand All @@ -397,21 +389,13 @@ public void unsubscribe() {
}

/**
* unsubscribe from a single stream
* @param stream stream name
*/
public void unsubscribe(String stream) {
if (this.webSocket == null) throw new RuntimeException("No websocket connection active.");
this.webSocket.unsubscribe(stream);
}

/**
* unsubscribe from multiple streams at once
* unsubscribe from one or more streams
* @param streams stream names
*/
public void unsubscribe(String[] streams) {
public void unsubscribe(String... streams) {
if (this.webSocket == null) throw new RuntimeException("No websocket connection active.");
for (String stream: streams) {
this.unsubscribe(stream);
this.webSocket.unsubscribe(stream);
}
}

Expand Down

0 comments on commit 3b57130

Please sign in to comment.