From 6a8535142c477bfa7b3a13f487c49202fff2f0a9 Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Thu, 27 Sep 2018 13:51:36 -0500 Subject: [PATCH] Pulled out webhook code and audit command check code into their own functions. --- .../listeners/CommandPreprocessListener.java | 55 ++++++++++++------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/src/main/java/xyz/etztech/qol/listeners/CommandPreprocessListener.java b/src/main/java/xyz/etztech/qol/listeners/CommandPreprocessListener.java index f5b2fca..6bd3b98 100644 --- a/src/main/java/xyz/etztech/qol/listeners/CommandPreprocessListener.java +++ b/src/main/java/xyz/etztech/qol/listeners/CommandPreprocessListener.java @@ -83,25 +83,8 @@ public class CommandPreprocessListener implements Listener { if (sender.hasPermission("qol.auditor") && targetIsAuditable(command)) { return; } else { - boolean auditable = false; - - for (String audit : QoL.getAudits()) { - if (noSlash(command).startsWith(noSlash(audit))) { - auditable = true; - break; - } - } - - if (auditable) { - Map post = new HashMap<>(); - - post.put("username", "QoL Auditor"); - post.put("content", "[" + StringUtils.capitalize(sender.getGameMode().name().toLowerCase()) + "] " + sender.getName() + " executed command: " + command); - String webhook = plugin.getConfig().getString("audit.webhook"); - if (StringUtils.isNotEmpty(webhook)) { - CoreWeb.asyncPost(plugin, webhook, post); - } - + if (commandIsAuditable(command)) { + sendAuditWebhook(sender.getName(), sender.getGameMode(), command); } } } @@ -119,6 +102,40 @@ public class CommandPreprocessListener implements Listener { } + public boolean commandIsAuditable(String command) { + for (String audit : QoL.getAudits()) { + if (noSlash(command).startsWith(noSlash(audit))) { + return true; + } + } + + return false; + } + + public void sendAuditWebhook(String commandSource, GameMode gamemode, String command) { + + String content = ""; + + if (gamemode != null) { + content += "[" + StringUtils.capitalize(gamemode.name().toLowerCase()) + "] "; + } + + content += commandSource + " executed command: " + command; + + sendWebhook("QoL Auditor", content); + } + + public void sendWebhook(String username, String content) { + String webhook = plugin.getConfig().getString("audit.webhook"); + + if (StringUtils.isNotEmpty(webhook)) { + Map post = new HashMap<>(); + post.put("username", username); + post.put("content", content); + CoreWeb.asyncPost(plugin, webhook, post); + } + } + public boolean targetIsAuditable(String command) { String[] commandSplit = command.split(" "); Collection players = plugin.getServer().getOnlinePlayers();