Java源码示例:org.spongepowered.api.util.ban.Ban
示例1
private void addDefaultAddons(Player player) {
TextFormat link = TextFormat.of(TextColors.BLUE, TextStyles.UNDERLINE);
if (player.hasPermission(PERM_KICK)) {
addAddon(listPlayer -> Text.builder("Kick").format(link).onClick(Utils.execClick(view -> listPlayer.kick())).build());
}
if (player.hasPermission(PERM_BAN)) {
addAddon(listPlayer -> Text.builder("Ban").format(link)
.onClick(Utils.execClick(view -> Sponge.getServiceManager().provideUnchecked(BanService.class).addBan(Ban.of(listPlayer.getProfile()))))
.build());
}
}
示例2
public BanView(Ban value) {
super(value);
this.createdOn = value.getCreationDate();
this.expiresOn = value.getExpirationDate().orElse(null);
this.reason = value.getReason().orElse(null);
this.banSource = value.getBanSource().orElse(null);
this.commandSource = value.getBanCommandSource().orElse(null);
}
示例3
@Override
public void run() {
if (this.ban) {
final Ban banProfile = Ban.builder()
.type(BanTypes.PROFILE)
.source(this.source)
.profile(player.getProfile())
.reason(this.reason)
.build();
this.banService.addBan(banProfile);
this.player.kick(this.reason);
} else {
this.player.kick(this.reason);
}
}
示例4
public CommandResult execute(CommandSource src, CommandContext ctx) throws CommandException
{
Game game = EssentialCmds.getEssentialCmds().getGame();
User player = ctx.<User> getOne("player").get();
String time = ctx.<String> getOne("time").get();
String reason = ctx.<String> getOne("reason").orElse("The BanHammer has spoken!");
BanService srv = game.getServiceManager().provide(BanService.class).get();
if (srv.isBanned(player.getProfile()))
{
src.sendMessage(Text.of(TextColors.RED, "That player has already been banned."));
return CommandResult.empty();
}
srv.addBan(Ban.builder()
.type(BanTypes.PROFILE)
.source(src).profile(player.getProfile())
.expirationDate(getInstantFromString(time))
.reason(TextSerializers.formattingCode('&').deserialize(reason))
.build());
if (player.isOnline())
{
player.getPlayer().get().kick(Text.builder()
.append(Text.of(TextColors.DARK_RED, "You have been tempbanned!\n", TextColors.RED, "Reason: "))
.append(TextSerializers.formattingCode('&').deserialize(reason), Text.of("\n"))
.append(Text.of(TextColors.GOLD, "Time: ", TextColors.GRAY, getFormattedString(time)))
.build());
}
src.sendMessage(Text.of(TextColors.GREEN, "Success! ", TextColors.YELLOW, player.getName() + " has been banned."));
return CommandResult.success();
}
示例5
public CommandResult execute(CommandSource src, CommandContext ctx) throws CommandException
{
Game game = EssentialCmds.getEssentialCmds().getGame();
User player = ctx.<User> getOne("player").get();
String reason = ctx.<String> getOne("reason").orElse("The BanHammer has spoken!");
BanService srv = game.getServiceManager().provide(BanService.class).get();
if (srv.isBanned(player.getProfile()))
{
src.sendMessage(Text.of(TextColors.RED, "That player has already been banned."));
return CommandResult.empty();
}
srv.addBan(Ban.builder().type(BanTypes.PROFILE).source(src).profile(player.getProfile()).reason(TextSerializers.formattingCode('&').deserialize(reason)).build());
if (player.isOnline())
{
player.getPlayer().get().kick(Text.builder()
.append(Text.of(TextColors.DARK_RED, "You have been banned!\n ", TextColors.RED, "Reason: "))
.append(TextSerializers.formattingCode('&').deserialize(reason))
.build());
}
src.sendMessage(Text.of(TextColors.GREEN, "Success! ", TextColors.YELLOW, player.getName() + " has been banned."));
return CommandResult.success();
}
示例6
@Listener(order = Order.LATE)
public void onMotd(ClientPingServerEvent event) {
try {
ModuleConfig config = Modules.BAN.get().getConfig().get();
if (!config.get().getNode("ban-motd", "enabled").getBoolean()) return;
String ip = event.getClient().getAddress().getAddress().toString().replace("/", "");
GlobalDataFile file = new GlobalDataFile("ipcache");
if (file.get().getChildrenMap().keySet().contains(ip)) {
//Player
GameProfile profile = Sponge.getServer().getGameProfileManager().get(UUID.fromString(file.get().getNode(ip, "uuid").getString())).get();
InetAddress address = InetAddress.getByName(ip);
//Check if banned
BanService bs = Sponge.getServiceManager().provide(BanService.class).get();
UserStorageService us = Sponge.getServiceManager().provide(UserStorageService.class).get();
if (bs.isBanned(profile) || bs.isBanned(address)) {
Text motd = VariableUtil.replaceVariables(Messages.toText(config.get().getNode("ban-motd", "text").getString()), us.get(profile.getUniqueId()).orElse(null));
//Replace ban vars
Ban ban = bs.isBanned(profile) ? bs.getBanFor(profile).get() : bs.getBanFor(address).get();
Long time = ban.getExpirationDate().map(date -> (date.toEpochMilli() - System.currentTimeMillis())).orElse(-1L);
motd = TextUtil.replace(motd, "%time%", (time == -1L ? Messages.getFormatted("core.time.ever") : Text.of(TimeUtil.format(time))));
motd = TextUtil.replace(motd, "%reason%", ban.getReason().orElse(Messages.getFormatted("ban.command.ban.defaultreason")));
event.getResponse().setDescription(motd);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
示例7
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
GameProfile profile = args.<GameProfile>getOne("player").orElse(null);
InetAddress address = args.<InetAddress>getOne("ip").orElse(null);
Long time = args.<Long>getOne("time").orElse(-1L);
Text reason = args.<String>getOne("reason").map(Messages::toText).orElse(Messages.getFormatted(src, "ban.command.ban.defaultreason"));
//Try to find user
User user = null;
if (profile != null) {
user = Sponge.getServiceManager().provide(UserStorageService.class).get().get(profile).get();
} else {
//Try to find user from ip address
for (GameProfile prof : Sponge.getServer().getGameProfileManager().getCache().getProfiles()) {
PlayerDataFile config = new PlayerDataFile(prof.getUniqueId());
CommentedConfigurationNode node = config.get();
if (node.getNode("lastip").getString("").equalsIgnoreCase(address.toString().replace("/", ""))) {
user = Sponge.getServiceManager().provide(UserStorageService.class).get().get(prof).get();
}
}
}
//If user is present, check exempt
if (user != null) {
if ((BanPermissions.UC_BAN_EXEMPTPOWER.getIntFor(user) > BanPermissions.UC_BAN_POWER.getIntFor(src)) && src instanceof Player) {
throw new ErrorMessageException(Messages.getFormatted(src, "ban.command.ban.exempt", "%player%", user));
}
}
//Ban user
BanService bs = Sponge.getServiceManager().provide(BanService.class).get();
Ban.Builder bb = Ban.builder();
if (profile != null) {
bb = bb.type(BanTypes.PROFILE).profile(profile);
} else {
bb = bb.type(BanTypes.IP).address(address);
}
bb = bb.source(src).startDate(Instant.now());
if (time > 0) bb = bb.expirationDate(Instant.now().plusMillis(time));
bb = bb.reason(reason);
bs.addBan(bb.build());
//Kick player
if (user != null && user.getPlayer().isPresent()) {
if (profile != null) {
user.getPlayer().get().kick(Messages.getFormatted(user.getPlayer().get(), "ban.banned", "%time%", (time == -1L ? Messages.getFormatted("core.time.ever") : TimeUtil.format(time)), "%reason%", reason));
} else {
user.getPlayer().get().kick(Messages.getFormatted(user.getPlayer().get(), "ban.ipbanned", "%time%", (time == -1L ? Messages.getFormatted("core.time.ever") : TimeUtil.format(time)), "%reason%", reason));
}
}
//Send message
if (profile != null) {
Messages.send(src, "ban.command.ban.success", "%player%", profile.getName().orElse(""), "%time%", (time == -1L ? Messages.getFormatted("core.time.ever") : TimeUtil.format(time)), "%reason%", reason);
} else {
Messages.send(src, "ban.command.ban.success-ip", "%ip%", address.toString().replace("/", ""), "%time%", (time == -1L ? Messages.getFormatted("core.time.ever") : TimeUtil.format(time)), "%reason%", reason);
}
return CommandResult.success();
}
示例8
private static Optional<Ban.Profile> getBan(PlayerIdentity playerIdentity) {
final GameProfile profile = getGameProfile(playerIdentity);
return getBanService().flatMap(banService -> banService.getBanFor(profile));
}
示例9
@Override
public String getBanReason(PlayerIdentity playerIdentity) {
return getBan(playerIdentity).flatMap(Ban.Profile::getReason).map(TextSerializers.FORMATTING_CODE::serialize).orElse(null);
}
示例10
@Override
public String getBanOperator(PlayerIdentity playerIdentity) {
return getBan(playerIdentity).flatMap(Ban.Profile::getBanSource).map(TextSerializers.FORMATTING_CODE::serialize).orElse(null);
}
示例11
@Override
public Date getBanExpiration(PlayerIdentity playerIdentity) {
return getBan(playerIdentity).flatMap(Ban.Profile::getExpirationDate).map(Date::from).orElse(null);
}