forked from Minecraft/PluginAPI
Initial commit for Gitea
commit
dc93779fd0
|
@ -0,0 +1,4 @@
|
|||
.idea/
|
||||
*.iml
|
||||
target/
|
||||
dependency-reduced-pom.xml
|
|
@ -0,0 +1,55 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>xyz.etztech</groupId>
|
||||
<artifactId>EtzCore</artifactId>
|
||||
<version>1.0</version>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>8</source>
|
||||
<target>8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.spigotmc</groupId>
|
||||
<artifactId>spigot-api</artifactId>
|
||||
<version>1.13.1-R0.1-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>4.5.5</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spigot-repo</id>
|
||||
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>mvn-repo</id>
|
||||
<url>https://mvnrepository.com/artifact/</url>
|
||||
</repository>
|
||||
<repository> <!-- This repo fixes issues with transitive dependencies -->
|
||||
<id>jcenter</id>
|
||||
<url>http://jcenter.bintray.com</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>jitpack.io</id>
|
||||
<url>https://jitpack.io</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,13 @@
|
|||
package xyz.etztech.core;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.scheduler.BukkitScheduler;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class CoreUtils {
|
||||
|
||||
private static final BukkitScheduler schedule = Bukkit.getScheduler();
|
||||
private static final Logger logger = Logger.getLogger("Minecraft");
|
||||
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package xyz.etztech.core.api;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public interface IMinecraftManager {
|
||||
|
||||
void logOverride(boolean override);
|
||||
|
||||
void globalLog(Player player, String message);
|
||||
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
package xyz.etztech.core.web;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class CoreResponse {
|
||||
|
||||
protected JsonObject json;
|
||||
private Boolean status;
|
||||
private String message;
|
||||
|
||||
private static Logger log = Logger.getLogger("Minecraft");
|
||||
|
||||
public CoreResponse(JsonArray httpResponse) {
|
||||
json = httpResponse.get(0).getAsJsonObject();
|
||||
setStatus(json.get("status").getAsBoolean());
|
||||
setMessage(json.get("message").getAsString());
|
||||
}
|
||||
|
||||
public CoreResponse(JsonObject httpResponse) {
|
||||
json = httpResponse;
|
||||
setStatus(httpResponse.get("status").getAsBoolean());
|
||||
setMessage(httpResponse.get("message").getAsString());
|
||||
}
|
||||
|
||||
public CoreResponse(String rawReponse) {
|
||||
JsonParser parser = new JsonParser();
|
||||
try {
|
||||
json = (JsonObject) parser.parse(rawReponse);
|
||||
setStatus(json.get("status").getAsBoolean());
|
||||
setMessage(json.get("message").getAsString());
|
||||
} catch (Exception ex) {
|
||||
log.warning("Could not parse JSON result");
|
||||
}
|
||||
}
|
||||
|
||||
public Boolean getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(Boolean status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
|
||||
public String getMCMessage() {
|
||||
if (status) {
|
||||
return ChatColor.GREEN + message;
|
||||
} else {
|
||||
return ChatColor.RED + message;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,122 @@
|
|||
package xyz.etztech.core.web;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.scheduler.BukkitScheduler;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class CoreWeb {
|
||||
|
||||
private static final BukkitScheduler schedule = Bukkit.getScheduler();
|
||||
private static final Logger logger = Logger.getLogger("Minecraft");
|
||||
|
||||
public static void asyncGet(final Plugin plugin, final String url, final Map<String, String> data) {
|
||||
schedule.runTaskAsynchronously(plugin, () -> {
|
||||
ArrayList<String> query = new ArrayList<>();
|
||||
for (String key : data.keySet()) {
|
||||
query.add(key + "=" + data.get(key));
|
||||
}
|
||||
String dataQuery = StringUtils.join(query, "&");
|
||||
try {
|
||||
HttpClient client = HttpClients.createDefault();
|
||||
HttpGet get = new HttpGet(url + "?" + dataQuery);
|
||||
client.execute(get);
|
||||
} catch (Exception ex) {
|
||||
logger.warning("GET request failed. (" + url + ")");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void asyncGetCallback(final Plugin plugin, final String url, final Map<String, String> data, final ICallback callback) {
|
||||
schedule.runTaskAsynchronously(plugin, () -> {
|
||||
StringBuilder result = new StringBuilder();
|
||||
ArrayList<String> query = new ArrayList<>();
|
||||
for (String key : data.keySet()) {
|
||||
query.add(key + "=" + data.get(key));
|
||||
}
|
||||
String dataQuery = StringUtils.join(query, "&");
|
||||
try {
|
||||
HttpClient client = HttpClients.createDefault();
|
||||
HttpGet get = new HttpGet(url + "?" + dataQuery);
|
||||
HttpResponse response = client.execute(get);
|
||||
HttpEntity entity = response.getEntity();
|
||||
|
||||
InputStream input = entity.getContent();
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(input));
|
||||
String inputLine;
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
result.append(inputLine);
|
||||
}
|
||||
input.close();
|
||||
} catch (Exception ex) {
|
||||
logger.warning("GET request failed. (" + url + ")");
|
||||
}
|
||||
callback.invoke(result.toString());
|
||||
});
|
||||
}
|
||||
|
||||
public static void asyncPost(final Plugin plugin, final String url, final Map<String, String> data) {
|
||||
schedule.runTaskAsynchronously(plugin, () -> {
|
||||
try {
|
||||
HttpClient client = HttpClients.createDefault();
|
||||
HttpPost post = new HttpPost(url);
|
||||
List<NameValuePair> params = new ArrayList<>();
|
||||
for (String key : data.keySet()) {
|
||||
params.add(new BasicNameValuePair(key, data.get(key)));
|
||||
}
|
||||
post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
|
||||
client.execute(post);
|
||||
} catch (Exception ex) {
|
||||
logger.warning("POST request failed. (" + url + ")");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void asyncPostCallback(final Plugin plugin, final String url, final Map<String, String> data, final ICallback callback) {
|
||||
schedule.runTaskAsynchronously(plugin, () -> {
|
||||
StringBuilder result = new StringBuilder();
|
||||
try {
|
||||
HttpClient client = HttpClients.createDefault();
|
||||
HttpPost post = new HttpPost(url);
|
||||
List<NameValuePair> params = new ArrayList<>();
|
||||
for (String key : data.keySet()) {
|
||||
params.add(new BasicNameValuePair(key, data.get(key)));
|
||||
}
|
||||
post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
|
||||
HttpResponse response = client.execute(post);
|
||||
HttpEntity entity = response.getEntity();
|
||||
|
||||
InputStream input = entity.getContent();
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(input));
|
||||
String inputLine;
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
result.append(inputLine);
|
||||
}
|
||||
input.close();
|
||||
} catch (Exception ex) {
|
||||
logger.warning("POST request failed. (" + url + ")");
|
||||
}
|
||||
callback.invoke(result.toString());
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package xyz.etztech.core.web;
|
||||
|
||||
public interface ICallback {
|
||||
|
||||
void invoke(String httpResponse);
|
||||
|
||||
}
|
Loading…
Reference in New Issue