diff options
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/notification')
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/notification/DistinctNotifiable.java | 45 | ||||
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/notification/NotificationManager.java | 49 |
2 files changed, 94 insertions, 0 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/notification/DistinctNotifiable.java b/src/main/java/com/keuin/kbackupfabric/notification/DistinctNotifiable.java new file mode 100644 index 0000000..b44bfce --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/notification/DistinctNotifiable.java @@ -0,0 +1,45 @@ +package com.keuin.kbackupfabric.notification; + +import net.minecraft.network.MessageType; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.text.Text; + +/** + * Decouple from ServerPlayerEntity, in case further migration to other APIs. + */ +public interface DistinctNotifiable { + + /** + * Does the receiver has privilege to receive some special message. + */ + boolean isPrivileged(); + + void notify(Text text); + + /** + * Get an unique, non-null object that identifies this notifiable instance. + * The identifier must be immutable and implement their own equals method. + * + * @return the identifier. + */ + Object getIdentifier(); + + static DistinctNotifiable fromServerPlayerEntity(ServerPlayerEntity serverPlayerEntity) { + return new DistinctNotifiable() { + @Override + public boolean isPrivileged() { + return serverPlayerEntity.server.getPermissionLevel(serverPlayerEntity.getGameProfile()) >= serverPlayerEntity.server.getOpPermissionLevel(); + } + + @Override + public void notify(Text text) { + serverPlayerEntity.sendChatMessage(text, MessageType.SYSTEM); + } + + @Override + public Object getIdentifier() { + return serverPlayerEntity.getUuid(); + } + }; + } +} 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(")")) + ); + } + } + +} |