mirror of
https://github.com/FancyInnovations/FancyPlugins.git
synced 2025-12-05 23:33:36 +00:00
quick-e2e: Add OPsService to manage player OPs and integrate Mojang API for UUID retrieval
This commit is contained in:
5
tools/quick-e2e/http/mojang-api.http
Normal file
5
tools/quick-e2e/http/mojang-api.http
Normal file
@@ -0,0 +1,5 @@
|
||||
### Query player's UUID
|
||||
GET https://api.mojang.com/users/profiles/minecraft/OliverHD
|
||||
|
||||
### Query player's UUID 2
|
||||
GET https://api.minecraftservices.com/minecraft/profile/lookup/name/OliverHD
|
||||
@@ -3,6 +3,7 @@ package de.oliver.quicke2e;
|
||||
import de.oliver.quicke2e.config.Configuration;
|
||||
import de.oliver.quicke2e.config.Context;
|
||||
import de.oliver.quicke2e.steps.eula.EulaService;
|
||||
import de.oliver.quicke2e.steps.ops.OPsService;
|
||||
import de.oliver.quicke2e.steps.paper.PaperDownloadService;
|
||||
import de.oliver.quicke2e.steps.startScript.StartScriptService;
|
||||
import de.oliver.quicke2e.steps.startServer.StartServerService;
|
||||
@@ -30,6 +31,9 @@ public class Main {
|
||||
eula.setEulaToTrue(String.format("%s/eula.txt", context.serverEnvPath().toString()));
|
||||
}
|
||||
|
||||
OPsService ops = new OPsService();
|
||||
ops.makePlayersOP(context);
|
||||
|
||||
StartScriptService startScript = new StartScriptService();
|
||||
startScript.writeStartScript(context);
|
||||
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package de.oliver.quicke2e.steps.ops;
|
||||
|
||||
public record MojangUuidResponse(
|
||||
String name,
|
||||
String id
|
||||
) {
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package de.oliver.quicke2e.steps.ops;
|
||||
|
||||
public record OPConfig(
|
||||
String uuid,
|
||||
String name,
|
||||
int level,
|
||||
boolean bypassesPlayerLimit
|
||||
) {
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package de.oliver.quicke2e.steps.ops;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import de.oliver.quicke2e.config.Context;
|
||||
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
public class OPsService {
|
||||
|
||||
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
|
||||
private final HttpClient client;
|
||||
|
||||
public OPsService() {
|
||||
this.client = HttpClient.newHttpClient();
|
||||
}
|
||||
|
||||
public void makePlayersOP(Context context) {
|
||||
OPConfig[] opConfig = new OPConfig[context.configuration().opPlayers().length];
|
||||
for (int i = 0; i < context.configuration().opPlayers().length; i++) {
|
||||
String username = context.configuration().opPlayers()[i];
|
||||
String uuid = getUUID(username);
|
||||
opConfig[i] = new OPConfig(
|
||||
uuid,
|
||||
username,
|
||||
4,
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
String json = GSON.toJson(opConfig);
|
||||
|
||||
Path opsFilePath = context.serverEnvPath().resolve("ops.json");
|
||||
try {
|
||||
Files.writeString(opsFilePath, json);
|
||||
} catch (java.io.IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private String getUUID(String username) {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.GET()
|
||||
.uri(java.net.URI.create(String.format("https://api.mojang.com/users/profiles/minecraft/%s", username)))
|
||||
.build();
|
||||
|
||||
HttpResponse<String> resp = client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).join();
|
||||
MojangUuidResponse mojangUuidResponse = GSON.fromJson(resp.body(), MojangUuidResponse.class);
|
||||
|
||||
return transformUUID(mojangUuidResponse.id());
|
||||
}
|
||||
|
||||
private String transformUUID(String hexUUID) {
|
||||
return hexUUID.replaceFirst(
|
||||
"([0-9a-fA-F]{8})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]{12})",
|
||||
"$1-$2-$3-$4-$5"
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user