2014-09-10 04:52:46 +02:00
package com.pahimar.ee3.network.message ;
2015-05-07 19:45:06 +02:00
import com.pahimar.ee3.api.exchange.EnergyValue ;
2014-09-11 22:13:39 +02:00
import com.pahimar.ee3.exchange.EnergyValueRegistry ;
2015-05-28 16:56:49 +02:00
import com.pahimar.ee3.exchange.EnergyValueStackMapping ;
2014-09-10 04:52:46 +02:00
import com.pahimar.ee3.exchange.WrappedStack ;
2015-05-28 16:56:49 +02:00
import com.pahimar.ee3.util.CompressionHelper ;
2014-09-11 22:13:39 +02:00
import com.pahimar.ee3.util.LogHelper ;
2014-09-10 04:52:46 +02:00
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 io.netty.buffer.ByteBuf ;
public class MessageSetEnergyValue implements IMessage , IMessageHandler < MessageSetEnergyValue , IMessage >
{
2015-05-28 16:56:49 +02:00
public EnergyValueStackMapping energyValueStackMapping ;
2014-09-10 04:52:46 +02:00
public MessageSetEnergyValue ( )
{
}
public MessageSetEnergyValue ( WrappedStack wrappedStack , EnergyValue energyValue )
{
2015-05-28 16:56:49 +02:00
this . energyValueStackMapping = new EnergyValueStackMapping ( wrappedStack , energyValue ) ;
2014-09-10 04:52:46 +02:00
}
@Override
public void fromBytes ( ByteBuf buf )
{
2015-05-28 16:56:49 +02:00
byte [ ] compressedEnergyValueStackMapping = null ;
int energyValueStackMappingByteCount = buf . readInt ( ) ;
2014-09-10 04:52:46 +02:00
2015-05-28 16:56:49 +02:00
if ( energyValueStackMappingByteCount > 0 )
2014-09-11 22:13:39 +02:00
{
2015-05-28 16:56:49 +02:00
compressedEnergyValueStackMapping = buf . readBytes ( energyValueStackMappingByteCount ) . array ( ) ;
2014-09-11 22:13:39 +02:00
}
2015-05-28 16:56:49 +02:00
if ( compressedEnergyValueStackMapping ! = null )
2014-09-11 22:13:39 +02:00
{
2015-05-28 16:56:49 +02:00
String decompressedEnergyValueStackMapping = CompressionHelper . decompressStringFromByteArray ( compressedEnergyValueStackMapping ) ;
this . energyValueStackMapping = EnergyValueStackMapping . createFromJson ( decompressedEnergyValueStackMapping ) ;
2014-09-11 22:13:39 +02:00
}
2014-09-10 04:52:46 +02:00
}
@Override
public void toBytes ( ByteBuf buf )
{
2015-05-28 16:56:49 +02:00
byte [ ] compressedBytes = null ;
String jsonEnergyValueStackMapping = this . energyValueStackMapping . toJson ( ) ;
2014-09-10 04:52:46 +02:00
2015-05-28 16:56:49 +02:00
if ( jsonEnergyValueStackMapping ! = null )
2014-09-10 04:52:46 +02:00
{
2015-05-28 16:56:49 +02:00
compressedBytes = CompressionHelper . compressStringToByteArray ( jsonEnergyValueStackMapping ) ;
2014-09-10 04:52:46 +02:00
}
2015-05-28 16:56:49 +02:00
if ( compressedBytes ! = null )
2014-09-10 04:52:46 +02:00
{
2015-05-28 16:56:49 +02:00
buf . writeInt ( compressedBytes . length ) ;
buf . writeBytes ( compressedBytes ) ;
2014-09-10 04:52:46 +02:00
}
else
{
buf . writeInt ( 0 ) ;
}
}
@Override
public IMessage onMessage ( MessageSetEnergyValue message , MessageContext ctx )
{
2015-05-28 16:56:49 +02:00
if ( message . energyValueStackMapping ! = null & & message . energyValueStackMapping . wrappedStack ! = null & & message . energyValueStackMapping . energyValue ! = null )
2014-09-11 22:13:39 +02:00
{
2015-05-28 16:56:49 +02:00
EnergyValueRegistry . getInstance ( ) . setEnergyValue ( message . energyValueStackMapping . wrappedStack , message . energyValueStackMapping . energyValue ) ;
2015-11-19 21:24:40 +01:00
LogHelper . info ( EnergyValueRegistry . ENERGY_VALUE_MARKER , " Client successfully received new EnergyValue '{}' for object '{}' " , message . energyValueStackMapping . wrappedStack , message . energyValueStackMapping . energyValue ) ;
2014-09-11 22:13:39 +02:00
}
else
{
2015-11-19 21:24:40 +01:00
LogHelper . info ( EnergyValueRegistry . ENERGY_VALUE_MARKER , " Client failed to receive new EnergyValue from server " ) ;
2014-09-11 22:13:39 +02:00
}
2014-09-10 04:52:46 +02:00
return null ;
}
}