@@ -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}
0 commit comments