Handle the futures stuff in one place

This commit is contained in:
Sollace 2018-09-06 14:12:28 +02:00
parent dec6ad249f
commit 10eda59bed
5 changed files with 57 additions and 61 deletions

View file

@ -4,10 +4,9 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.gson.annotations.Expose;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.exceptions.AuthenticationException;
import com.mojang.authlib.yggdrasil.response.MinecraftTexturesPayload;
import com.mojang.util.UUIDTypeAdapter;
import com.voxelmodpack.hdskins.HDSkinManager;
import com.voxelmodpack.hdskins.util.CallableFutures;
import com.voxelmodpack.hdskins.util.IndentedToStringStyle;
import com.voxelmodpack.hdskins.util.MoreHttpResponses;
import com.voxelmodpack.hdskins.util.NetClient;
@ -16,7 +15,6 @@ import net.minecraft.util.Session;
import java.io.IOException;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
@ServerType("bethlehem")
public class BethlehemSkinServer implements SkinServer {
@ -42,26 +40,23 @@ public class BethlehemSkinServer implements SkinServer {
}
@Override
public CompletableFuture<SkinUploadResponse> uploadSkin(Session session, SkinUpload upload) {
return CallableFutures.asyncFailableFuture(() -> {
SkinServer.verifyServerConnection(session, SERVER_ID);
public SkinUploadResponse performSkinUpload(Session session, SkinUpload upload) throws IOException, AuthenticationException {
SkinServer.verifyServerConnection(session, SERVER_ID);
NetClient client = new NetClient("POST", address);
NetClient client = new NetClient("POST", address);
client.putHeaders(createHeaders(session, upload));
client.putHeaders(createHeaders(session, upload));
if (upload.getImage() != null) {
client.putFile(upload.getType().toString().toLowerCase(Locale.US), "image/png", upload.getImage());
if (upload.getImage() != null) {
client.putFile(upload.getType().toString().toLowerCase(Locale.US), "image/png", upload.getImage());
}
try (MoreHttpResponses response = client.send()) {
if (!response.ok()) {
throw new IOException(response.text());
}
try (MoreHttpResponses response = client.send()) {
if (!response.ok()) {
throw new IOException(response.text());
}
return new SkinUploadResponse(response.text());
}
}, HDSkinManager.skinUploadExecutor);
return new SkinUploadResponse(response.text());
}
}
protected Map<String, ?> createHeaders(Session session, SkinUpload upload) {

View file

@ -5,6 +5,7 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.gson.annotations.Expose;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.exceptions.AuthenticationException;
import com.mojang.authlib.minecraft.MinecraftProfileTexture;
import com.mojang.authlib.yggdrasil.response.MinecraftTexturesPayload;
import com.mojang.util.UUIDTypeAdapter;
@ -99,7 +100,6 @@ public class LegacySkinServer implements SkinServer {
// Add the ETag onto the end of the texture hash. Should properly cache the textures.
return new MinecraftProfileTexture(url, null) {
@Override
public String getHash() {
return super.getHash() + eTag;
@ -109,33 +109,32 @@ public class LegacySkinServer implements SkinServer {
}
@Override
public CompletableFuture<SkinUploadResponse> uploadSkin(Session session, SkinUpload upload) {
public SkinUploadResponse performSkinUpload(Session session, SkinUpload upload) throws IOException, AuthenticationException {
if (Strings.isNullOrEmpty(gateway)) {
return CallableFutures.failedFuture(gatewayUnsupported());
throw gatewayUnsupported();
}
return CallableFutures.asyncFailableFuture(() -> {
SkinServer.verifyServerConnection(session, SERVER_ID);
SkinServer.verifyServerConnection(session, SERVER_ID);
NetClient client = new NetClient("POST", gateway);
NetClient client = new NetClient("POST", gateway);
client.putFormData(createHeaders(session, upload), "image/png");
client.putFormData(createHeaders(session, upload), "image/png");
if (upload.getImage() != null) {
client.putFile(upload.getType().toString().toLowerCase(Locale.US), "image/png", upload.getImage());
}
if (upload.getImage() != null) {
client.putFile(upload.getType().toString().toLowerCase(Locale.US), "image/png", upload.getImage());
}
String response = client.send().text();
String response = client.send().text();
if (response.startsWith("ERROR: ")) {
response = response.substring(7);
}
if (!response.equalsIgnoreCase("OK") && !response.endsWith("OK")) {
throw new IOException(response);
}
return new SkinUploadResponse(response);
if (response.startsWith("ERROR: ")) {
response = response.substring(7);
}
}, HDSkinManager.skinUploadExecutor);
if (!response.equalsIgnoreCase("OK") && !response.endsWith("OK")) {
throw new IOException(response);
}
return new SkinUploadResponse(response);
}
private UnsupportedOperationException gatewayUnsupported() {

View file

@ -32,7 +32,11 @@ public interface SkinServer extends Exposable {
MinecraftTexturesPayload loadProfileData(GameProfile profile) throws IOException;
CompletableFuture<SkinUploadResponse> uploadSkin(Session session, SkinUpload upload);
SkinUploadResponse performSkinUpload(Session session, SkinUpload upload) throws IOException, AuthenticationException;
default CompletableFuture<SkinUploadResponse> uploadSkin(Session session, SkinUpload upload) {
return CallableFutures.asyncFailableFuture(() -> performSkinUpload(session, upload), HDSkinManager.skinUploadExecutor);
}
default CompletableFuture<MinecraftTexturesPayload> getPreviewTextures(GameProfile profile) {
return CallableFutures.asyncFailableFuture(() -> loadProfileData(profile), HDSkinManager.skinDownloadExecutor);

View file

@ -31,4 +31,8 @@ public class SkinUpload {
public MinecraftProfileTexture.Type getType() {
return type;
}
public String getSchemaAction() {
return image == null ? "delete" : image.getScheme();
}
}

View file

@ -9,7 +9,6 @@ import com.mojang.authlib.minecraft.MinecraftProfileTexture;
import com.mojang.authlib.yggdrasil.response.MinecraftTexturesPayload;
import com.mojang.util.UUIDTypeAdapter;
import com.voxelmodpack.hdskins.HDSkinManager;
import com.voxelmodpack.hdskins.util.CallableFutures;
import com.voxelmodpack.hdskins.util.IndentedToStringStyle;
import com.voxelmodpack.hdskins.util.MoreHttpResponses;
import net.minecraft.client.Minecraft;
@ -29,7 +28,6 @@ import java.net.URI;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import javax.annotation.Nullable;
@ -58,30 +56,26 @@ public class ValhallaSkinServer implements SkinServer {
}
@Override
public CompletableFuture<SkinUploadResponse> uploadSkin(Session session, SkinUpload skin) {
URI image = skin.getImage();
Map<String, String> metadata = skin.getMetadata();
MinecraftProfileTexture.Type type = skin.getType();
public SkinUploadResponse performSkinUpload(Session session, SkinUpload upload) throws IOException, AuthenticationException {
URI image = upload.getImage();
Map<String, String> metadata = upload.getMetadata();
MinecraftProfileTexture.Type type = upload.getType();
return CallableFutures.asyncFailableFuture(() -> {
authorize(session);
try {
return upload(session, image, type, metadata);
} catch (IOException e) {
if (e.getMessage().equals("Authorization failed")) {
accessToken = null;
authorize(session);
try {
return upload(session, image, type, metadata);
} catch (IOException e) {
if (e.getMessage().equals("Authorization failed")) {
accessToken = null;
authorize(session);
return upload(session, image, type, metadata);
}
throw e;
}
}, HDSkinManager.skinUploadExecutor);
return upload(session, image, type, metadata);
}
throw e;
}
}
private SkinUploadResponse upload(Session session, @Nullable URI image,
MinecraftProfileTexture.Type type, Map<String, String> metadata)
throws IOException {
private SkinUploadResponse upload(Session session, @Nullable URI image, MinecraftProfileTexture.Type type, Map<String, String> metadata) throws IOException {
GameProfile profile = session.getProfile();
if (image == null) {