下面列出了怎么用org.bukkit.BanEntry的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 String getBanReason(PlayerIdentity playerIdentity) {
final BanEntry banEntry = getBanEntry(playerIdentity);
if (banEntry == null)
return null;
return banEntry.getReason();
}
@Override
public String getBanOperator(PlayerIdentity playerIdentity) {
final BanEntry banEntry = getBanEntry(playerIdentity);
if (banEntry == null)
return null;
return banEntry.getSource();
}
@Override
public Date getBanExpiration(PlayerIdentity playerIdentity) {
final BanEntry banEntry = getBanEntry(playerIdentity);
if (banEntry == null)
return null;
return banEntry.getExpiration();
}
@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 static BanEntry getBanEntry(PlayerIdentity playerIdentity) {
return getBanList().getBanEntry(playerIdentity.getName());
}
/**
* 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);
}