Files
Backend/src/main/java/cc/fascinated/model/player/Skin.java

132 lines
3.3 KiB
Java
Raw Normal View History

2024-04-08 05:29:05 +01:00
package cc.fascinated.model.player;
2024-04-08 04:56:59 +01:00
import cc.fascinated.config.Config;
import com.fasterxml.jackson.annotation.JsonProperty;
2024-04-08 06:13:03 +01:00
import com.google.gson.JsonObject;
2024-04-08 04:56:59 +01:00
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.log4j.Log4j2;
import java.util.HashMap;
import java.util.Map;
@Getter @Log4j2
public class Skin {
2024-04-08 06:13:03 +01:00
/**
* 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);
2024-04-08 04:56:59 +01:00
/**
* The URL of the skin
*/
private final String url;
/**
* The model of the skin
*/
2024-04-08 06:13:03 +01:00
private final Model model;
2024-04-08 04:56:59 +01:00
@JsonProperty("parts")
private final Map<String, String> partUrls = new HashMap<>();
2024-04-08 06:13:03 +01:00
public Skin(String url, Model model) {
2024-04-08 04:56:59 +01:00
this.url = url;
this.model = model;
}
/**
2024-04-08 06:13:03 +01:00
* Gets the skin from a {@link JsonObject}.
2024-04-08 04:56:59 +01:00
*
2024-04-08 06:13:03 +01:00
* @param json the JSON object
* @return the skin
2024-04-08 04:56:59 +01:00
*/
2024-04-08 06:13:03 +01:00
public static Skin fromJson(JsonObject json) {
if (json == null) {
2024-04-08 04:56:59 +01:00
return null;
}
2024-04-08 06:13:03 +01:00
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);
2024-04-08 04:56:59 +01:00
}
/**
2024-04-08 06:13:03 +01:00
* Populates the part URLs for the skin.
2024-04-08 04:56:59 +01:00
*
2024-04-08 06:13:03 +01:00
* @param playerUuid the player's UUID
2024-04-08 04:56:59 +01:00
*/
2024-04-08 06:13:03 +01:00
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=250");
}
return this;
2024-04-08 04:56:59 +01:00
}
/**
* The skin part enum that contains the
* information about the part.
*/
@Getter @AllArgsConstructor
2024-04-08 06:13:03 +01:00
public enum Parts {
2024-04-08 04:56:59 +01:00
HEAD(8, 8, 8, 8, 250);
/**
* 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;
2024-04-08 06:13:03 +01:00
/**
* 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;
}
2024-04-08 04:56:59 +01:00
}
/**
2024-04-08 06:13:03 +01:00
* The model of the skin.
2024-04-08 04:56:59 +01:00
*/
2024-04-08 06:13:03 +01:00
public enum Model {
2024-04-08 04:56:59 +01:00
DEFAULT,
2024-04-08 06:13:03 +01:00
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;
}
2024-04-08 04:56:59 +01:00
}
}