4
4
import com .mojang .brigadier .CommandDispatcher ;
5
5
import com .mojang .brigadier .arguments .StringArgumentType ;
6
6
import com .mojang .brigadier .exceptions .CommandSyntaxException ;
7
+ import com .mojang .brigadier .exceptions .SimpleCommandExceptionType ;
8
+ import dev .ftb .mods .ftblibrary .snbt .SNBT ;
9
+ import dev .ftb .mods .ftbranks .api .FTBRanksAPI ;
10
+ import dev .ftb .mods .ftbranks .api .PermissionValue ;
7
11
import dev .ftb .mods .ftbranks .api .Rank ;
12
+ import dev .ftb .mods .ftbranks .api .RankCondition ;
13
+ import dev .ftb .mods .ftbranks .impl .BooleanPermissionValue ;
8
14
import dev .ftb .mods .ftbranks .impl .FTBRanksAPIImpl ;
15
+ import dev .ftb .mods .ftbranks .impl .NumberPermissionValue ;
16
+ import dev .ftb .mods .ftbranks .impl .StringPermissionValue ;
17
+ import dev .ftb .mods .ftbranks .impl .condition .DefaultCondition ;
9
18
import net .minecraft .ChatFormatting ;
10
19
import net .minecraft .commands .CommandSourceStack ;
11
20
import net .minecraft .commands .Commands ;
12
21
import net .minecraft .commands .arguments .EntityArgument ;
13
22
import net .minecraft .commands .arguments .GameProfileArgument ;
23
+ import net .minecraft .nbt .StringTag ;
24
+ import net .minecraft .network .chat .Component ;
25
+ import net .minecraft .network .chat .MutableComponent ;
14
26
import net .minecraft .network .chat .TextComponent ;
27
+ import net .minecraft .network .chat .TranslatableComponent ;
15
28
import net .minecraft .server .level .ServerPlayer ;
29
+ import org .apache .commons .lang3 .math .NumberUtils ;
16
30
17
31
import java .io .IOException ;
18
32
import java .util .Collection ;
33
+ import java .util .Collections ;
19
34
20
35
/**
21
36
* @author LatvianModder
@@ -39,21 +54,21 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher, Co
39
54
)
40
55
)
41
56
.then (Commands .literal ("delete" )
42
- .then (Commands .argument ("rank" , StringArgumentType . word ())
43
- .executes (context -> deleteRank (context .getSource (), StringArgumentType . getString (context , "rank" )))
57
+ .then (Commands .argument ("rank" , RankArgumentType . rank ())
58
+ .executes (context -> deleteRank (context .getSource (), RankArgumentType . getRank (context , "rank" )))
44
59
)
45
60
)
46
61
.then (Commands .literal ("add" )
47
62
.then (Commands .argument ("players" , GameProfileArgument .gameProfile ())
48
- .then (Commands .argument ("rank" , StringArgumentType . word ())
49
- .executes (context -> addRank (context .getSource (), GameProfileArgument .getGameProfiles (context , "players" ), StringArgumentType . getString (context , "rank" )))
63
+ .then (Commands .argument ("rank" , RankArgumentType . rank ())
64
+ .executes (context -> addRank (context .getSource (), GameProfileArgument .getGameProfiles (context , "players" ), RankArgumentType . getRank (context , "rank" )))
50
65
)
51
66
)
52
67
)
53
68
.then (Commands .literal ("remove" )
54
69
.then (Commands .argument ("players" , GameProfileArgument .gameProfile ())
55
- .then (Commands .argument ("rank" , StringArgumentType . word ())
56
- .executes (context -> removeRank (context .getSource (), GameProfileArgument .getGameProfiles (context , "players" ), StringArgumentType . getString (context , "rank" )))
70
+ .then (Commands .argument ("rank" , RankArgumentType . rank ())
71
+ .executes (context -> removeRank (context .getSource (), GameProfileArgument .getGameProfiles (context , "players" ), RankArgumentType . getRank (context , "rank" )))
57
72
)
58
73
)
59
74
)
@@ -63,8 +78,38 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher, Co
63
78
)
64
79
)
65
80
.then (Commands .literal ("list_players_with" )
66
- .then (Commands .argument ("rank" , StringArgumentType .word ())
67
- .executes (context -> listPlayersWith (context .getSource (), StringArgumentType .getString (context , "rank" )))
81
+ .then (Commands .argument ("rank" , RankArgumentType .rank ())
82
+ .executes (context -> listPlayersWith (context .getSource (), RankArgumentType .getRank (context , "rank" )))
83
+ )
84
+ )
85
+ .then (Commands .literal ("node" )
86
+ .then (Commands .literal ("add" )
87
+ .then (Commands .argument ("rank" , RankArgumentType .rank ())
88
+ .then (Commands .argument ("node" , StringArgumentType .word ())
89
+ .then (Commands .argument ("value" , StringArgumentType .greedyString ())
90
+ .executes (context -> setNode (context .getSource (), RankArgumentType .getRank (context , "rank" ), StringArgumentType .getString (context , "node" ), StringArgumentType .getString (context , "value" )))
91
+ )
92
+ )
93
+ )
94
+ )
95
+ .then (Commands .literal ("remove" )
96
+ .then (Commands .argument ("rank" , RankArgumentType .rank ())
97
+ .then (Commands .argument ("node" , StringArgumentType .word ())
98
+ .executes (context -> setNode (context .getSource (), RankArgumentType .getRank (context , "rank" ), StringArgumentType .getString (context , "node" ), null ))
99
+ )
100
+ )
101
+ )
102
+ )
103
+ .then (Commands .literal ("condition" )
104
+ .then (Commands .argument ("rank" , RankArgumentType .rank ())
105
+ .then (Commands .argument ("value" , StringArgumentType .greedyString ())
106
+ .executes (context -> setCondition (context .getSource (), RankArgumentType .getRank (context , "rank" ), StringArgumentType .getString (context , "value" )))
107
+ )
108
+ )
109
+ )
110
+ .then (Commands .literal ("show_rank" )
111
+ .then (Commands .argument ("rank" , RankArgumentType .rank ())
112
+ .executes (context -> showRank (context .getSource (), RankArgumentType .getRank (context , "rank" )))
68
113
)
69
114
)
70
115
);
@@ -77,7 +122,7 @@ private static String normalizeRankName(String name) {
77
122
private static int reloadRanks (CommandSourceStack source ) {
78
123
try {
79
124
FTBRanksAPIImpl .manager .reload ();
80
- source .sendSuccess (new TextComponent ( "Ranks reloaded! " ), true );
125
+ source .sendSuccess (new TranslatableComponent ( "ftbranks.reload " ), true );
81
126
82
127
for (ServerPlayer p : source .getServer ().getPlayerList ().getPlayers ()) {
83
128
source .getServer ().getPlayerList ().sendPlayerPermissionLevel (p );
@@ -98,12 +143,12 @@ private static int refreshReadme(CommandSourceStack source) {
98
143
ex .printStackTrace ();
99
144
}
100
145
101
- source .sendSuccess (new TextComponent ( "Done! " ), false );
146
+ source .sendSuccess (new TranslatableComponent ( "ftbranks.refresh_readme " ), false );
102
147
return 1 ;
103
148
}
104
149
105
150
private static int listAllRanks (CommandSourceStack source ) {
106
- source .sendSuccess (new TextComponent ( "Ranks: " ), false );
151
+ source .sendSuccess (new TranslatableComponent ( "ftbranks.ranks " ), false );
107
152
108
153
for (Rank rank : FTBRanksAPIImpl .manager .getAllRanks ()) {
109
154
source .sendSuccess (new TextComponent ("- " + rank .getName ()).withStyle (rank .getCondition ().isDefaultCondition () ? ChatFormatting .AQUA : ChatFormatting .YELLOW ), false );
@@ -112,55 +157,48 @@ private static int listAllRanks(CommandSourceStack source) {
112
157
return 1 ;
113
158
}
114
159
115
- private static int createRank (CommandSourceStack source , String name ) throws CommandSyntaxException {
160
+ private static int createRank (CommandSourceStack source , String name ) {
116
161
String id = normalizeRankName (name );
117
162
118
163
if (FTBRanksAPIImpl .manager .getRank (id ).isPresent ()) {
119
- source .sendFailure (new TextComponent ( "Rank ID already taken!" ));
164
+ source .sendFailure (new TranslatableComponent ( "ftbranks.rank_taken" , name ));
120
165
return 0 ;
121
166
}
122
167
123
168
FTBRanksAPIImpl .manager .createRank (id , name );
124
- source .sendSuccess (new TextComponent ( "Rank created with id '" + id + "'!" ), false );
169
+ source .sendSuccess (new TranslatableComponent ( "ftbranks.rank_created" , id ), false );
125
170
return 1 ;
126
171
}
127
172
128
- private static int deleteRank (CommandSourceStack source , String name ) throws CommandSyntaxException {
129
- if (FTBRanksAPIImpl .manager .deleteRank (normalizeRankName (name )) == null ) {
130
- source .sendFailure (new TextComponent ("Rank not found!" ));
131
- return 0 ;
132
- }
173
+ private static int deleteRank (CommandSourceStack source , Rank rank ) {
174
+ FTBRanksAPI .INSTANCE .getManager ().deleteRank (rank .getId ());
175
+ source .sendSuccess (new TranslatableComponent ("ftbranks.rank_deleted" , rank .getName ()), false );
133
176
134
- source .sendSuccess (new TextComponent ("Rank deleted!" ), false );
135
177
return 1 ;
136
178
}
137
179
138
- private static int addRank (CommandSourceStack source , Collection <GameProfile > players , String name ) throws CommandSyntaxException {
139
- Rank r = FTBRanksAPIImpl .manager .getRank (normalizeRankName (name )).orElseThrow (NullPointerException ::new );
140
-
180
+ private static int addRank (CommandSourceStack source , Collection <GameProfile > players , Rank rank ) {
141
181
for (GameProfile profile : players ) {
142
- if (r .add (profile )) {
143
- source .sendSuccess (new TextComponent ( "Added '" + r .getName () + "' to " + profile .getName ()), false );
182
+ if (rank .add (profile )) {
183
+ source .sendSuccess (new TranslatableComponent ( "ftbranks.player_added" , profile .getName (), rank .getName ()), false );
144
184
}
145
185
}
146
186
147
187
return 1 ;
148
188
}
149
189
150
- private static int removeRank (CommandSourceStack source , Collection <GameProfile > players , String name ) throws CommandSyntaxException {
151
- Rank r = FTBRanksAPIImpl .manager .getRank (normalizeRankName (name )).orElseThrow (NullPointerException ::new );
152
-
190
+ private static int removeRank (CommandSourceStack source , Collection <GameProfile > players , Rank rank ) {
153
191
for (GameProfile profile : players ) {
154
- if (r .remove (profile )) {
155
- source .sendSuccess (new TextComponent ( "Removed '" + r .getName () + "' from " + profile .getName ()), false );
192
+ if (rank .remove (profile )) {
193
+ source .sendSuccess (new TranslatableComponent ( "ftbranks.player_removed" , profile .getName (), rank .getName ()), false );
156
194
}
157
195
}
158
196
159
197
return 1 ;
160
198
}
161
199
162
200
private static int listRanksOf (CommandSourceStack source , ServerPlayer player ) {
163
- source .sendSuccess (new TextComponent ( "Ranks added to " + player .getGameProfile ().getName () + ":" ), false );
201
+ source .sendSuccess (new TranslatableComponent ( "ftbranks.list_ranks_of" , player .getGameProfile ().getName ()), false );
164
202
165
203
for (Rank rank : FTBRanksAPIImpl .manager .getAllRanks ()) {
166
204
if (rank .isActive (player )) {
@@ -171,17 +209,86 @@ private static int listRanksOf(CommandSourceStack source, ServerPlayer player) {
171
209
return 1 ;
172
210
}
173
211
174
- private static int listPlayersWith (CommandSourceStack source , String name ) {
175
- Rank r = FTBRanksAPIImpl .manager .getRank (normalizeRankName (name )).orElseThrow (NullPointerException ::new );
176
-
177
- source .sendSuccess (new TextComponent ("Players with " + name + " added to them:" ), false );
212
+ private static int listPlayersWith (CommandSourceStack source , Rank rank ) {
213
+ source .sendSuccess (new TranslatableComponent ("ftbranks.list_players_with" , rank .getName ()), false );
178
214
179
215
for (ServerPlayer player : source .getServer ().getPlayerList ().getPlayers ()) {
180
- if (r .isActive (player )) {
216
+ if (rank .isActive (player )) {
181
217
source .sendSuccess (new TextComponent ("- " ).withStyle (ChatFormatting .YELLOW ).append (player .getDisplayName ()), false );
182
218
}
183
219
}
184
220
185
221
return 1 ;
186
222
}
223
+
224
+ private static int setNode (CommandSourceStack source , Rank rank , String node , String value ) throws CommandSyntaxException {
225
+ try {
226
+ rank .setPermission (node , strToPermissionValue (value ));
227
+ if (value != null ) {
228
+ source .sendSuccess (new TranslatableComponent ("ftbranks.node_added" , node , rank .getPermission (node ), rank ), false );
229
+ } else {
230
+ source .sendSuccess (new TranslatableComponent ("ftbranks.node_removed" , node , rank ), false );
231
+ }
232
+ } catch (IllegalArgumentException e ) {
233
+ throw new SimpleCommandExceptionType (new TextComponent (e .getMessage ())).create ();
234
+ }
235
+
236
+ return 1 ;
237
+ }
238
+
239
+ private static int setCondition (CommandSourceStack source , Rank rank , String value ) throws CommandSyntaxException {
240
+ try {
241
+ RankCondition condition ;
242
+ if (value .equals ("default" ) || value .equals ("\" \" " )) {
243
+ condition = new DefaultCondition (rank );
244
+ } else if (value .startsWith ("{" ) || value .contains (" " )) {
245
+ condition = FTBRanksAPI .INSTANCE .getManager ().createCondition (rank , SNBT .readLines (Collections .singletonList (value )));
246
+ } else {
247
+ condition = FTBRanksAPI .INSTANCE .getManager ().createCondition (rank , StringTag .valueOf (value ));
248
+ }
249
+ rank .setCondition (condition );
250
+ source .sendSuccess (new TranslatableComponent ("ftbranks.node_added" , "condition" , value , rank ), false );
251
+ } catch (Exception e ) {
252
+ throw new SimpleCommandExceptionType (new TextComponent (e .getMessage ())).create ();
253
+ }
254
+
255
+ return 1 ;
256
+ }
257
+
258
+ private static int showRank (CommandSourceStack source , Rank rank ) {
259
+ source .sendSuccess (new TextComponent ("=" .repeat (50 )).withStyle (ChatFormatting .GREEN ), false );
260
+
261
+ source .sendSuccess (new TranslatableComponent ("ftbranks.show_rank.header" , col (rank .getId (), ChatFormatting .WHITE ), col (rank .getName (), ChatFormatting .WHITE ), col (Integer .toString (rank .getPower ()), ChatFormatting .WHITE )).withStyle (ChatFormatting .YELLOW ), false );
262
+
263
+ String condStr = rank .getCondition ().asString ();
264
+ Component c = condStr .isEmpty () ?
265
+ new TranslatableComponent ("ftbranks.show_rank.condition.default" ).withStyle (ChatFormatting .WHITE , ChatFormatting .ITALIC ) :
266
+ col (condStr , ChatFormatting .WHITE );
267
+ source .sendSuccess (new TranslatableComponent ("ftbranks.show_rank.condition" , c ).withStyle (ChatFormatting .YELLOW ), false );
268
+
269
+ source .sendSuccess (new TranslatableComponent ("ftbranks.show_rank.nodes" ).withStyle (ChatFormatting .YELLOW ), false );
270
+ rank .getPermissions ().stream ().sorted ().forEach (node ->
271
+ source .sendSuccess (new TranslatableComponent ("ftbranks.show_rank.node" , col (node , ChatFormatting .AQUA ), rank .getPermission (node )).withStyle (ChatFormatting .WHITE ), false )
272
+ );
273
+
274
+ return 0 ;
275
+ }
276
+
277
+ private static MutableComponent col (String str , ChatFormatting color ) {
278
+ return new TextComponent (str ).withStyle (color );
279
+ }
280
+
281
+ private static PermissionValue strToPermissionValue (String str ) {
282
+ if (str == null ) {
283
+ return null ;
284
+ } else if (str .startsWith ("\" " ) && str .endsWith ("\" " )) {
285
+ return StringPermissionValue .of (str .substring (1 , str .length () - 1 ));
286
+ } if (str .equalsIgnoreCase ("true" ) || str .equalsIgnoreCase ("false" )) {
287
+ return BooleanPermissionValue .of (str .equalsIgnoreCase ("true" ));
288
+ } else if (NumberUtils .isCreatable (str )) {
289
+ return NumberPermissionValue .of (NumberUtils .createNumber (str ));
290
+ } else {
291
+ return StringPermissionValue .of (str );
292
+ }
293
+ }
187
294
}
0 commit comments