feat: code
This commit is contained in:
@@ -4,15 +4,13 @@ import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
/**
|
||||
* BindRequest handles server binding and verification with the Mineroo API.
|
||||
@@ -22,7 +20,6 @@ import java.util.concurrent.CompletableFuture;
|
||||
* to handle the actual HTTP transport and authentication.
|
||||
*/
|
||||
public class BindRequest {
|
||||
|
||||
private final Logger logger;
|
||||
private final NetworkServiceInterface networkService;
|
||||
|
||||
@@ -59,7 +56,6 @@ public class BindRequest {
|
||||
|
||||
return networkService.getData(path, params)
|
||||
.thenApply(response -> {
|
||||
|
||||
if (response.getStatusCode() != 200) {
|
||||
return false;
|
||||
}
|
||||
@@ -158,7 +154,9 @@ public class BindRequest {
|
||||
* @param description The server description (nullable)
|
||||
* @return A future that completes with true if the binding is successful
|
||||
*/
|
||||
public CompletableFuture<Boolean> finalizeServerBinding(@Nullable String name, @Nullable String description) {
|
||||
public CompletableFuture<Boolean> finalizeServerBinding(
|
||||
@Nullable String name, @Nullable String description
|
||||
) {
|
||||
String path = "/server/server-bind";
|
||||
|
||||
JsonObject json = new JsonObject();
|
||||
@@ -180,26 +178,20 @@ public class BindRequest {
|
||||
});
|
||||
}
|
||||
|
||||
public class PlayerBindResponse implements Serializable {
|
||||
public class PlayerBindStatusResponse implements Serializable {
|
||||
@SerializedName("status") private PlayerBindStatusEnum status;
|
||||
|
||||
@SerializedName("status")
|
||||
private PlayerBindStatus status;
|
||||
@SerializedName("message") private String message;
|
||||
|
||||
@SerializedName("message")
|
||||
private String message;
|
||||
@SerializedName("cached") private boolean cached;
|
||||
|
||||
@SerializedName("cached")
|
||||
private boolean cached;
|
||||
@SerializedName("timestamp") private long timestamp;
|
||||
|
||||
@SerializedName("timestamp")
|
||||
private long timestamp;
|
||||
|
||||
@SerializedName("info")
|
||||
private PlayerBindInfo info;
|
||||
@SerializedName("info") private PlayerBindStatusInfo info;
|
||||
|
||||
// --- Getters ---
|
||||
|
||||
public PlayerBindStatus getStatus() {
|
||||
public PlayerBindStatusEnum getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
@@ -215,11 +207,11 @@ public class BindRequest {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public PlayerBindInfo getInfo() {
|
||||
public PlayerBindStatusInfo getInfo() {
|
||||
return info;
|
||||
}
|
||||
|
||||
public void setStatus(PlayerBindStatus status) {
|
||||
public void setStatus(PlayerBindStatusEnum status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
@@ -228,35 +220,26 @@ public class BindRequest {
|
||||
}
|
||||
}
|
||||
|
||||
public static enum PlayerBindStatus {
|
||||
@SerializedName("notBound")
|
||||
NOT_BOUND,
|
||||
public static enum PlayerBindStatusEnum {
|
||||
@SerializedName("notBound") NOT_BOUND,
|
||||
|
||||
@SerializedName("bound")
|
||||
BOUND,
|
||||
@SerializedName("bound") BOUND,
|
||||
|
||||
@SerializedName("conflictUser")
|
||||
CONFLICT_USER,
|
||||
@SerializedName("conflictUser") CONFLICT_USER,
|
||||
|
||||
@SerializedName("conflictPlayer")
|
||||
CONFLICT_PLAYER,
|
||||
@SerializedName("conflictPlayer") CONFLICT_PLAYER,
|
||||
|
||||
@SerializedName("error")
|
||||
ERROR
|
||||
@SerializedName("error") ERROR
|
||||
}
|
||||
|
||||
public static class PlayerBindInfo implements Serializable {
|
||||
@SerializedName("bound_uuid")
|
||||
private String boundUuid;
|
||||
public static class PlayerBindStatusInfo implements Serializable {
|
||||
@SerializedName("bound_uuid") private String boundUuid;
|
||||
|
||||
@SerializedName("minecraft_uuid")
|
||||
private String minecraftUuid;
|
||||
@SerializedName("minecraft_uuid") private String minecraftUuid;
|
||||
|
||||
@SerializedName("bound_user_id")
|
||||
private Integer boundUserId;
|
||||
@SerializedName("bound_user_id") private Integer boundUserId;
|
||||
|
||||
@SerializedName("user_id")
|
||||
private Integer userId;
|
||||
@SerializedName("user_id") private Integer userId;
|
||||
|
||||
// --- Getters ---
|
||||
|
||||
@@ -285,20 +268,99 @@ public class BindRequest {
|
||||
}
|
||||
}
|
||||
|
||||
private PlayerBindResponse createErrorResponse(String errorMessage) {
|
||||
PlayerBindResponse errorRes = new PlayerBindResponse();
|
||||
errorRes.setStatus(PlayerBindStatus.ERROR);
|
||||
private PlayerBindStatusResponse createBindStatusErrorResponse(String errorMessage) {
|
||||
PlayerBindStatusResponse errorRes = new PlayerBindStatusResponse();
|
||||
errorRes.setStatus(PlayerBindStatusEnum.ERROR);
|
||||
errorRes.setMessage(errorMessage);
|
||||
return errorRes;
|
||||
}
|
||||
|
||||
public CompletableFuture<PlayerBindResponse> checkPlayerBindStatus(UUID player_uuid, String user_email) {
|
||||
public CompletableFuture<PlayerBindStatusResponse> checkPlayerBindStatus(
|
||||
UUID player_uuid, String user_email
|
||||
) {
|
||||
String path = "/server/user-bind-status";
|
||||
|
||||
Map<String, String> params = new HashMap<>();
|
||||
|
||||
params.put("user_email", user_email);
|
||||
params.put("player_uuid", player_uuid.toString());
|
||||
params.put("web_email", user_email);
|
||||
params.put("mc_uuid", player_uuid.toString());
|
||||
|
||||
return networkService.getData(path, params)
|
||||
.thenApply(response -> {
|
||||
Gson gson = new Gson();
|
||||
String responseBody = response.getBody();
|
||||
|
||||
if (response.getStatusCode() != 200) {
|
||||
try {
|
||||
return gson.fromJson(responseBody, PlayerBindStatusResponse.class);
|
||||
} catch (Exception ignored) {
|
||||
return createBindStatusErrorResponse("HTTP Error: " + response.getStatusCode());
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return gson.fromJson(responseBody, PlayerBindStatusResponse.class);
|
||||
} catch (Exception e) {
|
||||
logger.error("Mineroo Bind: Failed to parse JSON", e);
|
||||
return createBindStatusErrorResponse("JSON Parse Error");
|
||||
}
|
||||
})
|
||||
.exceptionally(e -> {
|
||||
logger.error("Mineroo Bind: Network exception", e);
|
||||
return createBindStatusErrorResponse("Network Error: " + e.getMessage());
|
||||
});
|
||||
}
|
||||
|
||||
public class PlayerBindResponse implements Serializable {
|
||||
@SerializedName("status") private PlayerBindEnum status;
|
||||
@SerializedName("timestamp") private long timestamp;
|
||||
@SerializedName("message") private String message;
|
||||
|
||||
public PlayerBindEnum getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setStatus(PlayerBindEnum status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
|
||||
public static enum PlayerBindEnum {
|
||||
|
||||
@SerializedName("pending") PENDING,
|
||||
|
||||
@SerializedName("bound") BOUND,
|
||||
|
||||
@SerializedName("expired") EXPIRED,
|
||||
|
||||
@SerializedName("error") ERROR
|
||||
}
|
||||
|
||||
private PlayerBindResponse createPlayerBindErrorResponse(String errorMessage) {
|
||||
PlayerBindResponse errorRes = new PlayerBindResponse();
|
||||
errorRes.setStatus(PlayerBindEnum.ERROR);
|
||||
errorRes.setMessage(errorMessage);
|
||||
return errorRes;
|
||||
}
|
||||
|
||||
public CompletableFuture<PlayerBindResponse> PlayerBindRequest(
|
||||
String webEmail, String playerUuid, String playerUsername
|
||||
) {
|
||||
String path = "/server/user-bind";
|
||||
|
||||
Map<String, String> params = new HashMap<>();
|
||||
|
||||
params.put("web_email", webEmail);
|
||||
params.put("mc_uuid", playerUuid);
|
||||
params.put("mc_username", playerUsername);
|
||||
|
||||
return networkService.getData(path, params)
|
||||
.thenApply(response -> {
|
||||
@@ -309,7 +371,7 @@ public class BindRequest {
|
||||
try {
|
||||
return gson.fromJson(responseBody, PlayerBindResponse.class);
|
||||
} catch (Exception ignored) {
|
||||
return createErrorResponse("HTTP Error: " + response.getStatusCode());
|
||||
return createPlayerBindErrorResponse("HTTP Error: " + response.getStatusCode());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -317,12 +379,12 @@ public class BindRequest {
|
||||
return gson.fromJson(responseBody, PlayerBindResponse.class);
|
||||
} catch (Exception e) {
|
||||
logger.error("Mineroo Bind: Failed to parse JSON", e);
|
||||
return createErrorResponse("JSON Parse Error");
|
||||
return createPlayerBindErrorResponse("JSON Parse Error");
|
||||
}
|
||||
})
|
||||
.exceptionally(e -> {
|
||||
logger.error("Mineroo Bind: Network exception", e);
|
||||
return createErrorResponse("Network Error: " + e.getMessage());
|
||||
return createPlayerBindErrorResponse("Network Error: " + e.getMessage());
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user