Skip to content

Commit 6227fa3

Browse files
committed
Simplify commands
1 parent 1b57a89 commit 6227fa3

File tree

4 files changed

+59
-75
lines changed

4 files changed

+59
-75
lines changed

eternalcore-api/src/main/java/com/eternalcode/core/feature/privatechat/toggle/PrivateChatState.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,10 @@ public enum PrivateChatState {
1313
/**
1414
* Player cannot receive private messages.
1515
*/
16-
DISABLE
16+
DISABLE;
17+
18+
PrivateChatState invert() {
19+
return this == ENABLE ? DISABLE : ENABLE;
20+
}
21+
1722
}

eternalcore-api/src/main/java/com/eternalcode/core/feature/privatechat/toggle/PrivateChatStateService.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,13 @@ public interface PrivateChatStateService {
2424
*/
2525
CompletableFuture<Void> setChatState(UUID playerUniqueId, PrivateChatState state);
2626

27+
28+
/**
29+
* Toggle blocking of incoming private messages.
30+
*
31+
* @param playerUniqueId player's UUID.
32+
* @return new state of player's private chat messages blocking.
33+
*/
34+
CompletableFuture<PrivateChatState> toggleChatState(UUID playerUniqueId);
35+
2736
}

eternalcore-core/src/main/java/com/eternalcode/core/feature/privatechat/toggle/PrivateChatStateCommand.java

