2020-01-09 03:05:42 +00:00
|
|
|
/*
|
2021-01-01 15:10:36 +00:00
|
|
|
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
|
2020-01-09 03:05:42 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2021-11-20 21:34:30 +00:00
|
|
|
package org.geysermc.geyser.skin;
|
2019-09-25 21:52:28 +00:00
|
|
|
|
2020-04-29 16:04:45 +00:00
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2020-12-04 21:55:24 +00:00
|
|
|
import com.github.steveice10.mc.auth.data.GameProfile;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.IntArrayTag;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.Tag;
|
2020-08-07 16:33:21 +00:00
|
|
|
import com.google.common.cache.Cache;
|
|
|
|
import com.google.common.cache.CacheBuilder;
|
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;
|
2021-11-20 21:34:30 +00:00
|
|
|
import org.geysermc.geyser.GeyserImpl;
|
2021-11-23 03:47:58 +00:00
|
|
|
import org.geysermc.geyser.entity.type.player.PlayerEntity;
|
2021-11-22 19:52:26 +00:00
|
|
|
import org.geysermc.geyser.session.GeyserSession;
|
2021-11-23 03:47:58 +00:00
|
|
|
import org.geysermc.geyser.text.GeyserLocale;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.util.FileUtils;
|
|
|
|
import org.geysermc.geyser.util.WebUtils;
|
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-12-04 21:55:24 +00:00
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2020-07-11 17:22:02 +00:00
|
|
|
import java.net.HttpURLConnection;
|
2019-09-25 21:52:28 +00:00
|
|
|
import java.net.URL;
|
2020-05-23 21:06:34 +00:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2020-12-04 21:55:24 +00:00
|
|
|
import java.util.List;
|
2020-10-23 04:01:03 +00:00
|
|
|
import java.util.*;
|
2019-09-25 21:52:28 +00:00
|
|
|
import java.util.concurrent.*;
|
|
|
|
|
|
|
|
public class SkinProvider {
|
2021-11-20 21:34:30 +00:00
|
|
|
public static final boolean ALLOW_THIRD_PARTY_CAPES = GeyserImpl.getInstance().getConfig().isAllowThirdPartyCapes();
|
2021-11-22 19:49:55 +00:00
|
|
|
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);
|
2021-09-28 23:25:34 +00:00
|
|
|
private static final Map<String, Skin> permanentSkins = new HashMap<>() {{
|
2020-08-12 13:23:11 +00:00
|
|
|
put("steve", EMPTY_SKIN);
|
|
|
|
put("alex", EMPTY_SKIN_ALEX);
|
|
|
|
}};
|
2020-08-07 16:33:21 +00:00
|
|
|
private static final Cache<String, Skin> cachedSkins = CacheBuilder.newBuilder()
|
|
|
|
.expireAfterAccess(1, TimeUnit.HOURS)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
private static final Map<String, CompletableFuture<Skin>> requestedSkins = new ConcurrentHashMap<>();
|
2019-10-02 20:45:29 +00:00
|
|
|
|
2019-11-19 20:31:24 +00:00
|
|
|
public static final Cape EMPTY_CAPE = new Cape("", "no-cape", new byte[0], -1, true);
|
2020-08-07 16:33:21 +00:00
|
|
|
private static final Cache<String, Cape> cachedCapes = CacheBuilder.newBuilder()
|
|
|
|
.expireAfterAccess(1, TimeUnit.HOURS)
|
|
|
|
.build();
|
|
|
|
private static final Map<String, CompletableFuture<Cape>> requestedCapes = new ConcurrentHashMap<>();
|
2019-09-25 21:52:28 +00:00
|
|
|
|
2020-08-07 16:33:21 +00:00
|
|
|
private static final Map<UUID, SkinGeometry> cachedGeometry = new ConcurrentHashMap<>();
|
2020-05-06 21:50:01 +00:00
|
|
|
|
2021-11-20 21:34:30 +00:00
|
|
|
public static final boolean ALLOW_THIRD_PARTY_EARS = GeyserImpl.getInstance().getConfig().isAllowThirdPartyEars();
|
2021-02-15 21:36:47 +00:00
|
|
|
public static final String EARS_GEOMETRY;
|
|
|
|
public static final String EARS_GEOMETRY_SLIM;
|
|
|
|
public static final SkinGeometry SKULL_GEOMETRY;
|
2021-11-22 19:49:55 +00:00
|
|
|
public static final SkinGeometry WEARING_CUSTOM_SKULL;
|
|
|
|
public static final SkinGeometry WEARING_CUSTOM_SKULL_SLIM;
|
2020-05-23 21:06:34 +00:00
|
|
|
|
2020-11-18 18:38:49 +00:00
|
|
|
public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
2019-09-25 21:52:28 +00:00
|
|
|
|
2020-05-23 21:06:34 +00:00
|
|
|
static {
|
|
|
|
/* Load in the normal ears geometry */
|
2020-12-04 21:55:24 +00:00
|
|
|
EARS_GEOMETRY = new String(FileUtils.readAllBytes(FileUtils.getResource("bedrock/skin/geometry.humanoid.ears.json")), StandardCharsets.UTF_8);
|
2020-05-23 21:06:34 +00:00
|
|
|
|
|
|
|
/* Load in the slim ears geometry */
|
2020-12-04 21:55:24 +00:00
|
|
|
EARS_GEOMETRY_SLIM = new String(FileUtils.readAllBytes(FileUtils.getResource("bedrock/skin/geometry.humanoid.earsSlim.json")), StandardCharsets.UTF_8);
|
2020-05-23 21:06:34 +00:00
|
|
|
|
2020-12-04 21:55:24 +00:00
|
|
|
/* Load in the custom skull geometry */
|
|
|
|
String skullData = new String(FileUtils.readAllBytes(FileUtils.getResource("bedrock/skin/geometry.humanoid.customskull.json")), StandardCharsets.UTF_8);
|
|
|
|
SKULL_GEOMETRY = new SkinGeometry("{\"geometry\" :{\"default\" :\"geometry.humanoid.customskull\"}}", skullData, false);
|
2020-05-23 21:06:34 +00:00
|
|
|
|
2021-11-22 19:49:55 +00:00
|
|
|
/* Load in the player head skull geometry */
|
|
|
|
String wearingCustomSkull = new String(FileUtils.readAllBytes(FileUtils.getResource("bedrock/skin/geometry.humanoid.wearingCustomSkull.json")), StandardCharsets.UTF_8);
|
|
|
|
WEARING_CUSTOM_SKULL = new SkinGeometry("{\"geometry\" :{\"default\" :\"geometry.humanoid.wearingCustomSkull\"}}", wearingCustomSkull, false);
|
|
|
|
String wearingCustomSkullSlim = new String(FileUtils.readAllBytes(FileUtils.getResource("bedrock/skin/geometry.humanoid.wearingCustomSkullSlim.json")), StandardCharsets.UTF_8);
|
|
|
|
WEARING_CUSTOM_SKULL_SLIM = new SkinGeometry("{\"geometry\" :{\"default\" :\"geometry.humanoid.wearingCustomSkullSlim\"}}", wearingCustomSkullSlim, false);
|
|
|
|
|
2020-08-07 16:33:21 +00:00
|
|
|
// Schedule Daily Image Expiry if we are caching them
|
2021-11-20 21:34:30 +00:00
|
|
|
if (GeyserImpl.getInstance().getConfig().getCacheImages() > 0) {
|
|
|
|
GeyserImpl.getInstance().getScheduledThread().scheduleAtFixedRate(() -> {
|
|
|
|
File cacheFolder = GeyserImpl.getInstance().getBootstrap().getConfigFolder().resolve("cache").resolve("images").toFile();
|
2020-08-07 16:33:21 +00:00
|
|
|
if (!cacheFolder.exists()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int count = 0;
|
2021-11-20 21:34:30 +00:00
|
|
|
final long expireTime = ((long) GeyserImpl.getInstance().getConfig().getCacheImages()) * ((long)1000 * 60 * 60 * 24);
|
2020-08-07 16:33:21 +00:00
|
|
|
for (File imageFile : Objects.requireNonNull(cacheFolder.listFiles())) {
|
|
|
|
if (imageFile.lastModified() < System.currentTimeMillis() - expireTime) {
|
|
|
|
//noinspection ResultOfMethodCallIgnored
|
|
|
|
imageFile.delete();
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count > 0) {
|
2021-11-20 21:34:30 +00:00
|
|
|
GeyserImpl.getInstance().getLogger().debug(String.format("Removed %d cached image files as they have expired", count));
|
2020-08-07 16:33:21 +00:00
|
|
|
}
|
|
|
|
}, 10, 1440, TimeUnit.MINUTES);
|
|
|
|
}
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean hasCapeCached(String capeUrl) {
|
2020-08-07 16:33:21 +00:00
|
|
|
return cachedCapes.getIfPresent(capeUrl) != null;
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
|
2020-08-07 16:33:21 +00:00
|
|
|
public static Skin getCachedSkin(String skinUrl) {
|
2020-08-12 13:23:11 +00:00
|
|
|
return permanentSkins.getOrDefault(skinUrl, cachedSkins.getIfPresent(skinUrl));
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static Cape getCachedCape(String capeUrl) {
|
2020-08-07 16:33:21 +00:00
|
|
|
Cape cape = capeUrl != null ? cachedCapes.getIfPresent(capeUrl) : EMPTY_CAPE;
|
|
|
|
return cape != null ? cape : EMPTY_CAPE;
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 19:49:55 +00:00
|
|
|
public static CompletableFuture<SkinProvider.SkinData> requestSkinData(PlayerEntity entity) {
|
|
|
|
SkinManager.GameProfileData data = SkinManager.GameProfileData.from(entity.getProfile());
|
|
|
|
|
|
|
|
return requestSkinAndCape(entity.getUuid(), data.skinUrl(), data.capeUrl())
|
|
|
|
.thenApplyAsync(skinAndCape -> {
|
|
|
|
try {
|
|
|
|
Skin skin = skinAndCape.getSkin();
|
|
|
|
Cape cape = skinAndCape.getCape();
|
|
|
|
SkinGeometry geometry = SkinGeometry.getLegacy(data.isAlex());
|
|
|
|
|
|
|
|
if (cape.isFailed()) {
|
|
|
|
cape = getOrDefault(requestBedrockCape(entity.getUuid()),
|
|
|
|
EMPTY_CAPE, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cape.isFailed() && ALLOW_THIRD_PARTY_CAPES) {
|
|
|
|
cape = getOrDefault(requestUnofficialCape(
|
|
|
|
cape, entity.getUuid(),
|
|
|
|
entity.getUsername(), false
|
|
|
|
), EMPTY_CAPE, CapeProvider.VALUES.length * 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
geometry = getOrDefault(requestBedrockGeometry(
|
|
|
|
geometry, entity.getUuid()
|
|
|
|
), geometry, 3);
|
|
|
|
|
|
|
|
boolean isDeadmau5 = "deadmau5".equals(entity.getUsername());
|
|
|
|
// Not a bedrock player check for ears
|
|
|
|
if (geometry.isFailed() && (ALLOW_THIRD_PARTY_EARS || isDeadmau5)) {
|
|
|
|
boolean isEars;
|
|
|
|
|
|
|
|
// Its deadmau5, gotta support his skin :)
|
|
|
|
if (isDeadmau5) {
|
|
|
|
isEars = true;
|
|
|
|
} else {
|
|
|
|
// Get the ears texture for the player
|
|
|
|
skin = getOrDefault(requestUnofficialEars(
|
|
|
|
skin, entity.getUuid(), entity.getUsername(), false
|
|
|
|
), skin, 3);
|
|
|
|
|
|
|
|
isEars = skin.isEars();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Does the skin have an ears texture
|
|
|
|
if (isEars) {
|
|
|
|
// Get the new geometry
|
|
|
|
geometry = SkinGeometry.getEars(data.isAlex());
|
|
|
|
|
|
|
|
// Store the skin and geometry for the ears
|
|
|
|
storeEarSkin(skin);
|
|
|
|
storeEarGeometry(entity.getUuid(), data.isAlex());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new SkinData(skin, cape, geometry);
|
|
|
|
} catch (Exception e) {
|
2021-11-23 03:47:58 +00:00
|
|
|
GeyserImpl.getInstance().getLogger().error(GeyserLocale.getLocaleStringLog("geyser.skin.fail", entity.getUuid()), e);
|
2021-11-22 19:49:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return new SkinData(skinAndCape.getSkin(), skinAndCape.getCape(), null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-10-23 04:01:03 +00:00
|
|
|
String newSkinUrl = skinUrl;
|
|
|
|
|
|
|
|
if ("steve".equals(skinUrl) || "alex".equals(skinUrl)) {
|
2021-11-22 19:52:26 +00:00
|
|
|
GeyserSession session = GeyserImpl.getInstance().connectionByUuid(playerId);
|
2020-12-17 16:58:49 +00:00
|
|
|
|
|
|
|
if (session != null) {
|
|
|
|
newSkinUrl = session.getClientData().getSkinId();
|
2020-10-23 04:01:03 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-25 21:52:28 +00:00
|
|
|
|
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(
|
2020-10-23 04:01:03 +00:00
|
|
|
getOrDefault(requestSkin(playerId, newSkinUrl, 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
|
|
|
);
|
|
|
|
|
2021-11-20 21:34:30 +00:00
|
|
|
GeyserImpl.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);
|
2021-09-10 20:32:09 +00:00
|
|
|
CompletableFuture<Skin> requestedSkin = requestedSkins.get(textureUrl);
|
|
|
|
if (requestedSkin != null) {
|
|
|
|
// already requested
|
|
|
|
return requestedSkin;
|
|
|
|
}
|
2019-09-25 21:52:28 +00:00
|
|
|
|
2020-08-12 13:23:11 +00:00
|
|
|
Skin cachedSkin = getCachedSkin(textureUrl);
|
2020-08-07 16:33:21 +00:00
|
|
|
if (cachedSkin != null) {
|
|
|
|
return CompletableFuture.completedFuture(cachedSkin);
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) -> {
|
2020-08-07 16:33:21 +00:00
|
|
|
skin.updated = true;
|
|
|
|
cachedSkins.put(textureUrl, skin);
|
|
|
|
requestedSkins.remove(textureUrl);
|
2019-09-25 21:52:28 +00:00
|
|
|
});
|
2020-08-07 16:33:21 +00:00
|
|
|
requestedSkins.put(textureUrl, future);
|
2019-09-25 21:52:28 +00:00
|
|
|
} else {
|
|
|
|
Skin skin = supplySkin(playerId, textureUrl);
|
|
|
|
future = CompletableFuture.completedFuture(skin);
|
2020-08-07 16:33:21 +00:00
|
|
|
cachedSkins.put(textureUrl, skin);
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
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-08-07 16:33:21 +00:00
|
|
|
Cape cachedCape = cachedCapes.getIfPresent(capeUrl);
|
|
|
|
if (cachedCape != null) {
|
|
|
|
return CompletableFuture.completedFuture(cachedCape);
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-02-15 21:36:47 +00:00
|
|
|
public static CompletableFuture<Skin> requestEars(String earsUrl, boolean newThread, Skin skin) {
|
2020-05-23 21:06:34 +00:00
|
|
|
if (earsUrl == null || earsUrl.isEmpty()) return CompletableFuture.completedFuture(skin);
|
|
|
|
|
|
|
|
CompletableFuture<Skin> future;
|
|
|
|
if (newThread) {
|
2021-02-15 21:36:47 +00:00
|
|
|
future = CompletableFuture.supplyAsync(() -> supplyEars(skin, earsUrl), EXECUTOR_SERVICE)
|
2020-05-23 21:06:34 +00:00
|
|
|
.whenCompleteAsync((outSkin, throwable) -> { });
|
|
|
|
} else {
|
2021-02-15 21:36:47 +00:00
|
|
|
Skin ears = supplyEars(skin, earsUrl); // blocking
|
2020-05-23 21:06:34 +00:00
|
|
|
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(
|
2021-02-15 21:36:47 +00:00
|
|
|
requestEars(provider.getUrlFor(playerId, username), newThread, officialSkin),
|
2020-05-23 21:06:34 +00:00
|
|
|
officialSkin, 4
|
|
|
|
);
|
|
|
|
if (skin1.isEars()) {
|
|
|
|
return CompletableFuture.completedFuture(skin1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CompletableFuture.completedFuture(officialSkin);
|
|
|
|
}
|
|
|
|
|
2020-12-04 21:55:24 +00:00
|
|
|
public static CompletableFuture<Cape> requestBedrockCape(UUID playerID) {
|
2020-08-07 16:33:21 +00:00
|
|
|
Cape bedrockCape = cachedCapes.getIfPresent(playerID.toString() + ".Bedrock");
|
|
|
|
if (bedrockCape == null) {
|
|
|
|
bedrockCape = EMPTY_CAPE;
|
|
|
|
}
|
2020-05-06 21:50:01 +00:00
|
|
|
return CompletableFuture.completedFuture(bedrockCape);
|
|
|
|
}
|
|
|
|
|
2020-12-04 21:55:24 +00:00
|
|
|
public static CompletableFuture<SkinGeometry> requestBedrockGeometry(SkinGeometry currentGeometry, UUID playerID) {
|
2020-05-06 21:50:01 +00:00
|
|
|
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-08-07 16:33:21 +00:00
|
|
|
cachedSkins.put(skin.getTextureUrl(), skin);
|
2020-05-06 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
/**
|
2021-02-15 21:36:47 +00:00
|
|
|
* Stores the adjusted skin with the ear texture to the cache
|
2020-05-23 21:06:34 +00:00
|
|
|
*
|
|
|
|
* @param skin The skin to cache
|
|
|
|
*/
|
2021-02-15 21:36:47 +00:00
|
|
|
public static void storeEarSkin(Skin skin) {
|
2020-08-07 16:33:21 +00:00
|
|
|
cachedSkins.put(skin.getTextureUrl(), skin);
|
2020-05-23 21:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
|
|
|
try {
|
2020-08-07 16:33:21 +00:00
|
|
|
byte[] skin = requestImage(textureUrl, null);
|
|
|
|
return new Skin(uuid, textureUrl, skin, System.currentTimeMillis(), false, false);
|
2019-09-25 21:52:28 +00:00
|
|
|
} catch (Exception ignored) {} // just ignore I guess
|
2020-08-07 16:33:21 +00:00
|
|
|
|
|
|
|
return new Skin(uuid, "empty", EMPTY_SKIN.getSkinData(), 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) {
|
2021-02-15 21:36:47 +00:00
|
|
|
byte[] cape = EMPTY_CAPE.getCapeData();
|
2019-09-25 21:52:28 +00:00
|
|
|
try {
|
2020-04-29 16:04:45 +00:00
|
|
|
cape = requestImage(capeUrl, provider);
|
2021-11-22 19:49:55 +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
|
2021-02-15 21:36:47 +00:00
|
|
|
cape,
|
2019-10-02 20:45:29 +00:00
|
|
|
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
|
|
|
|
* @return The updated skin with ears
|
|
|
|
*/
|
2021-02-15 21:36:47 +00:00
|
|
|
private static Skin supplyEars(Skin existingSkin, String earsUrl) {
|
2020-05-23 21:06:34 +00:00
|
|
|
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-08-07 16:33:21 +00:00
|
|
|
@SuppressWarnings("ResultOfMethodCallIgnored")
|
2020-04-29 16:04:45 +00:00
|
|
|
private static byte[] requestImage(String imageUrl, CapeProvider provider) throws Exception {
|
2020-08-07 16:33:21 +00:00
|
|
|
BufferedImage image = null;
|
|
|
|
|
|
|
|
// First see if we have a cached file. We also update the modification stamp so we know when the file was last used
|
2021-11-20 21:34:30 +00:00
|
|
|
File imageFile = GeyserImpl.getInstance().getBootstrap().getConfigFolder().resolve("cache").resolve("images").resolve(UUID.nameUUIDFromBytes(imageUrl.getBytes()).toString() + ".png").toFile();
|
2020-08-07 16:33:21 +00:00
|
|
|
if (imageFile.exists()) {
|
|
|
|
try {
|
2021-11-20 21:34:30 +00:00
|
|
|
GeyserImpl.getInstance().getLogger().debug("Reading cached image from file " + imageFile.getPath() + " for " + imageUrl);
|
2020-08-07 16:33:21 +00:00
|
|
|
imageFile.setLastModified(System.currentTimeMillis());
|
|
|
|
image = ImageIO.read(imageFile);
|
|
|
|
} catch (IOException ignored) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no image we download it
|
|
|
|
if (image == null) {
|
|
|
|
image = downloadImage(imageUrl, provider);
|
2021-11-20 21:34:30 +00:00
|
|
|
GeyserImpl.getInstance().getLogger().debug("Downloaded " + imageUrl);
|
2020-08-07 16:33:21 +00:00
|
|
|
|
|
|
|
// Write to cache if we are allowed
|
2021-11-20 21:34:30 +00:00
|
|
|
if (GeyserImpl.getInstance().getConfig().getCacheImages() > 0) {
|
2020-08-07 16:33:21 +00:00
|
|
|
imageFile.getParentFile().mkdirs();
|
|
|
|
try {
|
|
|
|
ImageIO.write(image, "png", imageFile);
|
2021-11-20 21:34:30 +00:00
|
|
|
GeyserImpl.getInstance().getLogger().debug("Writing cached skin to file " + imageFile.getPath() + " for " + imageUrl);
|
2020-08-07 16:33:21 +00:00
|
|
|
} catch (IOException e) {
|
2021-11-20 21:34:30 +00:00
|
|
|
GeyserImpl.getInstance().getLogger().error("Failed to write cached skin to file " + imageFile.getPath() + " for " + imageUrl);
|
2020-08-07 16:33:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-25 21:52:28 +00:00
|
|
|
|
2020-08-07 16:33:21 +00:00
|
|
|
// if the requested image is a cape
|
2020-04-29 16:04:45 +00:00
|
|
|
if (provider != null) {
|
2021-03-04 16:32:56 +00:00
|
|
|
if (image.getWidth() > 64 || image.getHeight() > 32) {
|
2021-03-03 23:00:29 +00:00
|
|
|
// Prevent weirdly-scaled capes from being cut off
|
|
|
|
BufferedImage newImage = new BufferedImage(128, 64, BufferedImage.TYPE_INT_ARGB);
|
|
|
|
Graphics g = newImage.createGraphics();
|
|
|
|
g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
|
|
|
|
g.dispose();
|
|
|
|
image.flush();
|
|
|
|
image = scale(newImage, 64, 32);
|
2021-03-04 16:32:56 +00:00
|
|
|
} else if (image.getWidth() < 64 || image.getHeight() < 32) {
|
|
|
|
// Bedrock doesn't like smaller-sized capes, either.
|
|
|
|
BufferedImage newImage = new BufferedImage(64, 32, BufferedImage.TYPE_INT_ARGB);
|
|
|
|
Graphics g = newImage.createGraphics();
|
|
|
|
g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
|
|
|
|
g.dispose();
|
|
|
|
image.flush();
|
|
|
|
image = newImage;
|
2021-02-15 21:36:47 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Very rarely, skins can be larger than Minecraft's default.
|
|
|
|
// Bedrock will not render anything above a width of 128.
|
|
|
|
if (image.getWidth() > 128) {
|
2021-03-03 23:00:29 +00:00
|
|
|
// On Height: Scale by the amount we divided width by, or simply cut down to 128
|
|
|
|
image = scale(image, 128, image.getHeight() >= 256 ? (image.getHeight() / (image.getWidth() / 128)) : 128);
|
2020-05-12 04:45:16 +00:00
|
|
|
}
|
2021-03-03 23:00:29 +00:00
|
|
|
|
|
|
|
// TODO remove alpha channel
|
2019-12-15 02:12:12 +00:00
|
|
|
}
|
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-12-04 21:55:24 +00:00
|
|
|
/**
|
|
|
|
* If a skull has a username but no textures, request them.
|
2021-11-22 19:49:55 +00:00
|
|
|
*
|
2020-12-04 21:55:24 +00:00
|
|
|
* @param skullOwner the CompoundTag of the skull with no textures
|
|
|
|
* @return a completable GameProfile with textures included
|
|
|
|
*/
|
|
|
|
public static CompletableFuture<GameProfile> requestTexturesFromUsername(CompoundTag skullOwner) {
|
|
|
|
return CompletableFuture.supplyAsync(() -> {
|
|
|
|
Tag uuidTag = skullOwner.get("Id");
|
|
|
|
String uuidToString = "";
|
|
|
|
JsonNode node;
|
|
|
|
GameProfile gameProfile = new GameProfile(UUID.randomUUID(), "");
|
|
|
|
boolean retrieveUuidFromInternet = !(uuidTag instanceof IntArrayTag); // also covers null check
|
|
|
|
|
|
|
|
if (!retrieveUuidFromInternet) {
|
|
|
|
int[] uuidAsArray = ((IntArrayTag) uuidTag).getValue();
|
|
|
|
// thank u viaversion
|
|
|
|
UUID uuid = new UUID((long) uuidAsArray[0] << 32 | ((long) uuidAsArray[1] & 0xFFFFFFFFL),
|
|
|
|
(long) uuidAsArray[2] << 32 | ((long) uuidAsArray[3] & 0xFFFFFFFFL));
|
|
|
|
retrieveUuidFromInternet = uuid.version() != 4;
|
|
|
|
uuidToString = uuid.toString().replace("-", "");
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (retrieveUuidFromInternet) {
|
|
|
|
// Offline skin, or no present UUID
|
|
|
|
node = WebUtils.getJson("https://api.mojang.com/users/profiles/minecraft/" + skullOwner.get("Name").getValue());
|
|
|
|
JsonNode id = node.get("id");
|
|
|
|
if (id == null) {
|
2021-11-20 21:34:30 +00:00
|
|
|
GeyserImpl.getInstance().getLogger().debug("No UUID found in Mojang response for " + skullOwner.get("Name").getValue());
|
2020-12-04 21:55:24 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
uuidToString = id.asText();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get textures from UUID
|
|
|
|
node = WebUtils.getJson("https://sessionserver.mojang.com/session/minecraft/profile/" + uuidToString);
|
|
|
|
List<GameProfile.Property> profileProperties = new ArrayList<>();
|
|
|
|
JsonNode properties = node.get("properties");
|
|
|
|
if (properties == null) {
|
2021-11-20 21:34:30 +00:00
|
|
|
GeyserImpl.getInstance().getLogger().debug("No properties found in Mojang response for " + uuidToString);
|
2020-12-04 21:55:24 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
profileProperties.add(new GameProfile.Property("textures", node.get("properties").get(0).get("value").asText()));
|
|
|
|
gameProfile.setProperties(profileProperties);
|
|
|
|
return gameProfile;
|
|
|
|
} catch (Exception e) {
|
2021-11-20 21:34:30 +00:00
|
|
|
if (GeyserImpl.getInstance().getConfig().isDebugMode()) {
|
2020-12-04 21:55:24 +00:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}, EXECUTOR_SERVICE);
|
|
|
|
}
|
|
|
|
|
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);
|
2020-07-11 17:22:02 +00:00
|
|
|
|
|
|
|
HttpURLConnection con = (HttpURLConnection) new URL(imageUrl).openConnection();
|
2021-11-20 21:34:30 +00:00
|
|
|
con.setRequestProperty("User-Agent", "Geyser-" + GeyserImpl.getInstance().getPlatformType().toString() + "/" + GeyserImpl.VERSION);
|
2020-07-11 17:22:02 +00:00
|
|
|
|
|
|
|
BufferedImage image = ImageIO.read(con.getInputStream());
|
2020-04-29 16:04:45 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-02-25 01:28:48 +00:00
|
|
|
public static BufferedImage scale(BufferedImage bufferedImage, int newWidth, int newHeight) {
|
2021-02-15 21:36:47 +00:00
|
|
|
BufferedImage resized = new BufferedImage(newWidth, newHeight, 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);
|
2021-02-15 21:36:47 +00:00
|
|
|
g2.drawImage(bufferedImage, 0, 0, newWidth, newHeight, null);
|
2019-12-15 02:12:12 +00:00
|
|
|
g2.dispose();
|
2021-02-15 21:36:47 +00:00
|
|
|
bufferedImage.flush();
|
2019-12-15 02:12:12 +00:00
|
|
|
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 {
|
2021-02-15 21:36:47 +00:00
|
|
|
private final Skin skin;
|
|
|
|
private final Cape cape;
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 19:49:55 +00:00
|
|
|
public record SkinData(Skin skin, Cape cape, SkinGeometry geometry) {
|
|
|
|
}
|
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
@AllArgsConstructor
|
|
|
|
@Getter
|
|
|
|
public static class Skin {
|
|
|
|
private UUID skinOwner;
|
2021-02-15 21:36:47 +00:00
|
|
|
private final String textureUrl;
|
|
|
|
private final byte[] skinData;
|
|
|
|
private final long requestedOn;
|
2019-09-25 21:52:28 +00:00
|
|
|
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 {
|
2021-02-15 21:36:47 +00:00
|
|
|
private final String textureUrl;
|
|
|
|
private final String capeId;
|
|
|
|
private final byte[] capeData;
|
|
|
|
private final long requestedOn;
|
|
|
|
private final boolean failed;
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 21:50:01 +00:00
|
|
|
@AllArgsConstructor
|
|
|
|
@Getter
|
|
|
|
public static class SkinGeometry {
|
2021-02-15 21:36:47 +00:00
|
|
|
private final String geometryName;
|
|
|
|
private final String geometryData;
|
|
|
|
private final boolean failed;
|
2020-05-23 21:06:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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,
|
2020-08-28 18:36:24 +00:00
|
|
|
OPTIFINE("https://optifine.net/capes/%s.png", CapeUrlType.USERNAME),
|
2020-11-14 17:52:10 +00:00
|
|
|
LABYMOD("https://dl.labymod.net/capes/%s", CapeUrlType.UUID_DASHED),
|
2020-04-29 16:04:45 +00:00
|
|
|
FIVEZIG("https://textures.5zigreborn.eu/profile/%s", CapeUrlType.UUID_DASHED),
|
2020-07-06 13:26:00 +00:00
|
|
|
MINECRAFTCAPES("https://minecraftcapes.net/profile/%s/cape", CapeUrlType.UUID);
|
2019-10-02 20:45:29 +00:00
|
|
|
|
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) {
|
2021-09-10 18:10:56 +00:00
|
|
|
return switch (type) {
|
|
|
|
case UUID -> uuid.toString().replace("-", "");
|
|
|
|
case UUID_DASHED -> uuid.toString();
|
|
|
|
default -> username;
|
|
|
|
};
|
2019-10-02 20:45:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-07-06 13:26:00 +00:00
|
|
|
MINECRAFTCAPES("https://minecraftcapes.net/profile/%s/ears", CapeUrlType.UUID);
|
2020-05-23 21:06:34 +00:00
|
|
|
|
|
|
|
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) {
|
2021-09-10 18:10:56 +00:00
|
|
|
return switch (type) {
|
|
|
|
case UUID -> uuid.toString().replace("-", "");
|
|
|
|
case UUID_DASHED -> uuid.toString();
|
|
|
|
default -> username;
|
|
|
|
};
|
2020-05-23 21:06:34 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|