    001package net.minecraft.server.management;
    002
    003import java.io.BufferedReader;
    004import java.io.File;
    005import java.io.FileNotFoundException;
    006import java.io.FileReader;
    007import java.io.FileWriter;
    008import java.io.IOException;
    009import java.io.PrintWriter;
    010import java.text.SimpleDateFormat;
    011import java.util.Date;
    012import java.util.Iterator;
    013import java.util.Map;
    014import net.minecraft.server.MinecraftServer;
    015
    016public class BanList
    017{
    018    private final LowerStringMap theBanList = new LowerStringMap();
    019    private final File fileName;
    020
    021    /** set to true if not singlePlayer */
    022    private boolean listActive = true;
    023
    024    public BanList(File par1File)
    025    {
    026        this.fileName = par1File;
    027    }
    028
    029    public boolean isListActive()
    030    {
    031        return this.listActive;
    032    }
    033
    034    public void setListActive(boolean par1)
    035    {
    036        this.listActive = par1;
    037    }
    038
    039    /**
    040     * removes expired Bans before returning
    041     */
    042    public Map getBannedList()
    043    {
    044        this.removeExpiredBans();
    045        return this.theBanList;
    046    }
    047
    048    public boolean isBanned(String par1Str)
    049    {
    050        if (!this.isListActive())
    051        {
    052            return false;
    053        }
    054        else
    055        {
    056            this.removeExpiredBans();
    057            return this.theBanList.containsKey(par1Str);
    058        }
    059    }
    060
    061    public void put(BanEntry par1BanEntry)
    062    {
    063        this.theBanList.putLower(par1BanEntry.getBannedUsername(), par1BanEntry);
    064        this.saveToFileWithHeader();
    065    }
    066
    067    public void remove(String par1Str)
    068    {
    069        this.theBanList.remove(par1Str);
    070        this.saveToFileWithHeader();
    071    }
    072
    073    public void removeExpiredBans()
    074    {
    075        Iterator iterator = this.theBanList.values().iterator();
    076
    077        while (iterator.hasNext())
    078        {
    079            BanEntry banentry = (BanEntry)iterator.next();
    080
    081            if (banentry.hasBanExpired())
    082            {
    083                iterator.remove();
    084            }
    085        }
    086    }
    087
    088    /**
    089     * Loads the ban list from the file (adds every entry, does not clear the current list).
    090     */
    091    public void loadBanList()
    092    {
    093        if (this.fileName.isFile())
    094        {
    095            BufferedReader bufferedreader;
    096
    097            try
    098            {
    099                bufferedreader = new BufferedReader(new FileReader(this.fileName));
    100            }
    101            catch (FileNotFoundException filenotfoundexception)
    102            {
    103                throw new Error();
    104            }
    105
    106            String s;
    107
    108            try
    109            {
    110                while ((s = bufferedreader.readLine()) != null)
    111                {
    112                    if (!s.startsWith("#"))
    113                    {
    114                        BanEntry banentry = BanEntry.parse(s);
    115
    116                        if (banentry != null)
    117                        {
    118                            this.theBanList.putLower(banentry.getBannedUsername(), banentry);
    119                        }
    120                    }
    121                }
    122            }
    123            catch (IOException ioexception)
    124            {
    125                MinecraftServer.getServer().func_98033_al().func_98234_c("Could not load ban list", ioexception);
    126            }
    127        }
    128    }
    129
    130    public void saveToFileWithHeader()
    131    {
    132        this.saveToFile(true);
    133    }
    134
    135    /**
    136     * par1: include header
    137     */
    138    public void saveToFile(boolean par1)
    139    {
    140        this.removeExpiredBans();
    141
    142        try
    143        {
    144            PrintWriter printwriter = new PrintWriter(new FileWriter(this.fileName, false));
    145
    146            if (par1)
    147            {
    148                printwriter.println("# Updated " + (new SimpleDateFormat()).format(new Date()) + " by Minecraft " + "1.5");
    149                printwriter.println("# victim name | ban date | banned by | banned until | reason");
    150                printwriter.println();
    151            }
    152
    153            Iterator iterator = this.theBanList.values().iterator();
    154
    155            while (iterator.hasNext())
    156            {
    157                BanEntry banentry = (BanEntry)iterator.next();
    158                printwriter.println(banentry.buildBanString());
    159            }
    160
    161            printwriter.close();
    162        }
    163        catch (IOException ioexception)
    164        {
    165            MinecraftServer.getServer().func_98033_al().func_98234_c("Could not save ban list", ioexception);
    166        }
    167    }
    168}