    001package cpw.mods.fml.common.network;
    002
    003import static cpw.mods.fml.common.network.FMLPacket.Type.MOD_IDENTIFIERS;
    004
    005import java.util.Collection;
    006import java.util.Map;
    007import java.util.Map.Entry;
    008
    009import net.minecraft.network.INetworkManager;
    010import net.minecraft.network.packet.NetHandler;
    011
    012import com.google.common.collect.Maps;
    013import com.google.common.io.ByteArrayDataInput;
    014import com.google.common.io.ByteArrayDataOutput;
    015import com.google.common.io.ByteStreams;
    016
    017public class ModIdentifiersPacket extends FMLPacket
    018{
    019
    020    private Map<String, Integer> modIds = Maps.newHashMap();
    021
    022    public ModIdentifiersPacket()
    023    {
    024        super(MOD_IDENTIFIERS);
    025    }
    026
    027    @Override
    028    public byte[] generatePacket(Object... data)
    029    {
    030        ByteArrayDataOutput dat = ByteStreams.newDataOutput();
    031        Collection<NetworkModHandler >networkMods = FMLNetworkHandler.instance().getNetworkIdMap().values();
    032
    033        dat.writeInt(networkMods.size());
    034        for (NetworkModHandler handler : networkMods)
    035        {
    036            dat.writeUTF(handler.getContainer().getModId());
    037            dat.writeInt(handler.getNetworkId());
    038        }
    039
    040        // TODO send the other id maps as well
    041        return dat.toByteArray();
    042    }
    043
    044    @Override
    045    public FMLPacket consumePacket(byte[] data)
    046    {
    047        ByteArrayDataInput dat = ByteStreams.newDataInput(data);
    048        int listSize = dat.readInt();
    049        for (int i = 0; i < listSize; i++)
    050        {
    051            String modId = dat.readUTF();
    052            int networkId = dat.readInt();
    053            modIds.put(modId, networkId);
    054        }
    055        return this;
    056    }
    057
    058    @Override
    059    public void execute(INetworkManager network, FMLNetworkHandler handler, NetHandler netHandler, String userName)
    060    {
    061        for (Entry<String,Integer> idEntry : modIds.entrySet())
    062        {
    063            handler.bindNetworkId(idEntry.getKey(), idEntry.getValue());
    064        }
    065        // TODO other id maps
    066    }
    067}