Switch away from NBT for WrappedStack and setting values

This commit is contained in:
pahimar 2015-05-28 10:56:49 -04:00
parent 1d617e53b5
commit a48196b580
2 changed files with 22 additions and 116 deletions

View file

@ -263,74 +263,6 @@ public class WrappedStack implements Comparable<WrappedStack>, JsonDeserializer<
this.stackSize = stackSize; this.stackSize = stackSize;
} }
public static NBTTagCompound toNBTTagCompound(WrappedStack wrappedStack)
{
if (wrappedStack != null && wrappedStack.getWrappedObject() != null)
{
NBTTagCompound wrappedStackTagCompound = new NBTTagCompound();
if (wrappedStack.getWrappedObject() instanceof ItemStack)
{
NBTTagCompound wrappedItemTagCompound = new NBTTagCompound();
((ItemStack) wrappedStack.getWrappedObject()).writeToNBT(wrappedItemTagCompound);
wrappedStackTagCompound.setInteger("type", 0);
wrappedStackTagCompound.setTag("objectData", wrappedItemTagCompound);
wrappedStackTagCompound.setInteger("stackSize", wrappedStack.getStackSize());
return wrappedStackTagCompound;
}
else if (wrappedStack.getWrappedObject() instanceof OreStack)
{
NBTTagCompound wrappedOreTagCompound = new NBTTagCompound();
((OreStack) wrappedStack.getWrappedObject()).writeToNBT(wrappedOreTagCompound);
wrappedStackTagCompound.setInteger("type", 1);
wrappedStackTagCompound.setTag("objectData", wrappedOreTagCompound);
wrappedStackTagCompound.setInteger("stackSize", wrappedStack.getStackSize());
return wrappedStackTagCompound;
}
else if (wrappedStack.getWrappedObject() instanceof FluidStack)
{
NBTTagCompound wrappedFluidTagCompound = new NBTTagCompound();
((FluidStack) wrappedStack.getWrappedObject()).writeToNBT(wrappedFluidTagCompound);
wrappedStackTagCompound.setInteger("type", 2);
wrappedStackTagCompound.setTag("objectData", wrappedFluidTagCompound);
wrappedStackTagCompound.setInteger("stackSize", wrappedStack.getStackSize());
return wrappedStackTagCompound;
}
}
return null;
}
public static WrappedStack fromNBTTagCompound(NBTTagCompound nbtTagCompound)
{
if (nbtTagCompound.hasKey("type") && nbtTagCompound.hasKey("objectData") && nbtTagCompound.hasKey("stackSize"))
{
int objectType = nbtTagCompound.getInteger("type");
int stackSize = nbtTagCompound.getInteger("stackSize");
if (objectType == 0)
{
ItemStack itemStack = ItemStack.loadItemStackFromNBT(nbtTagCompound.getCompoundTag("objectData"));
return new WrappedStack(itemStack, stackSize);
}
else if (objectType == 1)
{
OreStack oreStack = OreStack.loadOreStackFromNBT(nbtTagCompound.getCompoundTag("objectData"));
return new WrappedStack(oreStack, stackSize);
}
else if (objectType == 2)
{
FluidStack fluidStack = FluidStack.loadFluidStackFromNBT(nbtTagCompound.getCompoundTag("objectData"));
return new WrappedStack(fluidStack, stackSize);
}
else
{
return new WrappedStack();
}
}
return new WrappedStack();
}
public static WrappedStack wrap(Object object) public static WrappedStack wrap(Object object)
{ {
if (canBeWrapped(object)) if (canBeWrapped(object))

View file

@ -2,21 +2,18 @@ package com.pahimar.ee3.network.message;
import com.pahimar.ee3.api.exchange.EnergyValue; import com.pahimar.ee3.api.exchange.EnergyValue;
import com.pahimar.ee3.exchange.EnergyValueRegistry; import com.pahimar.ee3.exchange.EnergyValueRegistry;
import com.pahimar.ee3.exchange.EnergyValueStackMapping;
import com.pahimar.ee3.exchange.WrappedStack; import com.pahimar.ee3.exchange.WrappedStack;
import com.pahimar.ee3.util.CompressionHelper;
import com.pahimar.ee3.util.LogHelper; import com.pahimar.ee3.util.LogHelper;
import cpw.mods.fml.common.network.simpleimpl.IMessage; import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext; import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import net.minecraft.nbt.CompressedStreamTools;
import java.io.ByteArrayInputStream;
import java.io.IOException;
public class MessageSetEnergyValue implements IMessage, IMessageHandler<MessageSetEnergyValue, IMessage> public class MessageSetEnergyValue implements IMessage, IMessageHandler<MessageSetEnergyValue, IMessage>
{ {
public WrappedStack wrappedStack; public EnergyValueStackMapping energyValueStackMapping;
public EnergyValue energyValue;
public MessageSetEnergyValue() public MessageSetEnergyValue()
{ {
@ -24,66 +21,43 @@ public class MessageSetEnergyValue implements IMessage, IMessageHandler<MessageS
public MessageSetEnergyValue(WrappedStack wrappedStack, EnergyValue energyValue) public MessageSetEnergyValue(WrappedStack wrappedStack, EnergyValue energyValue)
{ {
this.wrappedStack = wrappedStack; this.energyValueStackMapping = new EnergyValueStackMapping(wrappedStack, energyValue);
this.energyValue = energyValue;
} }
@Override @Override
public void fromBytes(ByteBuf buf) public void fromBytes(ByteBuf buf)
{ {
byte[] compressedWrappedStack = null; byte[] compressedEnergyValueStackMapping = null;
int wrappedStackByteCount = buf.readInt(); int energyValueStackMappingByteCount = buf.readInt();
if (wrappedStackByteCount > 0) if (energyValueStackMappingByteCount > 0)
{ {
compressedWrappedStack = buf.readBytes(wrappedStackByteCount).array(); compressedEnergyValueStackMapping = buf.readBytes(energyValueStackMappingByteCount).array();
} }
byte[] compressedEnergyValue = null; if (compressedEnergyValueStackMapping != null)
int energyValueByteCount = buf.readInt();
if (energyValueByteCount > 0)
{ {
compressedEnergyValue = buf.readBytes(energyValueByteCount).array(); String decompressedEnergyValueStackMapping = CompressionHelper.decompressStringFromByteArray(compressedEnergyValueStackMapping);
} this.energyValueStackMapping = EnergyValueStackMapping.createFromJson(decompressedEnergyValueStackMapping);
if (compressedWrappedStack != null && compressedEnergyValue != null)
{
try
{
this.wrappedStack = WrappedStack.fromNBTTagCompound(CompressedStreamTools.readCompressed(new ByteArrayInputStream(compressedWrappedStack)));
this.energyValue = EnergyValue.loadEnergyValueFromNBT(CompressedStreamTools.readCompressed(new ByteArrayInputStream(compressedEnergyValue)));
}
catch (IOException e)
{
e.printStackTrace();
}
} }
} }
@Override @Override
public void toBytes(ByteBuf buf) public void toBytes(ByteBuf buf)
{ {
byte[] compressedWrappedStack = null; byte[] compressedBytes = null;
byte[] compressedEnergyValue = null; String jsonEnergyValueStackMapping = this.energyValueStackMapping.toJson();
try
if (jsonEnergyValueStackMapping != null)
{ {
compressedWrappedStack = CompressedStreamTools.compress(WrappedStack.toNBTTagCompound(wrappedStack)); compressedBytes = CompressionHelper.compressStringToByteArray(jsonEnergyValueStackMapping);
compressedEnergyValue = CompressedStreamTools.compress(EnergyValue.writeEnergyValueToNBT(energyValue));
}
catch (IOException e)
{
e.printStackTrace();
} }
if (compressedWrappedStack != null && compressedEnergyValue != null) if (compressedBytes != null)
{ {
buf.writeInt(compressedWrappedStack.length); buf.writeInt(compressedBytes.length);
buf.writeBytes(compressedWrappedStack); buf.writeBytes(compressedBytes);
buf.writeInt(compressedEnergyValue.length);
buf.writeBytes(compressedEnergyValue);
} }
else else
{ {
@ -94,10 +68,10 @@ public class MessageSetEnergyValue implements IMessage, IMessageHandler<MessageS
@Override @Override
public IMessage onMessage(MessageSetEnergyValue message, MessageContext ctx) public IMessage onMessage(MessageSetEnergyValue message, MessageContext ctx)
{ {
if (message.wrappedStack != null && message.energyValue != null) if (message.energyValueStackMapping != null && message.energyValueStackMapping.wrappedStack != null && message.energyValueStackMapping.energyValue != null)
{ {
EnergyValueRegistry.getInstance().setEnergyValue(message.wrappedStack, message.energyValue); EnergyValueRegistry.getInstance().setEnergyValue(message.energyValueStackMapping.wrappedStack, message.energyValueStackMapping.energyValue);
LogHelper.info(String.format("Client successfully received new EnergyValue '%s' for object '%s'", message.energyValue, message.wrappedStack)); LogHelper.info(String.format("Client successfully received new EnergyValue '%s' for object '%s'", message.energyValueStackMapping.wrappedStack, message.energyValueStackMapping.energyValue));
} }
else else
{ {