diff --git a/src/main/java/org/maxgamer/quickshop/QuickShop.java b/src/main/java/org/maxgamer/quickshop/QuickShop.java index 0125afe1e..bae2dff66 100644 --- a/src/main/java/org/maxgamer/quickshop/QuickShop.java +++ b/src/main/java/org/maxgamer/quickshop/QuickShop.java @@ -1066,7 +1066,12 @@ public class QuickShop extends JavaPlugin { getConfig().set("config-version", 70); selectedVersion = 70; } - + if (selectedVersion == 70){ + getConfig().set("webhook.enable", false); + getConfig().set("webhook.url", "https://example.com/webhookListener.php"); + getConfig().set("config-version", 71); + selectedVersion = 71; + } saveConfig(); reloadConfig(); File file = new File(getDataFolder(), "example.config.yml"); diff --git a/src/main/java/org/maxgamer/quickshop/Webhook/WebhookHelper.java b/src/main/java/org/maxgamer/quickshop/Webhook/WebhookHelper.java index 8e58866f4..b1773b795 100644 --- a/src/main/java/org/maxgamer/quickshop/Webhook/WebhookHelper.java +++ b/src/main/java/org/maxgamer/quickshop/Webhook/WebhookHelper.java @@ -1,7 +1,163 @@ package org.maxgamer.quickshop.Webhook; -public class WebhookHelper { - public static void noticeOnlyAsync(){ +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import lombok.AllArgsConstructor; +import lombok.Data; +import org.apache.http.HttpStatus; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.bukkit.scheduler.BukkitRunnable; +import org.jetbrains.annotations.NotNull; +import org.maxgamer.quickshop.QuickShop; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Collectors; + +public class WebhookHelper { + private QuickShop plugin; + private Gson gson = new GsonBuilder().disableHtmlEscaping().create(); + private RequestConfig requestConfig = RequestConfig.custom() + .setSocketTimeout(60000) + .setConnectTimeout(60000) + .setConnectionRequestTimeout(60000) + .build(); + + public WebhookHelper(QuickShop plugin) { + this.plugin = plugin; + } + + public void noticeOnlyAsync(@NotNull Object obj) { + new BukkitRunnable() { + @Override + public void run() { + HandledConnection connection; + try { + connection = handleConnection(); + } catch (IOException e) { + plugin.getSentryErrorReporter().ignoreThrow(); + e.printStackTrace(); + plugin.getLogger().warning("Failed request the webhook, check the stacktrace to fix the problem."); + return; + } + HttpPost httpPost = connection.getHttpPost(); + CloseableHttpClient httpClient = connection.getClient(); + Map gsonArgs = new HashMap<>(); + gsonArgs.put("type", RequestType.NOTICE_ONLY.name()); + gsonArgs.put("thread", ThreadType.ASYNC.name()); + gsonArgs.put("data", obj.toString()); + StringEntity entity; + try { + entity = new StringEntity(gson.toJson(gsonArgs)); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + return; + } + entity.setContentEncoding("UTF-8"); + entity.setContentType("application/json"); + httpPost.setEntity(entity); + CloseableHttpResponse response; + try { + response = httpClient.execute(httpPost); + } catch (IOException e) { + plugin.getSentryErrorReporter().ignoreThrow(); + e.printStackTrace(); + plugin.getLogger().warning("Failed request the webhook server."); + return; + } + if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { + try { + plugin.getLogger().warning("Webhook may not success: HTTP_CODE=" + response.getStatusLine().getStatusCode() + " REASON=" + response.getStatusLine().getReasonPhrase() + " RESPONSE=" + new BufferedReader(new InputStreamReader(response.getEntity().getContent())).lines().collect(Collectors.joining())); + } catch (IOException ignored) { + } + } + try { + httpClient.close(); + } catch (IOException ignored) { + } + } + }.runTaskAsynchronously(plugin); + } + + public void noticeOnlySync(@NotNull Object obj) { + HandledConnection connection; + try { + connection = handleConnection(); + } catch (IOException e) { + plugin.getSentryErrorReporter().ignoreThrow(); + e.printStackTrace(); + plugin.getLogger().warning("Failed request the webhook, check the stacktrace to fix the problem."); + return; + } + HttpPost httpPost = connection.getHttpPost(); + CloseableHttpClient httpClient = connection.getClient(); + Map gsonArgs = new HashMap<>(); + gsonArgs.put("type", RequestType.NOTICE_ONLY.name()); + gsonArgs.put("thread", ThreadType.ASYNC.name()); + gsonArgs.put("data", obj.toString()); + StringEntity entity; + try { + entity = new StringEntity(gson.toJson(gsonArgs)); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + return; + } + entity.setContentEncoding("UTF-8"); + entity.setContentType("application/json"); + httpPost.setEntity(entity); + CloseableHttpResponse response; + try { + response = httpClient.execute(httpPost); + } catch (IOException e) { + plugin.getSentryErrorReporter().ignoreThrow(); + e.printStackTrace(); + plugin.getLogger().warning("Failed request the webhook server."); + return; + } + if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { + try { + plugin.getLogger().warning("Webhook may not success: HTTP_CODE=" + response.getStatusLine().getStatusCode() + " REASON=" + response.getStatusLine().getReasonPhrase() + " RESPONSE=" + new BufferedReader(new InputStreamReader(response.getEntity().getContent())).lines().collect(Collectors.joining())); + } catch (IOException ignored) { + } + } + try { + httpClient.close(); + } catch (IOException ignored) { + } + } + + public HandledConnection handleConnection() throws IOException, IllegalArgumentException { + String urls = plugin.getConfig().getString("webhook.url"); + if (urls == null) { + throw new IllegalArgumentException("Webhook URL is null, did you set the url in config.yml?"); + } + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpPost httpPost = new HttpPost(urls); + httpPost.setHeader("User-Agent", "QuickShop-Reremake " + QuickShop.getVersion()); + return new HandledConnection(httpClient, httpPost); } } + +enum RequestType { + NOTICE_ONLY, NOTICE_AND_RESPONSE +} + +enum ThreadType { + SYNC, ASYNC +} + +@AllArgsConstructor +@Data +class HandledConnection { + private CloseableHttpClient client; + private HttpPost httpPost; +} \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index e378d5b74..3da9108e7 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,7 +1,7 @@ #This is example config, don't direct modify this file, all changes will revert when you reload the plugin. #Do not touch if you not clear know about this -config-version: 70 +config-version: 71 #Select the language you want to use, (e.g de), use only supported language codes from the list below. #If you use a not exist/not support language, plugin will move to use en_US to keep everything working. @@ -330,4 +330,5 @@ matcher: #Plugin should check the item custommodeldata? custommodeldata: true webhook: + enable: false url: "https://example.com/webhookListener.php" \ No newline at end of file