    001/*
    002 * Forge Mod Loader
    003 * Copyright (c) 2012-2013 cpw.
    004 * All rights reserved. This program and the accompanying materials
    005 * are made available under the terms of the GNU Lesser Public License v2.1
    006 * which accompanies this distribution, and is available at
    007 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
    008 *
    009 * Contributors:
    010 *     cpw - implementation
    011 */
    012
    013package cpw.mods.fml.common.network;
    014
    015import static cpw.mods.fml.common.network.FMLPacket.Type.MOD_IDENTIFIERS;
    016
    017import java.util.Collection;
    018import java.util.Map;
    019import java.util.Map.Entry;
    020
    021import net.minecraft.network.INetworkManager;
    022import net.minecraft.network.packet.NetHandler;
    023
    024import com.google.common.collect.Maps;
    025import com.google.common.io.ByteArrayDataInput;
    026import com.google.common.io.ByteArrayDataOutput;
    027import com.google.common.io.ByteStreams;
    028
    029public class ModIdentifiersPacket extends FMLPacket
    030{
    031
    032    private Map<String, Integer> modIds = Maps.newHashMap();
    033
    034    public ModIdentifiersPacket()
    035    {
    036        super(MOD_IDENTIFIERS);
    037    }
    038
    039    @Override
    040    public byte[] generatePacket(Object... data)
    041    {
    042        ByteArrayDataOutput dat = ByteStreams.newDataOutput();
    043        Collection<NetworkModHandler >networkMods = FMLNetworkHandler.instance().getNetworkIdMap().values();
    044
    045        dat.writeInt(networkMods.size());
    046        for (NetworkModHandler handler : networkMods)
    047        {
    048            dat.writeUTF(handler.getContainer().getModId());
    049            dat.writeInt(handler.getNetworkId());
    050        }
    051
    052        // TODO send the other id maps as well
    053        return dat.toByteArray();
    054    }
    055
    056    @Override
    057    public FMLPacket consumePacket(byte[] data)
    058    {
    059        ByteArrayDataInput dat = ByteStreams.newDataInput(data);
    060        int listSize = dat.readInt();
    061        for (int i = 0; i < listSize; i++)
    062        {
    063            String modId = dat.readUTF();
    064            int networkId = dat.readInt();
    065            modIds.put(modId, networkId);
    066        }
    067        return this;
    068    }
    069
    070    @Override
    071    public void execute(INetworkManager network, FMLNetworkHandler handler, NetHandler netHandler, String userName)
    072    {
    073        for (Entry<String,Integer> idEntry : modIds.entrySet())
    074        {
    075            handler.bindNetworkId(idEntry.getKey(), idEntry.getValue());
    076        }
    077        // TODO other id maps
    078    }
    079}