    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        this.saveWhiteList();
    040    }
    041
    042    public void setWhiteListEnabled(boolean par1)
    043    {
    044        super.setWhiteListEnabled(par1);
    045        this.getDedicatedServerInstance().setProperty("white-list", Boolean.valueOf(par1));
    046        this.getDedicatedServerInstance().saveProperties();
    047    }
    048
    049    /**
    050     * This adds a username to the ops list, then saves the op list
    051     */
    052    public void addOp(String par1Str)
    053    {
    054        super.addOp(par1Str);
    055        this.saveOpsList();
    056    }
    057
    058    /**
    059     * This removes a username from the ops list, then saves the op list
    060     */
    061    public void removeOp(String par1Str)
    062    {
    063        super.removeOp(par1Str);
    064        this.saveOpsList();
    065    }
    066
    067    /**
    068     * Remove the specified player from the whitelist.
    069     */
    070    public void removeFromWhitelist(String par1Str)
    071    {
    072        super.removeFromWhitelist(par1Str);
    073        this.saveWhiteList();
    074    }
    075
    076    /**
    077     * Add the specified player to the white list.
    078     */
    079    public void addToWhiteList(String par1Str)
    080    {
    081        super.addToWhiteList(par1Str);
    082        this.saveWhiteList();
    083    }
    084
    085    /**
    086     * Either does nothing, or calls readWhiteList.
    087     */
    088    public void loadWhiteList()
    089    {
    090        this.readWhiteList();
    091    }
    092
    093    private void loadOpsList()
    094    {
    095        try
    096        {
    097            this.getOps().clear();
    098            BufferedReader var1 = new BufferedReader(new FileReader(this.opsList));
    099            String var2 = "";
    100
    101            while ((var2 = var1.readLine()) != null)
    102            {
    103                this.getOps().add(var2.trim().toLowerCase());
    104            }
    105
    106            var1.close();
    107        }
    108        catch (Exception var3)
    109        {
    110            logger.warning("Failed to load operators list: " + var3);
    111        }
    112    }
    113
    114    private void saveOpsList()
    115    {
    116        try
    117        {
    118            PrintWriter var1 = new PrintWriter(new FileWriter(this.opsList, false));
    119            Iterator var2 = this.getOps().iterator();
    120
    121            while (var2.hasNext())
    122            {
    123                String var3 = (String)var2.next();
    124                var1.println(var3);
    125            }
    126
    127            var1.close();
    128        }
    129        catch (Exception var4)
    130        {
    131            logger.warning("Failed to save operators list: " + var4);
    132        }
    133    }
    134
    135    private void readWhiteList()
    136    {
    137        try
    138        {
    139            this.getWhiteListedPlayers().clear();
    140            BufferedReader var1 = new BufferedReader(new FileReader(this.whiteList));
    141            String var2 = "";
    142
    143            while ((var2 = var1.readLine()) != null)
    144            {
    145                this.getWhiteListedPlayers().add(var2.trim().toLowerCase());
    146            }
    147
    148            var1.close();
    149        }
    150        catch (Exception var3)
    151        {
    152            logger.warning("Failed to load white-list: " + var3);
    153        }
    154    }
    155
    156    private void saveWhiteList()
    157    {
    158        try
    159        {
    160            PrintWriter var1 = new PrintWriter(new FileWriter(this.whiteList, false));
    161            Iterator var2 = this.getWhiteListedPlayers().iterator();
    162
    163            while (var2.hasNext())
    164            {
    165                String var3 = (String)var2.next();
    166                var1.println(var3);
    167            }
    168
    169            var1.close();
    170        }
    171        catch (Exception var4)
    172        {
    173            logger.warning("Failed to save white-list: " + var4);
    174        }
    175    }
    176
    177    /**
    178     * Determine if the player is allowed to connect based on current server settings.
    179     */
    180    public boolean isAllowedToLogin(String par1Str)
    181    {
    182        par1Str = par1Str.trim().toLowerCase();
    183        return !this.isWhiteListEnabled() || this.areCommandsAllowed(par1Str) || this.getWhiteListedPlayers().contains(par1Str);
    184    }
    185
    186    public DedicatedServer getDedicatedServerInstance()
    187    {
    188        return (DedicatedServer)super.getServerInstance();
    189    }
    190
    191    public MinecraftServer getServerInstance()
    192    {
    193        return this.getDedicatedServerInstance();
    194    }
    195}