2020-01-09 03:05:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
|
|
|
*/
|
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
package org.geysermc.connector.utils;
|
|
|
|
|
2020-04-29 16:04:45 +00:00
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2019-09-25 21:52:28 +00:00
|
|
|
import lombok.AllArgsConstructor;
|
|
|
|
import lombok.Getter;
|
2020-04-29 16:04:45 +00:00
|
|
|
import lombok.NoArgsConstructor;
|
2019-10-02 20:45:29 +00:00
|
|
|
import org.geysermc.connector.GeyserConnector;
|
2019-09-25 21:52:28 +00:00
|
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
2019-10-02 20:45:29 +00:00
|
|
|
import java.awt.*;
|
2019-09-25 21:52:28 +00:00
|
|
|
import java.awt.image.BufferedImage;
|
2020-05-23 21:06:34 +00:00
|
|
|
import java.io.*;
|
2019-09-25 21:52:28 +00:00
|
|
|
import java.net.URL;
|
2020-05-23 21:06:34 +00:00
|
|
|
import java.nio.charset.Charset;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
2020-04-29 16:04:45 +00:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Base64;
|
2019-09-25 21:52:28 +00:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.UUID;
|
|
|
|
import java.util.concurrent.*;
|
|
|
|
|
|
|
|
public class SkinProvider {
|
2019-12-21 17:35:48 +00:00
|
|
|
public static final boolean ALLOW_THIRD_PARTY_CAPES = GeyserConnector.getInstance().getConfig().isAllowThirdPartyCapes();
|
2019-10-02 20:45:29 +00:00
|
|
|
private static final ExecutorService EXECUTOR_SERVICE = Executors.newFixedThreadPool(ALLOW_THIRD_PARTY_CAPES ? 21 : 14);
|
2019-09-25 21:52:28 +00:00
|
|
|
|
2019-10-02 20:45:29 +00:00
|
|
|
public static final byte[] STEVE_SKIN = new ProvidedSkin("bedrock/skin/skin_steve.png").getSkin();
|
2019-11-19 20:31:24 +00:00
|
|
|
public static final Skin EMPTY_SKIN = new Skin(-1, "steve", STEVE_SKIN);
|
2020-05-23 21:06:34 +00:00
|
|
|
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);
|
2019-09-25 21:52:28 +00:00
|
|
|
private static Map<UUID, Skin> cachedSkins = new ConcurrentHashMap<>();
|
2019-10-02 20:45:29 +00:00
|
|
|
private static Map<UUID, CompletableFuture<Skin>> requestedSkins = new ConcurrentHashMap<>();
|
|
|
|
|
2019-11-19 20:31:24 +00:00
|
|
|
public static final Cape EMPTY_CAPE = new Cape("", "no-cape", new byte[0], -1, true);
|
2019-09-25 21:52:28 +00:00
|
|
|
private static Map<String, Cape> cachedCapes = new ConcurrentHashMap<>();
|
2019-10-02 20:45:29 +00:00
|
|
|
private static Map<String, CompletableFuture<Cape>> requestedCapes = new ConcurrentHashMap<>();
|
2019-09-25 21:52:28 +00:00
|
|
|
|
2020-05-23 21:06:34 +00:00
|
|
|
public static final SkinGeometry EMPTY_GEOMETRY = SkinProvider.SkinGeometry.getLegacy(false);
|
2020-05-06 21:50:01 +00:00
|
|
|
private static Map<UUID, SkinGeometry> cachedGeometry = new ConcurrentHashMap<>();
|
|
|
|
|
2020-05-23 21:06:34 +00:00
|
|
|
public static final boolean ALLOW_THIRD_PARTY_EARS = GeyserConnector.getInstance().getConfig().isAllowThirdPartyEars();
|
|
|
|
public static String EARS_GEOMETRY;
|
|
|
|
public static String EARS_GEOMETRY_SLIM;
|
|
|
|
|
2020-04-29 16:04:45 +00:00
|
|
|
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
2019-09-25 21:52:28 +00:00
|
|
|
private static final int CACHE_INTERVAL = 8 * 60 * 1000; // 8 minutes
|
|
|
|
|
2020-05-23 21:06:34 +00:00
|
|
|
static {
|
|
|
|
/* Load in the normal ears geometry */
|
2020-05-25 01:07:05 +00:00
|
|
|
InputStream earsStream = FileUtils.getResource("bedrock/skin/geometry.humanoid.ears.json");
|
2020-05-23 21:06:34 +00:00
|
|
|
|
|
|
|
StringBuilder earsDataBuilder = new StringBuilder();
|
|
|
|
try (Reader reader = new BufferedReader(new InputStreamReader(earsStream, Charset.forName(StandardCharsets.UTF_8.name())))) {
|
|
|
|
int c = 0;
|
|
|
|
while ((c = reader.read()) != -1) {
|
|
|
|
earsDataBuilder.append((char) c);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new AssertionError("Unable to load ears geometry", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
EARS_GEOMETRY = earsDataBuilder.toString();
|
|
|
|
|
|
|
|
|
|
|
|
/* Load in the slim ears geometry */
|
2020-05-25 01:07:05 +00:00
|
|
|
earsStream = FileUtils.getResource("bedrock/skin/geometry.humanoid.earsSlim.json");
|
2020-05-23 21:06:34 +00:00
|
|
|
|
|
|
|
earsDataBuilder = new StringBuilder();
|
|
|
|
try (Reader reader = new BufferedReader(new InputStreamReader(earsStream, Charset.forName(StandardCharsets.UTF_8.name())))) {
|
|
|
|
int c = 0;
|
|
|
|
while ((c = reader.read()) != -1) {
|
|
|
|
earsDataBuilder.append((char) c);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new AssertionError("Unable to load ears geometry", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
EARS_GEOMETRY_SLIM = earsDataBuilder.toString();
|
|
|
|
}
|
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
public static boolean hasSkinCached(UUID uuid) {
|
|
|
|
return cachedSkins.containsKey(uuid);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean hasCapeCached(String capeUrl) {
|
|
|
|
return cachedCapes.containsKey(capeUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Skin getCachedSkin(UUID uuid) {
|
2019-10-10 21:16:07 +00:00
|
|
|
return cachedSkins.getOrDefault(uuid, EMPTY_SKIN);
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static Cape getCachedCape(String capeUrl) {
|
2019-10-10 21:16:07 +00:00
|
|
|
return capeUrl != null ? cachedCapes.getOrDefault(capeUrl, EMPTY_CAPE) : EMPTY_CAPE;
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 18:39:38 +00:00
|
|
|
public static CompletableFuture<SkinAndCape> requestSkinAndCape(UUID playerId, String skinUrl, String capeUrl) {
|
2019-09-25 21:52:28 +00:00
|
|
|
return CompletableFuture.supplyAsync(() -> {
|
|
|
|
long time = System.currentTimeMillis();
|
|
|
|
|
2020-04-29 16:04:45 +00:00
|
|
|
CapeProvider provider = capeUrl != null ? CapeProvider.MINECRAFT : null;
|
2019-09-25 21:52:28 +00:00
|
|
|
SkinAndCape skinAndCape = new SkinAndCape(
|
2019-10-09 18:39:38 +00:00
|
|
|
getOrDefault(requestSkin(playerId, skinUrl, false), EMPTY_SKIN, 5),
|
2020-04-29 16:04:45 +00:00
|
|
|
getOrDefault(requestCape(capeUrl, provider, false), EMPTY_CAPE, 5)
|
2019-09-25 21:52:28 +00:00
|
|
|
);
|
|
|
|
|
2019-12-21 17:35:48 +00:00
|
|
|
GeyserConnector.getInstance().getLogger().debug("Took " + (System.currentTimeMillis() - time) + "ms for " + playerId);
|
2019-09-25 21:52:28 +00:00
|
|
|
return skinAndCape;
|
2019-10-02 20:45:29 +00:00
|
|
|
}, EXECUTOR_SERVICE);
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 18:39:38 +00:00
|
|
|
public static CompletableFuture<Skin> requestSkin(UUID playerId, String textureUrl, boolean newThread) {
|
2019-09-25 21:52:28 +00:00
|
|
|
if (textureUrl == null || textureUrl.isEmpty()) return CompletableFuture.completedFuture(EMPTY_SKIN);
|
|
|
|
if (requestedSkins.containsKey(playerId)) return requestedSkins.get(playerId); // already requested
|
|
|
|
|
|
|
|
if ((System.currentTimeMillis() - CACHE_INTERVAL) < cachedSkins.getOrDefault(playerId, EMPTY_SKIN).getRequestedOn()) {
|
|
|
|
// no need to update, still cached
|
|
|
|
return CompletableFuture.completedFuture(cachedSkins.get(playerId));
|
|
|
|
}
|
|
|
|
|
|
|
|
CompletableFuture<Skin> future;
|
|
|
|
if (newThread) {
|
2019-10-02 20:45:29 +00:00
|
|
|
future = CompletableFuture.supplyAsync(() -> supplySkin(playerId, textureUrl), EXECUTOR_SERVICE)
|
2019-09-25 21:52:28 +00:00
|
|
|
.whenCompleteAsync((skin, throwable) -> {
|
|
|
|
if (!cachedSkins.getOrDefault(playerId, EMPTY_SKIN).getTextureUrl().equals(textureUrl)) {
|
|
|
|
skin.updated = true;
|
|
|
|
cachedSkins.put(playerId, skin);
|
|
|
|
}
|
|
|
|
requestedSkins.remove(skin.getSkinOwner());
|
|
|
|
});
|
|
|
|
requestedSkins.put(playerId, future);
|
|
|
|
} else {
|
|
|
|
Skin skin = supplySkin(playerId, textureUrl);
|
|
|
|
future = CompletableFuture.completedFuture(skin);
|
|
|
|
cachedSkins.put(playerId, skin);
|
|
|
|
}
|
|
|
|
return future;
|
|
|
|
}
|
|
|
|
|
2020-04-29 16:04:45 +00:00
|
|
|
public static CompletableFuture<Cape> requestCape(String capeUrl, CapeProvider provider, boolean newThread) {
|
2019-09-25 21:52:28 +00:00
|
|
|
if (capeUrl == null || capeUrl.isEmpty()) return CompletableFuture.completedFuture(EMPTY_CAPE);
|
|
|
|
if (requestedCapes.containsKey(capeUrl)) return requestedCapes.get(capeUrl); // already requested
|
|
|
|
|
2020-04-29 16:04:45 +00:00
|
|
|
boolean officialCape = provider == CapeProvider.MINECRAFT;
|
2019-10-02 20:45:29 +00:00
|
|
|
boolean validCache = (System.currentTimeMillis() - CACHE_INTERVAL) < cachedCapes.getOrDefault(capeUrl, EMPTY_CAPE).getRequestedOn();
|
|
|
|
|
|
|
|
if ((cachedCapes.containsKey(capeUrl) && officialCape) || validCache) {
|
|
|
|
// the cape is an official cape (static) or the cape doesn't need a update yet
|
2019-09-25 21:52:28 +00:00
|
|
|
return CompletableFuture.completedFuture(cachedCapes.get(capeUrl));
|
|
|
|
}
|
|
|
|
|
|
|
|
CompletableFuture<Cape> future;
|
|
|
|
if (newThread) {
|
2020-04-29 16:04:45 +00:00
|
|
|
future = CompletableFuture.supplyAsync(() -> supplyCape(capeUrl, provider), EXECUTOR_SERVICE)
|
2019-09-25 21:52:28 +00:00
|
|
|
.whenCompleteAsync((cape, throwable) -> {
|
|
|
|
cachedCapes.put(capeUrl, cape);
|
|
|
|
requestedCapes.remove(capeUrl);
|
|
|
|
});
|
|
|
|
requestedCapes.put(capeUrl, future);
|
|
|
|
} else {
|
2020-04-29 16:04:45 +00:00
|
|
|
Cape cape = supplyCape(capeUrl, provider); // blocking
|
2019-09-25 21:52:28 +00:00
|
|
|
future = CompletableFuture.completedFuture(cape);
|
|
|
|
cachedCapes.put(capeUrl, cape);
|
|
|
|
}
|
|
|
|
return future;
|
|
|
|
}
|
|
|
|
|
2019-10-09 18:39:38 +00:00
|
|
|
public static CompletableFuture<Cape> requestUnofficialCape(Cape officialCape, UUID playerId,
|
|
|
|
String username, boolean newThread) {
|
2019-10-02 20:45:29 +00:00
|
|
|
if (officialCape.isFailed() && ALLOW_THIRD_PARTY_CAPES) {
|
2020-04-29 16:04:45 +00:00
|
|
|
for (CapeProvider provider : CapeProvider.VALUES) {
|
2019-10-02 20:45:29 +00:00
|
|
|
Cape cape1 = getOrDefault(
|
2020-04-29 16:04:45 +00:00
|
|
|
requestCape(provider.getUrlFor(playerId, username), provider, newThread),
|
2019-10-02 20:45:29 +00:00
|
|
|
EMPTY_CAPE, 4
|
|
|
|
);
|
|
|
|
if (!cape1.isFailed()) {
|
|
|
|
return CompletableFuture.completedFuture(cape1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return CompletableFuture.completedFuture(officialCape);
|
|
|
|
}
|
|
|
|
|
2020-05-23 21:06:34 +00:00
|
|
|
public static CompletableFuture<Skin> requestEars(String earsUrl, EarsProvider provider, boolean newThread, Skin skin) {
|
|
|
|
if (earsUrl == null || earsUrl.isEmpty()) return CompletableFuture.completedFuture(skin);
|
|
|
|
|
|
|
|
CompletableFuture<Skin> future;
|
|
|
|
if (newThread) {
|
|
|
|
future = CompletableFuture.supplyAsync(() -> supplyEars(skin, earsUrl, provider), EXECUTOR_SERVICE)
|
|
|
|
.whenCompleteAsync((outSkin, throwable) -> { });
|
|
|
|
} else {
|
|
|
|
Skin ears = supplyEars(skin, earsUrl, provider); // blocking
|
|
|
|
future = CompletableFuture.completedFuture(ears);
|
|
|
|
}
|
|
|
|
return future;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try and find an ear texture for a Java player
|
|
|
|
*
|
|
|
|
* @param officialSkin The current players skin
|
|
|
|
* @param playerId The players UUID
|
|
|
|
* @param username The players username
|
|
|
|
* @param newThread Should we start in a new thread
|
|
|
|
* @return The updated skin with ears
|
|
|
|
*/
|
|
|
|
public static CompletableFuture<Skin> requestUnofficialEars(Skin officialSkin, UUID playerId, String username, boolean newThread) {
|
|
|
|
for (EarsProvider provider : EarsProvider.VALUES) {
|
|
|
|
Skin skin1 = getOrDefault(
|
|
|
|
requestEars(provider.getUrlFor(playerId, username), provider, newThread, officialSkin),
|
|
|
|
officialSkin, 4
|
|
|
|
);
|
|
|
|
if (skin1.isEars()) {
|
|
|
|
return CompletableFuture.completedFuture(skin1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CompletableFuture.completedFuture(officialSkin);
|
|
|
|
}
|
|
|
|
|
2020-05-06 21:50:01 +00:00
|
|
|
public static CompletableFuture<Cape> requestBedrockCape(UUID playerID, boolean newThread) {
|
|
|
|
Cape bedrockCape = cachedCapes.getOrDefault(playerID.toString() + ".Bedrock", EMPTY_CAPE);
|
|
|
|
return CompletableFuture.completedFuture(bedrockCape);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static CompletableFuture<SkinGeometry> requestBedrockGeometry(SkinGeometry currentGeometry, UUID playerID, boolean newThread) {
|
|
|
|
SkinGeometry bedrockGeometry = cachedGeometry.getOrDefault(playerID, currentGeometry);
|
|
|
|
return CompletableFuture.completedFuture(bedrockGeometry);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void storeBedrockSkin(UUID playerID, String skinID, byte[] skinData) {
|
2020-05-23 21:06:34 +00:00
|
|
|
Skin skin = new Skin(playerID, skinID, skinData, System.currentTimeMillis(), true, false);
|
2020-05-06 21:50:01 +00:00
|
|
|
cachedSkins.put(playerID, skin);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void storeBedrockCape(UUID playerID, byte[] capeData) {
|
|
|
|
Cape cape = new Cape(playerID.toString() + ".Bedrock", playerID.toString(), capeData, System.currentTimeMillis(), false);
|
|
|
|
cachedCapes.put(playerID.toString() + ".Bedrock", cape);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void storeBedrockGeometry(UUID playerID, byte[] geometryName, byte[] geometryData) {
|
2020-05-23 21:06:34 +00:00
|
|
|
SkinGeometry geometry = new SkinGeometry(new String(geometryName), new String(geometryData), false);
|
2020-05-06 21:50:01 +00:00
|
|
|
cachedGeometry.put(playerID, geometry);
|
|
|
|
}
|
|
|
|
|
2020-05-23 21:06:34 +00:00
|
|
|
/**
|
|
|
|
* Stores the ajusted skin with the ear texture to the cache
|
|
|
|
*
|
|
|
|
* @param playerID The UUID to cache it against
|
|
|
|
* @param skin The skin to cache
|
|
|
|
*/
|
|
|
|
public static void storeEarSkin(UUID playerID, Skin skin) {
|
|
|
|
cachedSkins.put(playerID, skin);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores the geometry for a Java player with ears
|
|
|
|
*
|
|
|
|
* @param playerID The UUID to cache it against
|
|
|
|
* @param isSlim If the player is using an slim base
|
|
|
|
*/
|
|
|
|
public static void storeEarGeometry(UUID playerID, boolean isSlim) {
|
|
|
|
cachedGeometry.put(playerID, SkinGeometry.getEars(isSlim));
|
|
|
|
}
|
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
private static Skin supplySkin(UUID uuid, String textureUrl) {
|
|
|
|
byte[] skin = EMPTY_SKIN.getSkinData();
|
|
|
|
try {
|
2020-04-29 16:04:45 +00:00
|
|
|
skin = requestImage(textureUrl, null);
|
2019-09-25 21:52:28 +00:00
|
|
|
} catch (Exception ignored) {} // just ignore I guess
|
2020-05-23 21:06:34 +00:00
|
|
|
return new Skin(uuid, textureUrl, skin, System.currentTimeMillis(), false, false);
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 16:04:45 +00:00
|
|
|
private static Cape supplyCape(String capeUrl, CapeProvider provider) {
|
2019-10-02 20:45:29 +00:00
|
|
|
byte[] cape = new byte[0];
|
2019-09-25 21:52:28 +00:00
|
|
|
try {
|
2020-04-29 16:04:45 +00:00
|
|
|
cape = requestImage(capeUrl, provider);
|
2019-09-25 21:52:28 +00:00
|
|
|
} catch (Exception ignored) {} // just ignore I guess
|
2019-10-02 20:45:29 +00:00
|
|
|
|
2019-11-19 20:31:24 +00:00
|
|
|
String[] urlSection = capeUrl.split("/"); // A real url is expected at this stage
|
|
|
|
|
2019-10-02 20:45:29 +00:00
|
|
|
return new Cape(
|
|
|
|
capeUrl,
|
2019-11-19 20:31:24 +00:00
|
|
|
urlSection[urlSection.length - 1], // get the texture id and use it as cape id
|
2019-10-02 20:45:29 +00:00
|
|
|
cape.length > 0 ? cape : EMPTY_CAPE.getCapeData(),
|
|
|
|
System.currentTimeMillis(),
|
|
|
|
cape.length == 0
|
|
|
|
);
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
|
2020-05-23 21:06:34 +00:00
|
|
|
/**
|
|
|
|
* Get the ears texture and place it on the skin from the given URL
|
|
|
|
*
|
|
|
|
* @param existingSkin The players current skin
|
|
|
|
* @param earsUrl The URL to get the ears texture from
|
|
|
|
* @param provider The ears texture provider
|
|
|
|
* @return The updated skin with ears
|
|
|
|
*/
|
|
|
|
private static Skin supplyEars(Skin existingSkin, String earsUrl, EarsProvider provider) {
|
|
|
|
try {
|
|
|
|
// Get the ears texture
|
|
|
|
BufferedImage ears = ImageIO.read(new URL(earsUrl));
|
|
|
|
if (ears == null) throw new NullPointerException();
|
|
|
|
|
|
|
|
// Convert the skin data to a BufferedImage
|
|
|
|
int height = (existingSkin.getSkinData().length / 4 / 64);
|
|
|
|
BufferedImage skinImage = imageDataToBufferedImage(existingSkin.getSkinData(), 64, height);
|
|
|
|
|
|
|
|
// Create a new image with the ears texture over it
|
|
|
|
BufferedImage newSkin = new BufferedImage(skinImage.getWidth(), skinImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
|
|
|
|
Graphics2D g = (Graphics2D) newSkin.getGraphics();
|
|
|
|
g.drawImage(skinImage, 0, 0, null);
|
|
|
|
g.drawImage(ears, 24, 0, null);
|
|
|
|
|
|
|
|
// Turn the buffered image back into an array of bytes
|
|
|
|
byte[] data = bufferedImageToImageData(newSkin);
|
|
|
|
skinImage.flush();
|
|
|
|
|
|
|
|
// Create a new skin object with the new infomation
|
|
|
|
return new Skin(
|
|
|
|
existingSkin.getSkinOwner(),
|
|
|
|
existingSkin.getTextureUrl(),
|
|
|
|
data,
|
|
|
|
System.currentTimeMillis(),
|
|
|
|
true,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
} catch (Exception ignored) {} // just ignore I guess
|
|
|
|
|
|
|
|
return existingSkin;
|
|
|
|
}
|
|
|
|
|
2020-04-29 16:04:45 +00:00
|
|
|
private static byte[] requestImage(String imageUrl, CapeProvider provider) throws Exception {
|
|
|
|
BufferedImage image = downloadImage(imageUrl, provider);
|
2019-12-21 17:35:48 +00:00
|
|
|
GeyserConnector.getInstance().getLogger().debug("Downloaded " + imageUrl);
|
2019-09-25 21:52:28 +00:00
|
|
|
|
2020-04-29 16:04:45 +00:00
|
|
|
// if the requested image is an cape
|
|
|
|
if (provider != null) {
|
2020-05-12 04:45:16 +00:00
|
|
|
while(image.getWidth() > 64) {
|
|
|
|
image = scale(image);
|
|
|
|
}
|
2020-05-23 21:06:34 +00:00
|
|
|
BufferedImage newImage = new BufferedImage(64, 32, BufferedImage.TYPE_INT_ARGB);
|
2019-12-15 02:12:12 +00:00
|
|
|
Graphics g = newImage.createGraphics();
|
|
|
|
g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
|
|
|
|
g.dispose();
|
|
|
|
image = newImage;
|
|
|
|
}
|
2019-10-02 20:45:29 +00:00
|
|
|
|
2020-05-23 21:06:34 +00:00
|
|
|
byte[] data = bufferedImageToImageData(image);
|
|
|
|
image.flush();
|
|
|
|
return data;
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 16:04:45 +00:00
|
|
|
private static BufferedImage downloadImage(String imageUrl, CapeProvider provider) throws IOException {
|
|
|
|
if (provider == CapeProvider.FIVEZIG)
|
|
|
|
return readFiveZigCape(imageUrl);
|
|
|
|
BufferedImage image = ImageIO.read(new URL(imageUrl));
|
|
|
|
if (image == null) throw new NullPointerException();
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static BufferedImage readFiveZigCape(String url) throws IOException {
|
|
|
|
JsonNode element = OBJECT_MAPPER.readTree(WebUtils.getBody(url));
|
|
|
|
if (element != null && element.isObject()) {
|
|
|
|
JsonNode capeElement = element.get("d");
|
|
|
|
if (capeElement == null || capeElement.isNull()) return null;
|
|
|
|
return ImageIO.read(new ByteArrayInputStream(Base64.getDecoder().decode(capeElement.textValue())));
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static BufferedImage scale(BufferedImage bufferedImage) {
|
2020-05-23 21:06:34 +00:00
|
|
|
BufferedImage resized = new BufferedImage(bufferedImage.getWidth() / 2, bufferedImage.getHeight() / 2, BufferedImage.TYPE_INT_ARGB);
|
2019-12-15 02:12:12 +00:00
|
|
|
Graphics2D g2 = resized.createGraphics();
|
|
|
|
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
|
|
|
g2.drawImage(bufferedImage, 0, 0, bufferedImage.getWidth() / 2, bufferedImage.getHeight() / 2, null);
|
|
|
|
g2.dispose();
|
|
|
|
return resized;
|
|
|
|
}
|
|
|
|
|
2020-05-23 21:06:34 +00:00
|
|
|
/**
|
|
|
|
* Get the RGBA int for a given index in some image data
|
|
|
|
*
|
|
|
|
* @param index Index to get
|
|
|
|
* @param data Image data to find in
|
|
|
|
* @return An int representing RGBA
|
|
|
|
*/
|
|
|
|
private static int getRGBA(int index, byte[] data) {
|
|
|
|
return (data[index] & 0xFF) << 16 | (data[index + 1] & 0xFF) << 8 |
|
|
|
|
data[index + 2] & 0xFF | (data[index + 3] & 0xFF) << 24;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a byte[] to a BufferedImage
|
|
|
|
*
|
|
|
|
* @param imageData The byte[] to convert
|
|
|
|
* @param imageWidth The width of the target image
|
|
|
|
* @param imageHeight The height of the target image
|
|
|
|
* @return The converted BufferedImage
|
|
|
|
*/
|
|
|
|
public static BufferedImage imageDataToBufferedImage(byte[] imageData, int imageWidth, int imageHeight) {
|
|
|
|
BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB);
|
|
|
|
int index = 0;
|
|
|
|
for (int y = 0; y < imageHeight; y++) {
|
|
|
|
for (int x = 0; x < imageWidth; x++) {
|
|
|
|
image.setRGB(x, y, getRGBA(index, imageData));
|
|
|
|
index += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a BufferedImage to a byte[]
|
|
|
|
*
|
|
|
|
* @param image The BufferedImage to convert
|
|
|
|
* @return The converted byte[]
|
|
|
|
*/
|
|
|
|
public static byte[] bufferedImageToImageData(BufferedImage image) {
|
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(image.getWidth() * 4 + image.getHeight() * 4);
|
|
|
|
for (int y = 0; y < image.getHeight(); y++) {
|
|
|
|
for (int x = 0; x < image.getWidth(); x++) {
|
|
|
|
int rgba = image.getRGB(x, y);
|
|
|
|
outputStream.write((rgba >> 16) & 0xFF);
|
|
|
|
outputStream.write((rgba >> 8) & 0xFF);
|
|
|
|
outputStream.write(rgba & 0xFF);
|
|
|
|
outputStream.write((rgba >> 24) & 0xFF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return outputStream.toByteArray();
|
|
|
|
}
|
|
|
|
|
2019-10-02 20:45:29 +00:00
|
|
|
public static <T> T getOrDefault(CompletableFuture<T> future, T defaultValue, int timeoutInSeconds) {
|
|
|
|
try {
|
|
|
|
return future.get(timeoutInSeconds, TimeUnit.SECONDS);
|
|
|
|
} catch (Exception ignored) {}
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
@AllArgsConstructor
|
|
|
|
@Getter
|
|
|
|
public static class SkinAndCape {
|
|
|
|
private Skin skin;
|
|
|
|
private Cape cape;
|
|
|
|
}
|
|
|
|
|
|
|
|
@AllArgsConstructor
|
|
|
|
@Getter
|
|
|
|
public static class Skin {
|
|
|
|
private UUID skinOwner;
|
|
|
|
private String textureUrl;
|
2019-11-19 20:31:24 +00:00
|
|
|
private byte[] skinData;
|
2019-09-25 21:52:28 +00:00
|
|
|
private long requestedOn;
|
|
|
|
private boolean updated;
|
2020-05-23 21:06:34 +00:00
|
|
|
private boolean ears;
|
2019-09-25 21:52:28 +00:00
|
|
|
|
2019-11-19 20:31:24 +00:00
|
|
|
private Skin(long requestedOn, String textureUrl, byte[] skinData) {
|
2019-09-25 21:52:28 +00:00
|
|
|
this.requestedOn = requestedOn;
|
|
|
|
this.textureUrl = textureUrl;
|
2019-11-19 20:31:24 +00:00
|
|
|
this.skinData = skinData;
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@AllArgsConstructor
|
|
|
|
@Getter
|
|
|
|
public static class Cape {
|
|
|
|
private String textureUrl;
|
2019-11-19 20:31:24 +00:00
|
|
|
private String capeId;
|
2019-09-25 21:52:28 +00:00
|
|
|
private byte[] capeData;
|
2019-10-02 20:45:29 +00:00
|
|
|
private long requestedOn;
|
|
|
|
private boolean failed;
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 21:50:01 +00:00
|
|
|
@AllArgsConstructor
|
|
|
|
@Getter
|
|
|
|
public static class SkinGeometry {
|
|
|
|
private String geometryName;
|
|
|
|
private String geometryData;
|
2020-05-23 21:06:34 +00:00
|
|
|
private boolean failed;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate generic geometry
|
|
|
|
*
|
|
|
|
* @param isSlim Should it be the alex model
|
|
|
|
* @return The generic geometry object
|
|
|
|
*/
|
|
|
|
public static SkinGeometry getLegacy(boolean isSlim) {
|
|
|
|
return new SkinProvider.SkinGeometry("{\"geometry\" :{\"default\" :\"geometry.humanoid.custom" + (isSlim ? "Slim" : "") + "\"}}", "", true);
|
|
|
|
}
|
2020-05-06 21:50:01 +00:00
|
|
|
|
2020-05-23 21:06:34 +00:00
|
|
|
/**
|
|
|
|
* Generate basic geometry with ears
|
|
|
|
*
|
|
|
|
* @param isSlim Should it be the alex model
|
|
|
|
* @return The generated geometry for the ears model
|
|
|
|
*/
|
|
|
|
public static SkinGeometry getEars(boolean isSlim) {
|
|
|
|
return new SkinProvider.SkinGeometry("{\"geometry\" :{\"default\" :\"geometry.humanoid.ears" + (isSlim ? "Slim" : "") + "\"}}", (isSlim ? EARS_GEOMETRY_SLIM : EARS_GEOMETRY), false);
|
2020-05-06 21:50:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-02 20:45:29 +00:00
|
|
|
/*
|
|
|
|
* Sorted by 'priority'
|
|
|
|
*/
|
|
|
|
@AllArgsConstructor
|
2020-04-29 16:04:45 +00:00
|
|
|
@NoArgsConstructor
|
2019-10-02 20:45:29 +00:00
|
|
|
@Getter
|
2020-04-29 16:04:45 +00:00
|
|
|
public enum CapeProvider {
|
|
|
|
MINECRAFT,
|
2019-10-02 20:45:29 +00:00
|
|
|
OPTIFINE("http://s.optifine.net/capes/%s.png", CapeUrlType.USERNAME),
|
2020-04-29 16:04:45 +00:00
|
|
|
LABYMOD("https://www.labymod.net/page/php/getCapeTexture.php?uuid=%s", CapeUrlType.UUID_DASHED),
|
|
|
|
FIVEZIG("https://textures.5zigreborn.eu/profile/%s", CapeUrlType.UUID_DASHED),
|
2019-10-02 20:45:29 +00:00
|
|
|
MINECRAFTCAPES("https://www.minecraftcapes.co.uk/getCape/%s", CapeUrlType.UUID);
|
|
|
|
|
2020-04-29 16:04:45 +00:00
|
|
|
public static final CapeProvider[] VALUES = Arrays.copyOfRange(values(), 1, 5);
|
2019-10-02 20:45:29 +00:00
|
|
|
private String url;
|
|
|
|
private CapeUrlType type;
|
|
|
|
|
|
|
|
public String getUrlFor(String type) {
|
|
|
|
return String.format(url, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getUrlFor(UUID uuid, String username) {
|
|
|
|
return getUrlFor(toRequestedType(type, uuid, username));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String toRequestedType(CapeUrlType type, UUID uuid, String username) {
|
|
|
|
switch (type) {
|
|
|
|
case UUID: return uuid.toString().replace("-", "");
|
|
|
|
case UUID_DASHED: return uuid.toString();
|
|
|
|
default: return username;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public enum CapeUrlType {
|
|
|
|
USERNAME,
|
|
|
|
UUID,
|
|
|
|
UUID_DASHED
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
2020-05-23 21:06:34 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Sorted by 'priority'
|
|
|
|
*/
|
|
|
|
@AllArgsConstructor
|
|
|
|
@NoArgsConstructor
|
|
|
|
@Getter
|
|
|
|
public enum EarsProvider {
|
|
|
|
MINECRAFTCAPES("https://www.minecraftcapes.co.uk/getEars/%s", CapeUrlType.UUID);
|
|
|
|
|
|
|
|
public static final EarsProvider[] VALUES = values();
|
|
|
|
private String url;
|
|
|
|
private CapeUrlType type;
|
|
|
|
|
|
|
|
public String getUrlFor(String type) {
|
|
|
|
return String.format(url, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getUrlFor(UUID uuid, String username) {
|
|
|
|
return getUrlFor(toRequestedType(type, uuid, username));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String toRequestedType(CapeUrlType type, UUID uuid, String username) {
|
|
|
|
switch (type) {
|
|
|
|
case UUID: return uuid.toString().replace("-", "");
|
|
|
|
case UUID_DASHED: return uuid.toString();
|
|
|
|
default: return username;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|