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