Skip to content

Commit 3331ea4

Browse files
committed
Fix file paste utility
1 parent 9525f48 commit 3331ea4

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

balancer/src/main/java/com/jaimemartz/playerbalancer/helper/PasteHelper.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public URL paste(PlayerBalancer plugin) throws Exception {
3737
try (FileInputStream stream = new FileInputStream(file)) {
3838
try (InputStreamReader reader = new InputStreamReader(stream, "UTF-8")) {
3939
String content = CharStreams.toString(reader);
40-
HastebinPaste paste = new HastebinPaste("https://file.properties/paste/", content);
40+
HastebinPaste paste = new HastebinPaste(HASTEBIN_HOST, content);
4141
return paste.paste();
4242
}
4343
}
@@ -62,7 +62,7 @@ public URL paste(PlayerBalancer plugin) throws Exception {
6262
try (InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
6363
String content = CharStreams.toString(reader);
6464
content = content.replaceAll("[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}", "?.?.?.?");
65-
HastebinPaste paste = new HastebinPaste("https://file.properties/paste/", content);
65+
HastebinPaste paste = new HastebinPaste(HASTEBIN_HOST, content);
6666
return paste.paste();
6767
}
6868
}
@@ -86,14 +86,16 @@ public URL paste(PlayerBalancer plugin) throws Exception {
8686
try (FileInputStream stream = new FileInputStream(file)) {
8787
try (InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
8888
String content = CharStreams.toString(reader);
89-
HastebinPaste paste = new HastebinPaste("https://file.properties/paste/", content);
89+
HastebinPaste paste = new HastebinPaste(HASTEBIN_HOST, content);
9090
return paste.paste();
9191
}
9292
}
9393
}
9494
};
9595

96-
private URL url;
96+
private static final String HASTEBIN_HOST = "https://hastebin.com/";
97+
98+
private URL lastPasteUrl;
9799

98100
private final BiConsumer<CommandSender, URL> consumer;
99101
private final boolean cache;
@@ -104,9 +106,9 @@ public URL paste(PlayerBalancer plugin) throws Exception {
104106
}
105107

106108
public void send(PlayerBalancer plugin, CommandSender sender) {
107-
if (url == null || !cache) {
109+
if (lastPasteUrl == null || !cache) {
108110
try {
109-
url = paste(plugin);
111+
lastPasteUrl = paste(plugin);
110112
} catch (PasteException e) {
111113
sender.sendMessage(new ComponentBuilder("An exception occurred while trying to send the paste: " + e.getMessage())
112114
.color(ChatColor.RED)
@@ -127,8 +129,8 @@ public void send(PlayerBalancer plugin, CommandSender sender) {
127129
);
128130
}
129131

130-
if (url != null) {
131-
consumer.accept(sender, url);
132+
if (lastPasteUrl != null) {
133+
consumer.accept(sender, lastPasteUrl);
132134
} else {
133135
sender.sendMessage(new ComponentBuilder("Could not create the paste, try again...")
134136
.color(ChatColor.RED)
@@ -137,15 +139,15 @@ public void send(PlayerBalancer plugin, CommandSender sender) {
137139
}
138140
}
139141

140-
public URL getURL() {
141-
return url;
142+
public URL getLastPasteURL() {
143+
return lastPasteUrl;
142144
}
143145

144146
public abstract URL paste(PlayerBalancer plugin) throws Exception;
145147

146148
public static void reset() {
147149
for (PasteHelper helper : values()) {
148-
helper.url = null;
150+
helper.lastPasteUrl = null;
149151
}
150152
}
151153
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import com.jaimemartz.playerbalancer.utils.HastebinPaste;
22
import org.junit.Test;
33

4+
import java.net.URL;
5+
import static org.junit.Assert.*;
6+
47
public class HastebinPasteTest {
58
@Test
6-
public void test() {
7-
HastebinPaste paste = new HastebinPaste("https://file.properties/paste/",
9+
public void test() throws Exception {
10+
HastebinPaste paste = new HastebinPaste("https://hastebin.com/",
811
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed " +
912
"iaculis, sapien et vehicula tristique, diam libero bibendum " +
1013
"nunc, et rutrum nisl nulla quis diam. Cras ipsum enim, molestie" +
@@ -16,10 +19,7 @@ public void test() {
1619
" orci, posuere malesuada ante non, elementum vehicula libero."
1720
);
1821

19-
try {
20-
System.out.println(paste.paste());
21-
} catch (Exception e) {
22-
e.printStackTrace();
23-
}
22+
URL pasteUrl = paste.paste();
23+
assertNotNull(pasteUrl);
2424
}
2525
}

0 commit comments

Comments
 (0)