Make `PlayerSkin` record

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-10-27 23:05:53 +02:00
parent 128b932ba6
commit d46db09080
3 changed files with 11 additions and 56 deletions

View File

@ -1982,11 +1982,8 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
// Skin support
if (skin != null) {
final String textures = skin.getTextures();
final String signature = skin.getSignature();
PlayerInfoPacket.AddPlayer.Property prop =
new PlayerInfoPacket.AddPlayer.Property("textures", textures, signature);
new PlayerInfoPacket.AddPlayer.Property("textures", skin.textures(), skin.signature());
addPlayer.properties.add(prop);
}

View File

@ -7,23 +7,13 @@ import net.minestom.server.utils.mojang.MojangUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Objects;
/**
* Contains all the data required to store a skin.
* <p>
* Can be applied to a player with {@link Player#setSkin(PlayerSkin)}
* or in the linked event {@link net.minestom.server.event.player.PlayerSkinInitEvent}.
*/
public class PlayerSkin {
private final String textures;
private final String signature;
public PlayerSkin(String textures, String signature) {
this.textures = textures;
this.signature = signature;
}
public record PlayerSkin(String textures, String signature) {
/**
* Gets a skin from a Mojang UUID.
@ -31,8 +21,7 @@ public class PlayerSkin {
* @param uuid Mojang UUID
* @return a player skin based on the UUID, null if not found
*/
@Nullable
public static PlayerSkin fromUuid(@NotNull String uuid) {
public static @Nullable PlayerSkin fromUuid(@NotNull String uuid) {
final JsonObject jsonObject = MojangUtils.fromUuid(uuid);
final JsonArray propertiesArray = jsonObject.get("properties").getAsJsonArray();
@ -54,8 +43,7 @@ public class PlayerSkin {
* @param username the Minecraft username
* @return a skin based on a Minecraft username, null if not found
*/
@Nullable
public static PlayerSkin fromUsername(@NotNull String username) {
public static @Nullable PlayerSkin fromUsername(@NotNull String username) {
final JsonObject jsonObject = MojangUtils.fromUsername(username);
final String uuid = jsonObject.get("id").getAsString();
// Retrieve the skin data from the mojang uuid
@ -63,49 +51,18 @@ public class PlayerSkin {
}
/**
* Gets the skin textures value.
*
* @return the textures value
* @deprecated use {@link #textures()}
*/
@Deprecated
public String getTextures() {
return textures;
}
/**
* Gets the skin signature.
*
* @return the skin signature
* @deprecated use {@link #signature()}
*/
@Deprecated
public String getSignature() {
return signature;
}
@Override
public String toString() {
return "PlayerSkin{" +
"textures='" + textures + '\'' +
", signature='" + signature + '\'' +
'}';
}
/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object object) {
if (this == object) return true;
if (object == null || getClass() != object.getClass()) return false;
PlayerSkin that = (PlayerSkin) object;
return Objects.equals(textures, that.textures) &&
Objects.equals(signature, that.signature);
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
return Objects.hash(textures, signature);
}
}

View File

@ -10,6 +10,7 @@ import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import org.jglrxavpok.hephaistos.nbt.NBTList;
import org.jglrxavpok.hephaistos.nbt.NBTTypes;
import java.util.Objects;
import java.util.UUID;
import java.util.function.Supplier;
@ -54,8 +55,8 @@ public class PlayerHeadMeta extends ItemMeta implements ItemMetaBuilder.Provider
}
NBTList<NBTCompound> textures = new NBTList<>(NBTTypes.TAG_Compound);
String value = this.playerSkin.getTextures() == null ? "" : this.playerSkin.getTextures();
String signature = this.playerSkin.getSignature() == null ? "" : this.playerSkin.getSignature();
final String value = Objects.requireNonNullElse(this.playerSkin.textures(), "");
final String signature = Objects.requireNonNullElse(this.playerSkin.signature(), "");
textures.add(new NBTCompound().setString("Value", value).setString("Signature", signature));
nbtCompound.set("Properties", new NBTCompound().set("textures", textures));
});