cleanup & add cape to player
This commit is contained in:
parent
ea75eb311a
commit
50265b6228
@ -3,11 +3,13 @@ package cc.fascinated;
|
||||
import com.google.gson.Gson;
|
||||
import lombok.Getter;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.experimental.Helper;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.http.HttpClient;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.Objects;
|
||||
@ -18,6 +20,9 @@ public class Main {
|
||||
@Getter
|
||||
private static final Gson GSON = new Gson();
|
||||
|
||||
@Getter
|
||||
private static final HttpClient CLIENT = HttpClient.newHttpClient();
|
||||
|
||||
@SneakyThrows
|
||||
public static void main(String[] args) {
|
||||
File config = new File("application.yml");
|
||||
|
@ -15,8 +15,6 @@ import java.net.http.HttpResponse;
|
||||
@Service
|
||||
public class MojangAPIService {
|
||||
|
||||
private static final HttpClient CLIENT = HttpClient.newHttpClient();
|
||||
|
||||
@Value("${mojang.session-server}")
|
||||
private String mojangSessionServerUrl;
|
||||
|
||||
@ -36,7 +34,7 @@ public class MojangAPIService {
|
||||
.GET()
|
||||
.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);
|
||||
}
|
||||
|
||||
@ -53,7 +51,7 @@ public class MojangAPIService {
|
||||
.GET()
|
||||
.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);
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,9 @@
|
||||
package cc.fascinated.mojang.types;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
|
||||
@Getter
|
||||
@Getter @ToString
|
||||
public class MojangApiProfile {
|
||||
|
||||
private String id;
|
||||
|
@ -64,6 +64,9 @@ public class PlayerManagerService {
|
||||
}
|
||||
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);
|
||||
players.put(player.getUuid(), player);
|
||||
playerNameToUUIDCache.put(player.getName().toUpperCase(), player.getUuid());
|
||||
|
13
src/main/java/cc/fascinated/player/impl/Cape.java
Normal file
13
src/main/java/cc/fascinated/player/impl/Cape.java
Normal file
@ -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;
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
this.uuid = UUID.fromString(UUIDUtils.addUUIDDashes(profile.getId()));
|
||||
this.name = profile.getName();
|
||||
@ -44,14 +52,13 @@ public class Player {
|
||||
|
||||
// Parse the decoded JSON
|
||||
JsonObject json = Main.getGSON().fromJson(decoded, JsonObject.class);
|
||||
JsonObject textures = json.getAsJsonObject("textures");
|
||||
JsonObject skin = textures.getAsJsonObject("SKIN");
|
||||
JsonObject metadata = skin.get("metadata").getAsJsonObject();
|
||||
JsonObject texturesJson = json.getAsJsonObject("textures");
|
||||
JsonObject skinJson = texturesJson.getAsJsonObject("SKIN");
|
||||
JsonObject capeJson = texturesJson.getAsJsonObject("CAPE");
|
||||
JsonObject metadataJson = skinJson.get("metadata").getAsJsonObject();
|
||||
|
||||
String url = skin.get("url").getAsString();
|
||||
SkinType model = SkinType.fromString(metadata.get("model").getAsString());
|
||||
|
||||
this.skin = new Skin(url, model);
|
||||
this.skin = new Skin(skinJson.get("url").getAsString(), SkinType.fromString(metadataJson.get("model").getAsString()));
|
||||
this.cape = new Cape(capeJson.get("url").getAsString());
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user