cleanup & add cape to player

This commit is contained in:
Lee 2024-04-06 04:28:32 +01:00
parent ea75eb311a
commit 50265b6228
6 changed files with 39 additions and 12 deletions

@ -3,11 +3,13 @@ package cc.fascinated;
import com.google.gson.Gson; import com.google.gson.Gson;
import lombok.Getter; import lombok.Getter;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.experimental.Helper;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.io.File; import java.io.File;
import java.net.http.HttpClient;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.util.Objects; import java.util.Objects;
@ -18,6 +20,9 @@ public class Main {
@Getter @Getter
private static final Gson GSON = new Gson(); private static final Gson GSON = new Gson();
@Getter
private static final HttpClient CLIENT = HttpClient.newHttpClient();
@SneakyThrows @SneakyThrows
public static void main(String[] args) { public static void main(String[] args) {
File config = new File("application.yml"); File config = new File("application.yml");

@ -15,8 +15,6 @@ import java.net.http.HttpResponse;
@Service @Service
public class MojangAPIService { public class MojangAPIService {
private static final HttpClient CLIENT = HttpClient.newHttpClient();
@Value("${mojang.session-server}") @Value("${mojang.session-server}")
private String mojangSessionServerUrl; private String mojangSessionServerUrl;
@ -36,7 +34,7 @@ public class MojangAPIService {
.GET() .GET()
.build(); .build();
HttpResponse<String> response = CLIENT.send(request, HttpResponse.BodyHandlers.ofString()); HttpResponse<String> response = Main.getCLIENT().send(request, HttpResponse.BodyHandlers.ofString());
return Main.getGSON().fromJson(response.body(), MojangSessionServerProfile.class); return Main.getGSON().fromJson(response.body(), MojangSessionServerProfile.class);
} }
@ -53,7 +51,7 @@ public class MojangAPIService {
.GET() .GET()
.build(); .build();
HttpResponse<String> response = CLIENT.send(request, HttpResponse.BodyHandlers.ofString()); HttpResponse<String> response = Main.getCLIENT().send(request, HttpResponse.BodyHandlers.ofString());
return Main.getGSON().fromJson(response.body(), MojangApiProfile.class); return Main.getGSON().fromJson(response.body(), MojangApiProfile.class);
} }
} }

@ -1,8 +1,9 @@
package cc.fascinated.mojang.types; package cc.fascinated.mojang.types;
import lombok.Getter; import lombok.Getter;
import lombok.ToString;
@Getter @Getter @ToString
public class MojangApiProfile { public class MojangApiProfile {
private String id; private String id;

@ -64,6 +64,9 @@ public class PlayerManagerService {
} }
profile = mojangAPIService.getSessionServerProfile(apiProfile.getId().length() == 32 ? UUIDUtils.addUUIDDashes(apiProfile.getId()) : apiProfile.getId()); profile = mojangAPIService.getSessionServerProfile(apiProfile.getId().length() == 32 ? UUIDUtils.addUUIDDashes(apiProfile.getId()) : apiProfile.getId());
} }
if (profile == null) { // The player cannot be found using their name or UUID
return null;
}
Player player = new Player(profile); Player player = new Player(profile);
players.put(player.getUuid(), player); players.put(player.getUuid(), player);
playerNameToUUIDCache.put(player.getName().toUpperCase(), player.getUuid()); playerNameToUUIDCache.put(player.getName().toUpperCase(), player.getUuid());

@ -0,0 +1,13 @@
package cc.fascinated.player.impl;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter @AllArgsConstructor
public class Cape {
/**
* The URL of the cape
*/
private final String url;
}

@ -30,6 +30,14 @@ public class Player {
*/ */
private Skin skin; private Skin skin;
/**
* The cape of the player
* <p>
* This will be null if the player does not have a cape.
* </p>
*/
private Cape cape;
public Player(MojangSessionServerProfile profile) { public Player(MojangSessionServerProfile profile) {
this.uuid = UUID.fromString(UUIDUtils.addUUIDDashes(profile.getId())); this.uuid = UUID.fromString(UUIDUtils.addUUIDDashes(profile.getId()));
this.name = profile.getName(); this.name = profile.getName();
@ -44,14 +52,13 @@ public class Player {
// Parse the decoded JSON // Parse the decoded JSON
JsonObject json = Main.getGSON().fromJson(decoded, JsonObject.class); JsonObject json = Main.getGSON().fromJson(decoded, JsonObject.class);
JsonObject textures = json.getAsJsonObject("textures"); JsonObject texturesJson = json.getAsJsonObject("textures");
JsonObject skin = textures.getAsJsonObject("SKIN"); JsonObject skinJson = texturesJson.getAsJsonObject("SKIN");
JsonObject metadata = skin.get("metadata").getAsJsonObject(); JsonObject capeJson = texturesJson.getAsJsonObject("CAPE");
JsonObject metadataJson = skinJson.get("metadata").getAsJsonObject();
String url = skin.get("url").getAsString(); this.skin = new Skin(skinJson.get("url").getAsString(), SkinType.fromString(metadataJson.get("model").getAsString()));
SkinType model = SkinType.fromString(metadata.get("model").getAsString()); this.cape = new Cape(capeJson.get("url").getAsString());
this.skin = new Skin(url, model);
} }
} }