All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bukkit.craftbukkit.CraftIpBanList Maven / Gradle / Ivy

package org.bukkit.craftbukkit;

import com.google.common.collect.ImmutableSet;
import net.minecraft.server.IpBanEntry;
import net.minecraft.server.IpBanList;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Date;
import java.util.Set;
import java.util.logging.Level;

public class CraftIpBanList implements org.bukkit.BanList {
  private final IpBanList list;

  public CraftIpBanList(IpBanList list) {
    this.list = list;
  }

  @Override
  public org.bukkit.BanEntry getBanEntry(String target) {
    Validate.notNull(target, "Target cannot be null");

    IpBanEntry entry = list.get(target);
    if (entry == null) {
      return null;
    }

    return new CraftIpBanEntry(target, entry, list);
  }

  @Override
  public org.bukkit.BanEntry addBan(String target, String reason, Date expires, String source) {
    Validate.notNull(target, "Ban target cannot be null");

    IpBanEntry entry = new IpBanEntry(target, new Date(),
      StringUtils.isBlank(source) ? null : source, expires,
      StringUtils.isBlank(reason) ? null : reason);

    list.add(entry);

    try {
      list.save();
    } catch (IOException ex) {
      Bukkit.getLogger().log(Level.SEVERE, "Failed to save banned-ips.json, {0}", ex.getMessage());
    }

    return new CraftIpBanEntry(target, entry, list);
  }

  @Override
  public Set getBanEntries() {
    ImmutableSet.Builder builder = ImmutableSet.builder();
    for (String target : list.getEntries()) {
      builder.add(new CraftIpBanEntry(target, list.get(target), list));
    }

    return builder.build();
  }

  @Override
  public boolean isBanned(String target) {
    Validate.notNull(target, "Target cannot be null");

    return list.isBanned(InetSocketAddress.createUnresolved(target, 0));
  }

  @Override
  public void pardon(String target) {
    Validate.notNull(target, "Target cannot be null");

    list.remove(target);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy