package net.minecraft.client.multiplayer;

import java.util.AbstractList;
import org.apache.logging.log4j.LogManager;
import java.util.Iterator;
import net.minecraft.nbt.Tag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtIo;
import java.io.File;
import com.google.common.collect.Lists;
import java.util.List;
import net.minecraft.client.Minecraft;
import org.apache.logging.log4j.Logger;

public class ServerList {
    private static final Logger LOGGER;
    private final Minecraft minecraft;
    private final List<ServerData> serverList;
    
    public ServerList(final Minecraft dbn) {
        this.serverList = Lists.newArrayList();
        this.minecraft = dbn;
        this.load();
    }
    
    public void load() {
        try {
            this.serverList.clear();
            final CompoundTag jt2 = NbtIo.read(new File(this.minecraft.gameDirectory, "servers.dat"));
            if (jt2 == null) {
                return;
            }
            final ListTag jz3 = jt2.getList("servers", 10);
            for (int integer4 = 0; integer4 < jz3.size(); ++integer4) {
                this.serverList.add(ServerData.read(jz3.getCompound(integer4)));
            }
        }
        catch (Exception exception2) {
            ServerList.LOGGER.error("Couldn't load server list", (Throwable)exception2);
        }
    }
    
    public void save() {
        try {
            final ListTag jz2 = new ListTag();
            for (final ServerData dnu4 : this.serverList) {
                ((AbstractList<CompoundTag>)jz2).add(dnu4.write());
            }
            final CompoundTag jt3 = new CompoundTag();
            jt3.put("servers", jz2);
            NbtIo.safeWrite(jt3, new File(this.minecraft.gameDirectory, "servers.dat"));
        }
        catch (Exception exception2) {
            ServerList.LOGGER.error("Couldn't save server list", (Throwable)exception2);
        }
    }
    
    public ServerData get(final int integer) {
        return this.serverList.get(integer);
    }
    
    public void remove(final ServerData dnu) {
        this.serverList.remove(dnu);
    }
    
    public void add(final ServerData dnu) {
        this.serverList.add(dnu);
    }
    
    public int size() {
        return this.serverList.size();
    }
    
    public void swap(final int integer1, final int integer2) {
        final ServerData dnu4 = this.get(integer1);
        this.serverList.set(integer1, this.get(integer2));
        this.serverList.set(integer2, dnu4);
        this.save();
    }
    
    public void replace(final int integer, final ServerData dnu) {
        this.serverList.set(integer, dnu);
    }
    
    public static void saveSingleServer(final ServerData dnu) {
        final ServerList dnv2 = new ServerList(Minecraft.getInstance());
        dnv2.load();
        for (int integer3 = 0; integer3 < dnv2.size(); ++integer3) {
            final ServerData dnu2 = dnv2.get(integer3);
            if (dnu2.name.equals(dnu.name) && dnu2.ip.equals(dnu.ip)) {
                dnv2.replace(integer3, dnu);
                break;
            }
        }
        dnv2.save();
    }
    
    static {
        LOGGER = LogManager.getLogger();
    }
}
