Files
minecraft-helper/src/main/java/cc/fascinated/service/mojang/model/MojangProfile.java

112 lines
2.9 KiB
Java
Raw Normal View History

2024-04-08 05:29:05 +01:00
package cc.fascinated.service.mojang.model;
2024-04-08 05:22:54 +01:00
import cc.fascinated.Main;
2024-04-08 05:29:05 +01:00
import cc.fascinated.model.player.Cape;
import cc.fascinated.model.player.Skin;
2024-04-08 05:22:54 +01:00
import cc.fascinated.util.Tuple;
2024-04-08 06:13:03 +01:00
import cc.fascinated.util.UUIDUtils;
2024-04-08 05:22:54 +01:00
import com.google.gson.JsonObject;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
2024-04-08 06:13:03 +01:00
import java.util.Base64;
2024-04-08 05:22:54 +01:00
import java.util.List;
@Getter @NoArgsConstructor
public class MojangProfile {
/**
* The UUID of the player.
*/
private String id;
/**
* The name of the player.
*/
private String name;
/**
* The properties of the player.
*/
private final List<ProfileProperty> properties = new ArrayList<>();
/**
* Get the skin and cape of the player.
*
* @return the skin and cape of the player
*/
public Tuple<Skin, Cape> getSkinAndCape() {
2024-04-08 06:13:03 +01:00
ProfileProperty textureProperty = getProfileProperty("textures");
2024-04-08 05:22:54 +01:00
if (textureProperty == null) {
return null;
}
2024-04-08 06:13:03 +01:00
JsonObject json = Main.GSON.fromJson(textureProperty.getDecodedValue(), JsonObject.class); // Decode the texture property
JsonObject texturesJson = json.getAsJsonObject("textures"); // Parse the decoded JSON and get the textures object
2024-04-08 05:22:54 +01:00
2024-04-08 06:13:03 +01:00
return new Tuple<>(Skin.fromJson(texturesJson.getAsJsonObject("SKIN")).populatePartUrls(this.getFormattedUuid()),
Cape.fromJson(texturesJson.getAsJsonObject("CAPE")));
}
2024-04-08 05:22:54 +01:00
2024-04-08 06:13:03 +01:00
/**
* Gets the formatted UUID of the player.
*
* @return the formatted UUID
*/
public String getFormattedUuid() {
return id.length() == 32 ? UUIDUtils.addUuidDashes(id) : id;
2024-04-08 05:22:54 +01:00
}
/**
2024-04-08 06:13:03 +01:00
* Get a profile property for the player
2024-04-08 05:22:54 +01:00
*
2024-04-08 06:13:03 +01:00
* @return the profile property
2024-04-08 05:22:54 +01:00
*/
2024-04-08 06:13:03 +01:00
public ProfileProperty getProfileProperty(String name) {
2024-04-08 05:22:54 +01:00
for (ProfileProperty property : properties) {
2024-04-08 06:13:03 +01:00
if (property.getName().equals(name)) {
2024-04-08 05:22:54 +01:00
return property;
}
}
return null;
}
@Getter @AllArgsConstructor
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;
2024-04-08 06:13:03 +01:00
/**
* Decodes the value for this property.
*
* @return the decoded value
*/
public String getDecodedValue() {
return new String(Base64.getDecoder().decode(this.value));
}
2024-04-08 05:22:54 +01:00
/**
* Check if the property is signed.
*
* @return true if the property is signed, false otherwise
*/
public boolean isSigned() {
return signature != null;
}
}
}