use of org.geysermc.floodgate.platform.command.CommandUtil in project Floodgate by GeyserMC.
the class WhitelistCommand method performCommand.
public void performCommand(CommandContext<UserAudience> 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());
}
if (name.length() < 1 || name.length() > 16) {
sender.sendMessage(Message.INVALID_USERNAME);
return;
}
// todo let it use translations
String tempName = name;
if (config.isReplaceSpaces()) {
tempName = tempName.replace(' ', '_');
}
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) {
sender.sendMessage(Message.API_UNAVAILABLE);
error.printStackTrace();
return;
}
JsonObject response = result.getResponse();
if (!result.isCodeOk()) {
sender.sendMessage(Message.UNEXPECTED_ERROR);
logger.error("Got an error from requesting the xuid of a Bedrock player: {}", response.get("message").getAsString());
}
JsonElement xuidElement = response.get("xuid");
if (xuidElement == null) {
sender.sendMessage(Message.USER_NOT_FOUND);
return;
}
String xuid = xuidElement.getAsString();
CommandUtil commandUtil = context.get("CommandUtil");
try {
if (add) {
if (commandUtil.whitelistPlayer(xuid, correctName)) {
sender.sendMessage(Message.PLAYER_ADDED, strippedName);
} else {
sender.sendMessage(Message.PLAYER_ALREADY_WHITELISTED, strippedName);
}
} else {
if (commandUtil.removePlayerFromWhitelist(xuid, correctName)) {
sender.sendMessage(Message.PLAYER_REMOVED, strippedName);
} else {
sender.sendMessage(Message.PLAYER_NOT_WHITELISTED, strippedName);
}
}
} catch (Exception exception) {
logger.error("An unexpected error happened while executing the whitelist command", exception);
}
});
}
use of org.geysermc.floodgate.platform.command.CommandUtil in project Floodgate by GeyserMC.
the class VelocityPlatformModule method configure.
@Override
protected void configure() {
VelocityCommandUtil commandUtil = new VelocityCommandUtil();
requestInjection(commandUtil);
bind(CommandUtil.class).to(VelocityCommandUtil.class);
bind(VelocityCommandUtil.class).toInstance(commandUtil);
Injector child = guice.createChildInjector(new CloudInjectionModule<>(UserAudience.class, CommandExecutionCoordinator.simpleCoordinator(), commandUtil::getAudience, audience -> (CommandSource) audience.source()));
CommandManager<UserAudience> commandManager = child.getInstance(new Key<VelocityCommandManager<UserAudience>>() {
});
bind(new Key<CommandManager<UserAudience>>() {
}).toInstance(commandManager);
commandManager.registerCommandPreProcessor(new FloodgateCommandPreprocessor<>(commandUtil));
}
Aggregations