move models to the model package
All checks were successful
deploy / deploy (push) Successful in 41s
All checks were successful
deploy / deploy (push) Successful in 41s
This commit is contained in:
111
src/main/java/cc/fascinated/model/mojang/MojangProfile.java
Normal file
111
src/main/java/cc/fascinated/model/mojang/MojangProfile.java
Normal file
@ -0,0 +1,111 @@
|
||||
package cc.fascinated.model.mojang;
|
||||
|
||||
import cc.fascinated.Main;
|
||||
import cc.fascinated.model.player.Cape;
|
||||
import cc.fascinated.model.player.Skin;
|
||||
import cc.fascinated.util.Tuple;
|
||||
import cc.fascinated.util.UUIDUtils;
|
||||
import com.google.gson.JsonObject;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
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() {
|
||||
ProfileProperty textureProperty = getProfileProperty("textures");
|
||||
if (textureProperty == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
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.addUuidDashes(id) : 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;
|
||||
}
|
||||
|
||||
@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;
|
||||
|
||||
/**
|
||||
* Decodes the value for this property.
|
||||
*
|
||||
* @return the decoded value
|
||||
*/
|
||||
public String getDecodedValue() {
|
||||
return new String(Base64.getDecoder().decode(this.value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the property is signed.
|
||||
*
|
||||
* @return true if the property is signed, false otherwise
|
||||
*/
|
||||
public boolean isSigned() {
|
||||
return signature != null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package cc.fascinated.model.mojang;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Getter @NoArgsConstructor
|
||||
public class MojangUsernameToUuid {
|
||||
|
||||
/**
|
||||
* The UUID of the player.
|
||||
*/
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* The name of the player.
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* Check if the profile is valid.
|
||||
*
|
||||
* @return if the profile is valid
|
||||
*/
|
||||
public boolean isValid() {
|
||||
return id != null && name != null;
|
||||
}
|
||||
}
|
27
src/main/java/cc/fascinated/model/player/Cape.java
Normal file
27
src/main/java/cc/fascinated/model/player/Cape.java
Normal file
@ -0,0 +1,27 @@
|
||||
package cc.fascinated.model.player;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter @AllArgsConstructor
|
||||
public class Cape {
|
||||
|
||||
/**
|
||||
* The URL of the cape
|
||||
*/
|
||||
private final String url;
|
||||
|
||||
/**
|
||||
* Gets the cape from a {@link JsonObject}.
|
||||
*
|
||||
* @param json the JSON object
|
||||
* @return the cape
|
||||
*/
|
||||
public static Cape fromJson(JsonObject json) {
|
||||
if (json == null) {
|
||||
return null;
|
||||
}
|
||||
return new Cape(json.get("url").getAsString());
|
||||
}
|
||||
}
|
46
src/main/java/cc/fascinated/model/player/Player.java
Normal file
46
src/main/java/cc/fascinated/model/player/Player.java
Normal file
@ -0,0 +1,46 @@
|
||||
package cc.fascinated.model.player;
|
||||
|
||||
import cc.fascinated.model.mojang.MojangProfile;
|
||||
import cc.fascinated.util.Tuple;
|
||||
import cc.fascinated.util.UUIDUtils;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Getter
|
||||
public class Player {
|
||||
|
||||
/**
|
||||
* The UUID of the player
|
||||
*/
|
||||
private final UUID uuid;
|
||||
|
||||
/**
|
||||
* The name of the player
|
||||
*/
|
||||
private final String name;
|
||||
|
||||
/**
|
||||
* The skin of the player, null if the
|
||||
* player does not have a skin
|
||||
*/
|
||||
private Skin skin;
|
||||
|
||||
/**
|
||||
* The cape of the player, null if the
|
||||
* player does not have a cape
|
||||
*/
|
||||
private Cape cape;
|
||||
|
||||
public Player(MojangProfile profile) {
|
||||
this.uuid = UUID.fromString(UUIDUtils.addUuidDashes(profile.getId()));
|
||||
this.name = profile.getName();
|
||||
|
||||
// Get the skin and cape
|
||||
Tuple<Skin, Cape> skinAndCape = profile.getSkinAndCape();
|
||||
if (skinAndCape != null) {
|
||||
this.skin = skinAndCape.getLeft();
|
||||
this.cape = skinAndCape.getRight();
|
||||
}
|
||||
}
|
||||
}
|
134
src/main/java/cc/fascinated/model/player/Skin.java
Normal file
134
src/main/java/cc/fascinated/model/player/Skin.java
Normal file
@ -0,0 +1,134 @@
|
||||
package cc.fascinated.model.player;
|
||||
|
||||
import cc.fascinated.config.Config;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.google.gson.JsonObject;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Getter @Log4j2
|
||||
public class Skin {
|
||||
/**
|
||||
* The default skin, usually used when the skin is not found.
|
||||
*/
|
||||
public static final Skin DEFAULT_SKIN = new Skin("http://textures.minecraft.net/texture/60a5bd016b3c9a1b9272e4929e30827a67be4ebb219017adbbc4a4d22ebd5b1",
|
||||
Model.DEFAULT);
|
||||
|
||||
/**
|
||||
* The URL of the skin
|
||||
*/
|
||||
private final String url;
|
||||
|
||||
/**
|
||||
* The model of the skin
|
||||
*/
|
||||
private final Model model;
|
||||
|
||||
/**
|
||||
* The part URLs of the skin
|
||||
*/
|
||||
@JsonProperty("parts")
|
||||
private final Map<String, String> partUrls = new HashMap<>();
|
||||
|
||||
public Skin(String url, Model model) {
|
||||
this.url = url;
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the skin from a {@link JsonObject}.
|
||||
*
|
||||
* @param json the JSON object
|
||||
* @return the skin
|
||||
*/
|
||||
public static Skin fromJson(JsonObject json) {
|
||||
if (json == null) {
|
||||
return null;
|
||||
}
|
||||
String url = json.get("url").getAsString();
|
||||
JsonObject metadata = json.getAsJsonObject("metadata");
|
||||
Model model = Model.fromName(metadata == null ? "slim" : // Fall back to slim if the model is not found
|
||||
metadata.get("model").getAsString());
|
||||
return new Skin(url, model);
|
||||
}
|
||||
|
||||
/**
|
||||
* Populates the part URLs for the skin.
|
||||
*
|
||||
* @param playerUuid the player's UUID
|
||||
*/
|
||||
public Skin populatePartUrls(String playerUuid) {
|
||||
for (Parts part : Parts.values()) {
|
||||
String partName = part.name().toLowerCase();
|
||||
this.partUrls.put(partName, Config.INSTANCE.getWebPublicUrl() + "/player/" + partName + "/" + playerUuid + "?size=" + part.getDefaultSize());
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* The skin part enum that contains the
|
||||
* information about the part.
|
||||
*/
|
||||
@Getter @AllArgsConstructor
|
||||
public enum Parts {
|
||||
|
||||
HEAD(8, 8, 8, 8, 128);
|
||||
|
||||
/**
|
||||
* The x and y position of the part.
|
||||
*/
|
||||
private final int x, y;
|
||||
|
||||
/**
|
||||
* The width and height of the part.
|
||||
*/
|
||||
private final int width, height;
|
||||
|
||||
/**
|
||||
* The scale of the part.
|
||||
*/
|
||||
private final int defaultSize;
|
||||
|
||||
/**
|
||||
* Gets the skin part from its name.
|
||||
*
|
||||
* @param name the name of the part
|
||||
* @return the skin part
|
||||
*/
|
||||
public static Parts fromName(String name) {
|
||||
for (Parts part : values()) {
|
||||
if (part.name().equalsIgnoreCase(name)) {
|
||||
return part;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The model of the skin.
|
||||
*/
|
||||
public enum Model {
|
||||
DEFAULT,
|
||||
SLIM;
|
||||
|
||||
/**
|
||||
* Gets the model from its name.
|
||||
*
|
||||
* @param name the name of the model
|
||||
* @return the model
|
||||
*/
|
||||
public static Model fromName(String name) {
|
||||
for (Model model : values()) {
|
||||
if (model.name().equalsIgnoreCase(name)) {
|
||||
return model;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user