Skip to content

Commit cea8351

Browse files
mydelkoniveaP1otrullaRollcziimDMKvLuckyyy
authored
GH-1041 Refactor AdminChat command to support admin chat channel. (#1063)
* upgrade admin chat command * Update AdminChatManagerController.java * Update AdminChatCommand.java * Update AdminChatManagerController.java * Update eternalcore-core/src/main/java/com/eternalcode/core/feature/adminchat/AdminTask.java Co-authored-by: Piotr Zych <[email protected]> * Refactor admin chat to use persistent mode and permission constants * isPersistentChat -> hasPersistentChat * Rename some methods * playerName -> sender * Review, apply rollczi suggestion. --------- Co-authored-by: Piotr Zych <[email protected]> Co-authored-by: Rollczi <[email protected]> Co-authored-by: DMK <[email protected]> Co-authored-by: vLuckyyy <[email protected]>
1 parent d405285 commit cea8351

File tree

10 files changed

+382
-52
lines changed

10 files changed

+382
-52
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.eternalcode.core.feature.adminchat;
2+
3+
import java.util.Collection;
4+
import java.util.UUID;
5+
import org.bukkit.command.CommandSender;
6+
import org.jetbrains.annotations.NotNull;
7+
import org.jetbrains.annotations.Unmodifiable;
8+
9+
/**
10+
* Service responsible for managing admin chat functionality.
11+
*/
12+
public interface AdminChatService {
13+
14+
/**
15+
* Toggles the persistent admin chat mode for the specified player.
16+
*
17+
* <p>When persistent admin chat is enabled, all messages sent by the player
18+
* will be automatically redirected to admin chat instead of public chat.
19+
*
20+
* @param playerUuid the UUID of the player to toggle admin chat for
21+
* @return {@code true} if persistent chat was enabled, {@code false} if it was disabled
22+
*/
23+
boolean toggleChat(@NotNull UUID playerUuid);
24+
25+
/**
26+
* Checks if the persistent admin chat mode is enabled for the specified player.
27+
*
28+
* @param playerUuid the UUID of the player to check
29+
* @return {@code true} if persistent chat is enabled, {@code false} otherwise
30+
*/
31+
boolean hasEnabledChat(@NotNull UUID playerUuid);
32+
33+
/**
34+
* Retrieves all players who currently have persistent admin chat enabled.
35+
*
36+
* <p>The returned collection is unmodifiable and represents the current state
37+
* at the time of the call. Changes to the underlying data will not be reflected
38+
* in the returned collection.
39+
*
40+
* @return an unmodifiable collection of player UUIDs with enabled admin chat
41+
*/
42+
@NotNull
43+
@Unmodifiable
44+
Collection<UUID> getPlayersWithEnabledChat();
45+
46+
/**
47+
* Sends an admin chat message to all players with appropriate permissions.
48+
*
49+
* <p>This method will trigger an {@link com.eternalcode.core.feature.adminchat.event.AdminChatEvent}
50+
* before sending the message. If the event is cancelled, the message will not be sent.
51+
*
52+
* @param message the message content to send
53+
* @param sender the command sender who is sending the message
54+
*/
55+
void sendAdminChatMessage(@NotNull String message, @NotNull CommandSender sender);
56+
57+
/**
58+
* Enables persistent admin chat for the specified player.
59+
*
60+
* @param playerUuid the UUID of the player
61+
*/
62+
void enableChat(@NotNull UUID playerUuid);
63+
64+
/**
65+
* Disables persistent admin chat for the specified player.
66+
*
67+
* @param playerUuid the UUID of the player
68+
*/
69+
void disableChat(@NotNull UUID playerUuid);
70+
71+
/**
72+
* Checks if the specified command sender has permission to see admin chat messages.
73+
*
74+
* @param sender the command sender to check
75+
* @return {@code true} if the sender can see admin chat messages, {@code false} otherwise
76+
*/
77+
boolean canSeeAdminChat(@NotNull CommandSender sender);
78+
}

eternalcore-api/src/main/java/com/eternalcode/core/feature/adminchat/event/AdminChatEvent.java

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@
44
import org.bukkit.event.Cancellable;
55
import org.bukkit.event.Event;
66
import org.bukkit.event.HandlerList;
7+
import org.jetbrains.annotations.NotNull;
78

9+
/**
10+
* Event called when an admin chat message is being sent.
11+
*
12+
* <p>This event is triggered before the message is actually sent to all recipients.
13+
* Plugins can cancel this event to prevent the message from being sent, or modify
14+
* the message content before it's delivered.
15+
*/
816
public class AdminChatEvent extends Event implements Cancellable {
917

1018
private static final HandlerList HANDLER_LIST = new HandlerList();
@@ -13,22 +21,40 @@ public class AdminChatEvent extends Event implements Cancellable {
1321
private String content;
1422
private boolean cancelled;
1523

16-
public AdminChatEvent(CommandSender sender, String content) {
17-
super(false);
24+
public AdminChatEvent(@NotNull CommandSender sender, @NotNull String content) {
25+
super(true);
26+
27+
if (sender == null) {
28+
throw new IllegalArgumentException("Sender cannot be null");
29+
}
30+
if (content == null) {
31+
throw new IllegalArgumentException("Content cannot be null");
32+
}
1833

1934
this.sender = sender;
2035
this.content = content;
36+
this.cancelled = false;
37+
}
38+
39+
@NotNull
40+
public static HandlerList getHandlerList() {
41+
return HANDLER_LIST;
2142
}
2243

44+
@NotNull
2345
public CommandSender getSender() {
2446
return this.sender;
2547
}
2648

49+
@NotNull
2750
public String getContent() {
2851
return this.content;
2952
}
3053

31-
public void setContent(String content) {
54+
public void setContent(@NotNull String content) {
55+
if (content == null) {
56+
throw new IllegalArgumentException("Content cannot be null");
57+
}
3258
this.content = content;
3359
}
3460

@@ -38,16 +64,13 @@ public boolean isCancelled() {
3864
}
3965

4066
@Override
41-
public void setCancelled(boolean cancel) {
42-
this.cancelled = cancel;
67+
public void setCancelled(boolean cancelled) {
68+
this.cancelled = cancelled;
4369
}
4470

4571
@Override
72+
@NotNull
4673
public HandlerList getHandlers() {
4774
return HANDLER_LIST;
4875
}
49-
50-
public static HandlerList getHandlerList() {
51-
return HANDLER_LIST;
52-
}
5376
}
Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,59 @@
11
package com.eternalcode.core.feature.adminchat;
22

33
import com.eternalcode.annotations.scan.command.DescriptionDocs;
4-
import com.eternalcode.annotations.scan.permission.PermissionDocs;
5-
import com.eternalcode.core.event.EventCaller;
6-
import com.eternalcode.core.feature.adminchat.event.AdminChatEvent;
74
import com.eternalcode.core.injector.annotations.Inject;
85
import com.eternalcode.core.notice.NoticeService;
9-
import com.eternalcode.multification.notice.NoticeBroadcast;
106
import dev.rollczi.litecommands.annotations.command.Command;
117
import dev.rollczi.litecommands.annotations.context.Context;
128
import dev.rollczi.litecommands.annotations.execute.Execute;
139
import dev.rollczi.litecommands.annotations.join.Join;
1410
import dev.rollczi.litecommands.annotations.permission.Permission;
15-
import org.bukkit.Server;
1611
import org.bukkit.command.CommandSender;
12+
import org.bukkit.entity.Player;
13+
import org.jetbrains.annotations.NotNull;
1714

18-
@Command(name = "adminchat", aliases = "ac")
19-
@Permission("eternalcore.adminchat")
20-
@PermissionDocs(
21-
name = "Admin Chat spy",
22-
permission = AdminChatCommand.ADMIN_CHAT_SPY_PERMISSION,
23-
description = "Allows the player to see messages sent in admin chat by other players."
24-
)
25-
class AdminChatCommand {
26-
27-
static final String ADMIN_CHAT_SPY_PERMISSION = "eternalcore.adminchat.spy";
15+
@Command(name = "adminchat", aliases = {"ac"})
16+
@Permission(AdminChatPermissionConstant.ADMIN_CHAT_PERMISSION)
17+
final class AdminChatCommand {
2818

19+
private final AdminChatService adminChatService;
2920
private final NoticeService noticeService;
30-
private final EventCaller eventCaller;
31-
private final Server server;
3221

3322
@Inject
34-
AdminChatCommand(NoticeService noticeService, EventCaller eventCaller, Server server) {
23+
AdminChatCommand(@NotNull AdminChatService adminChatService, @NotNull NoticeService noticeService) {
24+
this.adminChatService = adminChatService;
3525
this.noticeService = noticeService;
36-
this.eventCaller = eventCaller;
37-
this.server = server;
3826
}
3927

4028
@Execute
41-
@DescriptionDocs(description = "Sends a message to all staff members with eternalcore.adminchat.spy permissions", arguments = "<message>")
42-
void execute(@Context CommandSender sender, @Join String message) {
43-
AdminChatEvent event = this.eventCaller.callEvent(new AdminChatEvent(sender, message));
29+
@DescriptionDocs(
30+
description = "Toggles persistent admin chat mode. When enabled, all your messages will be sent to admin chat."
31+
)
32+
void executeToggle(@Context @NotNull Player sender) {
33+
boolean enabled = this.adminChatService.toggleChat(sender.getUniqueId());
34+
35+
this.noticeService.create()
36+
.notice(translation -> enabled
37+
? translation.adminChat().enabled()
38+
: translation.adminChat().disabled())
39+
.player(sender.getUniqueId())
40+
.send();
41+
}
4442

45-
if (event.isCancelled()) {
43+
@Execute
44+
@DescriptionDocs(
45+
description = "Sends a message to all staff members with admin chat permissions.",
46+
arguments = "<message>"
47+
)
48+
void executeSendMessage(@Context @NotNull CommandSender sender, @Join @NotNull String message) {
49+
if (message.trim().isEmpty()) {
50+
this.noticeService.create()
51+
.notice(translation -> translation.argument().noArgument())
52+
.sender(sender)
53+
.send();
4654
return;
4755
}
4856

49-
String eventMessage = event.getContent();
50-
51-
NoticeBroadcast notice = this.noticeService.create()
52-
.console()
53-
.notice(translation -> translation.adminChat().format())
54-
.placeholder("{PLAYER}", sender.getName())
55-
.placeholder("{TEXT}", eventMessage);
56-
57-
this.server.getOnlinePlayers().stream()
58-
.filter(player -> player.hasPermission(ADMIN_CHAT_SPY_PERMISSION))
59-
.forEach(player -> notice.player(player.getUniqueId()));
60-
61-
notice.send();
57+
this.adminChatService.sendAdminChatMessage(message, sender);
6258
}
6359
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.eternalcode.core.feature.adminchat;
2+
3+
import com.eternalcode.annotations.scan.permission.PermissionDocs;
4+
5+
final class AdminChatPermissionConstant {
6+
7+
@PermissionDocs(
8+
name = "Admin Chat",
9+
permission = AdminChatPermissionConstant.ADMIN_CHAT_PERMISSION,
10+
description = "Allows the player to use admin chat commands and functionality."
11+
)
12+
static final String ADMIN_CHAT_PERMISSION = "eternalcore.adminchat";
13+
14+
@PermissionDocs(
15+
name = "Admin Chat See",
16+
permission = AdminChatPermissionConstant.ADMIN_CHAT_SEE_PERMISSION,
17+
description = "Allows the player to see messages sent in admin chat by other players."
18+
)
19+
static final String ADMIN_CHAT_SEE_PERMISSION = "eternalcore.adminchat.see";
20+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.eternalcode.core.feature.adminchat;
2+
3+
import com.eternalcode.core.injector.annotations.Inject;
4+
import com.eternalcode.core.injector.annotations.component.Task;
5+
import com.eternalcode.core.notice.NoticeService;
6+
import com.eternalcode.core.shared.OnlinePlayersRunnable;
7+
import java.util.concurrent.TimeUnit;
8+
import org.bukkit.entity.Player;
9+
10+
@Task(period = 1, delay = 1, unit = TimeUnit.SECONDS)
11+
final class AdminChatReminderTask extends OnlinePlayersRunnable {
12+
13+
private final AdminChatService adminChatService;
14+
private final NoticeService noticeService;
15+
16+
@Inject
17+
public AdminChatReminderTask(
18+
AdminChatService adminChatService,
19+
NoticeService noticeService
20+
) {
21+
this.adminChatService = adminChatService;
22+
this.noticeService = noticeService;
23+
}
24+
25+
@Override
26+
public void runFor(Player player) {
27+
if (!this.adminChatService.hasEnabledChat(player.getUniqueId())) {
28+
return;
29+
}
30+
31+
this.noticeService.create()
32+
.notice(translation -> translation.adminChat().enabledReminder())
33+
.player(player.getUniqueId())
34+
.send();
35+
}
36+
}

0 commit comments

Comments
 (0)