Package crazypants.enderio.machine.still

Source Code of crazypants.enderio.machine.still.PacketTanks

package crazypants.enderio.machine.still;

import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
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 cpw.mods.fml.relauncher.Side;
import crazypants.enderio.EnderIO;
import crazypants.enderio.network.MessageTileEntity;
import crazypants.enderio.network.NetworkUtil;

public class PacketTanks extends MessageTileEntity<TileVat> implements IMessageHandler<PacketTanks, IMessage> {

  private NBTTagCompound nbtRoot;

  public PacketTanks() {
  }

  public PacketTanks(TileVat tile) {
    super(tile);
    nbtRoot = new NBTTagCompound();
    if(tile.inputTank.getFluidAmount() > 0) {
      NBTTagCompound tankRoot = new NBTTagCompound();
      tile.inputTank.writeToNBT(tankRoot);
      nbtRoot.setTag("inputTank", tankRoot);
    }
    if(tile.outputTank.getFluidAmount() > 0) {
      NBTTagCompound tankRoot = new NBTTagCompound();
      tile.outputTank.writeToNBT(tankRoot);
      nbtRoot.setTag("outputTank", tankRoot);
    }
  }

  @Override
  public void toBytes(ByteBuf buf) {
    super.toBytes(buf);
    NetworkUtil.writeNBTTagCompound(nbtRoot, buf);
  }

  @Override
  public void fromBytes(ByteBuf buf) {
    super.fromBytes(buf);
    nbtRoot = NetworkUtil.readNBTTagCompound(buf);
  }

  @Override
  public IMessage onMessage(PacketTanks message, MessageContext ctx) {
    EntityPlayer player = ctx.side == Side.SERVER ? ctx.getServerHandler().playerEntity : EnderIO.proxy.getClientPlayer();
    TileVat tile = message.getTileEntity(player.worldObj);
    if(tile == null) {
      return null;
    }
    if(message.nbtRoot.hasKey("inputTank")) {
      NBTTagCompound tankRoot = message.nbtRoot.getCompoundTag("inputTank");
      tile.inputTank.readFromNBT(tankRoot);
    } else {
      tile.inputTank.setFluid(null);
    }
    if(message.nbtRoot.hasKey("outputTank")) {
      NBTTagCompound tankRoot = message.nbtRoot.getCompoundTag("outputTank");
      tile.outputTank.readFromNBT(tankRoot);
    } else {
      tile.outputTank.setFluid(null);
    }
    return null;
  }

}
TOP

Related Classes of crazypants.enderio.machine.still.PacketTanks

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.