1
0
mirror of https://github.com/GeyserMC/Floodgate.git synced 2025-12-19 14:59:20 +00:00

Allow a UUID to be used in the Floodgate whitelist command

No API call needs to be made this way.
This commit is contained in:
Camotoy
2021-10-18 09:16:36 -04:00
parent 966ccffcf9
commit 4c66bb7a9f
4 changed files with 78 additions and 14 deletions

View File

@@ -34,7 +34,9 @@ import cloud.commandframework.context.CommandContext;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.inject.Inject; import com.google.inject.Inject;
import java.util.UUID;
import lombok.Getter; import lombok.Getter;
import org.geysermc.floodgate.api.FloodgateApi;
import org.geysermc.floodgate.api.logger.FloodgateLogger; import org.geysermc.floodgate.api.logger.FloodgateLogger;
import org.geysermc.floodgate.config.FloodgateConfig; import org.geysermc.floodgate.config.FloodgateConfig;
import org.geysermc.floodgate.config.ProxyFloodgateConfig; 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.platform.command.TranslatableMessage;
import org.geysermc.floodgate.player.UserAudience; import org.geysermc.floodgate.player.UserAudience;
import org.geysermc.floodgate.player.UserAudienceArgument; import org.geysermc.floodgate.player.UserAudienceArgument;
import org.geysermc.floodgate.player.UserAudienceArgument.PlayerType;
import org.geysermc.floodgate.util.Constants; import org.geysermc.floodgate.util.Constants;
import org.geysermc.floodgate.util.HttpUtils; import org.geysermc.floodgate.util.HttpUtils;
import org.geysermc.floodgate.util.Permissions; import org.geysermc.floodgate.util.Permissions;
@@ -59,12 +62,12 @@ public class WhitelistCommand implements FloodgateCommand {
commandManager.command(builder commandManager.command(builder
.literal("add", "a") .literal("add", "a")
.argument(UserAudienceArgument.of("player", true)) .argument(UserAudienceArgument.of("player", true, true, PlayerType.ONLY_BEDROCK))
.handler(context -> performCommand(context, true))); .handler(context -> performCommand(context, true)));
return builder return builder
.literal("remove", "r") .literal("remove", "r")
.argument(UserAudienceArgument.of("player", true)) .argument(UserAudienceArgument.of("player", true, true, PlayerType.ONLY_BEDROCK))
.handler(context -> performCommand(context, false)) .handler(context -> performCommand(context, false))
.build(); .build();
} }
@@ -72,8 +75,39 @@ public class WhitelistCommand implements FloodgateCommand {
public void performCommand(CommandContext<UserAudience> context, boolean add) { public void performCommand(CommandContext<UserAudience> context, boolean add) {
UserAudience sender = context.getSender(); UserAudience sender = context.getSender();
UserAudience player = context.get("player"); UserAudience player = context.get("player");
UUID uuid = player.uuid();
String name = player.username(); 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())) { if (name.startsWith(config.getUsernamePrefix())) {
name = name.substring(config.getUsernamePrefix().length()); name = name.substring(config.getUsernamePrefix().length());
} }
@@ -92,6 +126,7 @@ public class WhitelistCommand implements FloodgateCommand {
final String correctName = config.getUsernamePrefix() + tempName; final String correctName = config.getUsernamePrefix() + tempName;
final String strippedName = name; 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) HttpUtils.asyncGet(Constants.GET_XUID_URL + name)
.whenComplete((result, error) -> { .whenComplete((result, error) -> {
if (error != null) { if (error != null) {

View File

@@ -31,6 +31,7 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.floodgate.player.UserAudience; import org.geysermc.floodgate.player.UserAudience;
import org.geysermc.floodgate.player.UserAudienceArgument.PlayerType; 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 * 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. * Defaults to false when this platform doesn't support whitelisting.
*/ */
default boolean whitelistPlayer(String xuid, String username) { 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; return false;
} }
@@ -117,6 +131,19 @@ public interface CommandUtil {
* whitelisted. Defaults to false when this platform doesn't support whitelisting. * whitelisted. Defaults to false when this platform doesn't support whitelisting.
*/ */
default boolean removePlayerFromWhitelist(String xuid, String username) { 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; return false;
} }
} }

View File

@@ -107,7 +107,7 @@ public final class SpigotCommandUtil implements CommandUtil {
@Override @Override
public @NonNull Collection<String> getOnlineUsernames(@NonNull PlayerType limitTo) { public @NonNull Collection<String> getOnlineUsernames(@NonNull PlayerType limitTo) {
Collection<Player> players = (Collection<Player>) server.getOnlinePlayers(); Collection<? extends Player> players = server.getOnlinePlayers();
Collection<String> usernames = new ArrayList<>(); Collection<String> usernames = new ArrayList<>();
switch (limitTo) { switch (limitTo) {
@@ -170,13 +170,13 @@ public final class SpigotCommandUtil implements CommandUtil {
} }
@Override @Override
public boolean whitelistPlayer(String xuid, String username) { public boolean whitelistPlayer(UUID uuid, String username) {
return WhitelistUtils.addPlayer(xuid, username); return WhitelistUtils.addPlayer(uuid, username);
} }
@Override @Override
public boolean removePlayerFromWhitelist(String xuid, String username) { public boolean removePlayerFromWhitelist(UUID uuid, String username) {
return WhitelistUtils.removePlayer(xuid, username); return WhitelistUtils.removePlayer(uuid, username);
} }
public String translateAndTransform(String locale, TranslatableMessage message, Object... args) { 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); return message.translateMessage(manager, locale, args);
} }
protected Player cast(Object instance) { private Player cast(Object instance) {
try { try {
return (Player) instance; return (Player) instance;
} catch (ClassCastException exception) { } catch (ClassCastException exception) {

View File

@@ -34,21 +34,23 @@ import static org.geysermc.floodgate.util.ClassNames.REMOVE_WHITELIST_ENTRY;
import static org.geysermc.floodgate.util.ClassNames.WHITELIST_ENTRY; import static org.geysermc.floodgate.util.ClassNames.WHITELIST_ENTRY;
import com.mojang.authlib.GameProfile; import com.mojang.authlib.GameProfile;
import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@SuppressWarnings("ConstantConditions") @SuppressWarnings("ConstantConditions")
public final class WhitelistUtils { public final class WhitelistUtils {
/** /**
* Whitelist the given Bedrock player. * 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 * @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 * @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(); Object whitelist = getWhitelist();
GameProfile profile = new GameProfile(Utils.getJavaUuid(xuid), username); GameProfile profile = new GameProfile(uuid, username);
if (ReflectionUtils.castedInvoke(whitelist, IS_WHITELISTED, profile)) { if (ReflectionUtils.castedInvoke(whitelist, IS_WHITELISTED, profile)) {
return false; return false;
@@ -63,15 +65,15 @@ public final class WhitelistUtils {
/** /**
* Removes the given Bedrock player from the whitelist. * 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 * @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 * @return true if the player has been removed from the whitelist, false if the player wasn't
* whitelisted * whitelisted
*/ */
public static boolean removePlayer(String xuid, String username) { public static boolean removePlayer(UUID uuid, String username) {
Object whitelist = getWhitelist(); 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)) { if (!(boolean) ReflectionUtils.castedInvoke(whitelist, IS_WHITELISTED, profile)) {
return false; return false;