diff --git a/common/src/main/java/org/geysermc/floodgate/command/WhitelistCommand.java b/common/src/main/java/org/geysermc/floodgate/command/WhitelistCommand.java index 22365d68..21d31615 100644 --- a/common/src/main/java/org/geysermc/floodgate/command/WhitelistCommand.java +++ b/common/src/main/java/org/geysermc/floodgate/command/WhitelistCommand.java @@ -34,7 +34,9 @@ import cloud.commandframework.context.CommandContext; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.inject.Inject; +import java.util.UUID; import lombok.Getter; +import org.geysermc.floodgate.api.FloodgateApi; import org.geysermc.floodgate.api.logger.FloodgateLogger; import org.geysermc.floodgate.config.FloodgateConfig; import org.geysermc.floodgate.config.ProxyFloodgateConfig; @@ -43,6 +45,7 @@ import org.geysermc.floodgate.platform.command.FloodgateCommand; import org.geysermc.floodgate.platform.command.TranslatableMessage; import org.geysermc.floodgate.player.UserAudience; import org.geysermc.floodgate.player.UserAudienceArgument; +import org.geysermc.floodgate.player.UserAudienceArgument.PlayerType; import org.geysermc.floodgate.util.Constants; import org.geysermc.floodgate.util.HttpUtils; import org.geysermc.floodgate.util.Permissions; @@ -59,12 +62,12 @@ public class WhitelistCommand implements FloodgateCommand { commandManager.command(builder .literal("add", "a") - .argument(UserAudienceArgument.of("player", true)) + .argument(UserAudienceArgument.of("player", true, true, PlayerType.ONLY_BEDROCK)) .handler(context -> performCommand(context, true))); return builder .literal("remove", "r") - .argument(UserAudienceArgument.of("player", true)) + .argument(UserAudienceArgument.of("player", true, true, PlayerType.ONLY_BEDROCK)) .handler(context -> performCommand(context, false)) .build(); } @@ -72,8 +75,39 @@ public class WhitelistCommand implements FloodgateCommand { public void performCommand(CommandContext context, boolean add) { UserAudience sender = context.getSender(); UserAudience player = context.get("player"); + UUID uuid = player.uuid(); String name = player.username(); + if (name == null && uuid == null) { + sender.sendMessage(Message.UNEXPECTED_ERROR); + return; + } + + if (uuid != null) { + if (!FloodgateApi.getInstance().isFloodgateId(uuid)) { + sender.sendMessage(Message.INVALID_USERNAME); + return; + } + + CommandUtil commandUtil = context.get("CommandUtil"); + + if (add) { + if (commandUtil.whitelistPlayer(uuid, "unknown")) { + sender.sendMessage(Message.PLAYER_ADDED, uuid.toString()); + } else { + sender.sendMessage(Message.PLAYER_ALREADY_WHITELISTED, + uuid.toString()); + } + } else { + if (commandUtil.removePlayerFromWhitelist(uuid, "unknown")) { + sender.sendMessage(Message.PLAYER_REMOVED, uuid.toString()); + } else { + sender.sendMessage(Message.PLAYER_NOT_WHITELISTED, uuid.toString()); + } + } + return; + } + if (name.startsWith(config.getUsernamePrefix())) { name = name.substring(config.getUsernamePrefix().length()); } @@ -92,6 +126,7 @@ public class WhitelistCommand implements FloodgateCommand { final String correctName = config.getUsernamePrefix() + tempName; final String strippedName = name; + // We need to get the UUID of the player if it's not manually specified HttpUtils.asyncGet(Constants.GET_XUID_URL + name) .whenComplete((result, error) -> { if (error != null) { diff --git a/common/src/main/java/org/geysermc/floodgate/platform/command/CommandUtil.java b/common/src/main/java/org/geysermc/floodgate/platform/command/CommandUtil.java index e8ef6922..49df6491 100644 --- a/common/src/main/java/org/geysermc/floodgate/platform/command/CommandUtil.java +++ b/common/src/main/java/org/geysermc/floodgate/platform/command/CommandUtil.java @@ -31,6 +31,7 @@ import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; import org.geysermc.floodgate.player.UserAudience; import org.geysermc.floodgate.player.UserAudienceArgument.PlayerType; +import org.geysermc.floodgate.util.Utils; /** * An interface used across all Floodgate platforms to simple stuff in commands like kicking players @@ -105,6 +106,19 @@ public interface CommandUtil { * Defaults to false when this platform doesn't support whitelisting. */ default boolean whitelistPlayer(String xuid, String username) { + UUID uuid = Utils.getJavaUuid(xuid); + return whitelistPlayer(uuid, username); + } + + /** + * Whitelist the given Bedrock player. + * + * @param uuid the UUID of the username to be whitelisted + * @param username the username to be whitelisted + * @return true if the player has been whitelisted, false if the player was already whitelisted. + * Defaults to false when this platform doesn't support whitelisting. + */ + default boolean whitelistPlayer(UUID uuid, String username) { return false; } @@ -117,6 +131,19 @@ public interface CommandUtil { * whitelisted. Defaults to false when this platform doesn't support whitelisting. */ default boolean removePlayerFromWhitelist(String xuid, String username) { + UUID uuid = Utils.getJavaUuid(xuid); + return removePlayerFromWhitelist(uuid, username); + } + + /** + * Removes the given Bedrock player from the whitelist. + * + * @param uuid the UUID of the username to be removed from the whitelist + * @param username the username to be removed from the whitelist + * @return true if the player has been removed from the whitelist, false if the player wasn't + * whitelisted. Defaults to false when this platform doesn't support whitelisting. + */ + default boolean removePlayerFromWhitelist(UUID uuid, String username) { return false; } } diff --git a/spigot/src/main/java/org/geysermc/floodgate/util/SpigotCommandUtil.java b/spigot/src/main/java/org/geysermc/floodgate/util/SpigotCommandUtil.java index 35cef933..cd084298 100644 --- a/spigot/src/main/java/org/geysermc/floodgate/util/SpigotCommandUtil.java +++ b/spigot/src/main/java/org/geysermc/floodgate/util/SpigotCommandUtil.java @@ -107,7 +107,7 @@ public final class SpigotCommandUtil implements CommandUtil { @Override public @NonNull Collection getOnlineUsernames(@NonNull PlayerType limitTo) { - Collection players = (Collection) server.getOnlinePlayers(); + Collection players = server.getOnlinePlayers(); Collection usernames = new ArrayList<>(); switch (limitTo) { @@ -170,13 +170,13 @@ public final class SpigotCommandUtil implements CommandUtil { } @Override - public boolean whitelistPlayer(String xuid, String username) { - return WhitelistUtils.addPlayer(xuid, username); + public boolean whitelistPlayer(UUID uuid, String username) { + return WhitelistUtils.addPlayer(uuid, username); } @Override - public boolean removePlayerFromWhitelist(String xuid, String username) { - return WhitelistUtils.removePlayer(xuid, username); + public boolean removePlayerFromWhitelist(UUID uuid, String username) { + return WhitelistUtils.removePlayer(uuid, username); } public String translateAndTransform(String locale, TranslatableMessage message, Object... args) { @@ -184,7 +184,7 @@ public final class SpigotCommandUtil implements CommandUtil { return message.translateMessage(manager, locale, args); } - protected Player cast(Object instance) { + private Player cast(Object instance) { try { return (Player) instance; } catch (ClassCastException exception) { diff --git a/spigot/src/main/java/org/geysermc/floodgate/util/WhitelistUtils.java b/spigot/src/main/java/org/geysermc/floodgate/util/WhitelistUtils.java index ef8b274b..a8ff0e2e 100644 --- a/spigot/src/main/java/org/geysermc/floodgate/util/WhitelistUtils.java +++ b/spigot/src/main/java/org/geysermc/floodgate/util/WhitelistUtils.java @@ -34,21 +34,23 @@ import static org.geysermc.floodgate.util.ClassNames.REMOVE_WHITELIST_ENTRY; import static org.geysermc.floodgate.util.ClassNames.WHITELIST_ENTRY; import com.mojang.authlib.GameProfile; +import java.util.UUID; import org.bukkit.Bukkit; @SuppressWarnings("ConstantConditions") public final class WhitelistUtils { + /** * Whitelist the given Bedrock player. * - * @param xuid the xuid of the Bedrock player to be whitelisted + * @param uuid the UUID of the Bedrock player to be whitelisted * @param username the username of the Bedrock player to be whitelisted * @return true if the player has been whitelisted, false if the player is already whitelisted */ - public static boolean addPlayer(String xuid, String username) { + public static boolean addPlayer(UUID uuid, String username) { Object whitelist = getWhitelist(); - GameProfile profile = new GameProfile(Utils.getJavaUuid(xuid), username); + GameProfile profile = new GameProfile(uuid, username); if (ReflectionUtils.castedInvoke(whitelist, IS_WHITELISTED, profile)) { return false; @@ -63,15 +65,15 @@ public final class WhitelistUtils { /** * Removes the given Bedrock player from the whitelist. * - * @param xuid the xuid of the Bedrock player to be removed + * @param uuid the UUID of the Bedrock player to be removed * @param username the username of the Bedrock player to be removed * @return true if the player has been removed from the whitelist, false if the player wasn't * whitelisted */ - public static boolean removePlayer(String xuid, String username) { + public static boolean removePlayer(UUID uuid, String username) { Object whitelist = getWhitelist(); - GameProfile profile = new GameProfile(Utils.getJavaUuid(xuid), username); + GameProfile profile = new GameProfile(uuid, username); if (!(boolean) ReflectionUtils.castedInvoke(whitelist, IS_WHITELISTED, profile)) { return false;