package crazypants.enderio.machine.invpanel;

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.MessageTileEntity;
import io.netty.buffer.ByteBuf;
import java.io.UnsupportedEncodingException;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.tileentity.TileEntity;

public class PacketGuiSettings extends MessageTileEntity<TileInventoryPanel> implements IMessageHandler<PacketGuiSettings, IMessage> {

  private int sortMode;
  private String filterString;
  private boolean sync;
  
  public PacketGuiSettings() {
    filterString = "";
  }

  public PacketGuiSettings(TileInventoryPanel tile, int sortMode, String filterString, boolean sync) {
    super(tile);
    this.sortMode = sortMode;
    this.filterString = filterString;
    this.sync = sync;
  }

  @Override
  public void fromBytes(ByteBuf buf) {
    super.fromBytes(buf);
    sortMode = buf.readInt();
    int len = buf.readUnsignedShort();
    if(len > 0) {
      byte[] utf8 = new byte[len];
      buf.readBytes(utf8);
      try {
        filterString = new String(utf8, "UTF8");
      } catch (UnsupportedEncodingException ex) {
        // should not happen - if it does we can't do anything
      }
    }
  }

  @Override
  public void toBytes(ByteBuf buf) {
    super.toBytes(buf);
    buf.writeInt(sortMode);
    if(filterString.isEmpty()) {
      buf.writeShort(0);
    } else {
      try {
        byte[] utf8 = filterString.getBytes("UTF8");
        buf.writeShort((short) utf8.length);
        buf.writeBytes(utf8);
      } catch (UnsupportedEncodingException ex) {
        // should not happen - if it does we can't do anything
        buf.writeShort(0);
      }
    }
  }

  @Override
  public IMessage onMessage(PacketGuiSettings message, MessageContext ctx) {
    EntityPlayerMP player = ctx.getServerHandler().playerEntity;
    TileEntity te = player.worldObj.getTileEntity(message.x, message.y, message.z);
    if(te instanceof TileInventoryPanel) {
      TileInventoryPanel teInvPanel = (TileInventoryPanel) te;
      teInvPanel.setGuiParameter(message.sortMode, message.filterString, message.sync);
    }
    return null;
  }
}

@@ -39,16 +39,192 @@
 panel;%0A%0A
+import io.netty.buffer.ByteBuf;%0Aimport net.minecraft.entity.player.EntityPlayerMP;%0Aimport net.minecraft.tileentity.TileEntity;%0Aimport cpw.mods.fml.common.network.ByteBufUtils;%0A
 import c
@@ -452,180 +452,8 @@
 ity;
-%0Aimport io.netty.buffer.ByteBuf;%0Aimport java.io.UnsupportedEncodingException;%0Aimport net.minecraft.entity.player.EntityPlayerMP;%0Aimport net.minecraft.tileentity.TileEntity;
 %0A%0Apu
@@ -1057,310 +1057,86 @@
     
-int len = buf.readUnsignedShort();%0A    if(len %3E 0) %7B%0A      byte%5B%5D utf8 = new byte%5Blen%5D;%0A      buf.readBytes(utf8);%0A      try %7B%0A        filterString = new String(utf8, %22UTF8%22);%0A      %7D catch (UnsupportedEncodingException ex) %7B%0A        // should not happen - if it does we can't do anything%0A      %7D%0A    %7D
+ByteBufUtils.writeUTF8String(buf, filterString);%0A    sync = buf.readBoolean();
 %0A  %7D
@@ -1242,19 +1242,16 @@
 e);%0A    
-if(
 filterSt
@@ -1258,178 +1258,49 @@
 ring
-.isEmpty()) %7B%0A      buf.writeShort(0);%0A    %7D else %7B%0A      try %7B%0A        byte%5B%5D utf8 = filterString.getBytes(%22UTF8%22);%0A        buf.writeShort((short) utf8.length);%0A   
+ = ByteBufUtils.readUTF8String(buf);%0A
     
- 
 buf.
@@ -1309,173 +1309,21 @@
 iteB
-ytes(utf8);%0A      %7D catch (UnsupportedEncodingException ex) %7B%0A        // should not happen - if it does we can't do anything%0A        buf.writeShort(0);%0A      %7D%0A    %7D
+oolean(sync);
 %0A  %7D

Fix NEI sync not being sent to server, clean up packet