use of org.lanternpowered.server.command.element.GenericArguments2 in project LanternServer by LanternPowered.
the class CommandBanIp method completeSpec.
@Override
public void completeSpec(PluginContainer pluginContainer, CommandSpec.Builder specBuilder) {
specBuilder.arguments(GenericArguments.string(Text.of("address")), GenericArguments.optional(GenericArguments2.remainingString(Text.of("reason")))).executor((src, args) -> {
final String target = args.<String>getOne("address").get();
final String reason = args.<String>getOne("reason").orElse(null);
InetAddress address;
if (IP_PATTERN.matcher(target).matches()) {
try {
address = InetAddress.getByName(target);
} catch (UnknownHostException e) {
throw new IllegalStateException("Unable to parse a valid InetAddress: " + target, e);
}
} else {
// Ip address failed, try to find a player
Optional<Player> player = Sponge.getGame().getServer().getPlayer(target);
if (!player.isPresent()) {
throw new CommandException(t("commands.banip.invalid"));
}
address = player.get().getConnection().getAddress().getAddress();
}
final BanService banService = Sponge.getServiceManager().provideUnchecked(BanService.class);
final Ban ban = Ban.builder().type(BanTypes.IP).address(address).reason(reason == null ? null : Text.of(reason)).source(src).build();
banService.addBan(ban);
final List<LanternPlayer> playersToKick = Lantern.getServer().getRawOnlinePlayers().stream().filter(player -> player.getConnection().getAddress().getAddress().equals(address)).collect(Collectors.toList());
if (!playersToKick.isEmpty()) {
final Text kickReason = t("multiplayer.disconnect.ip_banned");
for (LanternPlayer player : playersToKick) {
player.kick(kickReason);
}
}
src.sendMessage(t("commands.banip.success", address.toString()));
return CommandResult.success();
});
}
Aggregations