Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ public static class SpriteUploaders {

private static final ResourceLocation TEXTURE = ResourceLocation.create("screenshotuploader", "themes/vanilla/textures/settings.png");

public static Icon IMGUR = Icon.sprite32(TEXTURE, 0, 1);
public static Icon XBACKBONE = Icon.sprite32(TEXTURE, 1, 1);
public static Icon ZIPLINE = Icon.sprite32(TEXTURE, 2, 1);
public static Icon CRAFTSHOT = Icon.sprite32(TEXTURE, 0, 1);
public static Icon IMGUR = Icon.sprite32(TEXTURE, 1, 1);
public static Icon XBACKBONE = Icon.sprite32(TEXTURE, 2, 1);
public static Icon ZIPLINE = Icon.sprite32(TEXTURE, 3, 1);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.rappytv.screenshotuploader.core.command.UploadCommand;
import com.rappytv.screenshotuploader.core.config.UploaderConfig;
import com.rappytv.screenshotuploader.core.listener.ScreenshotListener;
import com.rappytv.screenshotuploader.core.uploader.uploaders.CraftShotUploader;
import com.rappytv.screenshotuploader.core.uploader.uploaders.ImgurUploader;
import com.rappytv.screenshotuploader.core.uploader.uploaders.XBackBoneUploader;
import com.rappytv.screenshotuploader.core.uploader.uploaders.ZiplineUploader;
Expand Down Expand Up @@ -40,6 +41,7 @@ protected void enable() {
this.registerSettingCategory();
this.registerCommand(new UploadCommand(this));
this.registerListener(new ScreenshotListener());
uploaderRegistry().registerUploader(new CraftShotUploader());
uploaderRegistry().registerUploader(new ImgurUploader(this));
uploaderRegistry().registerUploader(new XBackBoneUploader(this));
uploaderRegistry().registerUploader(new ZiplineUploader(this));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ public class UploaderConfig extends AddonConfig {

@SettingSection("uploaders")
@IntroducedIn(namespace = "screenshotuploader", value = "1.0.4")
@SpriteSlot(size = 32, y = 1)
@SpriteSlot(size = 32, x = 1, y = 1)
private final ImgurConfig imgur = new ImgurConfig();

@SpriteSlot(size = 32, x = 1, y = 1)
@SpriteSlot(size = 32, x = 2, y = 1)
private final XBackBoneConfig xbackbone = new XBackBoneConfig();

@SpriteSlot(size = 32, x = 2, y = 1)
@SpriteSlot(size = 32, x = 3, y = 1)
private final ZiplineConfig zipline = new ZiplineConfig();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class XBackBoneConfig extends Config {
public ConfigProperty<String> base() {
return this.base;
}

public ConfigProperty<String> auth() {
return this.auth;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package com.rappytv.screenshotuploader.core.uploader.uploaders;

import com.google.gson.JsonObject;
import com.rappytv.screenshotuploader.api.ScreenshotUploaderTextures.SpriteUploaders;
import com.rappytv.screenshotuploader.api.UploadException;
import com.rappytv.screenshotuploader.api.Uploader;
import com.rappytv.screenshotuploader.api.Uploader.EmptyConfig;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import net.labymod.api.Laby;
import net.labymod.api.client.gui.icon.Icon;
import net.labymod.api.client.network.server.ServerData;
import net.labymod.api.labyconnect.LabyConnectSession;
import net.labymod.api.labyconnect.TokenStorage.Purpose;
import net.labymod.api.labyconnect.TokenStorage.Token;
import net.labymod.api.util.io.web.request.FormData;
import net.labymod.api.util.io.web.request.Request;
import net.labymod.api.util.io.web.request.Request.Method;
import net.labymod.api.util.io.web.request.Response;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class CraftShotUploader extends Uploader<EmptyConfig> {

private static final String UPLOAD_ENDPOINT = "https://craftshot.net/v1/upload";

public CraftShotUploader() {
super("craftshot", "CraftShot");
}

@Override
public Icon getIcon() { // TODO: Add icon
return SpriteUploaders.CRAFTSHOT;
}

@Override
public @NotNull EmptyConfig getConfig() {
return EMPTY_CONFIG;
}

@Override
public boolean validateConfig() {
return true;
}

@Override
public String uploadScreenshot(File file) throws UploadException {
String token = this.getLabyConnectToken();
if (token == null) {
throw new UploadException("You're not connected to LabyConnect!", this);
}

List<FormData> formData = new ArrayList<>();
formData.add(FormData.builder().name("access_token").value(token).build());

String host = this.getHost();
if (host != null) {
formData.add(FormData.builder().name("server_ip").value(host).build());
}

try {
formData.add(
FormData.builder()
.name("screenshot")
.fileName("screenshot.png")
.contentType("image/png")
.value(file.toPath())
.build()
);
} catch (IOException e) {
throw new UploadException(e, this);
}

Response<JsonObject> response = Request.ofGson(JsonObject.class)
.url(UPLOAD_ENDPOINT)
.method(Method.POST)
.form(formData)
.handleErrorStream()
.executeSync();

if(response.hasException()) {
throw new UploadException(response.exception(), this);
}

try {
int statusCode = response.getStatusCode();
JsonObject data = response.get().get("data").getAsJsonObject();

if(statusCode != 200) {
if(data != null && data.has("message")) {
throw new UploadException(data.get("message").getAsString(), this);
}

throw new UploadException("Failed to upload file with status " + statusCode, this);
}

if(data != null && data.has("url")) {
return data.get("url").getAsString();
}

throw new UploadException("Response body does not contain the file link", this);
} catch (IllegalArgumentException e) {
throw new UploadException(e, this);
}
}

@Nullable
private String getLabyConnectToken() {
LabyConnectSession session = Laby.labyAPI().labyConnect().getSession();
if (session == null) {
return null;
}

Token token = session.tokenStorage().getToken(Purpose.JWT, session.self().getUniqueId());

if (token == null || token.isExpired()) {
return null;
}

return token.getToken();
}

@Nullable
private String getHost() {
ServerData server = Laby.labyAPI().serverController().getCurrentServerData();
if (server == null || server.address().getHost().isEmpty()) {
return null;
}
return server.address().getHost();
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.