Fix invalid username/uuid in static PlayerSkin methods

This commit is contained in:
themode 2022-02-14 21:04:37 +01:00
parent 7d752bce88
commit be100fa5b8
3 changed files with 24 additions and 1 deletions

View File

@ -25,6 +25,7 @@ public record PlayerSkin(String textures, String signature) {
@Blocking
public static @Nullable PlayerSkin fromUuid(@NotNull String uuid) {
final JsonObject jsonObject = MojangUtils.fromUuid(uuid);
if (jsonObject == null) return null;
final JsonArray propertiesArray = jsonObject.get("properties").getAsJsonArray();
for (JsonElement jsonElement : propertiesArray) {
final JsonObject propertyObject = jsonElement.getAsJsonObject();
@ -46,6 +47,7 @@ public record PlayerSkin(String textures, String signature) {
@Blocking
public static @Nullable PlayerSkin fromUsername(@NotNull String username) {
final JsonObject jsonObject = MojangUtils.fromUsername(username);
if (jsonObject == null) return null;
final String uuid = jsonObject.get("id").getAsString();
// Retrieve the skin data from the mojang uuid
return fromUuid(uuid);

View File

@ -0,0 +1,21 @@
package net.minestom.server.entity;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
public class PlayerSkinTest {
@Test
public void validName() {
var skin = PlayerSkin.fromUsername("jeb_");
assertNotNull(skin);
}
@Test
public void invalidName() {
var skin = PlayerSkin.fromUsername("jfdsa84vvcxadubasdfcvn");
assertNull(skin);
}
}

View File

@ -16,6 +16,6 @@ public class TestMojangUtils {
@Test
public void testInvalidNameReturnsNull() {
var result = MojangUtils.fromUsername("jfdsa84vvcxadubasdfcvn"); // Longer than 16, always invalid
assert result == null;
assertNull(result);
}
}