    001    package net.minecraft.client.multiplayer;
    002
    003    import cpw.mods.fml.common.Side;
    004    import cpw.mods.fml.common.asm.SideOnly;
    005    import java.io.File;
    006    import java.util.ArrayList;
    007    import java.util.Iterator;
    008    import java.util.List;
    009    import net.minecraft.client.Minecraft;
    010    import net.minecraft.nbt.CompressedStreamTools;
    011    import net.minecraft.nbt.NBTTagCompound;
    012    import net.minecraft.nbt.NBTTagList;
    013
    014    @SideOnly(Side.CLIENT)
    015    public class ServerList
    016    {
    017        /** The Minecraft instance. */
    018        private final Minecraft mc;
    019
    020        /** List of ServerData instances. */
    021        private final List servers = new ArrayList();
    022
    023        public ServerList(Minecraft par1Minecraft)
    024        {
    025            this.mc = par1Minecraft;
    026            this.loadServerList();
    027        }
    028
    029        /**
    030         * Loads a list of servers from servers.dat, by running ServerData.getServerDataFromNBTCompound on each NBT compound
    031         * found in the "servers" tag list.
    032         */
    033        public void loadServerList()
    034        {
    035            try
    036            {
    037                NBTTagCompound var1 = CompressedStreamTools.read(new File(this.mc.mcDataDir, "servers.dat"));
    038                NBTTagList var2 = var1.getTagList("servers");
    039                this.servers.clear();
    040
    041                for (int var3 = 0; var3 < var2.tagCount(); ++var3)
    042                {
    043                    this.servers.add(ServerData.getServerDataFromNBTCompound((NBTTagCompound)var2.tagAt(var3)));
    044                }
    045            }
    046            catch (Exception var4)
    047            {
    048                var4.printStackTrace();
    049            }
    050        }
    051
    052        /**
    053         * Runs getNBTCompound on each ServerData instance, puts everything into a "servers" NBT list and writes it to
    054         * servers.dat.
    055         */
    056        public void saveServerList()
    057        {
    058            try
    059            {
    060                NBTTagList var1 = new NBTTagList();
    061                Iterator var2 = this.servers.iterator();
    062
    063                while (var2.hasNext())
    064                {
    065                    ServerData var3 = (ServerData)var2.next();
    066                    var1.appendTag(var3.getNBTCompound());
    067                }
    068
    069                NBTTagCompound var5 = new NBTTagCompound();
    070                var5.setTag("servers", var1);
    071                CompressedStreamTools.safeWrite(var5, new File(this.mc.mcDataDir, "servers.dat"));
    072            }
    073            catch (Exception var4)
    074            {
    075                var4.printStackTrace();
    076            }
    077        }
    078
    079        /**
    080         * Gets the ServerData instance stored for the given index in the list.
    081         */
    082        public ServerData getServerData(int par1)
    083        {
    084            return (ServerData)this.servers.get(par1);
    085        }
    086
    087        /**
    088         * Removes the ServerData instance stored for the given index in the list.
    089         */
    090        public void removeServerData(int par1)
    091        {
    092            this.servers.remove(par1);
    093        }
    094
    095        /**
    096         * Adds the given ServerData instance to the list.
    097         */
    098        public void addServerData(ServerData par1ServerData)
    099        {
    100            this.servers.add(par1ServerData);
    101        }
    102
    103        /**
    104         * Counts the number of ServerData instances in the list.
    105         */
    106        public int countServers()
    107        {
    108            return this.servers.size();
    109        }
    110
    111        /**
    112         * Takes two list indexes, and swaps their order around.
    113         */
    114        public void swapServers(int par1, int par2)
    115        {
    116            ServerData var3 = this.getServerData(par1);
    117            this.servers.set(par1, this.getServerData(par2));
    118            this.servers.set(par2, var3);
    119            this.saveServerList();
    120        }
    121
    122        /**
    123         * Sets the given index in the list to the given ServerData instance.
    124         */
    125        public void setServer(int par1, ServerData par2ServerData)
    126        {
    127            this.servers.set(par1, par2ServerData);
    128        }
    129
    130        public static void func_78852_b(ServerData par0ServerData)
    131        {
    132            ServerList var1 = new ServerList(Minecraft.getMinecraft());
    133            var1.loadServerList();
    134
    135            for (int var2 = 0; var2 < var1.countServers(); ++var2)
    136            {
    137                ServerData var3 = var1.getServerData(var2);
    138
    139                if (var3.serverName.equals(par0ServerData.serverName) && var3.serverIP.equals(par0ServerData.serverIP))
    140                {
    141                    var1.setServer(var2, par0ServerData);
    142                    break;
    143                }
    144            }
    145
    146            var1.saveServerList();
    147        }
    148    }