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-04-29 16:04:45 +00:00
|
|
|
import java.io.ByteArrayInputStream;
|
2019-09-25 21:52:28 +00:00
|
|
|
import java.io.ByteArrayOutputStream;
|
2020-04-29 16:04:45 +00:00
|
|
|
import java.io.IOException;
|
2019-09-25 21:52:28 +00:00
|
|
|
import java.net.URL;
|
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);
|
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-06 21:50:01 +00:00
|
|
|
public static final SkinGeometry EMPTY_GEOMETRY = SkinProvider.SkinGeometry.getLegacy("geometry.humanoid");
|
|
|
|
private static Map<UUID, SkinGeometry> cachedGeometry = new ConcurrentHashMap<>();
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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-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) {
|
|
|
|
Skin skin = new Skin(playerID, skinID, skinData, System.currentTimeMillis(), true);
|
|
|
|
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) {
|
|
|
|
SkinGeometry geometry = new SkinGeometry(new String(geometryName), new String(geometryData));
|
|
|
|
cachedGeometry.put(playerID, geometry);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
return new Skin(uuid, textureUrl, skin, System.currentTimeMillis(), false);
|
|
|
|
}
|
|
|
|
|
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-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);
|
|
|
|
}
|
2019-12-15 02:12:12 +00:00
|
|
|
BufferedImage newImage = new BufferedImage(64, 32, BufferedImage.TYPE_INT_RGB);
|
|
|
|
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
|
|
|
|
2019-12-21 17:35:48 +00:00
|
|
|
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(image.getWidth() * 4 + image.getHeight() * 4)) {
|
2019-09-25 21:52:28 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
image.flush();
|
|
|
|
return outputStream.toByteArray();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2019-12-15 02:12:12 +00:00
|
|
|
BufferedImage resized = new BufferedImage(bufferedImage.getWidth() / 2, bufferedImage.getHeight() / 2, BufferedImage.TYPE_INT_RGB);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
public static SkinGeometry getLegacy(String name) {
|
|
|
|
return new SkinProvider.SkinGeometry("{\"geometry\" :{\"default\" :\"" + name + "\"}}", "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|