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;
|
package lol.pyr.znpcsplus.api.skin;
|
||||||
|
|
||||||
import java.net.MalformedURLException;
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
||||||
public interface SkinDescriptorFactory {
|
public interface SkinDescriptorFactory {
|
||||||
|
@ -8,6 +7,6 @@ public interface SkinDescriptorFactory {
|
||||||
SkinDescriptor createRefreshingDescriptor(String playerName);
|
SkinDescriptor createRefreshingDescriptor(String playerName);
|
||||||
SkinDescriptor createStaticDescriptor(String playerName);
|
SkinDescriptor createStaticDescriptor(String playerName);
|
||||||
SkinDescriptor createStaticDescriptor(String texture, String signature);
|
SkinDescriptor createStaticDescriptor(String texture, String signature);
|
||||||
SkinDescriptor createUrlDescriptor(String url) throws MalformedURLException;
|
SkinDescriptor createUrlDescriptor(String url, String variant);
|
||||||
SkinDescriptor createUrlDescriptor(URL url);
|
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 + "\""));
|
context.halt(Component.text("The NPC's skin will now be resolved per-player from \"" + name + "\""));
|
||||||
} else if (type.equalsIgnoreCase("url")) {
|
} else if (type.equalsIgnoreCase("url")) {
|
||||||
context.ensureArgsNotEmpty();
|
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();
|
String urlString = context.dumpAllArgs();
|
||||||
try {
|
try {
|
||||||
URL url = new URL(urlString);
|
URL url = new URL(urlString);
|
||||||
context.send(Component.text("Fetching skin from url \"" + urlString + "\"...", NamedTextColor.GREEN));
|
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) {
|
if (skin.getSkin() == null) {
|
||||||
context.send(Component.text("Failed to fetch skin, are you sure the url is valid?", NamedTextColor.RED));
|
context.send(Component.text("Failed to fetch skin, are you sure the url is valid?", NamedTextColor.RED));
|
||||||
return;
|
return;
|
||||||
|
@ -94,6 +99,9 @@ public class SkinCommand implements CommandHandler {
|
||||||
if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds());
|
if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds());
|
||||||
if (context.argSize() == 2) return context.suggestLiteral("mirror", "static", "dynamic", "url");
|
if (context.argSize() == 2) return context.suggestLiteral("mirror", "static", "dynamic", "url");
|
||||||
if (context.matchSuggestion("*", "static")) return context.suggestPlayers();
|
if (context.matchSuggestion("*", "static")) return context.suggestPlayers();
|
||||||
|
if (context.argSize() == 3 && context.matchSuggestion("*", "url")) {
|
||||||
|
return context.suggestLiteral("slim", "classic");
|
||||||
|
}
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,12 +40,16 @@ public class SkinDescriptorFactoryImpl implements SkinDescriptorFactory {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SkinDescriptor createUrlDescriptor(String url) throws MalformedURLException {
|
public SkinDescriptor createUrlDescriptor(String url, String variant) {
|
||||||
return createUrlDescriptor(new URL(url));
|
try {
|
||||||
|
return createUrlDescriptor(new URL(url), variant);
|
||||||
|
} catch (MalformedURLException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SkinDescriptor createUrlDescriptor(URL url) {
|
public SkinDescriptor createUrlDescriptor(URL url, String variant) {
|
||||||
return PrefetchedDescriptor.fromUrl(skinCache, url).join();
|
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(() -> {
|
return CompletableFuture.supplyAsync(() -> {
|
||||||
URL apiUrl = parseUrl("https://api.mineskin.org/generate/url");
|
URL apiUrl = parseUrl("https://api.mineskin.org/generate/url");
|
||||||
|
@ -80,7 +80,7 @@ public class MojangSkinCache {
|
||||||
connection.setDoOutput(true);
|
connection.setDoOutput(true);
|
||||||
OutputStream outStream = connection.getOutputStream();
|
OutputStream outStream = connection.getOutputStream();
|
||||||
DataOutputStream out = new DataOutputStream(outStream);
|
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.flush();
|
||||||
out.close();
|
out.close();
|
||||||
outStream.close();
|
outStream.close();
|
||||||
|
|
|
@ -21,8 +21,8 @@ public class PrefetchedDescriptor implements BaseSkinDescriptor, SkinDescriptor
|
||||||
return CompletableFuture.supplyAsync(() -> new PrefetchedDescriptor(cache.fetchByName(name).join()));
|
return CompletableFuture.supplyAsync(() -> new PrefetchedDescriptor(cache.fetchByName(name).join()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static CompletableFuture<PrefetchedDescriptor> fromUrl(MojangSkinCache cache, URL url) {
|
public static CompletableFuture<PrefetchedDescriptor> fromUrl(MojangSkinCache cache, URL url, String variant) {
|
||||||
return CompletableFuture.supplyAsync(() -> new PrefetchedDescriptor(cache.fetchByUrl(url).join()));
|
return CompletableFuture.supplyAsync(() -> new PrefetchedDescriptor(cache.fetchByUrl(url, variant).join()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in a new issue