Tuple -> SkinProperties

This commit is contained in:
Braydon 2024-04-10 11:18:42 -04:00
parent 27beec9dc7
commit 160fed45e8
4 changed files with 31 additions and 59 deletions

@ -1,47 +0,0 @@
/*
* MIT License
*
* Copyright (c) 2024 Braydon (Rainnny).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.braydon.mc.common;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
/**
* A simple tuple utility.
*
* @author Braydon
*/
@NoArgsConstructor @AllArgsConstructor @Setter @Getter
public class Tuple<L, R> {
/**
* The left value.
*/
private L left;
/**
* The right value.
*/
private R right;
}

@ -23,8 +23,8 @@
*/ */
package me.braydon.mc.model; package me.braydon.mc.model;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.annotations.SerializedName;
import lombok.*; import lombok.*;
import me.braydon.mc.common.ImageUtils; import me.braydon.mc.common.ImageUtils;
import me.braydon.mc.config.AppConfig; import me.braydon.mc.config.AppConfig;
@ -54,7 +54,7 @@ public final class Skin {
/** /**
* URLs to the parts of this skin. * URLs to the parts of this skin.
*/ */
@NonNull @SerializedName("parts") private Map<String, String> partUrls = new HashMap<>(); @NonNull @JsonProperty("parts") private Map<String, String> partUrls = new HashMap<>();
/** /**
* Populate the part URLs for this skin. * Populate the part URLs for this skin.

@ -25,7 +25,6 @@ package me.braydon.mc.model.token;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import lombok.*; import lombok.*;
import me.braydon.mc.common.Tuple;
import me.braydon.mc.config.AppConfig; import me.braydon.mc.config.AppConfig;
import me.braydon.mc.model.Cape; import me.braydon.mc.model.Cape;
import me.braydon.mc.model.ProfileAction; import me.braydon.mc.model.ProfileAction;
@ -62,20 +61,21 @@ public final class MojangProfileToken {
@NonNull private final ProfileAction[] profileActions; @NonNull private final ProfileAction[] profileActions;
/** /**
* Get the skin and cape of this profile. * Get the properties of this skin.
* *
* @return the skin and cape of this profile * @return the properties
*/ */
public Tuple<Skin, Cape> getSkinAndCape() { @NonNull
public SkinProperties getSkinProperties() {
ProfileProperty textures = getPropertyByName("textures"); // Get the profile textures ProfileProperty textures = getPropertyByName("textures"); // Get the profile textures
if (textures == null) { // No profile textures if (textures == null) { // No profile textures
return new Tuple<>(); return new SkinProperties();
} }
JsonObject jsonObject = AppConfig.GSON.fromJson(textures.getDecodedValue(), JsonObject.class); // Get the Json object JsonObject jsonObject = AppConfig.GSON.fromJson(textures.getDecodedValue(), JsonObject.class); // Get the Json object
JsonObject texturesJsonObject = jsonObject.getAsJsonObject("textures"); // Get the textures object JsonObject texturesJsonObject = jsonObject.getAsJsonObject("textures"); // Get the textures object
// Return the tuple containing the skin and cape // Return the tuple containing the skin and cape
return new Tuple<>( return new SkinProperties(
Skin.fromJsonObject(texturesJsonObject.getAsJsonObject("SKIN")).populatePartUrls(id), Skin.fromJsonObject(texturesJsonObject.getAsJsonObject("SKIN")).populatePartUrls(id),
Cape.fromJsonObject(texturesJsonObject.getAsJsonObject("CAPE")) Cape.fromJsonObject(texturesJsonObject.getAsJsonObject("CAPE"))
); );
@ -137,4 +137,20 @@ public final class MojangProfileToken {
return signature != null; return signature != null;
} }
} }
/**
* The properties for a skin.
*/
@NoArgsConstructor @AllArgsConstructor @Getter @ToString
public static class SkinProperties {
/**
* The skin of the profile.
*/
private Skin skin;
/**
* The cape of the profile.
*/
private Cape cape;
}
} }

@ -37,7 +37,10 @@ import me.braydon.mc.common.web.JsonWebRequest;
import me.braydon.mc.exception.impl.BadRequestException; import me.braydon.mc.exception.impl.BadRequestException;
import me.braydon.mc.exception.impl.MojangRateLimitException; import me.braydon.mc.exception.impl.MojangRateLimitException;
import me.braydon.mc.exception.impl.ResourceNotFoundException; import me.braydon.mc.exception.impl.ResourceNotFoundException;
import me.braydon.mc.model.*; import me.braydon.mc.model.MinecraftServer;
import me.braydon.mc.model.Player;
import me.braydon.mc.model.ProfileAction;
import me.braydon.mc.model.Skin;
import me.braydon.mc.model.cache.CachedMinecraftServer; import me.braydon.mc.model.cache.CachedMinecraftServer;
import me.braydon.mc.model.cache.CachedPlayer; import me.braydon.mc.model.cache.CachedPlayer;
import me.braydon.mc.model.cache.CachedPlayerName; import me.braydon.mc.model.cache.CachedPlayerName;
@ -257,14 +260,14 @@ public final class MojangService {
MojangProfileToken token = JsonWebRequest.makeRequest( MojangProfileToken token = JsonWebRequest.makeRequest(
UUID_TO_PROFILE.formatted(uuid), HttpMethod.GET UUID_TO_PROFILE.formatted(uuid), HttpMethod.GET
).execute(MojangProfileToken.class); ).execute(MojangProfileToken.class);
Tuple<Skin, Cape> skinAndCape = token.getSkinAndCape(); // Get the skin and cape MojangProfileToken.SkinProperties skinProperties = token.getSkinProperties(); // Get the skin and cape
ProfileAction[] profileActions = token.getProfileActions(); ProfileAction[] profileActions = token.getProfileActions();
// Build our player model, cache it, and then return it // Build our player model, cache it, and then return it
CachedPlayer player = new CachedPlayer( CachedPlayer player = new CachedPlayer(
uuid, token.getName(), uuid, token.getName(),
skinAndCape.getLeft() == null ? Skin.DEFAULT_STEVE : skinAndCape.getLeft(), skinProperties.getSkin() == null ? Skin.DEFAULT_STEVE : skinProperties.getSkin(),
skinAndCape.getRight(), skinProperties.getCape(),
profileActions.length == 0 ? null : profileActions, profileActions.length == 0 ? null : profileActions,
System.currentTimeMillis() System.currentTimeMillis()
); );