summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/notification/NotificationManager.java
blob: 92c119bc7cd09ead1d2c4c64eae9b84df0c85404 (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
package com.keuin.kbackupfabric.notification;

import com.keuin.kbackupfabric.metadata.BackupMetadata;
import com.keuin.kbackupfabric.metadata.MetadataHolder;
import com.keuin.kbackupfabric.util.DateUtil;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Style;
import net.minecraft.util.Formatting;

import java.util.HashSet;
import java.util.Set;

/**
 * Notify some users when the server has been restored to a backup.
 */
public class NotificationManager {

    public static final NotificationManager INSTANCE = new NotificationManager();

    private final Set<Object> notified = new HashSet<>();

    private NotificationManager() {
    }

    public void notifyPlayer(DistinctNotifiable distinctNotifiable) {
        Object identifier = distinctNotifiable.getIdentifier();
        if (distinctNotifiable.isPrivileged() && !notified.contains(identifier)) {
            notified.add(identifier);
            notify(distinctNotifiable);
        }
    }

    /**
     * Just notify if necessary. It will not update the set.
     */
    private void notify(DistinctNotifiable notifiable) {
        if (MetadataHolder.hasMetadata()) {
            BackupMetadata backup = MetadataHolder.getMetadata();
            notifiable.notify(
                    new LiteralText("The world has been restored to backup ")
                            .append(new LiteralText("[" + backup.getBackupName() + "]").setStyle(Style.EMPTY.withColor(Formatting.GREEN)))
                            .append(new LiteralText(" (created at "))
                            .append(new LiteralText("[" + DateUtil.fromEpochMillis(backup.getBackupTime()) + "]").setStyle(Style.EMPTY.withColor(Formatting.GREEN)))
                            .append(new LiteralText(")"))
            );
        }
    }

}