2014-09-04 17:30:22 +02:00
|
|
|
package com.pahimar.ee3.network.message;
|
|
|
|
|
2016-05-18 19:53:13 +02:00
|
|
|
import com.google.gson.JsonParseException;
|
2015-05-07 19:45:06 +02:00
|
|
|
import com.pahimar.ee3.api.exchange.EnergyValue;
|
2014-09-04 17:30:22 +02:00
|
|
|
import com.pahimar.ee3.exchange.EnergyValueRegistry;
|
2015-05-03 01:23:35 +02:00
|
|
|
import com.pahimar.ee3.exchange.WrappedStack;
|
|
|
|
import com.pahimar.ee3.util.CompressionHelper;
|
2014-09-04 17:30:22 +02:00
|
|
|
import com.pahimar.ee3.util.LogHelper;
|
2016-05-18 19:53:13 +02:00
|
|
|
import com.pahimar.ee3.util.SerializationHelper;
|
2014-09-04 17:30:22 +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;
|
|
|
|
|
2015-05-03 01:23:35 +02:00
|
|
|
import java.util.Map;
|
2014-09-04 17:30:22 +02:00
|
|
|
|
2016-05-18 19:53:13 +02:00
|
|
|
public class MessageSyncEnergyValues implements IMessage, IMessageHandler<MessageSyncEnergyValues, IMessage> {
|
2014-09-04 17:30:22 +02:00
|
|
|
|
2016-05-25 05:12:49 +02:00
|
|
|
public Map<WrappedStack, EnergyValue> valueMap;
|
2014-09-04 17:30:22 +02:00
|
|
|
|
2016-05-18 19:53:13 +02:00
|
|
|
public MessageSyncEnergyValues() {
|
2016-05-25 05:12:49 +02:00
|
|
|
this.valueMap = EnergyValueRegistry.INSTANCE.getEnergyValues();
|
2014-09-04 17:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert from the supplied buffer into your specific message type
|
|
|
|
*
|
|
|
|
* @param buf
|
|
|
|
*/
|
|
|
|
@Override
|
2016-05-18 19:53:13 +02:00
|
|
|
public void fromBytes(ByteBuf buf) {
|
2014-09-04 17:30:22 +02:00
|
|
|
|
2016-05-25 05:12:49 +02:00
|
|
|
int compressedJsonLength = buf.readInt();
|
2016-05-18 19:53:13 +02:00
|
|
|
|
2016-05-25 05:12:49 +02:00
|
|
|
if (compressedJsonLength != 0) {
|
|
|
|
byte[] compressedValueMap = buf.readBytes(compressedJsonLength).array();
|
2016-05-18 19:53:13 +02:00
|
|
|
|
2016-05-25 05:12:49 +02:00
|
|
|
if (compressedValueMap != null) {
|
|
|
|
|
|
|
|
String jsonString = CompressionHelper.decompress(compressedValueMap);
|
|
|
|
|
|
|
|
try {
|
|
|
|
valueMap = SerializationHelper.GSON.fromJson(jsonString, SerializationHelper.ENERGY_VALUE_MAP_TYPE);
|
|
|
|
}
|
|
|
|
catch (JsonParseException e) {
|
|
|
|
LogHelper.warn("Failed to read energy value map from server");
|
|
|
|
valueMap = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
valueMap = null;
|
2016-05-11 21:59:52 +02:00
|
|
|
}
|
2014-09-04 17:30:22 +02:00
|
|
|
}
|
2016-05-25 05:12:49 +02:00
|
|
|
else {
|
|
|
|
valueMap = null;
|
|
|
|
}
|
2014-09-04 17:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deconstruct your message into the supplied byte buffer
|
|
|
|
*
|
|
|
|
* @param buf
|
|
|
|
*/
|
|
|
|
@Override
|
2016-05-18 19:53:13 +02:00
|
|
|
public void toBytes(ByteBuf buf) {
|
2014-09-04 17:30:22 +02:00
|
|
|
|
2016-05-25 05:12:49 +02:00
|
|
|
if (valueMap != null) {
|
2016-05-18 19:53:13 +02:00
|
|
|
|
2016-05-25 05:12:49 +02:00
|
|
|
byte[] compressedValueMap = CompressionHelper.compress(SerializationHelper.GSON.toJson(valueMap, SerializationHelper.ENERGY_VALUE_MAP_TYPE));
|
2014-09-04 17:30:22 +02:00
|
|
|
|
2016-05-25 05:12:49 +02:00
|
|
|
if (compressedValueMap != null) {
|
|
|
|
buf.writeInt(compressedValueMap.length);
|
|
|
|
buf.writeBytes(compressedValueMap);
|
2016-05-18 19:53:13 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
buf.writeInt(0);
|
|
|
|
}
|
2014-09-04 17:30:22 +02:00
|
|
|
}
|
2016-05-18 19:53:13 +02:00
|
|
|
else {
|
2014-09-04 17:30:22 +02:00
|
|
|
buf.writeInt(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when a message is received of the appropriate type. You can optionally return a reply message, or null if no reply
|
|
|
|
* is needed.
|
|
|
|
*
|
|
|
|
* @param message The message
|
|
|
|
* @param ctx
|
|
|
|
* @return an optional return message
|
|
|
|
*/
|
|
|
|
@Override
|
2016-05-18 19:53:13 +02:00
|
|
|
public IMessage onMessage(MessageSyncEnergyValues message, MessageContext ctx) {
|
|
|
|
|
2016-05-25 05:12:49 +02:00
|
|
|
if (message.valueMap != null) {
|
|
|
|
EnergyValueRegistry.INSTANCE.load(message.valueMap);
|
|
|
|
LogHelper.info(EnergyValueRegistry.ENERGY_VALUE_MARKER, "Client successfully received {} energy values from server", message.valueMap.size());
|
2014-09-04 17:30:22 +02:00
|
|
|
}
|
2016-05-18 19:53:13 +02:00
|
|
|
else {
|
|
|
|
LogHelper.info(EnergyValueRegistry.ENERGY_VALUE_MARKER, "Client failed to receive energy values from server - falling back to local values");
|
2014-09-04 17:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|