2024-04-10 07:43:38 +01:00
|
|
|
package cc.fascinated.model.mojang;
|
|
|
|
|
|
|
|
import cc.fascinated.Main;
|
|
|
|
import cc.fascinated.common.Tuple;
|
|
|
|
import cc.fascinated.common.UUIDUtils;
|
|
|
|
import cc.fascinated.model.player.Cape;
|
|
|
|
import cc.fascinated.model.player.Skin;
|
2024-04-11 00:15:12 +01:00
|
|
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
2024-04-10 07:43:38 +01:00
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import lombok.AllArgsConstructor;
|
|
|
|
import lombok.Getter;
|
|
|
|
import lombok.NoArgsConstructor;
|
|
|
|
|
|
|
|
import java.util.Base64;
|
|
|
|
|
2024-04-10 12:13:04 +01:00
|
|
|
@Getter @NoArgsConstructor @AllArgsConstructor
|
2024-04-10 07:43:38 +01:00
|
|
|
public class MojangProfile {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The UUID of the player.
|
|
|
|
*/
|
|
|
|
private String id;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The name of the player.
|
|
|
|
*/
|
|
|
|
private String name;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The properties of the player.
|
|
|
|
*/
|
2024-04-11 00:15:12 +01:00
|
|
|
private ProfileProperty[] properties = new ProfileProperty[0];
|
2024-04-10 07:43:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the skin and cape of the player.
|
|
|
|
*
|
|
|
|
* @return the skin and cape of the player
|
|
|
|
*/
|
|
|
|
public Tuple<Skin, Cape> getSkinAndCape() {
|
|
|
|
ProfileProperty textureProperty = getProfileProperty("textures");
|
|
|
|
if (textureProperty == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2024-04-11 03:08:17 +01:00
|
|
|
JsonObject texturesJson = textureProperty.getDecodedValue().getAsJsonObject("textures"); // Parse the decoded JSON and get the texture object
|
2024-04-10 07:43:38 +01:00
|
|
|
return new Tuple<>(Skin.fromJson(texturesJson.getAsJsonObject("SKIN")).populatePartUrls(this.getFormattedUuid()),
|
|
|
|
Cape.fromJson(texturesJson.getAsJsonObject("CAPE")));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the formatted UUID of the player.
|
|
|
|
*
|
|
|
|
* @return the formatted UUID
|
|
|
|
*/
|
|
|
|
public String getFormattedUuid() {
|
|
|
|
return id.length() == 32 ? UUIDUtils.addDashes(id).toString() : id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a profile property for the player
|
|
|
|
*
|
|
|
|
* @return the profile property
|
|
|
|
*/
|
|
|
|
public ProfileProperty getProfileProperty(String name) {
|
|
|
|
for (ProfileProperty property : properties) {
|
|
|
|
if (property.getName().equals(name)) {
|
|
|
|
return property;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-04-10 12:14:58 +01:00
|
|
|
@Getter @NoArgsConstructor
|
2024-04-10 07:43:38 +01:00
|
|
|
public static class ProfileProperty {
|
|
|
|
/**
|
|
|
|
* The name of the property.
|
|
|
|
*/
|
|
|
|
private String name;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The base64 value of the property.
|
|
|
|
*/
|
|
|
|
private String value;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The signature of the property.
|
|
|
|
*/
|
|
|
|
private String signature;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decodes the value for this property.
|
|
|
|
*
|
|
|
|
* @return the decoded value
|
|
|
|
*/
|
2024-04-11 00:15:12 +01:00
|
|
|
@JsonIgnore
|
2024-04-11 03:08:17 +01:00
|
|
|
public JsonObject getDecodedValue() {
|
|
|
|
return Main.GSON.fromJson(new String(Base64.getDecoder().decode(this.value)), JsonObject.class);
|
2024-04-10 07:43:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the property is signed.
|
|
|
|
*
|
|
|
|
* @return true if the property is signed, false otherwise
|
|
|
|
*/
|
|
|
|
public boolean isSigned() {
|
|
|
|
return signature != null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|