From 3ac0bf2febaa97554564f74f28750fac92c274e5 Mon Sep 17 00:00:00 2001 From: Kyle <48519776+xKylee@users.noreply.github.com> Date: Tue, 27 Aug 2019 15:08:50 +0100 Subject: [PATCH] Update ScreenshotPlugin.java --- .../plugins/screenshot/ScreenshotPlugin.java | 71 ++++++++++--------- 1 file changed, 37 insertions(+), 34 deletions(-) diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/screenshot/ScreenshotPlugin.java b/runelite-client/src/main/java/net/runelite/client/plugins/screenshot/ScreenshotPlugin.java index fe2d419090..a20f6f9151 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/screenshot/ScreenshotPlugin.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/screenshot/ScreenshotPlugin.java @@ -32,7 +32,10 @@ import com.google.inject.Provides; import java.awt.Desktop; import java.awt.Graphics; import java.awt.Image; +import java.awt.Toolkit; import java.awt.TrayIcon; +import java.awt.datatransfer.Clipboard; +import java.awt.datatransfer.StringSelection; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; @@ -98,7 +101,6 @@ import net.runelite.client.ui.ClientUI; import net.runelite.client.ui.DrawManager; import net.runelite.client.ui.NavigationButton; import net.runelite.client.ui.overlay.OverlayManager; -import net.runelite.client.util.Clipboard; import net.runelite.client.util.HotkeyListener; import net.runelite.client.util.ImageUtil; import net.runelite.api.util.Text; @@ -781,6 +783,13 @@ public class ScreenshotPlugin extends Plugin } } + /** + * Uploads a screenshot to the Imgur image-hosting service, + * and copies the image link to the clipboard. + * + * @param screenshotFile Image file to upload. + * @throws IOException Thrown if the file cannot be read. + */ /** * Uploads a screenshot to the Imgur image-hosting service, * and copies the image link to the clipboard. @@ -792,50 +801,44 @@ public class ScreenshotPlugin extends Plugin { String json = RuneLiteAPI.GSON.toJson(new ImageUploadRequest(screenshotFile)); - Request request = null; - if (IMGUR_IMAGE_UPLOAD_URL != null) - { - RequestBody body = RequestBody.Companion.create(json, JSON); - request = new Request.Builder() - .url(IMGUR_IMAGE_UPLOAD_URL) - .addHeader("Authorization", "Client-ID " + IMGUR_CLIENT_ID) - .post(body) - .build(); - } + Request request = new Request.Builder() + .url(IMGUR_IMAGE_UPLOAD_URL) + .addHeader("Authorization", "Client-ID " + IMGUR_CLIENT_ID) + .post(RequestBody.create(JSON, json)) + .build(); - if (request != null) + RuneLiteAPI.CLIENT.newCall(request).enqueue(new Callback() { - RuneLiteAPI.CLIENT.newCall(request).enqueue(new Callback() + @Override + public void onFailure(Call call, IOException ex) { - @Override - public void onFailure(Call call, IOException ex) - { - log.warn("error uploading screenshot", ex); - } + log.warn("error uploading screenshot", ex); + } - @Override - public void onResponse(Call call, Response response) throws IOException + @Override + public void onResponse(Call call, Response response) throws IOException + { + try (InputStream in = response.body().byteStream()) { - try (InputStream in = response.body().byteStream()) + ImageUploadResponse imageUploadResponse = RuneLiteAPI.GSON + .fromJson(new InputStreamReader(in), ImageUploadResponse.class); + + if (imageUploadResponse.isSuccess()) { - ImageUploadResponse imageUploadResponse = RuneLiteAPI.GSON - .fromJson(new InputStreamReader(in), ImageUploadResponse.class); + String link = imageUploadResponse.getData().getLink(); - if (imageUploadResponse.isSuccess()) + StringSelection selection = new StringSelection(link); + Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); + clipboard.setContents(selection, selection); + + if (notifyWhenTaken) { - String link = imageUploadResponse.getData().getLink(); - - Clipboard.store(link); - - if (notifyWhenTaken) - { - notifier.notify("A screenshot was uploaded and inserted into your clipboard!", TrayIcon.MessageType.INFO); - } + notifier.notify("A screenshot was uploaded and inserted into your clipboard!", TrayIcon.MessageType.INFO); } } } - }); - } + } + }); } @VisibleForTesting