Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
BigVirusBoi committed Oct 17, 2024
1 parent 6ccb44a commit cf5b752
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 199 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Advanced whitelist system for Velocity. \
- ***proxy.whitelist.disable* | /pwl disable** - Disable the whitelist
- ***proxy.whitelist.enable* | /pwl enable** - Enable the whitelist
- ***proxy.whitelist.get* | /pwl get \<player\>** - Get the whitelist status of a player
- ***proxy.whitelist* | /pwl help** - View the command help
- ***proxy.whitelist.kick* | /pwl kick** - Kick everyone who is not whitelisted
- ***proxy.whitelist.list* | /pwl list** - List all whitelisted players
- ***proxy.whitelist.remove* | /pwl remove \<player\>** - Remove a player from the whitelist
Expand Down
9 changes: 2 additions & 7 deletions src/main/java/me/bigvirusboi/whitelist/ProxyWhitelist.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,17 @@
import java.nio.file.Path;
import java.util.logging.Logger;

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

private final PlayerCache cache;
private final Whitelist whitelist;

@Inject
public ProxyWhitelist(ProxyServer server, Logger logger, @DataDirectory Path dataDirectory) {
this.server = server;
this.logger = logger;
this.dataDirectory = dataDirectory;

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

Expand Down
10 changes: 8 additions & 2 deletions src/main/java/me/bigvirusboi/whitelist/cache/MinecraftAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.UUID;
import java.util.logging.Logger;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class MinecraftAPI {
Expand Down Expand Up @@ -38,11 +39,16 @@ public static CachedPlayer getPlayer(String query) {
String uuidString = jsonObject.get("id").getAsString();
UUID uuid = Util.parseUUID(uuidString);
String name = jsonObject.get("name").getAsString();
if (uuid == null || name == null) return null;
if (uuid == null || name == null) {
Logger.getLogger("MinecraftAPI").severe("Unable to GET player " + query + ": " + response.toString());
return null;
}

return new CachedPlayer(uuid, name);
}
} catch (Exception ignored) {
} catch (Exception ex) {
Logger.getLogger("MinecraftAPI").severe("Unable to GET player " + query + ": " + ex);
ex.printStackTrace();
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
import me.bigvirusboi.whitelist.cache.PlayerCache;
import me.bigvirusboi.whitelist.manager.Whitelist;
import me.bigvirusboi.whitelist.util.Constants;
import me.bigvirusboi.whitelist.util.DurationParser;
import me.bigvirusboi.whitelist.util.time.DurationParser;
import me.bigvirusboi.whitelist.util.Permissions;
import me.bigvirusboi.whitelist.util.TimeUtil;
import me.bigvirusboi.whitelist.util.time.TimeUtil;
import net.kyori.adventure.text.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

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

if (args.length == 0) {
source.sendMessage(Component.text("§6§lCommand usage§7 (Proxy Whitelist)"));
source.sendMessage(Component.text("§6 /" + CMD + "§e clear <all/expired>§8 - §7Clear the whitelist"));
source.sendMessage(Component.text("§6 /" + CMD + "§e disable§8 - §7Disable the whitelist"));
source.sendMessage(Component.text("§6 /" + CMD + "§e enable§8 - §7Enable the whitelist"));
source.sendMessage(Component.text("§6 /" + CMD + "§e get <player>§8 - §7Get the whitelist status of a player"));
source.sendMessage(Component.text("§6 /" + CMD + "§e kick§8 - §7Kick everyone who is not whitelisted"));
source.sendMessage(Component.text("§6 /" + CMD + "§e list§8 - §7List all whitelisted players"));
source.sendMessage(Component.text("§6 /" + CMD + "§e remove <player>§8 - §7Remove a player from the whitelist"));
source.sendMessage(Component.text("§6 /" + CMD + "§e set <player> [duration]§8 - §7Add a player to the whitelist"));
source.sendMessage(Component.newline().append(Component.text(
"§7Running §6§lProxy Whitelist v" + BuildConstants.VERSION + "§7 by §e" + Constants.CREDITS)));
source.sendMessage(Component.text("§7Running §6§lProxy Whitelist §8(v" + BuildConstants.VERSION + ")§7 by §e" + Constants.CREDITS));
source.sendMessage(Component.text("§7To view subcommands, use §e/" + CMD + " help"));
return;
}

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

@Override
public List<String> suggest(Invocation invocation) {
CommandSource source = invocation.source();
String[] args = invocation.arguments();
List<String> arguments = new ArrayList<>();

if (args.length == 1) {
arguments.addAll(List.of("clear", "disable", "enable", "get", "kick", "list", "remove", "set"));
arguments.add("help");
if (source.hasPermission(Permissions.CLEAR)) arguments.add("clear");
if (source.hasPermission(Permissions.DISABLE)) arguments.add("disable");
if (source.hasPermission(Permissions.ENABLE)) arguments.add("enable");
if (source.hasPermission(Permissions.GET)) arguments.add("get");
if (source.hasPermission(Permissions.KICK)) arguments.add("kick");
if (source.hasPermission(Permissions.LIST)) arguments.add("list");
if (source.hasPermission(Permissions.REMOVE)) arguments.add("remove");
if (source.hasPermission(Permissions.SET)) arguments.add("set");
} else if (args.length == 2) {
Map<String, List<String>> permittedArguments = new HashMap<>();
switch (args[0].toLowerCase()) {
case "clear" -> arguments.addAll(List.of("all", "expired"));
case "get", "set" -> arguments.addAll(proxy.getAllPlayers().stream().map(Player::getUsername).toList());
case "remove" -> arguments.addAll(whitelist.getWhitelisted().keySet().stream().map(CachedPlayer::name).toList());
case "clear" -> permittedArguments.put(Permissions.CLEAR, List.of("all", "expired"));
case "get" -> permittedArguments.put(Permissions.GET, whitelist.getWhitelisted().keySet().stream().map(CachedPlayer::name).toList());
case "set" -> permittedArguments.put(Permissions.SET, proxy.getAllPlayers().stream().map(Player::getUsername).toList());
case "remove" -> permittedArguments.put(Permissions.REMOVE, whitelist.getWhitelisted().keySet().stream().map(CachedPlayer::name).toList());
}

for (String permission : permittedArguments.keySet()) {
if (source.hasPermission(permission)) arguments.addAll(permittedArguments.get(permission));
}
}

return arguments;
}

private void handleHelp(CommandSource source) {
source.sendMessage(Component.text("§6§lCommand usage"));
source.sendMessage(Component.text("§6 /" + CMD + "§e clear <all/expired>§8 - §7Clear the whitelist"));
source.sendMessage(Component.text("§6 /" + CMD + "§e disable§8 - §7Disable the whitelist"));
source.sendMessage(Component.text("§6 /" + CMD + "§e enable§8 - §7Enable the whitelist"));
source.sendMessage(Component.text("§6 /" + CMD + "§e get <player>§8 - §7Get the whitelist status of a player"));
source.sendMessage(Component.text("§6 /" + CMD + "§e help§8 - §7View the command help"));
source.sendMessage(Component.text("§6 /" + CMD + "§e kick§8 - §7Kick everyone who is not whitelisted"));
source.sendMessage(Component.text("§6 /" + CMD + "§e list§8 - §7List all whitelisted players"));
source.sendMessage(Component.text("§6 /" + CMD + "§e remove <player>§8 - §7Remove a player from the whitelist"));
source.sendMessage(Component.text("§6 /" + CMD + "§e set <player> [duration]§8 - §7Add a player to the whitelist"));
}

private void handleClear(CommandSource source, String[] args) {
if (!source.hasPermission(Permissions.CLEAR)) {
sendPermission(source);
return;
}
if (args.length == 1) {
source.sendMessage(Component.text("§cUsage: /" + CMD + " clear <all/expired>"));
return;
Expand All @@ -101,6 +127,10 @@ private void handleClear(CommandSource source, String[] args) {
}

private void handleDisable(CommandSource source) {
if (!source.hasPermission(Permissions.DISABLE)) {
sendPermission(source);
return;
}
if (!whitelist.isEnabled()) {
source.sendMessage(Component.text("§cWhitelist is already disabled"));
return;
Expand All @@ -112,6 +142,10 @@ private void handleDisable(CommandSource source) {
}

private void handleEnable(CommandSource source) {
if (!source.hasPermission(Permissions.ENABLE)) {
sendPermission(source);
return;
}
if (whitelist.isEnabled()) {
source.sendMessage(Component.text("§cWhitelist is already enabled"));
return;
Expand All @@ -123,6 +157,10 @@ private void handleEnable(CommandSource source) {
}

private void handleGet(CommandSource source, String[] args) {
if (!source.hasPermission(Permissions.GET)) {
sendPermission(source);
return;
}
if (args.length == 1) {
source.sendMessage(Component.text("§cUsage: /" + CMD + " get <player>"));
return;
Expand All @@ -142,23 +180,36 @@ private void handleGet(CommandSource source, String[] args) {
}

private void handleKick(CommandSource source) {
source.sendMessage(Component.text("§7Kicked all non whitelisted players"));
if (!source.hasPermission(Permissions.KICK)) {
sendPermission(source);
return;
}

source.sendMessage(Component.text("§7Kicked all non-whitelisted players"));
whitelist.kickNotWhitelisted();
}

private void handleList(CommandSource source) {
if (!source.hasPermission(Permissions.LIST)) {
sendPermission(source);
return;
}
if (whitelist.isEmpty()) {
source.sendMessage(Component.text("§7The whitelist is empty"));
return;
}

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

private void handleRemove(CommandSource source, String[] args) {
if (!source.hasPermission(Permissions.REMOVE)) {
sendPermission(source);
return;
}
if (args.length == 1) {
source.sendMessage(Component.text("§cUsage: /" + CMD + " remove <player>"));
return;
Expand All @@ -179,6 +230,10 @@ private void handleRemove(CommandSource source, String[] args) {
}

private void handleSet(CommandSource source, String[] args) {
if (!source.hasPermission(Permissions.SET)) {
sendPermission(source);
return;
}
if (args.length == 1) {
source.sendMessage(Component.text("§cUsage: /" + CMD + " set <player> [duration]"));
return;
Expand Down Expand Up @@ -206,6 +261,10 @@ private void handleSet(CommandSource source, String[] args) {

long expire = parser.isPermanent() ? -1 : System.currentTimeMillis() + parser.getMillis();
whitelist.set(player, expire);
source.sendMessage(Component.text("§7Set whitelist of §f%s§7 to §a%s§7!".formatted(player.name(), TimeUtil.formatToSeconds(parser.getMillis()))));
source.sendMessage(Component.text("§7Set whitelist of §f%s§7 to §a%s§7!".formatted(player.name(), TimeUtil.toFormattedString(parser.getMillis()))));
}

private void sendPermission(CommandSource source) {
source.sendMessage(Component.text("§cYou do not have permission to use this command"));
}
}
4 changes: 2 additions & 2 deletions src/main/java/me/bigvirusboi/whitelist/manager/Whitelist.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import me.bigvirusboi.whitelist.cache.CachedPlayer;
import me.bigvirusboi.whitelist.cache.PlayerCache;
import me.bigvirusboi.whitelist.util.Permissions;
import me.bigvirusboi.whitelist.util.TimeUtil;
import me.bigvirusboi.whitelist.util.time.TimeUtil;
import me.bigvirusboi.whitelist.util.Util;
import net.kyori.adventure.text.Component;

Expand Down Expand Up @@ -49,7 +49,7 @@ public String getDurationFormatted(CachedPlayer player) {
return "§cEXPIRED";
}

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

public long getDuration(UUID uuid) {
Expand Down
106 changes: 0 additions & 106 deletions src/main/java/me/bigvirusboi/whitelist/util/TimeUtil.java

This file was deleted.

Loading

0 comments on commit cf5b752

Please sign in to comment.