Package crazypants.enderio.machine.transceiver

Source Code of crazypants.enderio.machine.transceiver.PacketChannelList

package crazypants.enderio.machine.transceiver;

import java.util.ArrayList;
import java.util.List;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;

import io.netty.buffer.ByteBuf;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import crazypants.enderio.network.NetworkUtil;

public class PacketChannelList implements IMessage, IMessageHandler<PacketChannelList, IMessage> {

  private List<Channel> channels;

  public PacketChannelList() {
  }

  public PacketChannelList(ChannelRegister register) {
    channels = new ArrayList<Channel>();
    for (ChannelType type : ChannelType.values()) {
      for (Channel channel : register.getChannelsForType(type)) {
        channels.add(channel);
      }
    }
  }

  @Override
  public void toBytes(ByteBuf buf) {
    NBTTagList tagList = new NBTTagList();
    for(Channel chan : channels) {
      NBTTagCompound tag = new NBTTagCompound();
      chan.writeToNBT(tag);
      tagList.appendTag(tag);
    }
    NBTTagCompound root = new NBTTagCompound();
    root.setTag("chanList", tagList);
    NetworkUtil.writeNBTTagCompound(root, buf);
  }

  @Override
  public void fromBytes(ByteBuf buf) {
    NBTTagCompound root = NetworkUtil.readNBTTagCompound(buf);
    NBTTagList tagList = (NBTTagList)root.getTag("chanList");
    channels = new ArrayList<Channel>();
    for(int i=0; i < tagList.tagCount();i++) {
      NBTTagCompound tag = tagList.getCompoundTagAt(i);
      Channel chan = Channel.readFromNBT(tag);
      if(chan != null) {
        channels.add(chan);
      }
    }

  }

  @Override
  public IMessage onMessage(PacketChannelList message, MessageContext ctx) {
    for(Channel channel : message.channels) {
      ClientChannelRegister.instance.addChannel(channel);
    }
    return null;
  }

}
TOP

Related Classes of crazypants.enderio.machine.transceiver.PacketChannelList

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.