diff options
4 files changed, 83 insertions, 45 deletions
diff --git a/build.gradle b/build.gradle index 9c45247..7711c7c 100644 --- a/build.gradle +++ b/build.gradle @@ -23,7 +23,7 @@ subprojects { repositories { mavenCentral() - maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' } + maven { url "https://oss.sonatype.org/content/repositories/snapshots/" } maven { url "https://repo.lucko.me/" } maven { url "https://hub.spigotmc.org/nexus/content/repositories/snapshots/" } maven { url "https://repo.spongepowered.org/maven" } diff --git a/spark-common/build.gradle b/spark-common/build.gradle index b0b1bf3..df33721 100644 --- a/spark-common/build.gradle +++ b/spark-common/build.gradle @@ -7,14 +7,17 @@ dependencies { compile 'com.squareup.okhttp3:okhttp:3.14.1' compile 'com.squareup.okio:okio:1.17.3' compile 'org.tukaani:xz:1.8' - compile('net.kyori:text-api:3.0.0') { + compile('net.kyori:text-api:3.0.2') { exclude(module: 'checker-qual') } - compile('net.kyori:text-serializer-gson:3.0.0') { + compile('net.kyori:text-serializer-gson:3.0.2') { exclude(module: 'text-api') exclude(module: 'gson') } - compile('net.kyori:text-serializer-legacy:3.0.0') { + compile('net.kyori:text-serializer-legacy:3.0.2') { + exclude(module: 'text-api') + } + compile('net.kyori:text-feature-pagination:3.0.2') { exclude(module: 'text-api') } compileOnly 'com.google.code.gson:gson:2.7' diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/CommandResponseHandler.java b/spark-common/src/main/java/me/lucko/spark/common/command/CommandResponseHandler.java index d31d1cf..88a597d 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/command/CommandResponseHandler.java +++ b/spark-common/src/main/java/me/lucko/spark/common/command/CommandResponseHandler.java @@ -67,11 +67,15 @@ public class CommandResponseHandler { } public void replyPrefixed(Component message) { - reply(PREFIX.append(message)); + reply(applyPrefix(message)); } public void broadcastPrefixed(Component message) { - broadcast(PREFIX.append(message)); + broadcast(applyPrefix(message)); + } + + public static Component applyPrefix(Component message) { + return PREFIX.append(message); } diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/modules/ActivityLogModule.java b/spark-common/src/main/java/me/lucko/spark/common/command/modules/ActivityLogModule.java index edb8537..9c81f28 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/command/modules/ActivityLogModule.java +++ b/spark-common/src/main/java/me/lucko/spark/common/command/modules/ActivityLogModule.java @@ -20,68 +20,99 @@ package me.lucko.spark.common.command.modules; -import me.lucko.spark.common.ActivityLog; +import me.lucko.spark.common.ActivityLog.Activity; import me.lucko.spark.common.command.Command; import me.lucko.spark.common.command.CommandModule; +import me.lucko.spark.common.command.tabcomplete.TabCompleter; +import net.kyori.text.Component; import net.kyori.text.TextComponent; import net.kyori.text.event.ClickEvent; +import net.kyori.text.feature.pagination.Pagination; +import net.kyori.text.feature.pagination.Pagination.Renderer; +import net.kyori.text.feature.pagination.Pagination.Renderer.RowRenderer; import net.kyori.text.format.TextColor; import net.kyori.text.format.TextDecoration; +import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.function.Consumer; -public class ActivityLogModule implements CommandModule { +import static me.lucko.spark.common.command.CommandResponseHandler.*; + +public class ActivityLogModule implements CommandModule, RowRenderer<Activity> { + + private final Pagination.Builder pagination = Pagination.builder() + .renderer(new Renderer() { + @Override + public Component renderEmpty() { + return applyPrefix(TextComponent.of("There are no entries present in the log.")); + } + + @Override + public Component renderUnknownPage(int page, int pages) { + return applyPrefix(TextComponent.of("Unknown page selected. " + pages + " total pages.")); + } + }) + .resultsPerPage(4); + + @Override + public Collection<Component> renderRow(Activity activity, int index) { + List<Component> reply = new ArrayList<>(5); + reply.add(TextComponent.builder("") + .append(TextComponent.builder(">").color(TextColor.DARK_GRAY).decoration(TextDecoration.BOLD, true).build()) + .append(TextComponent.space()) + .append(TextComponent.of("#" + (index + 1), TextColor.WHITE)) + .append(TextComponent.of(" - ", TextColor.DARK_GRAY)) + .append(TextComponent.of(activity.getType(), TextColor.YELLOW)) + .append(TextComponent.of(" - ", TextColor.DARK_GRAY)) + .append(TextComponent.of(formatDateDiff(activity.getTime()), TextColor.GRAY)) + .build() + ); + reply.add(TextComponent.builder(" ") + .append(TextComponent.of("Created by: ", TextColor.GRAY)) + .append(TextComponent.of(activity.getUser().getName(), TextColor.WHITE)) + .build() + ); + + TextComponent.Builder valueComponent = TextComponent.builder(activity.getDataValue(), TextColor.WHITE); + if (activity.getDataType().equals("url")) { + valueComponent.clickEvent(ClickEvent.openUrl(activity.getDataValue())); + } + + reply.add(TextComponent.builder(" ") + .append(TextComponent.of(Character.toUpperCase(activity.getDataType().charAt(0)) + activity.getDataType().substring(1) + ": ", TextColor.GRAY)) + .append(valueComponent) + .build() + ); + reply.add(TextComponent.space()); + return reply; + } @Override public void registerCommands(Consumer<Command> consumer) { consumer.accept(Command.builder() .aliases("activity", "activitylog", "log") + .argumentUsage("page", "page no") .executor((platform, sender, resp, arguments) -> { - List<ActivityLog.Activity> log = platform.getActivityLog().getLog(); - log.removeIf(ActivityLog.Activity::shouldExpire); + List<Activity> log = platform.getActivityLog().getLog(); + log.removeIf(Activity::shouldExpire); if (log.isEmpty()) { resp.replyPrefixed(TextComponent.of("There are no entries present in the log.")); return; } - resp.replyPrefixed(TextComponent.of("Showing recent spark activity...", TextColor.GOLD)); - - int count = 0; - for (ActivityLog.Activity activity : log) { - count++; - - resp.replyPrefixed(TextComponent.builder("") - .append(TextComponent.builder(">").color(TextColor.DARK_GRAY).decoration(TextDecoration.BOLD, true).build()) - .append(TextComponent.space()) - .append(TextComponent.of("#" + count, TextColor.WHITE)) - .append(TextComponent.of(" - ", TextColor.DARK_GRAY)) - .append(TextComponent.of(activity.getType(), TextColor.YELLOW)) - .append(TextComponent.of(" - ", TextColor.DARK_GRAY)) - .append(TextComponent.of(formatDateDiff(activity.getTime()), TextColor.GRAY)) - .build() - ); - resp.replyPrefixed(TextComponent.builder(" ") - .append(TextComponent.of("Created by: ", TextColor.GRAY)) - .append(TextComponent.of(activity.getUser().getName(), TextColor.WHITE)) - .build() - ); - - TextComponent.Builder valueComponent = TextComponent.builder(activity.getDataValue(), TextColor.WHITE); - if (activity.getDataType().equals("url")) { - valueComponent.clickEvent(ClickEvent.openUrl(activity.getDataValue())); - } - - resp.replyPrefixed(TextComponent.builder(" ") - .append(TextComponent.of(Character.toUpperCase(activity.getDataType().charAt(0)) + activity.getDataType().substring(1) + ": ", TextColor.GRAY)) - .append(valueComponent) - .build() - ); - resp.reply(TextComponent.space()); - } + int page = Math.max(1, arguments.intFlag("page")); + + Pagination<Activity> activityPagination = this.pagination.build( + TextComponent.of("Recent spark activity", TextColor.GOLD), + this, + value -> "/" + platform.getPlugin().getLabel() + " activity --page " + value + ); + activityPagination.render(log, page).forEach(resp::reply); }) - .tabCompleter(Command.TabCompleter.empty()) + .tabCompleter((platform, sender, arguments) -> TabCompleter.completeForOpts(arguments, "--page")) .build() ); } |