Fix Alex/Steve skins being sent incorrectly (#1135)

* Return permanent skins (alex/steve) when queried instead of returning an empty skin due to invalid lookup
* Fix Alex/Steve being shown incorrectly due to java signed integers

Co-authored-by: bundabrg <bundabrg@grieve.com.au>
This commit is contained in:
bundabrg 2020-08-12 21:23:11 +08:00 committed by GitHub
parent fb5a894595
commit e02495ca7f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 4 deletions

View file

@ -45,6 +45,7 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Arrays; import java.util.Arrays;
import java.util.Base64; import java.util.Base64;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.UUID; import java.util.UUID;
@ -58,6 +59,10 @@ public class SkinProvider {
public static final Skin EMPTY_SKIN = new Skin(-1, "steve", STEVE_SKIN); public static final Skin EMPTY_SKIN = new Skin(-1, "steve", STEVE_SKIN);
public static final byte[] ALEX_SKIN = new ProvidedSkin("bedrock/skin/skin_alex.png").getSkin(); public static final byte[] ALEX_SKIN = new ProvidedSkin("bedrock/skin/skin_alex.png").getSkin();
public static final Skin EMPTY_SKIN_ALEX = new Skin(-1, "alex", ALEX_SKIN); public static final Skin EMPTY_SKIN_ALEX = new Skin(-1, "alex", ALEX_SKIN);
private static final Map<String, Skin> permanentSkins = new HashMap<String, Skin>() {{
put("steve", EMPTY_SKIN);
put("alex", EMPTY_SKIN_ALEX);
}};
private static final Cache<String, Skin> cachedSkins = CacheBuilder.newBuilder() private static final Cache<String, Skin> cachedSkins = CacheBuilder.newBuilder()
.expireAfterAccess(1, TimeUnit.HOURS) .expireAfterAccess(1, TimeUnit.HOURS)
.build(); .build();
@ -141,8 +146,7 @@ public class SkinProvider {
} }
public static Skin getCachedSkin(String skinUrl) { public static Skin getCachedSkin(String skinUrl) {
Skin skin = cachedSkins.getIfPresent(skinUrl); return permanentSkins.getOrDefault(skinUrl, cachedSkins.getIfPresent(skinUrl));
return skin != null ? skin : EMPTY_SKIN;
} }
public static Cape getCachedCape(String capeUrl) { public static Cape getCachedCape(String capeUrl) {
@ -169,7 +173,7 @@ public class SkinProvider {
if (textureUrl == null || textureUrl.isEmpty()) return CompletableFuture.completedFuture(EMPTY_SKIN); if (textureUrl == null || textureUrl.isEmpty()) return CompletableFuture.completedFuture(EMPTY_SKIN);
if (requestedSkins.containsKey(textureUrl)) return requestedSkins.get(textureUrl); // already requested if (requestedSkins.containsKey(textureUrl)) return requestedSkins.get(textureUrl); // already requested
Skin cachedSkin = cachedSkins.getIfPresent(textureUrl); Skin cachedSkin = getCachedSkin(textureUrl);
if (cachedSkin != null) { if (cachedSkin != null) {
return CompletableFuture.completedFuture(cachedSkin); return CompletableFuture.completedFuture(cachedSkin);
} }

View file

@ -55,6 +55,9 @@ public class SkinUtils {
SkinProvider.SkinGeometry geometry = SkinProvider.SkinGeometry.getLegacy(data.isAlex()); SkinProvider.SkinGeometry geometry = SkinProvider.SkinGeometry.getLegacy(data.isAlex());
SkinProvider.Skin skin = SkinProvider.getCachedSkin(data.getSkinUrl()); SkinProvider.Skin skin = SkinProvider.getCachedSkin(data.getSkinUrl());
if (skin == null) {
skin = SkinProvider.EMPTY_SKIN;
}
return buildEntryManually( return buildEntryManually(
session, session,
@ -138,7 +141,7 @@ public class SkinUtils {
*/ */
public static GameProfileData from(GameProfile profile) { public static GameProfileData from(GameProfile profile) {
// Fallback to the offline mode of working it out // Fallback to the offline mode of working it out
boolean isAlex = ((profile.getId().hashCode() % 2) == 1); boolean isAlex = (Math.abs(profile.getId().hashCode() % 2) == 1);
try { try {
GameProfile.Property skinProperty = profile.getProperty("textures"); GameProfile.Property skinProperty = profile.getProperty("textures");