    001package net.minecraft.server.management;
    002
    003import java.text.ParseException;
    004import java.text.SimpleDateFormat;
    005import java.util.Date;
    006import java.util.regex.Pattern;
    007import net.minecraft.server.MinecraftServer;
    008
    009public class BanEntry
    010{
    011    public static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
    012    private final String username;
    013    private Date banStartDate = new Date();
    014    private String bannedBy = "(Unknown)";
    015    private Date banEndDate = null;
    016    private String reason = "Banned by an operator.";
    017
    018    public BanEntry(String par1Str)
    019    {
    020        this.username = par1Str;
    021    }
    022
    023    public String getBannedUsername()
    024    {
    025        return this.username;
    026    }
    027
    028    public Date getBanStartDate()
    029    {
    030        return this.banStartDate;
    031    }
    032
    033    /**
    034     * null == start ban now
    035     */
    036    public void setBanStartDate(Date par1Date)
    037    {
    038        this.banStartDate = par1Date != null ? par1Date : new Date();
    039    }
    040
    041    public String getBannedBy()
    042    {
    043        return this.bannedBy;
    044    }
    045
    046    public void setBannedBy(String par1Str)
    047    {
    048        this.bannedBy = par1Str;
    049    }
    050
    051    public Date getBanEndDate()
    052    {
    053        return this.banEndDate;
    054    }
    055
    056    public void setBanEndDate(Date par1Date)
    057    {
    058        this.banEndDate = par1Date;
    059    }
    060
    061    public boolean hasBanExpired()
    062    {
    063        return this.banEndDate == null ? false : this.banEndDate.before(new Date());
    064    }
    065
    066    public String getBanReason()
    067    {
    068        return this.reason;
    069    }
    070
    071    public void setBanReason(String par1Str)
    072    {
    073        this.reason = par1Str;
    074    }
    075
    076    public String buildBanString()
    077    {
    078        StringBuilder stringbuilder = new StringBuilder();
    079        stringbuilder.append(this.getBannedUsername());
    080        stringbuilder.append("|");
    081        stringbuilder.append(dateFormat.format(this.getBanStartDate()));
    082        stringbuilder.append("|");
    083        stringbuilder.append(this.getBannedBy());
    084        stringbuilder.append("|");
    085        stringbuilder.append(this.getBanEndDate() == null ? "Forever" : dateFormat.format(this.getBanEndDate()));
    086        stringbuilder.append("|");
    087        stringbuilder.append(this.getBanReason());
    088        return stringbuilder.toString();
    089    }
    090
    091    public static BanEntry parse(String par0Str)
    092    {
    093        if (par0Str.trim().length() < 2)
    094        {
    095            return null;
    096        }
    097        else
    098        {
    099            String[] astring = par0Str.trim().split(Pattern.quote("|"), 5);
    100            BanEntry banentry = new BanEntry(astring[0].trim());
    101            byte b0 = 0;
    102            int i = astring.length;
    103            int j = b0 + 1;
    104
    105            if (i <= j)
    106            {
    107                return banentry;
    108            }
    109            else
    110            {
    111                try
    112                {
    113                    banentry.setBanStartDate(dateFormat.parse(astring[j].trim()));
    114                }
    115                catch (ParseException parseexception)
    116                {
    117                    MinecraftServer.getServer().getLogAgent().logWarningException("Could not read creation date format for ban entry \'" + banentry.getBannedUsername() + "\' (was: \'" + astring[j] + "\')", parseexception);
    118                }
    119
    120                i = astring.length;
    121                ++j;
    122
    123                if (i <= j)
    124                {
    125                    return banentry;
    126                }
    127                else
    128                {
    129                    banentry.setBannedBy(astring[j].trim());
    130                    i = astring.length;
    131                    ++j;
    132
    133                    if (i <= j)
    134                    {
    135                        return banentry;
    136                    }
    137                    else
    138                    {
    139                        try
    140                        {
    141                            String s1 = astring[j].trim();
    142
    143                            if (!s1.equalsIgnoreCase("Forever") && s1.length() > 0)
    144                            {
    145                                banentry.setBanEndDate(dateFormat.parse(s1));
    146                            }
    147                        }
    148                        catch (ParseException parseexception1)
    149                        {
    150                            MinecraftServer.getServer().getLogAgent().logWarningException("Could not read expiry date format for ban entry \'" + banentry.getBannedUsername() + "\' (was: \'" + astring[j] + "\')", parseexception1);
    151                        }
    152
    153                        i = astring.length;
    154                        ++j;
    155
    156                        if (i <= j)
    157                        {
    158                            return banentry;
    159                        }
    160                        else
    161                        {
    162                            banentry.setBanReason(astring[j].trim());
    163                            return banentry;
    164                        }
    165                    }
    166                }
    167            }
    168        }
    169    }
    170}