Fix/mojang utils long name (#1792)

This commit is contained in:
Zak Shearman 2023-03-18 00:02:18 +00:00 committed by GitHub
parent 7b0b314707
commit 067227421f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 2 deletions

View File

@ -41,10 +41,15 @@ public final class MojangUtils {
final String response = URLUtils.getText(url);
// If our response is "", that means the url did not get a proper object from the url
// So the username or UUID was invalid, and therefore we return null
if(response.isEmpty()) {
if (response.isEmpty()) {
return null;
}
return JsonParser.parseString(response).getAsJsonObject();
JsonObject jsonObject = JsonParser.parseString(response).getAsJsonObject();
if (jsonObject.has("errorMessage")) {
return null;
}
return jsonObject;
} catch (IOException e) {
MinecraftServer.getExceptionManager().handleException(e);
throw new RuntimeException(e);

View File

@ -18,4 +18,24 @@ public class TestMojangUtils {
var result = MojangUtils.fromUsername("jfdsa84vvcxadubasdfcvn"); // Longer than 16, always invalid
assertNull(result);
}
@Test
public void testValidUuidWorks() {
var result = MojangUtils.fromUuid("853c80ef3c3749fdaa49938b674adae6");
assertNotNull(result);
assertEquals("jeb_", result.get("name").getAsString());
assertEquals("853c80ef3c3749fdaa49938b674adae6", result.get("id").getAsString());
}
@Test
public void testInvalidUuidReturnsNull() {
var result = MojangUtils.fromUuid("853c80ef3c3749fdaa49938b674adae6a"); // Longer than 32, always invalid
assertNull(result);
}
@Test
public void testNonExistentUuidReturnsNull() {
var result = MojangUtils.fromUuid("00000000-0000-0000-0000-000000000000");
assertNull(result);
}
}