下面列出了怎么用org.bukkit.BanList的API类实例代码及写法,或者点击链接到github查看源代码。
public String getPunishmentScreenFromName(Player viewer, String name) {
if (isBanStillValid(name)) {
BanEntry ban = Bukkit.getBanList(BanList.Type.NAME).getBanEntry(name);
PunishmentType type =
ban.getExpiration() != null ? PunishmentType.TEMP_BAN : PunishmentType.BAN;
Duration length =
type.equals(PunishmentType.TEMP_BAN)
? Duration.between(Instant.now(), ban.getExpiration().toInstant())
: null;
return formatPunishmentScreen(
type, TextComponent.of(ban.getSource(), TextColor.AQUA), ban.getReason(), length);
}
return null;
}
@Override
public void onGuildBan(GuildBanEvent event) {
UUID linkedUuid = DiscordSRV.getPlugin().getAccountLinkManager().getUuid(event.getUser().getId());
if (linkedUuid == null) {
DiscordSRV.debug("Not handling ban for user " + event.getUser() + " because they didn't have a linked account");
return;
}
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(linkedUuid);
if (!offlinePlayer.hasPlayedBefore()) return;
if (!DiscordSRV.config().getBoolean("BanSynchronizationDiscordToMinecraft")) {
DiscordSRV.debug("Not handling ban for user " + event.getUser() + " because doing so is disabled in the config");
return;
}
Bukkit.getBanList(BanList.Type.NAME).addBan(offlinePlayer.getName(), LangUtil.Message.BAN_DISCORD_TO_MINECRAFT.toString(), null, "Discord");
}
@Override
public void onGuildUnban(GuildUnbanEvent event) {
UUID linkedUuid = DiscordSRV.getPlugin().getAccountLinkManager().getUuid(event.getUser().getId());
if (linkedUuid == null) {
DiscordSRV.debug("Not handling unban for user " + event.getUser() + " because they didn't have a linked account");
return;
}
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(linkedUuid);
if (!offlinePlayer.hasPlayedBefore()) return;
if (!DiscordSRV.config().getBoolean("BanSynchronizationDiscordToMinecraft")) {
DiscordSRV.debug("Not handling unban for user " + event.getUser() + " because doing so is disabled in the config");
return;
}
String playerName = offlinePlayer.getName();
if (StringUtils.isNotBlank(playerName)) //this literally should not happen but intellij likes bitching about not null checking
Bukkit.getBanList(BanList.Type.NAME).pardon(playerName);
}
@Override
public void run() {
timers.remove(uuid);
final Player player = Bukkit.getPlayer(uuid);
player.sendMessage(message);
if (types.contains(BanType.BAN)) {
final Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.SECOND, (int) duration);
Bukkit.getBanList(BanList.Type.NAME).addBan(player.getName(), message, calendar.getTime(), "UHC");
}
if (types.contains(BanType.MOVE_SERVER)) {
player.sendPluginMessage(plugin, "BungeeCord", ("Connect" + serverName).getBytes());
}
if (types.contains(BanType.KICK)) {
player.kickPlayer(message);
}
}
private void banPlayer(
String target, String reason, Component source, @Nullable Instant expires) {
Bukkit.getBanList(BanList.Type.NAME)
.addBan(
target,
reason,
expires != null ? Date.from(expires) : null,
TextTranslations.translateLegacy(source, null));
}
public boolean isBanned() {
if (getName() == null) {
return false;
}
return server.getBanList(BanList.Type.NAME).isBanned(getName());
}
public void setBanned(boolean value) {
if (getName() == null) {
return;
}
if (value) {
server.getBanList(BanList.Type.NAME).addBan(getName(), null, null, null);
} else {
server.getBanList(BanList.Type.NAME).pardon(getName());
}
}
@Override
public BanList getBanList(BanList.Type type){
Validate.notNull(type, "Type cannot be null");
switch(type){
case IP:
return new CraftIpBanList(playerList.getBannedIPs());
case NAME:
default:
return new CraftProfileBanList(playerList.func_152608_h());
}
}
@EventHandler
public void onPlayerConnect(PlayerLoginEvent event) {
if (Bukkit.getBanList(BanList.Type.NAME).isBanned(event.getPlayer().getName())) {
event.disallow(PlayerLoginEvent.Result.KICK_BANNED, Bukkit.getBanList(BanList.Type.NAME).getBanEntry(event.getPlayer().getName()).getReason());
} else if (Rank.whitelistBypass(event.getPlayer().getUniqueId())) {
event.allow();
}
}
@Command(aliases = {"ban", "pb"}, usage = "<player> [reason]", desc = "Ban a player.", min = 1)
@CommandPermissions("cardinal.punish.ban")
public static void ban(CommandContext args, CommandSender sender) throws CommandException {
Bukkit.getScheduler().runTaskAsynchronously(Cardinal.getInstance(), new AsyncCommand(args, sender) {
@Override
public void run() {
final OfflinePlayer banned = Bukkit.getOfflinePlayer(args.getString(0));
if (!sender.isOp() && banned.isOp()) {
sender.sendMessage(ChatColor.RED + ChatConstant.ERROR_PLAYER_NOT_AFFECTED.getMessage(ChatUtil.getLocale(sender)));
return;
}
if (banned.isBanned()) {
sender.sendMessage(ChatColor.RED + ChatConstant.ERROR_PLAYER_ALREADY_BANNED.getMessage(ChatUtil.getLocale(sender)));
return;
}
Bukkit.getScheduler().callSyncMethod(Cardinal.getInstance(), new Callable<Void>() {
@Override
public Void call() throws Exception {
String reason = args.argsLength() > 1 ? args.getJoinedStrings(1) : "You have been banned!";
if (banned.isOnline()) {
banned.getPlayer().kickPlayer(ChatColor.RED + "Permanently Banned" + ChatColor.GOLD + " \u00BB " + ChatColor.AQUA + reason);
}
Bukkit.broadcastMessage(Players.getName(sender) + ChatColor.GOLD + " \u00BB Permanent Ban \u00BB " + Players.getName(banned) + ChatColor.GOLD + " \u00BB " + reason);
Bukkit.getBanList(BanList.Type.NAME).addBan(args.getString(0), ChatColor.RED + "Permanently Banned" + ChatColor.GOLD + " \u00BB " + reason, null, sender.getName());
return null;
}
});
}
});
}
@Command(
aliases = {"ipban", "banip", "ipb"},
usage = "<player|ip address> <reason> -s (silent)",
desc = "IP Ban a player from the server",
flags = "s",
perms = Permissions.BAN)
public void ipBan(
Audience viewer,
CommandSender sender,
MatchManager manager,
String target,
@Text String reason,
@Switch('s') boolean silent)
throws CommandException {
silent = checkSilent(silent, sender);
Player targetPlayer = Bukkit.getPlayerExact(target);
String address = target; // Default address to what was input
if (targetPlayer != null) {
// If target is a player, fetch their IP and use that
address = targetPlayer.getAddress().getAddress().getHostAddress();
} else if (getBanWithMatchingName(target).isPresent()) {
address = getBanWithMatchingName(target).get().getAddress();
}
// Validate if the IP is a valid IP
if (InetAddresses.isInetAddress(address)) {
// Special method for IP Ban
Bukkit.getBanList(BanList.Type.IP)
.addBan(
address,
reason,
null,
TextTranslations.translateLegacy(
UsernameFormatUtils.formatStaffName(sender, manager.getMatch(sender)), sender));
int onlineBans = 0;
// Check all online players to find those with same IP.
for (Player player : Bukkit.getOnlinePlayers()) {
MatchPlayer matchPlayer = manager.getPlayer(player);
if (player.getAddress().getAddress().getHostAddress().equals(address)) {
// Kick players with same IP
if (punish(PunishmentType.BAN, matchPlayer, sender, reason, silent)) {
// Ban username to prevent rejoining
banPlayer(
player.getName(),
reason,
UsernameFormatUtils.formatStaffName(sender, matchPlayer.getMatch()),
null);
player.kickPlayer(
formatPunishmentScreen(
PunishmentType.BAN,
UsernameFormatUtils.formatStaffName(sender, manager.getMatch(sender)),
reason,
null));
onlineBans++;
}
}
}
Component formattedTarget = TextComponent.of(target, TextColor.DARK_AQUA);
if (onlineBans > 0) {
viewer.sendWarning(
TranslatableComponent.of(
"moderation.ipBan.bannedWithAlts",
formattedTarget,
TextComponent.of(
Integer.toString(
targetPlayer == null ? onlineBans : Math.max(0, onlineBans - 1)),
TextColor.AQUA)));
} else {
viewer.sendMessage(
TranslatableComponent.of("moderation.ipBan.banned", TextColor.RED, formattedTarget));
}
} else {
viewer.sendMessage(
TranslatableComponent.of(
"moderation.ipBan.invalidIP",
TextColor.GRAY,
TextComponent.of(address, TextColor.RED, TextDecoration.ITALIC)));
}
}
@Command(
aliases = {"baninfo", "lookup", "l"},
usage = "[player/uuid]",
desc = "Lookup baninfo about a player",
perms = Permissions.STAFF)
public void banInfo(Audience viewer, CommandSender sender, String target)
throws CommandException {
if (!XMLUtils.USERNAME_REGEX.matcher(target).matches()) {
UUID uuid = UUID.fromString(target);
Username username = PGM.get().getDatastore().getUsername(uuid);
if (username.getNameLegacy() != null) {
target = username.getNameLegacy();
} else {
throw new CommandException(
TextTranslations.translateLegacy(
TranslatableComponent.of(
"command.notJoinedServer",
TextColor.RED,
TextComponent.of(target, TextColor.AQUA)),
sender));
}
}
BanEntry ban = Bukkit.getBanList(BanList.Type.NAME).getBanEntry(target);
if (ban == null
|| ban.getExpiration() != null && ban.getExpiration().toInstant().isBefore(Instant.now())) {
throw new CommandException(
TextTranslations.translateLegacy(
TranslatableComponent.of(
"moderation.records.lookupNone",
TextColor.GRAY,
TextComponent.of(target, TextColor.DARK_AQUA)),
sender));
}
Component header =
TextComponent.builder()
.append(TranslatableComponent.of("moderation.records.header", TextColor.GRAY))
.append(BROADCAST_DIV)
.append(target, TextColor.DARK_AQUA, TextDecoration.ITALIC)
.build();
boolean expires = ban.getExpiration() != null;
Component banType = TranslatableComponent.of("moderation.type.ban", TextColor.GOLD);
Component expireDate = TextComponent.empty();
if (expires) {
String length =
TextTranslations.translateLegacy(
PeriodFormats.briefNaturalApproximate(
ban.getCreated().toInstant(), ban.getExpiration().toInstant()),
sender);
Component remaining =
PeriodFormats.briefNaturalApproximate(Instant.now(), ban.getExpiration().toInstant())
.color(TextColor.YELLOW);
banType =
TranslatableComponent.of(
"moderation.type.temp_ban",
TextColor.GOLD,
TextComponent.of(
length.lastIndexOf('s') != -1
? length.substring(0, length.lastIndexOf('s'))
: length));
expireDate = TranslatableComponent.of("moderation.screen.expires", TextColor.GRAY, remaining);
}
Component createdAgo =
PeriodFormats.relativePastApproximate(ban.getCreated().toInstant()).color(TextColor.GRAY);
Component banTypeFormatted =
TranslatableComponent.of("moderation.type", TextColor.GRAY, banType);
Component reason =
TranslatableComponent.of(
"moderation.records.reason",
TextColor.GRAY,
TextComponent.of(ban.getReason(), TextColor.RED));
Component source =
TextComponent.builder()
.append(
TranslatableComponent.of(
"moderation.screen.signoff",
TextColor.GRAY,
TextComponent.of(ban.getSource(), TextColor.AQUA)))
.append(TextComponent.space())
.append(createdAgo)
.build();
viewer.sendMessage(TextFormatter.horizontalLineHeading(sender, header, TextColor.DARK_PURPLE));
viewer.sendMessage(banTypeFormatted);
viewer.sendMessage(reason);
viewer.sendMessage(source);
if (expires) {
viewer.sendMessage(expireDate);
}
}
private boolean isBanStillValid(String name) {
return Bukkit.getBanList(BanList.Type.NAME).isBanned(name);
}
@Override
public void banIP(String address) {
Validate.notNull(address, "Address cannot be null.");
this.getBanList(org.bukkit.BanList.Type.IP).addBan(address, null, null, null);
}
@Override
public void unbanIP(String address) {
Validate.notNull(address, "Address cannot be null.");
this.getBanList(org.bukkit.BanList.Type.IP).pardon(address);
}
private static BanList getBanList() {
return Bukkit.getServer().getBanList(BanList.Type.NAME);
}
/**
* Adds a ban to the this list. If a previous ban exists, this will
* update the previous entry.
*
* @param ip the ip of the ban
* @param reason reason for the ban, null indicates implementation default
* @param expires date for the ban's expiration (unban), or null to imply
* forever
* @param source source of the ban, null indicates implementation default
* @return the entry for the newly created ban, or the entry for the
* (updated) previous ban
*/
public BanEntry banIp(String ip, String reason, Date expires, String source) {
return Bukkit.getServer().getBanList(BanList.Type.IP).addBan(ip, reason, expires, source);
}