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