diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/misc/VerboseCommand.java b/common/src/main/java/me/lucko/luckperms/common/commands/misc/VerboseCommand.java index 711407a9b..2be0f71f8 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/misc/VerboseCommand.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/misc/VerboseCommand.java @@ -32,6 +32,9 @@ import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.debug.DebugListener; import me.lucko.luckperms.common.utils.Predicates; +import io.github.mkremins.fanciful.ChatColor; +import io.github.mkremins.fanciful.FancyMessage; + import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -90,12 +93,15 @@ public class VerboseCommand extends SingleCommand { if (listener == null) { Message.VERBOSE_OFF.send(sender); } else { + Message.VERBOSE_RECORDING_UPLOAD_START.send(sender); + String url = listener.uploadPastedData(); if (url == null) { url = "null"; } - Message.VERBOSE_RECORDING_URL.send(sender, url); + Message.VERBOSE_RECORDING_URL.send(sender); + sender.sendMessage(new FancyMessage(url).color(ChatColor.getByChar('b')).link(url)); return CommandResult.SUCCESS; } } else { diff --git a/common/src/main/java/me/lucko/luckperms/common/constants/Message.java b/common/src/main/java/me/lucko/luckperms/common/constants/Message.java index 353fda0f9..763acd6f5 100644 --- a/common/src/main/java/me/lucko/luckperms/common/constants/Message.java +++ b/common/src/main/java/me/lucko/luckperms/common/constants/Message.java @@ -99,7 +99,7 @@ public enum Message { VERBOSE_RECORDING_ON("&bVerbose recording set to &aTRUE &bfor all permissions.", true), VERBOSE_RECORDING_ON_QUERY("&bVerbose recording set to &aTRUE &bfor permissions matching the following filters: &f{0}", true), VERBOSE_RECORDING_UPLOAD_START("&bVerbose recording was disabled. Uploading results...", true), - VERBOSE_RECORDING_URL("&aVerbose results URL: {0}", true), + VERBOSE_RECORDING_URL("&aVerbose results URL:", true), SEARCH_SEARCHING("&aSearching for users and groups with &b{0}&a...", true), SEARCH_RESULT("&aFound &b{0}&a entries from &b{1}&a users and &b{2}&a groups.", true), diff --git a/common/src/main/java/me/lucko/luckperms/common/debug/DebugListener.java b/common/src/main/java/me/lucko/luckperms/common/debug/DebugListener.java index 33cb302cc..5919020de 100644 --- a/common/src/main/java/me/lucko/luckperms/common/debug/DebugListener.java +++ b/common/src/main/java/me/lucko/luckperms/common/debug/DebugListener.java @@ -28,31 +28,44 @@ import com.google.common.collect.ImmutableList; import me.lucko.luckperms.common.commands.sender.Sender; import me.lucko.luckperms.common.constants.Message; +import me.lucko.luckperms.common.utils.DateUtil; import me.lucko.luckperms.common.utils.PasteUtils; import me.lucko.luckperms.common.utils.Scripting; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.StringTokenizer; +import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; import javax.script.ScriptEngine; @RequiredArgsConstructor public class DebugListener { + private static final int DATA_TRUNCATION = 700; + private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); + + private final long startTime = System.currentTimeMillis(); + private final String pluginVersion; private final Sender holder; private final String filter; private final boolean notify; - private List results = new ArrayList<>(); + private final AtomicInteger counter = new AtomicInteger(0); + private final AtomicInteger matchedCounter = new AtomicInteger(0); + private final List results = new ArrayList<>(); public void acceptData(CheckData data) { + counter.incrementAndGet(); if (!matches(data, filter)) { return; } + matchedCounter.incrementAndGet(); - if (results.size() < 500) { + if (results.size() < DATA_TRUNCATION) { results.add(data); } @@ -108,30 +121,52 @@ public class DebugListener { } public String uploadPastedData() { + long now = System.currentTimeMillis(); + String startDate = DATE_FORMAT.format(new Date(startTime)); + String endDate = DATE_FORMAT.format(new Date(now)); + long secondsTaken = (now - startTime) / 1000L; + String duration = DateUtil.formatTime(secondsTaken); + String filter = this.filter; + if (filter == null || filter.equals("")){ + filter = "any"; + } else { + filter = "`" + filter + "`"; + } + ImmutableList.Builder output = ImmutableList.builder() + .add("## Verbose Checking Output") .add("#### This file was automatically generated by [LuckPerms](https://github.com/lucko/LuckPerms) " + pluginVersion) .add("") - .add("Format: `` `` ``") - .add("") - .add("Size: " + results.size()) + .add("### Metadata") + .add("| Key | Value |") + .add("|-----|-------|") + .add("| Start Time | " + startDate + " |") + .add("| End Time | " + endDate + " |") + .add("| Duration | " + duration +" |") + .add("| Count | **" + matchedCounter.get() + "** / " + counter + " |") + .add("| User | " + holder.getName() + " |") + .add("| Filter | " + filter + " |") .add(""); - if (results.size() >= 500) { - output.add("**WARN:** Result set exceeded max size of 500. Output was trimmed."); + if (matchedCounter.get() > results.size()) { + output.add("**WARN:** Result set exceeded max size of " + DATA_TRUNCATION + ". The output below was truncated to " + DATA_TRUNCATION + " entries."); output.add(""); } + output.add("### Output") + .add("Format: `` `` ``") + .add("") + .add("___") + .add(""); + List ret = results.stream() .map(c -> "`" + c.getChecked() + "` - " + c.getNode() + " - **" + c.getValue().toString() + "** ") .collect(Collectors.toList()); - output.add("___") - .add("") - .addAll(ret) - .build(); + output.addAll(ret); results.clear(); - return PasteUtils.paste(output.build().stream().collect(Collectors.joining("\n"))); + return PasteUtils.paste("luckperms-verbose.md", "LuckPerms Verbose Checking Output", output.build().stream().collect(Collectors.joining("\n"))); } } diff --git a/common/src/main/java/me/lucko/luckperms/common/utils/DateUtil.java b/common/src/main/java/me/lucko/luckperms/common/utils/DateUtil.java index 128455653..d8f6b4252 100644 --- a/common/src/main/java/me/lucko/luckperms/common/utils/DateUtil.java +++ b/common/src/main/java/me/lucko/luckperms/common/utils/DateUtil.java @@ -136,6 +136,14 @@ public class DateUtil { return DateUtil.formatDateDiff(now, c); } + public static String formatTime(long seconds) { + Calendar c = new GregorianCalendar(); + c.setTimeInMillis(0L); + Calendar c2 = new GregorianCalendar(); + c2.setTimeInMillis(seconds * 1000L); + return DateUtil.formatDateDiff(c, c2); + } + private static String formatDateDiff(Calendar fromDate, Calendar toDate) { boolean future = false; if (toDate.equals(fromDate)) { diff --git a/common/src/main/java/me/lucko/luckperms/common/utils/PasteUtils.java b/common/src/main/java/me/lucko/luckperms/common/utils/PasteUtils.java index da4c8a297..8be958100 100644 --- a/common/src/main/java/me/lucko/luckperms/common/utils/PasteUtils.java +++ b/common/src/main/java/me/lucko/luckperms/common/utils/PasteUtils.java @@ -35,12 +35,7 @@ import java.net.URL; public class PasteUtils { - public static void main(String[] args) { - String s = "hey\nlol\nxd"; - System.out.println(paste(s)); - } - - public static String paste(String contents) { + public static String paste(String name, String desc, String contents) { try { HttpURLConnection connection = (HttpURLConnection) new URL("https://api.github.com/gists").openConnection(); connection.setRequestMethod("POST"); @@ -50,10 +45,10 @@ public class PasteUtils { try (OutputStream os = connection.getOutputStream()) { StringWriter sw = new StringWriter(); new JsonWriter(sw).beginObject() - .name("description").value("LuckPerms Verbose Output") + .name("description").value(desc) .name("public").value(false) .name("files") - .beginObject().name("lp-verbose.md") + .beginObject().name(name) .beginObject().name("content").value(contents) .endObject() .endObject() diff --git a/default-lang.yml b/default-lang.yml index 28bf1c662..c1c17290d 100644 --- a/default-lang.yml +++ b/default-lang.yml @@ -57,7 +57,7 @@ verbose-off: "&bVerbose checking output set to &cFALSE&b." verbose-recording-on: "&bVerbose recording set to &aTRUE &bfor all permissions." verbose-recording-on-query: "&bVerbose recording set to &aTRUE &bfor permissions matching the following filters: &f{0}" verbose-recording-upload-start: "&bVerbose recording was disabled. Uploading results..." -verbose-recording-url: "&aVerbose results URL: {0}" +verbose-recording-url: "&aVerbose results URL:" search-searching: "&aSearching for users and groups with &b{0}&a..." search-result: "&aFound &b{0}&a entries from &b{1}&a users and &b{2}&a groups."