Skip to content
This repository was archived by the owner on Mar 16, 2025. It is now read-only.

Commit cf5b752

Browse files
committed
improvements
1 parent 6ccb44a commit cf5b752

File tree

10 files changed

+221
-199
lines changed

10 files changed

+221
-199
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Advanced whitelist system for Velocity. \
1515
- ***proxy.whitelist.disable* | /pwl disable** - Disable the whitelist
1616
- ***proxy.whitelist.enable* | /pwl enable** - Enable the whitelist
1717
- ***proxy.whitelist.get* | /pwl get \<player\>** - Get the whitelist status of a player
18+
- ***proxy.whitelist* | /pwl help** - View the command help
1819
- ***proxy.whitelist.kick* | /pwl kick** - Kick everyone who is not whitelisted
1920
- ***proxy.whitelist.list* | /pwl list** - List all whitelisted players
2021
- ***proxy.whitelist.remove* | /pwl remove \<player\>** - Remove a player from the whitelist

src/main/java/me/bigvirusboi/whitelist/ProxyWhitelist.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,17 @@
2020
import java.nio.file.Path;
2121
import java.util.logging.Logger;
2222

23-
@Plugin(id = "whitelist", name = "Whitelist", version = BuildConstants.VERSION,
24-
description = "Adds an advanced whitelist to Velocity", authors = {"BigVirusBoi"})
23+
@Plugin(id = "whitelist", name = "ProxyWhitelist", version = BuildConstants.VERSION,
24+
description = "Advanced whitelist system for Velocity", authors = {"BigVirusBoi"})
2525
public final class ProxyWhitelist {
2626
private final ProxyServer server;
27-
private final Logger logger;
28-
private final Path dataDirectory;
2927

3028
private final PlayerCache cache;
3129
private final Whitelist whitelist;
3230

3331
@Inject
3432
public ProxyWhitelist(ProxyServer server, Logger logger, @DataDirectory Path dataDirectory) {
3533
this.server = server;
36-
this.logger = logger;
37-
this.dataDirectory = dataDirectory;
3834

3935
logger.info("ProxyWhitelist is loading");
4036
this.cache = new PlayerCache(server);
@@ -58,7 +54,6 @@ public void onProxyInitialization(ProxyInitializeEvent event) {
5854
}
5955
if (!whitelist.isWhitelisted(player)) {
6056
e.setResult(ResultedEvent.ComponentResult.denied(Component.text("§cYou are not whitelisted")));
61-
return;
6257
}
6358
});
6459

src/main/java/me/bigvirusboi/whitelist/cache/MinecraftAPI.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.net.HttpURLConnection;
1212
import java.net.URL;
1313
import java.util.UUID;
14+
import java.util.logging.Logger;
1415

1516
@NoArgsConstructor(access = AccessLevel.PRIVATE)
1617
public final class MinecraftAPI {
@@ -38,11 +39,16 @@ public static CachedPlayer getPlayer(String query) {
3839
String uuidString = jsonObject.get("id").getAsString();
3940
UUID uuid = Util.parseUUID(uuidString);
4041
String name = jsonObject.get("name").getAsString();
41-
if (uuid == null || name == null) return null;
42+
if (uuid == null || name == null) {
43+
Logger.getLogger("MinecraftAPI").severe("Unable to GET player " + query + ": " + response.toString());
44+
return null;
45+
}
4246

4347
return new CachedPlayer(uuid, name);
4448
}
45-
} catch (Exception ignored) {
49+
} catch (Exception ex) {
50+
Logger.getLogger("MinecraftAPI").severe("Unable to GET player " + query + ": " + ex);
51+
ex.printStackTrace();
4652
}
4753
return null;
4854
}

src/main/java/me/bigvirusboi/whitelist/command/WhitelistCommand.java

Lines changed: 79 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
import me.bigvirusboi.whitelist.cache.PlayerCache;
99
import me.bigvirusboi.whitelist.manager.Whitelist;
1010
import me.bigvirusboi.whitelist.util.Constants;
11-
import me.bigvirusboi.whitelist.util.DurationParser;
11+
import me.bigvirusboi.whitelist.util.time.DurationParser;
1212
import me.bigvirusboi.whitelist.util.Permissions;
13-
import me.bigvirusboi.whitelist.util.TimeUtil;
13+
import me.bigvirusboi.whitelist.util.time.TimeUtil;
1414
import net.kyori.adventure.text.Component;
1515

1616
import java.util.ArrayList;
17+
import java.util.HashMap;
1718
import java.util.List;
19+
import java.util.Map;
1820

1921
import static me.bigvirusboi.whitelist.util.Constants.CMD;
2022

@@ -36,21 +38,13 @@ public void execute(Invocation invocation) {
3638
String[] args = invocation.arguments();
3739

3840
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"));
5043
return;
5144
}
5245

5346
switch (args[0].toLowerCase()) {
47+
case "help" -> handleHelp(source);
5448
case "clear" -> handleClear(source, args);
5549
case "disable" -> handleDisable(source);
5650
case "enable" -> handleEnable(source);
@@ -65,23 +59,55 @@ public void execute(Invocation invocation) {
6559

6660
@Override
6761
public List<String> suggest(Invocation invocation) {
62+
CommandSource source = invocation.source();
6863
String[] args = invocation.arguments();
6964
List<String> arguments = new ArrayList<>();
7065

7166
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");
7376
} else if (args.length == 2) {
77+
Map<String, List<String>> permittedArguments = new HashMap<>();
7478
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));
7887
}
7988
}
8089

