Skip to content

Commit e5bbd4c

Browse files
author
TheProgramSrc
committed
v4.2.3 Changelog:
+ Added method to build an Exception into a String + Added method to upload data to https://paste.theprogramsrc.xyz/ + Added method to validate json string
1 parent 27c6572 commit e5bbd4c

File tree

4 files changed

+89
-12
lines changed

4 files changed

+89
-12
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## v4.2.3 Changelog:
2+
```
3+
+ Added method to build an Exception into a String
4+
+ Added method to upload data to https://paste.theprogramsrc.xyz/
5+
+ Added method to validate json string
6+
```
7+
18
## v4.2.2 Changelog:
29
```
310
+ Added API Update Checker

pom.xml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>xyz.theprogramsrc</groupId>
88
<artifactId>SuperCoreAPI</artifactId>
9-
<version>4.2.3_BETA1</version>
9+
<version>4.2.3</version>
1010
<packaging>jar</packaging>
1111

1212
<name>SuperCoreAPI</name>
@@ -165,12 +165,5 @@
165165
<version>2.13.2</version>
166166
<scope>provided</scope>
167167
</dependency>
168-
<!-- Tests
169-
<dependency>
170-
<groupId>org.junit.jupiter</groupId>
171-
<artifactId>junit-jupiter</artifactId>
172-
<version>RELEASE</version>
173-
<scope>test</scope>
174-
</dependency> -->
175168
</dependencies>
176169
</project>

src/main/java/xyz/theprogramsrc/supercoreapi/Base.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@ public Base(SuperPlugin<?> plugin){
1717

1818
private void loadUpdateChecker(){
1919
new SpigotUpdateChecker("77096"){
20-
2120
@Override
2221
public void onCheckFail() {
2322
Base.this.plugin.log("&cError while checking &4SuperCoreAPI &cupdates.");
2423
}
2524

2625
@Override
2726
public void onCheckSuccess(String lastVersion) {
28-
String currentVersion = "4.2.2";
27+
String currentVersion = "4.2.3";
2928
int latest = Integer.parseInt(lastVersion.split(" ")[0].replaceAll("\\.", ""));
3029
int current = Integer.parseInt(currentVersion.split(" ")[0].replaceAll("\\.", ""));
3130
if(latest > current){

src/main/java/xyz/theprogramsrc/supercoreapi/global/utils/Utils.java

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package xyz.theprogramsrc.supercoreapi.global.utils;
22

3+
import com.google.gson.JsonObject;
4+
import com.google.gson.JsonParser;
35
import org.apache.commons.io.FileUtils;
46
import org.bukkit.Bukkit;
57
import org.bukkit.plugin.Plugin;
68

79
import java.io.*;
10+
import java.net.HttpURLConnection;
811
import java.net.URL;
912
import java.net.URLConnection;
1013
import java.net.UnknownHostException;
@@ -290,7 +293,7 @@ public static String readWithInputStream(String url) {
290293
try{
291294
URL javaURL = new URL(url);
292295
URLConnection connection = javaURL.openConnection();
293-
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
296+
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36");
294297
return new BufferedReader(new InputStreamReader(connection.getInputStream())).lines().collect(Collectors.joining());
295298
}catch (IOException ex){
296299
ex.printStackTrace();
@@ -623,7 +626,82 @@ public static List<String> ct(List<String> list){
623626
* and {@code false} otherwise
624627
*/
625628
public static boolean equals(Object a, Object b) {
626-
return (a == b) || (a != null && a.equals(b));
629+
return Objects.equals(a, b);
630+
}
631+
632+
/**
633+
* Returns {@code true} if the input is a valid json
634+
* @return {@code true} if the input is a valid json, otherwise {@code false}
635+
*/
636+
public static boolean isJSONEncoded(String input){
637+
try{
638+
new JsonParser().parse(input);
639+
return true;
640+
}catch (Exception e){
641+
return false;
642+
}
643+
}
644+
645+
/**
646+
* Create a new post request
647+
* @param url the url
648+
* @param contentData the data to send
649+
* @return the url response output
650+
*/
651+
public static String postRequest(String url, String contentData) throws IOException{
652+
URL javaURL = new URL(url);
653+
HttpURLConnection connection = ((HttpURLConnection)javaURL.openConnection());
654+
connection.setRequestMethod("POST");
655+
connection.setDoOutput(true);
656+
connection.addRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36");
657+
connection.addRequestProperty("Content-Type", "text/plain; charset=UTF-8");
658+
connection.addRequestProperty("Content-Length", contentData.length()+"");
659+
OutputStream out = connection.getOutputStream();
660+
out.write(contentData.getBytes());
661+
connection.connect();
662+
InputStream in = connection.getInputStream();
663+
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
664+
String result = reader.lines().collect(Collectors.joining());
665+
connection.disconnect();
666+
return result;
667+
}
668+
669+
/**
670+
* Upload a new paste to <a href="https://paste.theprogramsrc.xyz/">https://paste.theprogramsrc.xyz/</a>
671+
*
672+
* @param body the body of the paste
673+
* @return the paste key
674+
*/
675+
public static String uploadPaste(String body){
676+
String url = "https://paste.theprogramsrc.xyz/documents";
677+
try{
678+
String post = postRequest(url, body);
679+
if(post != null){
680+
if(isJSONEncoded(post)){
681+
JsonObject json = new JsonParser().parse(post).getAsJsonObject();
682+
return json.get("key").getAsString();
683+
}
684+
}
685+
}catch (IOException e){
686+
e.printStackTrace();
687+
}
688+
689+
return null;
690+
}
691+
692+
/**
693+
* Build an {@link Exception exception} into a string
694+
* @param e the exception
695+
* @return the stack trace as string
696+
*/
697+
public static String exceptionToString(Exception e){
698+
StringBuilder builder = new StringBuilder();
699+
builder.append(e.getClass().getName()).append(": ").append(e.getMessage() != null ? e.getMessage() : "null").append("\n");
700+
for (StackTraceElement ste : e.getStackTrace()) {
701+
builder.append("\tat ").append(ste.getClassName()).append(".").append(ste.getMethodName()).append("(").append(ste.getFileName()).append(":").append(ste.getLineNumber()).append(")").append("\n");
702+
}
703+
704+
return builder.toString();
627705
}
628706
}
629707

0 commit comments

Comments
 (0)