diff options
author | Keuin <[email protected]> | 2021-01-29 18:54:53 +0800 |
---|---|---|
committer | keuin <[email protected]> | 2021-01-29 18:54:53 +0800 |
commit | 602fb9e88685041abac73f868cb697123e15865c (patch) | |
tree | fab97907433b15ab88a55af03c88f6f22ce90676 /src/main/java/com/keuin/kbackupfabric/notification/NotificationManager.java | |
parent | b0208136e0dab098d8270b56b65521e13ff3d732 (diff) |
Add Op login hint in the next start after restoring
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/notification/NotificationManager.java')
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/notification/NotificationManager.java | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/notification/NotificationManager.java b/src/main/java/com/keuin/kbackupfabric/notification/NotificationManager.java new file mode 100644 index 0000000..96ecf41 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/notification/NotificationManager.java @@ -0,0 +1,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 server has been restored to backup ") + .append(new LiteralText("[" + backup.getBackupName() + "]").setStyle(new Style().setColor(Formatting.GREEN))) + .append(new LiteralText(" (created at ")) + .append(new LiteralText("[" + DateUtil.fromEpochMillis(backup.getBackupTime()) + "]").setStyle(new Style().setColor(Formatting.GREEN))) + .append(new LiteralText(")")) + ); + } + } + +} |