Added skin variant option when using url
This commit is contained in:
parent
f7a7b96648
commit
6e1d089cc7
5 changed files with 23 additions and 12 deletions
|
@ -1,6 +1,5 @@
|
|||
package lol.pyr.znpcsplus.api.skin;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
public interface SkinDescriptorFactory {
|
||||
|
@ -8,6 +7,6 @@ public interface SkinDescriptorFactory {
|
|||
SkinDescriptor createRefreshingDescriptor(String playerName);
|
||||
SkinDescriptor createStaticDescriptor(String playerName);
|
||||
SkinDescriptor createStaticDescriptor(String texture, String signature);
|
||||
SkinDescriptor createUrlDescriptor(String url) throws MalformedURLException;
|
||||
SkinDescriptor createUrlDescriptor(URL url);
|
||||
SkinDescriptor createUrlDescriptor(String url, String variant);
|
||||
SkinDescriptor createUrlDescriptor(URL url, String variant);
|
||||
}
|
||||
|
|
|
@ -68,11 +68,16 @@ public class SkinCommand implements CommandHandler {
|
|||
context.halt(Component.text("The NPC's skin will now be resolved per-player from \"" + name + "\""));
|
||||
} else if (type.equalsIgnoreCase("url")) {
|
||||
context.ensureArgsNotEmpty();
|
||||
String variant = context.popString().toLowerCase();
|
||||
if (!variant.equalsIgnoreCase("slim") && !variant.equalsIgnoreCase("classic")) {
|
||||
context.send(Component.text("Invalid skin variant! Please use one of the following: slim, classic", NamedTextColor.RED));
|
||||
return;
|
||||
}
|
||||
String urlString = context.dumpAllArgs();
|
||||
try {
|
||||
URL url = new URL(urlString);
|
||||
context.send(Component.text("Fetching skin from url \"" + urlString + "\"...", NamedTextColor.GREEN));
|
||||
PrefetchedDescriptor.fromUrl(skinCache, url).thenAccept(skin -> {
|
||||
PrefetchedDescriptor.fromUrl(skinCache, url , variant).thenAccept(skin -> {
|
||||
if (skin.getSkin() == null) {
|
||||
context.send(Component.text("Failed to fetch skin, are you sure the url is valid?", NamedTextColor.RED));
|
||||
return;
|
||||
|
@ -94,6 +99,9 @@ public class SkinCommand implements CommandHandler {
|
|||
if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds());
|
||||
if (context.argSize() == 2) return context.suggestLiteral("mirror", "static", "dynamic", "url");
|
||||
if (context.matchSuggestion("*", "static")) return context.suggestPlayers();
|
||||
if (context.argSize() == 3 && context.matchSuggestion("*", "url")) {
|
||||
return context.suggestLiteral("slim", "classic");
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,12 +40,16 @@ public class SkinDescriptorFactoryImpl implements SkinDescriptorFactory {
|
|||
}
|
||||
|
||||
@Override
|
||||
public SkinDescriptor createUrlDescriptor(String url) throws MalformedURLException {
|
||||
return createUrlDescriptor(new URL(url));
|
||||
public SkinDescriptor createUrlDescriptor(String url, String variant) {
|
||||
try {
|
||||
return createUrlDescriptor(new URL(url), variant);
|
||||
} catch (MalformedURLException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public SkinDescriptor createUrlDescriptor(URL url) {
|
||||
return PrefetchedDescriptor.fromUrl(skinCache, url).join();
|
||||
public SkinDescriptor createUrlDescriptor(URL url, String variant) {
|
||||
return PrefetchedDescriptor.fromUrl(skinCache, url, variant).join();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -67,7 +67,7 @@ public class MojangSkinCache {
|
|||
});
|
||||
}
|
||||
|
||||
public CompletableFuture<Skin> fetchByUrl(URL url) {
|
||||
public CompletableFuture<Skin> fetchByUrl(URL url, String variant) {
|
||||
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
URL apiUrl = parseUrl("https://api.mineskin.org/generate/url");
|
||||
|
@ -80,7 +80,7 @@ public class MojangSkinCache {
|
|||
connection.setDoOutput(true);
|
||||
OutputStream outStream = connection.getOutputStream();
|
||||
DataOutputStream out = new DataOutputStream(outStream);
|
||||
out.writeBytes("{\"variant\":\"classic\",\"url\":\"" + url.toString() + "\"}"); // TODO: configurable variant (slim, classic) default: classic
|
||||
out.writeBytes("{\"variant\":\"" + variant + "\",\"url\":\"" + url.toString() + "\"}"); // TODO: configurable variant (slim, classic) default: classic
|
||||
out.flush();
|
||||
out.close();
|
||||
outStream.close();
|
||||
|
|
|
@ -21,8 +21,8 @@ public class PrefetchedDescriptor implements BaseSkinDescriptor, SkinDescriptor
|
|||
return CompletableFuture.supplyAsync(() -> new PrefetchedDescriptor(cache.fetchByName(name).join()));
|
||||
}
|
||||
|
||||
public static CompletableFuture<PrefetchedDescriptor> fromUrl(MojangSkinCache cache, URL url) {
|
||||
return CompletableFuture.supplyAsync(() -> new PrefetchedDescriptor(cache.fetchByUrl(url).join()));
|
||||
public static CompletableFuture<PrefetchedDescriptor> fromUrl(MojangSkinCache cache, URL url, String variant) {
|
||||
return CompletableFuture.supplyAsync(() -> new PrefetchedDescriptor(cache.fetchByUrl(url, variant).join()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in a new issue