Lines changed: 31 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -29,101 +29,60 @@ public PrivateChatStateCommand(PrivateChatStateService privateChatStateService,
2929
@Execute
3030
@DescriptionDocs(description = "Toggle receiving private messages")
3131
public void execute(@Context Player sender) {
32-
this.privateChatStateService.getChatState(sender.getUniqueId()).thenAccept(presentState -> {
33-
if (presentState == PrivateChatState.DISABLE) {
34-
this.enable(sender.getUniqueId());
35-
}
36-
else {
37-
this.disable(sender.getUniqueId());
38-
}
39-
});
32+
UUID player = sender.getUniqueId();
33+
this.privateChatStateService.toggleChatState(player)
34+
.thenAccept(toggledState -> noticePlayer(toggledState, player));
4035
}
4136

42-
4337
@Execute
44-
@DescriptionDocs(description = "Switch receiving private messages", arguments = "<toggle>")
38+
@DescriptionDocs(description = "Switch receiving private messages", arguments = "<state>")
4539
public void execute(@Context Player sender, @Arg PrivateChatState state) {
46-
UUID uniqueId = sender.getUniqueId();
47-
48-
if (state == PrivateChatState.DISABLE) {
49-
this.disable(uniqueId);
50-
}
51-
else {
52-
this.enable(uniqueId);
53-
}
54-
40+
UUID player = sender.getUniqueId();
41+
this.privateChatStateService.setChatState(player, state)
42+
.thenAccept(ignored -> noticePlayer(state, player));
5543
}
5644

5745
@Execute
5846
@Permission("eternalcore.msgtoggle.other")
59-
@DescriptionDocs(description = "Switch receiving private messages for other player", arguments = "<player> <toggle>")
60-
public void other(@Context CommandSender sender, @Arg Player target, @Arg PrivateChatState state) {
61-
handleToggle(sender, target, state);
47+
@DescriptionDocs(description = "Switch receiving private messages for other player", arguments = "<player>")
48+
public void other(@Context CommandSender sender, @Arg Player target) {
49+
UUID player = target.getUniqueId();
50+
this.privateChatStateService.toggleChatState(player)
51+
.thenAccept(toggledState -> noticeOtherPlayer(sender, toggledState, target));
6252
}
6353

6454
@Execute
6555
@Permission("eternalcore.msgtoggle.other")
66-
@DescriptionDocs(description = "Switch receiving private messages for other player", arguments = "<player>")
67-
public void other(@Context CommandSender sender, @Arg Player target) {
68-
this.privateChatStateService.getChatState(target.getUniqueId()).thenAccept(presentState -> {
69-
if (presentState == PrivateChatState.DISABLE) {
70-
this.handleToggle(sender, target, PrivateChatState.ENABLE);
71-
}
72-
else {
73-
this.handleToggle(sender, target, PrivateChatState.DISABLE);
74-
}
75-
});
56+
@DescriptionDocs(description = "Switch receiving private messages for other player", arguments = "<player> <toggle>")
57+
public void other(@Context CommandSender sender, @Arg Player target, @Arg PrivateChatState state) {
58+
UUID player = target.getUniqueId();
59+
this.privateChatStateService.setChatState(player, state)
60+
.thenAccept(ignored -> noticeOtherPlayer(sender, state, target));
7661
}
7762

78-
private void handleToggle(CommandSender sender, Player target, @NotNull PrivateChatState desiredState) {
79-
UUID uniqueId = target.getUniqueId();
80-
81-
if (desiredState == PrivateChatState.DISABLE) {
82-
this.disable(uniqueId);
83-
if (!this.isCommandSenderSameAsTarget(sender, target)) {
84-
this.noticeService.create()
85-
.notice(translation -> translation.privateChat().otherMessagesDisabled())
86-
.sender(sender)
87-
.placeholder("{PLAYER}", target.getName())
88-
.send();
89-
}
90-
63+
private void noticeOtherPlayer(CommandSender sender, PrivateChatState state, Player target) {
64+
if (sender.equals(target)) {
9165
return;
9266
}
9367

94-
this.enable(uniqueId);
95-
if (!this.isCommandSenderSameAsTarget(sender, target)) {
96-
this.noticeService.create()
97-
.notice(translation -> translation.privateChat().otherMessagesEnabled())
98-
.sender(sender)
99-
.placeholder("{PLAYER}", target.getName())
100-
.send();
101-
}
102-
103-
}
104-
105-
private void enable(UUID uniqueId) {
106-
this.privateChatStateService.setChatState(uniqueId, PrivateChatState.ENABLE);
107-
10868
this.noticeService.create()
109-
.notice(translation -> translation.privateChat().selfMessagesEnabled())
110-
.player(uniqueId)
69+
.sender(sender)
70+
.notice(translation -> state == PrivateChatState.DISABLE
71+
? translation.privateChat().otherMessagesDisabled()
72+
: translation.privateChat().otherMessagesEnabled()
73+
)
74+
.placeholder("{PLAYER}", target.getName())
11175
.send();
11276
}
11377

114-
private void disable(UUID uniqueId) {
115-
this.privateChatStateService.setChatState(uniqueId, PrivateChatState.DISABLE);
116-
78+
private void noticePlayer(PrivateChatState state, UUID player) {
11779
this.noticeService.create()
118-
.notice(translation -> translation.privateChat().selfMessagesDisabled())
119-
.player(uniqueId)
80+
.player(player)
81+
.notice(translation -> state == PrivateChatState.ENABLE
82+
? translation.privateChat().selfMessagesEnabled()
83+
: translation.privateChat().selfMessagesDisabled()
84+
)
12085
.send();
12186
}
12287

123-
private boolean isCommandSenderSameAsTarget(CommandSender context, Player player) {
124-
if (context instanceof Player commandSender) {
125-
return commandSender.getUniqueId().equals(player.getUniqueId());
126-
}
127-
return false;
128-
}
12988
}

eternalcore-core/src/main/java/com/eternalcode/core/feature/privatechat/toggle/PrivateChatStateServiceImpl.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@
55
import java.util.UUID;
66
import java.util.concurrent.CompletableFuture;
77
import java.util.concurrent.ConcurrentHashMap;
8+
import java.util.concurrent.ConcurrentSkipListMap;
89

910
@Service
10-
public class PrivateChatStateServiceImpl implements PrivateChatStateService {
11+
class PrivateChatStateServiceImpl implements PrivateChatStateService {
1112

1213
private final PrivateChatStateRepository msgToggleRepository;
1314
private final ConcurrentHashMap<UUID, PrivateChatState> cachedToggleStates;
1415

1516
@Inject
16-
public PrivateChatStateServiceImpl(PrivateChatStateRepository msgToggleRepository) {
17+
PrivateChatStateServiceImpl(PrivateChatStateRepository msgToggleRepository) {
1718
this.cachedToggleStates = new ConcurrentHashMap<>();
1819
this.msgToggleRepository = msgToggleRepository;
20+
1921
}
2022

2123

@@ -38,4 +40,13 @@ public CompletableFuture<Void> setChatState(UUID playerUniqueId, PrivateChatStat
3840
return null;
3941
});
4042
}
43+
44+
@Override
45+
public CompletableFuture<PrivateChatState> toggleChatState(UUID playerUniqueId) {
46+
return this.getChatState(playerUniqueId).thenCompose(state -> {
47+
PrivateChatState newState = state.invert();
48+
return this.setChatState(playerUniqueId, newState)
49+
.thenApply(aVoid -> newState);
50+
});
51+
}
4152
}

0 commit comments

Comments
 (0)