-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathChatUserImpl.java
More file actions
205 lines (167 loc) · 5.01 KB
/
ChatUserImpl.java
File metadata and controls
205 lines (167 loc) · 5.01 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package at.helpch.chatchat.user;
import at.helpch.chatchat.ChatChatPlugin;
import at.helpch.chatchat.api.channel.Channel;
import at.helpch.chatchat.api.format.Format;
import at.helpch.chatchat.api.user.ChatUser;
import at.helpch.chatchat.api.user.User;
import at.helpch.chatchat.cache.ExpiringCache;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.identity.Identity;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
public final class ChatUserImpl implements ChatUser {
public ChatUserImpl(@NotNull final UUID uuid) {
this.uuid = uuid;
}
private final ExpiringCache<ChatUser> lastMessagedUser =
new ExpiringCache<>(ChatChatPlugin.cacheDuration(), TimeUnit.SECONDS);
private final UUID uuid;
private Channel channel;
// TODO: 8/9/22 Remove unused field!
private Format format;
private boolean privateMessages = true;
private boolean personalMentions = true;
private boolean channelMentions = true;
private boolean socialSpy = false;
private boolean chatEnabled = true;
private boolean rangedChat = false;
private Set<UUID> ignoredUsers = new HashSet<>();
@Override
public @NotNull Channel channel() {
return channel;
}
@Override
public void channel(@NotNull final Channel channel) {
this.channel = channel;
}
@Override
public @NotNull Format format() {
return format;
}
@Override
public void format(@NotNull final Format format) {
this.format = format;
}
@Override
public @NotNull UUID uuid() {
return uuid;
}
@Override
public boolean hasPermission(@NotNull final String node) {
return player().map(value -> value.hasPermission(node)).orElse(false);
}
@Override
public @NotNull Optional<ChatUser> lastMessagedUser() {
return lastMessagedUser.get();
}
@Override
public void lastMessagedUser(@Nullable final ChatUser user) {
lastMessagedUser.put(user);
}
@Override
public boolean privateMessages() {
return privateMessages;
}
@Override
public void privateMessages(final boolean enabled) {
this.privateMessages = enabled;
}
@Override
public boolean personalMentions() {
return personalMentions;
}
@Override
public void personalMentions(boolean receivesPersonalMentions) {
this.personalMentions = receivesPersonalMentions;
}
@Override
public boolean channelMentions() {
return channelMentions;
}
@Override
public void channelMentions(boolean receivesChannelMentions) {
this.channelMentions = receivesChannelMentions;
}
public void socialSpy(final boolean enabled) {
socialSpy = enabled;
}
@Override
public boolean socialSpy() {
return socialSpy;
}
@Override
public @NotNull Set<UUID> ignoredUsers() {
return ignoredUsers;
}
@Override
public void ignoredUsers(@NotNull Set<UUID> users) {
this.ignoredUsers = users;
}
@Override
public void ignoreUser(@NotNull User user) {
ignoredUsers.add(user.uuid());
}
@Override
public void unignoreUser(@NotNull User user) {
ignoredUsers.remove(user.uuid());
}
@Override
public void chatState(final boolean enabled) {
this.chatEnabled = enabled;
}
@Override
public boolean chatEnabled() {
return this.chatEnabled;
}
@Override
public boolean rangedChat() {
return rangedChat;
}
@Override
public void rangedChat(final boolean enabled) {
this.rangedChat = enabled;
}
@Override
public boolean canSee(@NotNull final User target) {
if (!(target instanceof ChatUser)) {
return true;
}
final var chatUser = (ChatUser) target;
final var plugin = JavaPlugin.getPlugin(ChatChatPlugin.class);
return plugin.hookManager()
.vanishHooks()
.stream()
.filter(Objects::nonNull)
.allMatch(hook -> hook.canSee(this, chatUser));
}
@Override
public @NotNull Optional<Player> player() {
return Optional.ofNullable(Bukkit.getPlayer(uuid));
}
@Override
public @NotNull Audience audience() {
return player().map(player -> (Audience) player).orElse(Audience.empty());
}
@Override
public @NotNull Identity identity() {
return Identity.identity(uuid);
}
@Override
public String toString() {
return "ChatUserImpl{" +
"uuid=" + uuid +
", lastMessaged=" + lastMessagedUser().map(ChatUser::uuid) +
", channel=" + channel +
", format=" + format +
'}';
}
}