Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
import org.jetbrains.annotations.NotNull;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.jspecify.annotations.NonNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.CompletableFuture;

@UtilityClass
Expand All @@ -38,11 +37,15 @@ public class NetworkAPI {
public static void setBuildTeamToolsInstalled(boolean installed) {
String apiKey = BuildTeamTools.getInstance().getMainConfig().apiKey();

JSONObject requestBodyJson = new JSONObject();
Map<String, Boolean> requestBodyJson = new HashMap<>();
requestBodyJson.put("hasBuildTeamToolsInstalled", installed);
String requestBodyString = requestBodyJson.toString();

RequestBody requestBody = RequestBody.create(requestBodyString, MediaType.parse("application/json"));
String requestBodyString = JSONValue.toJSONString(requestBodyJson);

RequestBody requestBody = RequestBody.create(
requestBodyString,
MediaType.parse("application/json")
);

API.putAsync("https://nwapi.buildtheearth.net/api/teams/" + apiKey + "/hasBuildTeamToolsInstalled", requestBody, new API.ApiResponseCallback() {
@Override
Expand Down Expand Up @@ -376,19 +379,26 @@ public static void deleteWarpGroup(WarpGroup warpGroup, API.ApiResponseCallback
API.deleteAsync("https://nwapi.buildtheearth.net/api/teams/" + apiKey + "/warpgroups", requestBody, callback);
}

@SuppressWarnings("unchecked")
public static void syncPlayerList() {
BuildTeam buildTeam = NetworkModule.getInstance().getBuildTeam();
if (buildTeam != null && buildTeam.isConnected())
return;

String apiKey = BuildTeamTools.getInstance().getMainConfig().apiKey();

JSONArray requestBodyArray = new JSONArray();
for (Player player : Bukkit.getOnlinePlayers())
requestBodyArray.add(new String[]{player.getUniqueId().toString(), player.getName()});
List<List<String>> players = new ArrayList<>();

for (Player player : Bukkit.getOnlinePlayers()) {
players.add(Arrays.asList(
player.getUniqueId().toString(),
player.getName()
));
}

RequestBody requestBody = RequestBody.create(requestBodyArray.toString(), MediaType.parse("application/json"));
RequestBody requestBody = RequestBody.create(
JSONValue.toJSONString(players),
MediaType.parse("application/json")
);

API.postAsync("https://nwapi.buildtheearth.net/api/teams/" + apiKey + "/playerlist", requestBody, new API.ApiResponseCallback() {
@Override
Expand All @@ -398,7 +408,7 @@ public void onResponse(String response) {

@Override
public void onFailure(IOException e) {
ChatHelper.getErrorString("Failed to sync the player list with the network API: %s", e);
ChatHelper.logError("Failed to sync the player list with the network API: %s", e.getMessage());
}
});
}
Expand Down
Loading