diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml new file mode 100644 index 0000000..11adacf --- /dev/null +++ b/dependency-reduced-pom.xml @@ -0,0 +1,234 @@ + + + 4.0.0 + net + communityanalytics + communityanalytics + 1.0.1 + http://maven.apache.org + + + + maven-compiler-plugin + 3.8.0 + + 8 + 8 + + + + maven-shade-plugin + 3.1.0 + + + package + + shade + + + + + + + org.bstats + net.communityanalytics.util + + + + + + + + + spigot-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + bungeecord-repo + https://oss.sonatype.org/content/repositories/snapshots + + + papermc + https://repo.papermc.io/repository/maven-public/ + + + + + junit + junit + 4.13.1 + test + + + hamcrest-core + org.hamcrest + + + + + org.spigotmc + spigot-api + 1.19.3-R0.1-SNAPSHOT + provided + + + guava + com.google.guava + + + gson + com.google.code.gson + + + bungeecord-chat + net.md-5 + + + snakeyaml + org.yaml + + + + + net.md-5 + bungeecord-api + 1.19-R0.1-SNAPSHOT + provided + + + bungeecord-config + net.md-5 + + + bungeecord-event + net.md-5 + + + bungeecord-protocol + net.md-5 + + + netty-transport-native-unix-common + io.netty + + + bungeecord-chat + net.md-5 + + + snakeyaml + org.yaml + + + guava + com.google.guava + + + + + net.md-5 + bungeecord-api + 1.19-R0.1-SNAPSHOT + javadoc + provided + + + bungeecord-config + net.md-5 + + + bungeecord-event + net.md-5 + + + bungeecord-protocol + net.md-5 + + + netty-transport-native-unix-common + io.netty + + + bungeecord-chat + net.md-5 + + + snakeyaml + org.yaml + + + guava + com.google.guava + + + + + com.velocitypowered + velocity-api + 3.1.1 + provided + + + toml4j + com.moandjiezana.toml + + + adventure-api + net.kyori + + + adventure-text-serializer-gson + net.kyori + + + adventure-text-serializer-legacy + net.kyori + + + adventure-text-serializer-plain + net.kyori + + + slf4j-api + org.slf4j + + + guice + com.google.inject + + + checker-qual + org.checkerframework + + + velocity-brigadier + com.velocitypowered + + + configurate-hocon + org.spongepowered + + + configurate-yaml + org.spongepowered + + + configurate-gson + org.spongepowered + + + gson + com.google.code.gson + + + guava + com.google.guava + + + + + + UTF-8 + + + diff --git a/pom.xml b/pom.xml index eb7258a..b650e1e 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ net communityanalytics - 1.0.0 + 1.0.1 jar communityanalytics @@ -21,10 +21,12 @@ maven-compiler-plugin 3.8.0 - 17 - 17 + 8 + 8 + + org.apache.maven.plugins maven-shade-plugin @@ -65,6 +67,12 @@ + + org.apache.httpcomponents + httpclient + 4.5.13 + + org.bstats bstats-bukkit @@ -87,7 +95,7 @@ junit junit - 3.8.1 + 4.13.1 test diff --git a/src/main/java/net/communityanalytics/CommunityAnalytics.java b/src/main/java/net/communityanalytics/CommunityAnalytics.java index 356af60..b197033 100644 --- a/src/main/java/net/communityanalytics/CommunityAnalytics.java +++ b/src/main/java/net/communityanalytics/CommunityAnalytics.java @@ -3,5 +3,5 @@ public class CommunityAnalytics { public static final String CHANNEL_INFO = "community-analytics:player-info"; public static final String API_URL = "https://communityanalytics.net/api"; - public static final int SESSION_API_MINUTES = 1; + public static final int SESSION_API_MINUTES = 5; } diff --git a/src/main/java/net/communityanalytics/spigot/api/APIRequest.java b/src/main/java/net/communityanalytics/spigot/api/APIRequest.java index fcf9203..4bf4912 100644 --- a/src/main/java/net/communityanalytics/spigot/api/APIRequest.java +++ b/src/main/java/net/communityanalytics/spigot/api/APIRequest.java @@ -1,37 +1,113 @@ package net.communityanalytics.spigot.api; -import com.google.gson.JsonObject; +import com.google.gson.*; import net.communityanalytics.CommunityAnalytics; import net.communityanalytics.spigot.SpigotPlugin; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.*; +import org.apache.http.client.utils.URIBuilder; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; -import java.io.IOException; +import java.io.*; import java.net.URI; -import java.net.http.HttpClient; -import java.net.http.HttpResponse; +import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Collectors; public class APIRequest { - private final String api; + // call + private final String url; private final MethodEnum method; - private final JsonObject data; + private final JsonObject parameters; - public APIRequest(String api, MethodEnum method, JsonObject data) { - this.api = api; + public APIRequest(String api, MethodEnum method, JsonObject parameters) { + this.url = CommunityAnalytics.API_URL + "/" + api; this.method = method; - this.data = data; + this.parameters = parameters; } - public ApiResponse sendRequest() throws IOException, InterruptedException { - // Send request - java.net.http.HttpRequest request = java.net.http.HttpRequest.newBuilder() - .uri(URI.create(CommunityAnalytics.API_URL + "/" + api)) - .header("X-Community-Analytics-Token", SpigotPlugin.config().getPlatformApiToken()) - .header("Content-Type", "application/json") - .header("Accept", "application/json") - .method(method.name(), java.net.http.HttpRequest.BodyPublishers.ofString(data.toString())) - .build(); - - // Return response - HttpResponse response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString()); - return new ApiResponse(response); + /** + * Get default header for API requests + * + * @return Map + */ + private Map getHeaders() { + Map headers = new HashMap<>(); + headers.put("X-Community-Analytics-Token", SpigotPlugin.config().getPlatformApiToken()); + headers.put("Content-Type", "application/json"); + headers.put("Accept", "application/json"); + return headers; } -} + + public ApiResponse sendRequest() throws URISyntaxException, IOException { + CloseableHttpClient httpclient = HttpClients.createDefault(); + HttpResponse response; + HttpRequestBase request; + + switch (method.name()) { + case "GET": + HttpGet httpGet = new HttpGet(this.url); + // Request parameters + if (parameters.size() > 0) { + URIBuilder uriBuilder = new URIBuilder(httpGet.getURI()); + for (Map.Entry entry : parameters.entrySet()) { + uriBuilder.addParameter(entry.getKey(), entry.getValue().getAsString()); + } + URI uri = uriBuilder.build(); + httpGet.setURI(uri); + } + + request = httpGet; + break; + case "DELETE": + request = new HttpDelete(this.url); + break; + case "PUT": + HttpPut httpput = new HttpPut(this.url); + // Request parameters and other properties. + StringEntity entity = new StringEntity(parameters.toString()); + httpput.setEntity(entity); + + request = httpput; + break; + default: // POST + HttpPost httppost = new HttpPost(this.url); + // Request parameters and other properties. + entity = new StringEntity(parameters.toString()); + httppost.setEntity(entity); + + request = httppost; + break; + } + for (Map.Entry entry : getHeaders().entrySet()) + request.addHeader(entry.getKey(), entry.getValue()); + + // Call API + response = httpclient.execute(request); + + // Build response + int status_code = response.getStatusLine().getStatusCode(); + String response_string = null; + JsonElement json_element = null; + HttpEntity entity = response.getEntity(); + if (entity != null) { + try (InputStream inputStream = entity.getContent()) { + response_string = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n")); + JsonParser parser = new JsonParser(); + + json_element = parser.parse(response_string); + if (!json_element.isJsonNull()) { + JsonObject json_object = (JsonObject) json_element; + } + } + } + httpclient.close(); + + return new ApiResponse(status_code, response_string, json_element); + } +} \ No newline at end of file diff --git a/src/main/java/net/communityanalytics/spigot/api/ApiResponse.java b/src/main/java/net/communityanalytics/spigot/api/ApiResponse.java index e304e73..ba00345 100644 --- a/src/main/java/net/communityanalytics/spigot/api/ApiResponse.java +++ b/src/main/java/net/communityanalytics/spigot/api/ApiResponse.java @@ -1,25 +1,30 @@ package net.communityanalytics.spigot.api; -import com.google.gson.JsonParser; +import com.google.gson.JsonElement; -import java.net.http.HttpResponse; +import java.io.IOException; public class ApiResponse { - private final HttpResponse response; + // response + public int status_code; + public String response_string = null; + public JsonElement json_object = null; - public ApiResponse(java.net.http.HttpResponse response) { - this.response = response; + public ApiResponse(int status_code, String response_string, JsonElement json_object) { + this.status_code = status_code; + this.response_string = response_string; + this.json_object = json_object; } - public boolean has(String arg) { - return JsonParser.parseString(response.body()).getAsJsonObject().has(arg); + public boolean has(String arg) throws IOException { + return json_object.getAsJsonObject().has(arg); } - public String getStringArg(String arg) { - return JsonParser.parseString(response.body()).getAsJsonObject().get(arg).getAsString(); + public String getStringArg(String arg) throws IOException { + return json_object.getAsJsonObject().get(arg).getAsString(); } public int getStatus() { - return response.statusCode(); + return status_code; } -} +} \ No newline at end of file diff --git a/src/main/java/net/communityanalytics/velocity/VelocityPlugin.java b/src/main/java/net/communityanalytics/velocity/VelocityPlugin.java index 636f63b..d34d5ec 100644 --- a/src/main/java/net/communityanalytics/velocity/VelocityPlugin.java +++ b/src/main/java/net/communityanalytics/velocity/VelocityPlugin.java @@ -13,7 +13,7 @@ import org.bstats.velocity.Metrics; import org.slf4j.Logger; -@Plugin(id = "communityanalytics", name = "CommunityAnalytics", version = "1.0.0") +@Plugin(id = "communityanalytics", name = "CommunityAnalytics", version = "1.0.1") public class VelocityPlugin { public static VelocityPlugin instance; private final ChannelIdentifier channel = new LegacyChannelIdentifier(CommunityAnalytics.CHANNEL_INFO); diff --git a/src/main/java/net/communityanalytics/velocity/listeners/PlayerInfoListener.java b/src/main/java/net/communityanalytics/velocity/listeners/PlayerInfoListener.java index 8593fd0..765625c 100644 --- a/src/main/java/net/communityanalytics/velocity/listeners/PlayerInfoListener.java +++ b/src/main/java/net/communityanalytics/velocity/listeners/PlayerInfoListener.java @@ -70,7 +70,8 @@ public void onMessage(PluginMessageEvent event) { out.writeUTF(playerInfo.getIpConnect()); out.writeUTF(playerInfo.getIpUser()); - if (event.getSource() instanceof ServerConnection serverConnection) { + if (event.getSource() instanceof ServerConnection) { + ServerConnection serverConnection = (ServerConnection) event.getSource(); serverConnection.sendPluginMessage(VelocityPlugin.instance.getChannel(), out.toByteArray()); } } diff --git a/src/main/resources/bungee.yml b/src/main/resources/bungee.yml index d1b1e06..b868c2b 100644 --- a/src/main/resources/bungee.yml +++ b/src/main/resources/bungee.yml @@ -1,4 +1,4 @@ name: CommunityAnalytics author: FNetwork main: net.communityanalytics.bungee.BungeePlugin -version: 1.0.0 \ No newline at end of file +version: 1.0.1 \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 557e042..51b0e07 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -2,7 +2,7 @@ name: CommunityAnalytics author: FNetwork main: net.communityanalytics.spigot.SpigotPlugin website: https://communityanalytics.net -version: 1.0.0 +version: 1.0.1 api-version: 1.13 commands: communityanalytics: diff --git a/src/main/resources/velocity-plugin.json b/src/main/resources/velocity-plugin.json index 5c2cf55..86da6e3 100644 --- a/src/main/resources/velocity-plugin.json +++ b/src/main/resources/velocity-plugin.json @@ -1,7 +1,7 @@ { "id": "communityanalytics", "name": "CommunityAnalytics", - "version": "1.0.0", + "version": "1.0.1", "authors": [ "FNetwork" ],