    001package net.minecraft.server.dedicated;
    002
    003import java.io.BufferedReader;
    004import java.io.File;
    005import java.io.FileReader;
    006import java.io.FileWriter;
    007import java.io.PrintWriter;
    008import java.util.Iterator;
    009import net.minecraft.server.MinecraftServer;
    010import net.minecraft.server.management.ServerConfigurationManager;
    011
    012public class DedicatedPlayerList extends ServerConfigurationManager
    013{
    014    private File opsList;
    015    private File whiteList;
    016
    017    public DedicatedPlayerList(DedicatedServer par1DedicatedServer)
    018    {
    019        super(par1DedicatedServer);
    020        this.opsList = par1DedicatedServer.getFile("ops.txt");
    021        this.whiteList = par1DedicatedServer.getFile("white-list.txt");
    022        this.viewDistance = par1DedicatedServer.getIntProperty("view-distance", 10);
    023        this.maxPlayers = par1DedicatedServer.getIntProperty("max-players", 20);
    024        this.setWhiteListEnabled(par1DedicatedServer.getBooleanProperty("white-list", false));
    025
    026        if (!par1DedicatedServer.isSinglePlayer())
    027        {
    028            this.getBannedPlayers().setListActive(true);
    029            this.getBannedIPs().setListActive(true);
    030        }
    031
    032        this.getBannedPlayers().loadBanList();
    033        this.getBannedPlayers().saveToFileWithHeader();
    034        this.getBannedIPs().loadBanList();
    035        this.getBannedIPs().saveToFileWithHeader();
    036        this.loadOpsList();
    037        this.readWhiteList();
    038        this.saveOpsList();
    039
    040        if (!this.whiteList.exists())
    041        {
    042            this.saveWhiteList();
    043        }
    044    }
    045
    046    public void setWhiteListEnabled(boolean par1)
    047    {
    048        super.setWhiteListEnabled(par1);
    049        this.getDedicatedServerInstance().setProperty("white-list", Boolean.valueOf(par1));
    050        this.getDedicatedServerInstance().saveProperties();
    051    }
    052
    053    /**
    054     * This adds a username to the ops list, then saves the op list
    055     */
    056    public void addOp(String par1Str)
    057    {
    058        super.addOp(par1Str);
    059        this.saveOpsList();
    060    }
    061
    062    /**
    063     * This removes a username from the ops list, then saves the op list
    064     */
    065    public void removeOp(String par1Str)
    066    {
    067        super.removeOp(par1Str);
    068        this.saveOpsList();
    069    }
    070
    071    /**
    072     * Remove the specified player from the whitelist.
    073     */
    074    public void removeFromWhitelist(String par1Str)
    075    {
    076        super.removeFromWhitelist(par1Str);
    077        this.saveWhiteList();
    078    }
    079
    080    /**
    081     * Add the specified player to the white list.
    082     */
    083    public void addToWhiteList(String par1Str)
    084    {
    085        super.addToWhiteList(par1Str);
    086        this.saveWhiteList();
    087    }
    088
    089    /**
    090     * Either does nothing, or calls readWhiteList.
    091     */
    092    public void loadWhiteList()
    093    {
    094        this.readWhiteList();
    095    }
    096
    097    private void loadOpsList()
    098    {
    099        try
    100        {
    101            this.getOps().clear();
    102            BufferedReader bufferedreader = new BufferedReader(new FileReader(this.opsList));
    103            String s = "";
    104
    105            while ((s = bufferedreader.readLine()) != null)
    106            {
    107                this.getOps().add(s.trim().toLowerCase());
    108            }
    109
    110            bufferedreader.close();
    111        }
    112        catch (Exception exception)
    113        {
    114            this.getDedicatedServerInstance().func_98033_al().func_98236_b("Failed to load operators list: " + exception);
    115        }
    116    }
    117
    118    private void saveOpsList()
    119    {
    120        try
    121        {
    122            PrintWriter printwriter = new PrintWriter(new FileWriter(this.opsList, false));
    123            Iterator iterator = this.getOps().iterator();
    124
    125            while (iterator.hasNext())
    126            {
    127                String s = (String)iterator.next();
    128                printwriter.println(s);
    129            }
    130
    131            printwriter.close();
    132        }
    133        catch (Exception exception)
    134        {
    135            this.getDedicatedServerInstance().func_98033_al().func_98236_b("Failed to save operators list: " + exception);
    136        }
    137    }
    138
    139    private void readWhiteList()
    140    {
    141        try
    142        {
    143            this.getWhiteListedPlayers().clear();
    144            BufferedReader bufferedreader = new BufferedReader(new FileReader(this.whiteList));
    145            String s = "";
    146
    147            while ((s = bufferedreader.readLine()) != null)
    148            {
    149                this.getWhiteListedPlayers().add(s.trim().toLowerCase());
    150            }
    151
    152            bufferedreader.close();
    153        }
    154        catch (Exception exception)
    155        {
    156            this.getDedicatedServerInstance().func_98033_al().func_98236_b("Failed to load white-list: " + exception);
    157        }
    158    }
    159
    160    private void saveWhiteList()
    161    {
    162        try
    163        {
    164            PrintWriter printwriter = new PrintWriter(new FileWriter(this.whiteList, false));
    165            Iterator iterator = this.getWhiteListedPlayers().iterator();
    166
    167            while (iterator.hasNext())
    168            {
    169                String s = (String)iterator.next();
    170                printwriter.println(s);
    171            }
    172
    173            printwriter.close();
    174        }
    175        catch (Exception exception)
    176        {
    177            this.getDedicatedServerInstance().func_98033_al().func_98236_b("Failed to save white-list: " + exception);
    178        }
    179    }
    180
    181    /**
    182     * Determine if the player is allowed to connect based on current server settings.
    183     */
    184    public boolean isAllowedToLogin(String par1Str)
    185    {
    186        par1Str = par1Str.trim().toLowerCase();
    187        return !this.isWhiteListEnabled() || this.areCommandsAllowed(par1Str) || this.getWhiteListedPlayers().contains(par1Str);
    188    }
    189
    190    public DedicatedServer getDedicatedServerInstance()
    191    {
    192        return (DedicatedServer)super.getServerInstance();
    193    }
    194
    195    public MinecraftServer getServerInstance()
    196    {
    197        return this.getDedicatedServerInstance();
    198    }
    199}