aboutsummaryrefslogtreecommitdiff
path: root/spark-common/src/main/java/me/lucko/spark/common/command/modules/ActivityLogModule.java
blob: 6252ac79ba6160e2e88289024ff83034125ff0fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * This file is part of spark.
 *
 *  Copyright (c) lucko (Luck) <luck@lucko.me>
 *  Copyright (c) contributors
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package me.lucko.spark.common.command.modules;

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.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.feature.pagination.Pagination;
import net.kyori.adventure.text.feature.pagination.Pagination.Renderer;
import net.kyori.adventure.text.feature.pagination.Pagination.Renderer.RowRenderer;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;

import static me.lucko.spark.common.command.CommandResponseHandler.applyPrefix;
import static net.kyori.adventure.text.Component.space;
import static net.kyori.adventure.text.Component.text;
import static net.kyori.adventure.text.format.NamedTextColor.DARK_GRAY;
import static net.kyori.adventure.text.format.NamedTextColor.GOLD;
import static net.kyori.adventure.text.format.NamedTextColor.GRAY;
import static net.kyori.adventure.text.format.NamedTextColor.WHITE;
import static net.kyori.adventure.text.format.NamedTextColor.YELLOW;
import static net.kyori.adventure.text.format.TextDecoration.BOLD;

public class ActivityLogModule implements CommandModule, RowRenderer<Activity> {

    private final Pagination.Builder pagination = Pagination.builder()
            .width(45)
            .renderer(new Renderer() {
                @Override
                public Component renderEmpty() {
                    return applyPrefix(text("There are no entries present in the log."));
                }

                @Override
                public Component renderUnknownPage(int page, int pages) {
                    return applyPrefix(text("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(text()
                .append(text(">", DARK_GRAY, BOLD))
                .append(space())
                .append(text("#" + (index + 1), WHITE))
                .append(text(" - ", DARK_GRAY))
                .append(text(activity.getType(), YELLOW))
                .append(text(" - ", DARK_GRAY))
                .append(text(formatDateDiff(activity.getTime()), GRAY))
                .build()
        );
        reply.add(text()
                .content("  ")
                .append(text("Created by: ", GRAY))
                .append(text(activity.getUser().getName(), WHITE))
                .build()
        );

        TextComponent.Builder valueComponent = text().content(activity.getDataValue()).color(WHITE);
        if (activity.getDataType().equals(Activity.DATA_TYPE_URL)) {
            valueComponent.clickEvent(ClickEvent.openUrl(activity.getDataValue()));
        }

        reply.add(text()
                .content("  ")
                .append(text(Character.toUpperCase(activity.getDataType().charAt(0)) + activity.getDataType().substring(1) + ": ", GRAY))
                .append(valueComponent)
                .build()
        );
        reply.add(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<Activity> log = platform.getActivityLog().getLog();
                    log.removeIf(Activity::shouldExpire);

                    if (log.isEmpty()) {
                        resp.replyPrefixed(text("There are no entries present in the log."));
                        return;
                    }

                    int page = Math.max(1, arguments.intFlag("page"));

                    Pagination<Activity> activityPagination = this.pagination.build(
                            text("Recent spark activity", GOLD),
                            this,
                            value -> "/" + platform.getPlugin().getCommandName() + " activity --page " + value
                    );
                    resp.reply(activityPagination.render(log, page));
                })
                .tabCompleter((platform, sender, arguments) -> TabCompleter.completeForOpts(arguments, "--page"))
                .build()
        );
    }

    private static String formatDateDiff(long time) {
        long seconds = (System.currentTimeMillis() - time) / 1000;

        if (seconds <= 0) {
            return "now";
        }

        long minute = seconds / 60;
        seconds = seconds % 60;
        long hour = minute / 60;
        minute = minute % 60;
        long day = hour / 24;
        hour = hour % 24;

        StringBuilder sb = new StringBuilder();
        if (day != 0) {
            sb.append(day).append("d ");
        }
        if (hour != 0) {
            sb.append(hour).append("h ");
        }
        if (minute != 0) {
            sb.append(minute).append("m ");
        }
        if (seconds != 0) {
            sb.append(seconds).append("s");
        }

        return sb.toString().trim() + " ago";
    }

}