11
11
import dev .emortal .api .utils .callback .FunctionalFutureCallback ;
12
12
import dev .emortal .api .utils .resolvers .PlayerResolver ;
13
13
import dev .emortal .velocity .lang .TempLang ;
14
+ import dev .emortal .velocity .party .commands .PartyCommand ;
14
15
import io .grpc .Status ;
15
16
import io .grpc .protobuf .StatusProto ;
16
17
import net .kyori .adventure .text .Component ;
17
18
import net .kyori .adventure .text .format .NamedTextColor ;
19
+ import net .kyori .adventure .text .minimessage .MiniMessage ;
18
20
import net .kyori .adventure .text .minimessage .tag .resolver .Placeholder ;
19
21
import org .slf4j .Logger ;
20
22
import org .slf4j .LoggerFactory ;
23
25
24
26
public class PartyInviteSub {
25
27
private static final Logger LOGGER = LoggerFactory .getLogger (PartyInviteSub .class );
28
+ private static final MiniMessage MINI_MESSAGE = MiniMessage .miniMessage ();
29
+
30
+
31
+ private static final String INVITED_MESSAGE = "<green>Invited <username> to the party" ;
32
+ private static final String NO_PERMISSION_MESSAGE = "<red>You must be the leader of the party to invite another player" ;
33
+ private static final String ALREADY_INVITED_MESSAGE = "<red><username> has already been invited to your party" ;
34
+ private static final String ALREADY_IN_PARTY_MESSAGE = "<red><username> is already in the party" ;
35
+ private static final String ALREADY_IN_PARTY_OTHER_MESSAGE = "<red><username> is in another party" ;
36
+ private static final String PARTY_IS_OPEN_MESSAGE = "<red>The party is open, anyone can join" ;
37
+
26
38
27
39
private final PartyServiceGrpc .PartyServiceFutureStub partyService = GrpcStubCollection .getPartyService ().orElse (null );
28
40
@@ -46,36 +58,34 @@ public int execute(CommandContext<CommandSource> context) {
46
58
);
47
59
48
60
Futures .addCallback (inviteResponseFuture , FunctionalFutureCallback .create (
49
- inviteResponse -> executor .sendMessage (Component . text ( "Invited " + target . username () + " to your party " , NamedTextColor . GREEN )),
61
+ inviteResponse -> executor .sendMessage (MINI_MESSAGE . deserialize ( INVITED_MESSAGE , Placeholder . unparsed ( "username " , target . username ()) )),
50
62
throwable -> {
51
63
com .google .rpc .Status status = StatusProto .fromThrowable (throwable );
52
64
if (status == null || status .getDetailsCount () == 0 ) {
53
65
LOGGER .error ("An error occurred PartyInviteSub invitePlayerToParty: " , throwable );
54
- executor .sendMessage (Component . text ( "An error occurred inviting " + target . username (), NamedTextColor . RED ) );
66
+ executor .sendMessage (PartyCommand . ERROR_MESSAGE );
55
67
return ;
56
68
}
57
69
58
70
try {
59
71
PartyProto .InvitePlayerErrorResponse errorResponse = status .getDetails (0 ).unpack (PartyProto .InvitePlayerErrorResponse .class );
60
72
61
73
executor .sendMessage (switch (errorResponse .getErrorType ()) {
62
- case NO_PERMISSION ->
63
- Component .text ("You must be the leader of the party to invite another player." , NamedTextColor .RED );
64
- case TARGET_ALREADY_INVITED ->
65
- Component .text (target .username () + " is already invited to your party." , NamedTextColor .RED );
66
- case TARGET_ALREADY_IN_SELF_PARTY ->
67
- Component .text (target .username () + " is already in your party." , NamedTextColor .RED );
68
- case TARGET_ALREADY_IN_ANOTHER_PARTY ->
69
- Component .text (target .username () + " is already in another party." , NamedTextColor .RED );
70
- case PARTY_IS_OPEN -> Component .text ("Your party is open, anyone can join." , NamedTextColor .RED );
74
+ case NO_PERMISSION -> MINI_MESSAGE .deserialize (NO_PERMISSION_MESSAGE );
75
+ case TARGET_ALREADY_INVITED -> MINI_MESSAGE .deserialize (ALREADY_INVITED_MESSAGE , Placeholder .unparsed ("username" , target .username ()));
76
+ case TARGET_ALREADY_IN_SELF_PARTY -> MINI_MESSAGE .deserialize (ALREADY_IN_PARTY_MESSAGE , Placeholder .unparsed ("username" , target .username ()));
77
+ // TODO: Why is this an error?
78
+ case TARGET_ALREADY_IN_ANOTHER_PARTY -> MINI_MESSAGE .deserialize (ALREADY_IN_PARTY_OTHER_MESSAGE , Placeholder .unparsed ("username" , target .username ()));
79
+ // TODO: This too.
80
+ case PARTY_IS_OPEN -> MINI_MESSAGE .deserialize (PARTY_IS_OPEN_MESSAGE );
71
81
default -> {
72
82
LOGGER .error ("An error occurred PartyInviteSub invitePlayerToParty: " , throwable );
73
- yield Component . text ( "An error occurred" , NamedTextColor . RED ) ;
83
+ yield PartyCommand . ERROR_MESSAGE ;
74
84
}
75
85
});
76
86
} catch (InvalidProtocolBufferException e ) {
77
87
LOGGER .error ("An error occurred PartyInviteSub invitePlayerToParty: " , throwable );
78
- executor .sendMessage (Component . text ( "An error occurred" , NamedTextColor . RED ) );
88
+ executor .sendMessage (PartyCommand . ERROR_MESSAGE );
79
89
}
80
90
}
81
91
), ForkJoinPool .commonPool ());
@@ -87,7 +97,7 @@ public int execute(CommandContext<CommandSource> context) {
87
97
}
88
98
89
99
LOGGER .error ("An error occurred PartyInviteSub getPlayerByUsername: " , status .asException ());
90
- executor .sendMessage (Component . text ( "An error occurred" , NamedTextColor . RED ) );
100
+ executor .sendMessage (PartyCommand . ERROR_MESSAGE );
91
101
}
92
102
);
93
103
0 commit comments