-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatManager.java
More file actions
550 lines (488 loc) · 17.8 KB
/
Copy pathChatManager.java
File metadata and controls
550 lines (488 loc) · 17.8 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
package com.hyperfactions.manager;
import com.hyperfactions.Permissions;
import com.hyperfactions.api.events.*;
import com.hyperfactions.config.ConfigManager;
import com.hyperfactions.data.ChatMessage;
import com.hyperfactions.data.Faction;
import com.hyperfactions.data.FactionRelation;
import com.hyperfactions.gui.ActivePageTracker;
import com.hyperfactions.gui.GuiUpdateService;
import com.hyperfactions.integration.PermissionManager;
import com.hyperfactions.util.ErrorHandler;
import com.hyperfactions.util.HFMessages;
import com.hyperfactions.util.Logger;
import com.hyperfactions.util.CommonKeys;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Function;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Manages chat channels for faction and ally chat.
* Players can toggle between normal, faction, and ally chat modes.
* Integrates with ChatHistoryManager for message persistence and
* GuiUpdateService for real-time GUI refresh.
*/
public class ChatManager {
/**
* Chat channel types.
*/
public enum ChatChannel {
NORMAL,
FACTION,
ALLY
}
/**
* Result of a chat channel toggle operation.
*/
public enum ChatResult {
SUCCESS,
NO_PERMISSION
}
/**
* Result of toggling a chat channel.
*
* @param result the result of the operation
* @param channel the new channel if successful, null otherwise
*/
public record ToggleResult(
@NotNull ChatResult result,
@Nullable ChatChannel channel
) {
/** Checks if success. */
public boolean isSuccess() {
return result == ChatResult.SUCCESS;
}
}
/**
* Listener for chat messages. Called after each message broadcast.
* Useful for external integrations (e.g., Discord bridge).
*/
@FunctionalInterface
public interface ChatMessageListener {
void onMessage(@NotNull ChatMessage message, @NotNull UUID factionId);
}
// Player UUID -> current chat channel
private final Map<UUID, ChatChannel> playerChannels = new ConcurrentHashMap<>();
private final FactionManager factionManager;
private final RelationManager relationManager;
private final Function<UUID, PlayerRef> playerLookup;
private @Nullable ChatHistoryManager chatHistoryManager;
private @Nullable GuiUpdateService guiUpdateService;
private final List<ChatMessageListener> messageListeners = new CopyOnWriteArrayList<>();
/**
* Creates a new ChatManager.
*
* @param factionManager the faction manager
* @param relationManager the relation manager
* @param playerLookup function to look up online PlayerRef by UUID
*/
public ChatManager(@NotNull FactionManager factionManager,
@NotNull RelationManager relationManager,
@NotNull Function<UUID, PlayerRef> playerLookup) {
this.factionManager = factionManager;
this.relationManager = relationManager;
this.playerLookup = playerLookup;
}
/**
* Sets the chat history manager for message persistence.
*
* @param chatHistoryManager the history manager
*/
public void setChatHistoryManager(@Nullable ChatHistoryManager chatHistoryManager) {
this.chatHistoryManager = chatHistoryManager;
}
/**
* Sets the GUI update service for real-time page refresh.
*
* @param guiUpdateService the GUI update service
*/
public void setGuiUpdateService(@Nullable GuiUpdateService guiUpdateService) {
this.guiUpdateService = guiUpdateService;
}
/**
* Adds a listener that is notified after each faction/ally message is broadcast.
*
* @param listener the listener
*/
public void addMessageListener(@NotNull ChatMessageListener listener) {
messageListeners.add(listener);
}
/**
* Removes a message listener.
*
* @param listener the listener to remove
*/
public void removeMessageListener(@NotNull ChatMessageListener listener) {
messageListeners.remove(listener);
}
// ============================================================
// Channel Management
// ============================================================
/**
* Gets a player's current chat channel.
*
* @param playerUuid the player's UUID
* @return the current channel (defaults to NORMAL)
*/
@NotNull
public ChatChannel getChannel(@NotNull UUID playerUuid) {
return playerChannels.getOrDefault(playerUuid, ChatChannel.NORMAL);
}
/**
* Sets a player's chat channel.
*
* @param playerUuid the player's UUID
* @param channel the channel to set
*/
public void setChannel(@NotNull UUID playerUuid, @NotNull ChatChannel channel) {
if (channel == ChatChannel.NORMAL) {
playerChannels.remove(playerUuid);
} else {
playerChannels.put(playerUuid, channel);
}
}
/**
* Cycles through chat channels: Normal -> Faction -> Ally -> Normal,
* skipping modes the player lacks permission for.
*
* @param playerUuid the player's UUID
* @return the result with new channel state
*/
@NotNull
public ToggleResult cycleChannelChecked(@NotNull UUID playerUuid) {
ChatChannel current = getChannel(playerUuid);
boolean hasFaction = PermissionManager.get().hasPermission(playerUuid, Permissions.CHAT_FACTION);
boolean hasAlly = PermissionManager.get().hasPermission(playerUuid, Permissions.CHAT_ALLY);
ChatChannel next = switch (current) {
case NORMAL -> {
if (hasFaction) {
yield ChatChannel.FACTION;
}
if (hasAlly) {
yield ChatChannel.ALLY;
}
yield ChatChannel.NORMAL;
}
case FACTION -> {
if (hasAlly) {
yield ChatChannel.ALLY;
}
yield ChatChannel.NORMAL;
}
case ALLY -> ChatChannel.NORMAL;
};
setChannel(playerUuid, next);
return new ToggleResult(ChatResult.SUCCESS, next);
}
/**
* Sets a player directly to faction chat mode with permission check.
*
* @param playerUuid the player's UUID
* @return the toggle result
*/
@NotNull
public ToggleResult setFactionChatChecked(@NotNull UUID playerUuid) {
if (!PermissionManager.get().hasPermission(playerUuid, Permissions.CHAT_FACTION)) {
return new ToggleResult(ChatResult.NO_PERMISSION, null);
}
setChannel(playerUuid, ChatChannel.FACTION);
return new ToggleResult(ChatResult.SUCCESS, ChatChannel.FACTION);
}
/**
* Sets a player directly to ally chat mode with permission check.
*
* @param playerUuid the player's UUID
* @return the toggle result
*/
@NotNull
public ToggleResult setAllyChatChecked(@NotNull UUID playerUuid) {
if (!PermissionManager.get().hasPermission(playerUuid, Permissions.CHAT_ALLY)) {
return new ToggleResult(ChatResult.NO_PERMISSION, null);
}
setChannel(playerUuid, ChatChannel.ALLY);
return new ToggleResult(ChatResult.SUCCESS, ChatChannel.ALLY);
}
/**
* Sets a player's chat to normal mode (no permission check needed).
*
* @param playerUuid the player's UUID
*/
public void setNormalChat(@NotNull UUID playerUuid) {
setChannel(playerUuid, ChatChannel.NORMAL);
}
/**
* Toggles faction chat for a player with permission check.
*
* @param playerUuid the player's UUID
* @return the result with new channel state if successful
*/
@NotNull
public ToggleResult toggleFactionChatChecked(@NotNull UUID playerUuid) {
if (!PermissionManager.get().hasPermission(playerUuid, Permissions.CHAT_FACTION)) {
return new ToggleResult(ChatResult.NO_PERMISSION, null);
}
ChatChannel newChannel = toggleFactionChat(playerUuid);
return new ToggleResult(ChatResult.SUCCESS, newChannel);
}
/**
* Toggles faction chat for a player.
*
* @param playerUuid the player's UUID
* @return the new channel state
*/
@NotNull
public ChatChannel toggleFactionChat(@NotNull UUID playerUuid) {
ChatChannel current = getChannel(playerUuid);
if (current == ChatChannel.FACTION) {
setChannel(playerUuid, ChatChannel.NORMAL);
return ChatChannel.NORMAL;
} else {
setChannel(playerUuid, ChatChannel.FACTION);
return ChatChannel.FACTION;
}
}
/**
* Toggles ally chat for a player with permission check.
*
* @param playerUuid the player's UUID
* @return the result with new channel state if successful
*/
@NotNull
public ToggleResult toggleAllyChatChecked(@NotNull UUID playerUuid) {
if (!PermissionManager.get().hasPermission(playerUuid, Permissions.CHAT_ALLY)) {
return new ToggleResult(ChatResult.NO_PERMISSION, null);
}
ChatChannel newChannel = toggleAllyChat(playerUuid);
return new ToggleResult(ChatResult.SUCCESS, newChannel);
}
/**
* Toggles ally chat for a player.
*
* @param playerUuid the player's UUID
* @return the new channel state
*/
@NotNull
public ChatChannel toggleAllyChat(@NotNull UUID playerUuid) {
ChatChannel current = getChannel(playerUuid);
if (current == ChatChannel.ALLY) {
setChannel(playerUuid, ChatChannel.NORMAL);
return ChatChannel.NORMAL;
} else {
setChannel(playerUuid, ChatChannel.ALLY);
return ChatChannel.ALLY;
}
}
/**
* Resets a player's chat channel to normal.
* Call when player leaves faction or disconnects.
*
* @param playerUuid the player's UUID
*/
public void resetChannel(@NotNull UUID playerUuid) {
playerChannels.remove(playerUuid);
}
// ============================================================
// Message Processing
// ============================================================
/**
* Processes a chat message based on the player's current channel.
* Returns true if the message was handled (faction/ally chat), false if normal chat.
*
* @param sender the sender's PlayerRef
* @param message the message content
* @return true if message was handled as faction/ally chat
*/
public boolean processChatMessage(@NotNull PlayerRef sender, @NotNull String message) {
UUID senderUuid = sender.getUuid();
ChatChannel channel = getChannel(senderUuid);
if (channel == ChatChannel.NORMAL) {
return false;
}
Faction senderFaction = factionManager.getPlayerFaction(senderUuid);
if (senderFaction == null) {
resetChannel(senderUuid);
return false;
}
if (channel == ChatChannel.FACTION) {
FactionChatEvent.Channel eventChannel = FactionChatEvent.Channel.FACTION;
if (EventBus.publishCancellable(new FactionChatPreEvent(senderUuid, senderFaction.id(), eventChannel, message))) {
return true; // Message "handled" but blocked
}
sendFactionMessage(sender, senderFaction, message);
EventBus.publish(new FactionChatEvent(senderUuid, senderFaction.id(), eventChannel, message));
return true;
} else if (channel == ChatChannel.ALLY) {
FactionChatEvent.Channel eventChannel = FactionChatEvent.Channel.ALLY;
if (EventBus.publishCancellable(new FactionChatPreEvent(senderUuid, senderFaction.id(), eventChannel, message))) {
return true; // Message "handled" but blocked
}
sendAllyMessage(sender, senderFaction, message);
EventBus.publish(new FactionChatEvent(senderUuid, senderFaction.id(), eventChannel, message));
return true;
}
return false;
}
/**
* Sends a message from the GUI on a specific channel.
* Used by the chat history page to send messages without toggling the player's mode.
*
* @param sender the sender's PlayerRef
* @param faction the sender's faction
* @param channel the target channel
* @param message the message text
*/
public void sendFromGui(@NotNull PlayerRef sender, @NotNull Faction faction,
@NotNull ChatMessage.Channel channel, @NotNull String message) {
FactionChatEvent.Channel eventChannel = (channel == ChatMessage.Channel.FACTION)
? FactionChatEvent.Channel.FACTION : FactionChatEvent.Channel.ALLY;
if (EventBus.publishCancellable(new FactionChatPreEvent(sender.getUuid(), faction.id(), eventChannel, message))) {
return; // Message blocked by listener
}
if (channel == ChatMessage.Channel.FACTION) {
sendFactionMessage(sender, faction, message);
} else {
sendAllyMessage(sender, faction, message);
}
EventBus.publish(new FactionChatEvent(sender.getUuid(), faction.id(), eventChannel, message));
}
/**
* Sends a message to all faction members.
*/
private void sendFactionMessage(@NotNull PlayerRef sender, @NotNull Faction faction, @NotNull String message) {
ConfigManager config = ConfigManager.get();
String prefix = config.getFactionChatPrefix();
String prefixColor = config.getFactionChatColor();
String nameColor = config.getSenderNameColor();
String msgColor = config.getMessageColor();
Message formatted = Message.raw(prefix + " ").color(prefixColor)
.insert(Message.raw(sender.getUsername()).color(nameColor))
.insert(Message.raw(": ").color("#AAAAAA"))
.insert(Message.raw(message).color(msgColor));
// Send to all online faction members
for (UUID memberUuid : faction.members().keySet()) {
PlayerRef member = playerLookup.apply(memberUuid);
if (member != null) {
member.sendMessage(formatted);
}
}
// Record in history
String tag = faction.tag() != null ? faction.tag() : faction.name();
ChatMessage chatMessage = ChatMessage.create(
sender.getUuid(), sender.getUsername(), tag,
ChatMessage.Channel.FACTION, message);
if (chatHistoryManager != null) {
chatHistoryManager.recordMessage(faction.id(), chatMessage);
}
// Notify listeners
notifyListeners(chatMessage, faction.id());
// Refresh chat GUI pages for faction members
if (guiUpdateService != null) {
guiUpdateService.onChatMessage(faction.id());
}
Logger.debug("[FChat] %s: %s", sender.getUsername(), message);
}
/**
* Sends a message to all faction and ally faction members.
*/
private void sendAllyMessage(@NotNull PlayerRef sender, @NotNull Faction faction, @NotNull String message) {
ConfigManager config = ConfigManager.get();
String prefix = config.getAllyChatPrefix();
String prefixColor = config.getAllyChatColor();
String nameColor = config.getSenderNameColor();
String msgColor = config.getMessageColor();
String tag = faction.tag() != null ? faction.tag() : faction.name();
Message formatted = Message.raw(prefix + " ").color(prefixColor)
.insert(Message.raw("[" + tag + "] ").color("#AAAAAA"))
.insert(Message.raw(sender.getUsername()).color(nameColor))
.insert(Message.raw(": ").color("#AAAAAA"))
.insert(Message.raw(message).color(msgColor));
// Send to sender's faction members
for (UUID memberUuid : faction.members().keySet()) {
PlayerRef member = playerLookup.apply(memberUuid);
if (member != null) {
member.sendMessage(formatted);
}
}
// Send to all ally faction members
for (UUID allyFactionId : faction.relations().keySet()) {
FactionRelation relation = faction.relations().get(allyFactionId);
if (relation != null && relation.isAlly()) {
Faction allyFaction = factionManager.getFaction(allyFactionId);
if (allyFaction != null) {
for (UUID allyMemberUuid : allyFaction.members().keySet()) {
PlayerRef allyMember = playerLookup.apply(allyMemberUuid);
if (allyMember != null) {
allyMember.sendMessage(formatted);
}
}
}
}
}
// Record in sender's faction history only (ally tab merges at read time)
ChatMessage chatMessage = ChatMessage.create(
sender.getUuid(), sender.getUsername(), tag,
ChatMessage.Channel.ALLY, message);
if (chatHistoryManager != null) {
chatHistoryManager.recordMessage(faction.id(), chatMessage);
}
// Notify listeners
notifyListeners(chatMessage, faction.id());
// Refresh chat GUI pages for sender's faction + all allies
if (guiUpdateService != null) {
guiUpdateService.onChatMessage(faction.id());
for (UUID allyFactionId : faction.relations().keySet()) {
FactionRelation relation = faction.relations().get(allyFactionId);
if (relation != null && relation.isAlly()) {
guiUpdateService.onChatMessage(allyFactionId);
}
}
}
Logger.debug("[AChat] %s: %s", sender.getUsername(), message);
}
private void notifyListeners(@NotNull ChatMessage message, @NotNull UUID factionId) {
for (ChatMessageListener listener : messageListeners) {
try {
listener.onMessage(message, factionId);
} catch (Exception e) {
ErrorHandler.report("Chat message listener threw exception", e);
}
}
}
// ============================================================
// Display Helpers
// ============================================================
/**
* Gets a display string for a chat channel.
*
* @param channel the channel
* @return display string
*/
@NotNull
public static String getChannelDisplay(@NotNull ChatChannel channel) {
return switch (channel) {
case NORMAL -> HFMessages.get((PlayerRef) null, CommonKeys.ChatDisplay.PUBLIC);
case FACTION -> HFMessages.get((PlayerRef) null, CommonKeys.ChatDisplay.FACTION);
case ALLY -> HFMessages.get((PlayerRef) null, CommonKeys.ChatDisplay.ALLY);
};
}
/**
* Gets the color for a chat channel from config.
*
* @param channel the channel
* @return hex color string
*/
@NotNull
public static String getChannelColor(@NotNull ChatChannel channel) {
return switch (channel) {
case NORMAL -> "#FFFFFF";
case FACTION -> ConfigManager.get().getFactionChatColor();
case ALLY -> ConfigManager.get().getAllyChatColor();
};
}
}