    001package net.minecraft.client.multiplayer;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import java.io.File;
    006import java.util.ArrayList;
    007import java.util.Iterator;
    008import java.util.List;
    009import net.minecraft.client.Minecraft;
    010import net.minecraft.nbt.CompressedStreamTools;
    011import net.minecraft.nbt.NBTTagCompound;
    012import net.minecraft.nbt.NBTTagList;
    013
    014@SideOnly(Side.CLIENT)
    015public 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 nbttagcompound = CompressedStreamTools.read(new File(this.mc.mcDataDir, "servers.dat"));
    038            NBTTagList nbttaglist = nbttagcompound.getTagList("servers");
    039            this.servers.clear();
    040
    041            for (int i = 0; i < nbttaglist.tagCount(); ++i)
    042            {
    043                this.servers.add(ServerData.getServerDataFromNBTCompound((NBTTagCompound)nbttaglist.tagAt(i)));
    044            }
    045        }
    046        catch (Exception exception)
    047        {
    048            exception.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 nbttaglist = new NBTTagList();
    061            Iterator iterator = this.servers.iterator();
    062
    063            while (iterator.hasNext())
    064            {
    065                ServerData serverdata = (ServerData)iterator.next();
    066                nbttaglist.appendTag(serverdata.getNBTCompound());
    067            }
    068
    069            NBTTagCompound nbttagcompound = new NBTTagCompound();
    070            nbttagcompound.setTag("servers", nbttaglist);
    071            CompressedStreamTools.safeWrite(nbttagcompound, new File(this.mc.mcDataDir, "servers.dat"));
    072        }
    073        catch (Exception exception)
    074        {
    075            exception.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 serverdata = this.getServerData(par1);
    117        this.servers.set(par1, this.getServerData(par2));
    118        this.servers.set(par2, serverdata);
    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 serverlist = new ServerList(Minecraft.getMinecraft());
    133        serverlist.loadServerList();
    134
    135        for (int i = 0; i < serverlist.countServers(); ++i)
    136        {
    137            ServerData serverdata1 = serverlist.getServerData(i);
    138
    139            if (serverdata1.serverName.equals(par0ServerData.serverName) && serverdata1.serverIP.equals(par0ServerData.serverIP))
    140            {
    141                serverlist.setServer(i, par0ServerData);
    142                break;
    143            }
    144        }
    145
    146        serverlist.saveServerList();
    147    }
    148}