From b201b097197bfcc31d7332cec48f368acc8718b7 Mon Sep 17 00:00:00 2001 From: Jeremy Wood Date: Thu, 31 Jan 2019 22:02:13 -0500 Subject: [PATCH] Added a theoretically working Hastebin option for mvv output. --- .../commands/VersionCommand.java | 5 +- .../utils/webpaste/HastebinPasteService.java | 81 +++++++++++++++++++ .../utils/webpaste/PasteServiceFactory.java | 2 + .../utils/webpaste/PasteServiceType.java | 4 + 4 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/HastebinPasteService.java diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/VersionCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/VersionCommand.java index 3d1e3f7e..e3f16742 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/VersionCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/VersionCommand.java @@ -36,7 +36,7 @@ public class VersionCommand extends MultiverseCommand { public VersionCommand(MultiverseCore plugin) { super(plugin); this.setName("Multiverse Version"); - this.setCommandUsage("/mv version " + ChatColor.GOLD + "-[pbg]"); + this.setCommandUsage("/mv version " + ChatColor.GOLD + "-[bh]"); this.setArgRange(0, 1); this.addKey("mv version"); this.addKey("mvv"); @@ -163,6 +163,9 @@ public class VersionCommand extends MultiverseCommand { if (args.get(0).equalsIgnoreCase("-b")) { // private post to pastebin pasteUrl = postToService(PasteServiceType.PASTEBIN, true, data, files); + } else if (args.get(0).equalsIgnoreCase("-h")) { + // private post to pastebin + pasteUrl = postToService(PasteServiceType.HASTEBIN, true, data, files); } else { return; } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/HastebinPasteService.java b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/HastebinPasteService.java new file mode 100644 index 00000000..69438cfd --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/HastebinPasteService.java @@ -0,0 +1,81 @@ +package com.onarandombox.MultiverseCore.utils.webpaste; + +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.Map; + +/** + * Pastes to {@code hastebin.com}. + */ +public class HastebinPasteService implements PasteService { + + @Override + public String encodeData(String data) { + return data; + } + + @Override + public String encodeData(Map data) { + throw new UnsupportedOperationException(); + } + + @Override + public URL getPostURL() { + try { + return new URL("https://hastebin.com/documents"); + } catch (MalformedURLException e) { + return null; // should never hit here + } + } + + @Override + public String postData(String encodedData, URL url) throws PasteFailedException { + OutputStreamWriter wr = null; + BufferedReader rd = null; + try { + URLConnection conn = url.openConnection(); + conn.setDoOutput(true); + + wr = new OutputStreamWriter(conn.getOutputStream()); + rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); + + wr.write(encodedData); + wr.flush(); + + String line; + StringBuilder responseString = new StringBuilder(); + while ((line = rd.readLine()) != null) { + responseString.append(line); + } + String key = new JsonParser().parse(responseString.toString()).getAsJsonObject().get("key").getAsString(); + + return "https://hastebin.com/" + key; + } catch (Exception e) { + throw new PasteFailedException(e); + } finally { + if (wr != null) { + try { + wr.close(); + } catch (IOException ignore) { } + } + if (rd != null) { + try { + rd.close(); + } catch (IOException ignore) { } + } + } + } + + @Override + public boolean supportsMultiFile() { + return false; + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PasteServiceFactory.java b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PasteServiceFactory.java index 4ef179be..df4fb831 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PasteServiceFactory.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PasteServiceFactory.java @@ -16,6 +16,8 @@ public class PasteServiceFactory { switch(type) { case PASTEBIN: return new PastebinPasteService(isPrivate); + case HASTEBIN: + return new HastebinPasteService(); case GITHUB: return new GithubPasteService(isPrivate); default: diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PasteServiceType.java b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PasteServiceType.java index ccdd5feb..0bd93e63 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PasteServiceType.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PasteServiceType.java @@ -11,6 +11,10 @@ public enum PasteServiceType { * @see PastebinPasteService */ PASTEBIN, + /** + * @see HastebinPasteService + */ + HASTEBIN, /** * @see GithubPasteService */