8190
return arguments;
8291
}
8392

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+
84106
private void handleClear(CommandSource source, String[] args) {
107+
if (!source.hasPermission(Permissions.CLEAR)) {
108+
sendPermission(source);
109+
return;
110+
}
85111
if (args.length == 1) {
86112
source.sendMessage(Component.text("§cUsage: /" + CMD + " clear <all/expired>"));
87113
return;
@@ -101,6 +127,10 @@ private void handleClear(CommandSource source, String[] args) {
101127
}
102128

103129
private void handleDisable(CommandSource source) {
130+
if (!source.hasPermission(Permissions.DISABLE)) {
131+
sendPermission(source);
132+
return;
133+
}
104134
if (!whitelist.isEnabled()) {
105135
source.sendMessage(Component.text("§cWhitelist is already disabled"));
106136
return;
@@ -112,6 +142,10 @@ private void handleDisable(CommandSource source) {
112142
}
113143

114144
private void handleEnable(CommandSource source) {
145+
if (!source.hasPermission(Permissions.ENABLE)) {
146+
sendPermission(source);
147+
return;
148+
}
115149
if (whitelist.isEnabled()) {
116150
source.sendMessage(Component.text("§cWhitelist is already enabled"));
117151
return;
@@ -123,6 +157,10 @@ private void handleEnable(CommandSource source) {
123157
}
124158

125159
private void handleGet(CommandSource source, String[] args) {
160+
if (!source.hasPermission(Permissions.GET)) {
161+
sendPermission(source);
162+
return;
163+
}
126164
if (args.length == 1) {
127165
source.sendMessage(Component.text("§cUsage: /" + CMD + " get <player>"));
128166
return;
@@ -142,23 +180,36 @@ private void handleGet(CommandSource source, String[] args) {
142180
}
143181

144182
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"));
146189
whitelist.kickNotWhitelisted();
147190
}
148191

149192
private void handleList(CommandSource source) {
193+
if (!source.hasPermission(Permissions.LIST)) {
194+
sendPermission(source);
195+
return;
196+
}
150197
if (whitelist.isEmpty()) {
151198
source.sendMessage(Component.text("§7The whitelist is empty"));
152199
return;
153200
}
154201

155-
source.sendMessage(Component.text("§6§lWhitelisted players §7(Proxy Whitelist)"));
202+
source.sendMessage(Component.text("§6§lWhitelisted players"));
156203
for (CachedPlayer player : whitelist.getWhitelisted().keySet()) {
157204
source.sendMessage(Component.text("§8 - §7%s §e%s".formatted(player.name(), whitelist.getDurationFormatted(player))));
158205
}
159206
}
160207

161208
private void handleRemove(CommandSource source, String[] args) {
209+
if (!source.hasPermission(Permissions.REMOVE)) {
210+
sendPermission(source);
211+
return;
212+
}
162213
if (args.length == 1) {
163214
source.sendMessage(Component.text("§cUsage: /" + CMD + " remove <player>"));
164215
return;
@@ -179,6 +230,10 @@ private void handleRemove(CommandSource source, String[] args) {
179230
}
180231

181232
private void handleSet(CommandSource source, String[] args) {
233+
if (!source.hasPermission(Permissions.SET)) {
234+
sendPermission(source);
235+
return;
236+
}
182237
if (args.length == 1) {
183238
source.sendMessage(Component.text("§cUsage: /" + CMD + " set <player> [duration]"));
184239
return;
@@ -206,6 +261,10 @@ private void handleSet(CommandSource source, String[] args) {
206261

207262
long expire = parser.isPermanent() ? -1 : System.currentTimeMillis() + parser.getMillis();
208263
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"));
210269
}
211270
}

src/main/java/me/bigvirusboi/whitelist/manager/Whitelist.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import me.bigvirusboi.whitelist.cache.CachedPlayer;
99
import me.bigvirusboi.whitelist.cache.PlayerCache;
1010
import me.bigvirusboi.whitelist.util.Permissions;
11-
import me.bigvirusboi.whitelist.util.TimeUtil;
11+
import me.bigvirusboi.whitelist.util.time.TimeUtil;
1212
import me.bigvirusboi.whitelist.util.Util;
1313
import net.kyori.adventure.text.Component;
1414

@@ -49,7 +49,7 @@ public String getDurationFormatted(CachedPlayer player) {
4949
return "§cEXPIRED";
5050
}
5151

52-
return "§a" + TimeUtil.formatToSeconds(expire - System.currentTimeMillis());
52+
return "§a" + TimeUtil.toFormattedString(expire - System.currentTimeMillis());
5353
}
5454

5555
public long getDuration(UUID uuid) {

src/main/java/me/bigvirusboi/whitelist/util/TimeUtil.java

Lines changed: 0 additions & 106 deletions
This file was deleted.

0 commit comments

Comments
 (0)