8
8
import me .bigvirusboi .whitelist .cache .PlayerCache ;
9
9
import me .bigvirusboi .whitelist .manager .Whitelist ;
10
10
import me .bigvirusboi .whitelist .util .Constants ;
11
- import me .bigvirusboi .whitelist .util .DurationParser ;
11
+ import me .bigvirusboi .whitelist .util .time . DurationParser ;
12
12
import me .bigvirusboi .whitelist .util .Permissions ;
13
- import me .bigvirusboi .whitelist .util .TimeUtil ;
13
+ import me .bigvirusboi .whitelist .util .time . TimeUtil ;
14
14
import net .kyori .adventure .text .Component ;
15
15
16
16
import java .util .ArrayList ;
17
+ import java .util .HashMap ;
17
18
import java .util .List ;
19
+ import java .util .Map ;
18
20
19
21
import static me .bigvirusboi .whitelist .util .Constants .CMD ;
20
22
@@ -36,21 +38,13 @@ public void execute(Invocation invocation) {
36
38
String [] args = invocation .arguments ();
37
39
38
40
if (args .length == 0 ) {
39
- source .sendMessage (Component .text ("§6§lCommand usage§7 (Proxy Whitelist)" ));
40
- source .sendMessage (Component .text ("§6 /" + CMD + "§e clear <all/expired>§8 - §7Clear the whitelist" ));
41
- source .sendMessage (Component .text ("§6 /" + CMD + "§e disable§8 - §7Disable the whitelist" ));
42
- source .sendMessage (Component .text ("§6 /" + CMD + "§e enable§8 - §7Enable the whitelist" ));
43
- source .sendMessage (Component .text ("§6 /" + CMD + "§e get <player>§8 - §7Get the whitelist status of a player" ));
44
- source .sendMessage (Component .text ("§6 /" + CMD + "§e kick§8 - §7Kick everyone who is not whitelisted" ));
45
- source .sendMessage (Component .text ("§6 /" + CMD + "§e list§8 - §7List all whitelisted players" ));
46
- source .sendMessage (Component .text ("§6 /" + CMD + "§e remove <player>§8 - §7Remove a player from the whitelist" ));
47
- source .sendMessage (Component .text ("§6 /" + CMD + "§e set <player> [duration]§8 - §7Add a player to the whitelist" ));
48
- source .sendMessage (Component .newline ().append (Component .text (
49
- "§7Running §6§lProxy Whitelist v" + BuildConstants .VERSION + "§7 by §e" + Constants .CREDITS )));
41
+ source .sendMessage (Component .text ("§7Running §6§lProxy Whitelist §8(v" + BuildConstants .VERSION + ")§7 by §e" + Constants .CREDITS ));
42
+ source .sendMessage (Component .text ("§7To view subcommands, use §e/" + CMD + " help" ));
50
43
return ;
51
44
}
52
45
53
46
switch (args [0 ].toLowerCase ()) {
47
+ case "help" -> handleHelp (source );
54
48
case "clear" -> handleClear (source , args );
55
49
case "disable" -> handleDisable (source );
56
50
case "enable" -> handleEnable (source );
@@ -65,23 +59,55 @@ public void execute(Invocation invocation) {
65
59
66
60
@ Override
67
61
public List <String > suggest (Invocation invocation ) {
62
+ CommandSource source = invocation .source ();
68
63
String [] args = invocation .arguments ();
69
64
List <String > arguments = new ArrayList <>();
70
65
71
66
if (args .length == 1 ) {
72
- arguments .addAll (List .of ("clear" , "disable" , "enable" , "get" , "kick" , "list" , "remove" , "set" ));
67
+ arguments .add ("help" );
68
+ if (source .hasPermission (Permissions .CLEAR )) arguments .add ("clear" );
69
+ if (source .hasPermission (Permissions .DISABLE )) arguments .add ("disable" );
70
+ if (source .hasPermission (Permissions .ENABLE )) arguments .add ("enable" );
71
+ if (source .hasPermission (Permissions .GET )) arguments .add ("get" );
72
+ if (source .hasPermission (Permissions .KICK )) arguments .add ("kick" );
73
+ if (source .hasPermission (Permissions .LIST )) arguments .add ("list" );
74
+ if (source .hasPermission (Permissions .REMOVE )) arguments .add ("remove" );
75
+ if (source .hasPermission (Permissions .SET )) arguments .add ("set" );
73
76
} else if (args .length == 2 ) {
77
+ Map <String , List <String >> permittedArguments = new HashMap <>();
74
78
switch (args [0 ].toLowerCase ()) {
75
- case "clear" -> arguments .addAll (List .of ("all" , "expired" ));
76
- case "get" , "set" -> arguments .addAll (proxy .getAllPlayers ().stream ().map (Player ::getUsername ).toList ());
77
- case "remove" -> arguments .addAll (whitelist .getWhitelisted ().keySet ().stream ().map (CachedPlayer ::name ).toList ());
79
+ case "clear" -> permittedArguments .put (Permissions .CLEAR , List .of ("all" , "expired" ));
80
+ case "get" -> permittedArguments .put (Permissions .GET , whitelist .getWhitelisted ().keySet ().stream ().map (CachedPlayer ::name ).toList ());
81
+ case "set" -> permittedArguments .put (Permissions .SET , proxy .getAllPlayers ().stream ().map (Player ::getUsername ).toList ());
82
+ case "remove" -> permittedArguments .put (Permissions .REMOVE , whitelist .getWhitelisted ().keySet ().stream ().map (CachedPlayer ::name ).toList ());
83
+ }
84
+
85
+ for (String permission : permittedArguments .keySet ()) {
86
+ if (source .hasPermission (permission )) arguments .addAll (permittedArguments .get (permission ));
78
87
}
79
88
}
80
89
81
90
return arguments ;
82
91
}
83
92
93
+ private void handleHelp (CommandSource source ) {
94
+ source .sendMessage (Component .text ("§6§lCommand usage" ));
95
+ source .sendMessage (Component .text ("§6 /" + CMD + "§e clear <all/expired>§8 - §7Clear the whitelist" ));
96
+ source .sendMessage (Component .text ("§6 /" + CMD + "§e disable§8 - §7Disable the whitelist" ));
97
+ source .sendMessage (Component .text ("§6 /" + CMD + "§e enable§8 - §7Enable the whitelist" ));
98
+ source .sendMessage (Component .text ("§6 /" + CMD + "§e get <player>§8 - §7Get the whitelist status of a player" ));
99
+ source .sendMessage (Component .text ("§6 /" + CMD + "§e help§8 - §7View the command help" ));
100
+ source .sendMessage (Component .text ("§6 /" + CMD + "§e kick§8 - §7Kick everyone who is not whitelisted" ));
101
+ source .sendMessage (Component .text ("§6 /" + CMD + "§e list§8 - §7List all whitelisted players" ));
102
+ source .sendMessage (Component .text ("§6 /" + CMD + "§e remove <player>§8 - §7Remove a player from the whitelist" ));
103
+ source .sendMessage (Component .text ("§6 /" + CMD + "§e set <player> [duration]§8 - §7Add a player to the whitelist" ));
104
+ }
105
+
84
106
private void handleClear (CommandSource source , String [] args ) {
107
+ if (!source .hasPermission (Permissions .CLEAR )) {
108
+ sendPermission (source );
109
+ return ;
110
+ }
85
111
if (args .length == 1 ) {
86
112
source .sendMessage (Component .text ("§cUsage: /" + CMD + " clear <all/expired>" ));
87
113
return ;
@@ -101,6 +127,10 @@ private void handleClear(CommandSource source, String[] args) {
101
127
}
102
128
103
129
private void handleDisable (CommandSource source ) {
130
+ if (!source .hasPermission (Permissions .DISABLE )) {
131
+ sendPermission (source );
132
+ return ;
133
+ }
104
134
if (!whitelist .isEnabled ()) {
105
135
source .sendMessage (Component .text ("§cWhitelist is already disabled" ));
106
136
return ;
@@ -112,6 +142,10 @@ private void handleDisable(CommandSource source) {
112
142
}
113
143
114
144
private void handleEnable (CommandSource source ) {
145
+ if (!source .hasPermission (Permissions .ENABLE )) {
146
+ sendPermission (source );
147
+ return ;
148
+ }
115
149
if (whitelist .isEnabled ()) {
116
150
source .sendMessage (Component .text ("§cWhitelist is already enabled" ));
117
151
return ;
@@ -123,6 +157,10 @@ private void handleEnable(CommandSource source) {
123
157
}
124
158
125
159
private void handleGet (CommandSource source , String [] args ) {
160
+ if (!source .hasPermission (Permissions .GET )) {
161
+ sendPermission (source );
162
+ return ;
163
+ }
126
164
if (args .length == 1 ) {
127
165
source .sendMessage (Component .text ("§cUsage: /" + CMD + " get <player>" ));
128
166
return ;
@@ -142,23 +180,36 @@ private void handleGet(CommandSource source, String[] args) {
142
180
}
143
181
144
182
private void handleKick (CommandSource source ) {
145
- source .sendMessage (Component .text ("§7Kicked all non whitelisted players" ));
183
+ if (!source .hasPermission (Permissions .KICK )) {
184
+ sendPermission (source );
185
+ return ;
186
+ }
187
+
188
+ source .sendMessage (Component .text ("§7Kicked all non-whitelisted players" ));
146
189
whitelist .kickNotWhitelisted ();
147
190
}
148
191
149
192
private void handleList (CommandSource source ) {
193
+ if (!source .hasPermission (Permissions .LIST )) {
194
+ sendPermission (source );
195
+ return ;
196
+ }
150
197
if (whitelist .isEmpty ()) {
151
198
source .sendMessage (Component .text ("§7The whitelist is empty" ));
152
199
return ;
153
200
}
154
201
155
- source .sendMessage (Component .text ("§6§lWhitelisted players §7(Proxy Whitelist) " ));
202
+ source .sendMessage (Component .text ("§6§lWhitelisted players" ));
156
203
for (CachedPlayer player : whitelist .getWhitelisted ().keySet ()) {
157
204
source .sendMessage (Component .text ("§8 - §7%s §e%s" .formatted (player .name (), whitelist .getDurationFormatted (player ))));
158
205
}
159
206
}
160
207
161
208
private void handleRemove (CommandSource source , String [] args ) {
209
+ if (!source .hasPermission (Permissions .REMOVE )) {
210
+ sendPermission (source );
211
+ return ;
212
+ }
162
213
if (args .length == 1 ) {
163
214
source .sendMessage (Component .text ("§cUsage: /" + CMD + " remove <player>" ));
164
215
return ;
@@ -179,6 +230,10 @@ private void handleRemove(CommandSource source, String[] args) {
179
230
}
180
231
181
232
private void handleSet (CommandSource source , String [] args ) {
233
+ if (!source .hasPermission (Permissions .SET )) {
234
+ sendPermission (source );
235
+ return ;
236
+ }
182
237
if (args .length == 1 ) {
183
238
source .sendMessage (Component .text ("§cUsage: /" + CMD + " set <player> [duration]" ));
184
239
return ;
@@ -206,6 +261,10 @@ private void handleSet(CommandSource source, String[] args) {
206
261
207
262
long expire = parser .isPermanent () ? -1 : System .currentTimeMillis () + parser .getMillis ();
208
263
whitelist .set (player , expire );
209
- source .sendMessage (Component .text ("§7Set whitelist of §f%s§7 to §a%s§7!" .formatted (player .name (), TimeUtil .formatToSeconds (parser .getMillis ()))));
264
+ source .sendMessage (Component .text ("§7Set whitelist of §f%s§7 to §a%s§7!" .formatted (player .name (), TimeUtil .toFormattedString (parser .getMillis ()))));
265
+ }
266
+
267
+ private void sendPermission (CommandSource source ) {
268
+ source .sendMessage (Component .text ("§cYou do not have permission to use this command" ));
210
269
}
211
270
}
0 commit comments