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

import net.minecraft.network.MessageType;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Util;

/**
 * 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.sendMessage(text, MessageType.SYSTEM, Util.NIL_UUID);
            }

            @Override
            public Object getIdentifier() {
                return serverPlayerEntity.getUuid();
            }
        };
    }
}