Getting close now

This commit is contained in:
pahimar 2015-05-02 19:23:35 -04:00
parent 87e1f9fb1a
commit 74bb716395
4 changed files with 84 additions and 94 deletions

View file

@ -2,5 +2,5 @@
#Sat Dec 28 00:14:08 EST 2013
minecraft_version = 1.7.10
forge_version = 10.13.2.1291
mod_version = 0.2
release_type = alpha
mod_version = 0.3
release_type = beta

View file

@ -14,7 +14,7 @@ import net.minecraft.world.World;
public class AlchemyArray implements Comparable<AlchemyArray>
{
private ResourceLocation texture;
private String unLocalizedName;
private String unlocalizedName;
private String className;
private int lightLevel;
private int chalkPerBlockCost;
@ -24,10 +24,10 @@ public class AlchemyArray implements Comparable<AlchemyArray>
}
public AlchemyArray(ResourceLocation texture, String unLocalizedName)
public AlchemyArray(ResourceLocation texture, String unlocalizedName)
{
this.texture = texture;
this.unLocalizedName = unLocalizedName;
this.unlocalizedName = unlocalizedName;
this.chalkPerBlockCost = 1;
this.lightLevel = 0;
}
@ -42,19 +42,19 @@ public class AlchemyArray implements Comparable<AlchemyArray>
this.texture = texture;
}
public String getUnLocalizedName()
public String getUnlocalizedName()
{
return unLocalizedName;
return unlocalizedName;
}
public void setUnLocalizedName(String unLocalizedName)
public void setUnlocalizedName(String unlocalizedName)
{
this.unLocalizedName = unLocalizedName;
this.unlocalizedName = unlocalizedName;
}
public String getDisplayName()
{
return StatCollector.translateToLocal(unLocalizedName);
return StatCollector.translateToLocal(unlocalizedName);
}
public int getChalkCostPerBlock()
@ -90,13 +90,13 @@ public class AlchemyArray implements Comparable<AlchemyArray>
this.texture = new ResourceLocation("");
}
if (nbtTagCompound.hasKey("unLocalizedName"))
if (nbtTagCompound.hasKey("unlocalizedName"))
{
this.unLocalizedName = nbtTagCompound.getString("unLocalizedName");
this.unlocalizedName = nbtTagCompound.getString("unlocalizedName");
}
else
{
this.unLocalizedName = "";
this.unlocalizedName = "";
}
if (nbtTagCompound.hasKey("className"))
@ -120,7 +120,7 @@ public class AlchemyArray implements Comparable<AlchemyArray>
else
{
this.texture = new ResourceLocation("");
this.unLocalizedName = "";
this.unlocalizedName = "";
this.className = "";
this.lightLevel = 0;
}
@ -130,7 +130,7 @@ public class AlchemyArray implements Comparable<AlchemyArray>
{
nbtTagCompound.setString("textureDomain", texture.getResourceDomain());
nbtTagCompound.setString("texturePath", texture.getResourcePath());
nbtTagCompound.setString("unLocalizedName", unLocalizedName);
nbtTagCompound.setString("unlocalizedName", unlocalizedName);
nbtTagCompound.setString("className", this.getClass().getCanonicalName());
nbtTagCompound.setInteger("lightLevel", lightLevel);
}

View file

@ -16,8 +16,6 @@ import com.pahimar.ee3.util.SerializationHelper;
import cpw.mods.fml.common.FMLCommonHandler;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.oredict.OreDictionary;
@ -585,24 +583,10 @@ public class EnergyValueRegistry implements JsonSerializer<EnergyValueRegistry>,
return stacksInRange;
}
public void readFromNBT(NBTTagCompound nbtTagCompound)
public void loadFromMap(Map<WrappedStack, EnergyValue> stackValueMap)
{
if (nbtTagCompound != null && nbtTagCompound.hasKey("stackMappingList"))
if (stackValueMap != null)
{
TreeMap<WrappedStack, EnergyValue> stackValueMap = new TreeMap<WrappedStack, EnergyValue>();
/**
* Read stack value mappings from NBTTagCompound
*/
NBTTagList stackMappingTagList = nbtTagCompound.getTagList("stackMappingList", 10);
for (int i = 0; i < stackMappingTagList.tagCount(); i++)
{
NBTTagCompound tagCompound = stackMappingTagList.getCompoundTagAt(i);
WrappedStack wrappedStack = WrappedStack.fromNBTTagCompound(tagCompound.getCompoundTag("wrappedStack"));
EnergyValue energyValue = EnergyValue.loadEnergyValueFromNBT(tagCompound.getCompoundTag("energyValue"));
stackValueMap.put(wrappedStack, energyValue);
}
ImmutableSortedMap.Builder<WrappedStack, EnergyValue> stackMappingsBuilder = ImmutableSortedMap.naturalOrder();
stackMappingsBuilder.putAll(stackValueMap);
stackMappings = stackMappingsBuilder.build();
@ -614,23 +598,6 @@ public class EnergyValueRegistry implements JsonSerializer<EnergyValueRegistry>,
}
}
public void writeToNBT(NBTTagCompound nbtTagCompound)
{
NBTTagList stackMappingTagList = new NBTTagList();
for (WrappedStack wrappedStack : stackMappings.keySet())
{
if (wrappedStack != null && stackMappings.get(wrappedStack) != null)
{
NBTTagCompound stackMappingCompound = new NBTTagCompound();
stackMappingCompound.setTag("wrappedStack", WrappedStack.toNBTTagCompound(wrappedStack));
stackMappingCompound.setTag("energyValue", EnergyValue.writeEnergyValueToNBT(stackMappings.get(wrappedStack)));
stackMappingTagList.appendTag(stackMappingCompound);
}
}
nbtTagCompound.setTag("stackMappingList", stackMappingTagList);
nbtTagCompound.setString("modListMD5", SerializationHelper.getModListMD5());
}
public void setEnergyValue(WrappedStack wrappedStack, EnergyValue energyValue)
{
if (wrappedStack != null && energyValue != null && Float.compare(energyValue.getValue(), 0f) > 0)
@ -740,26 +707,33 @@ public class EnergyValueRegistry implements JsonSerializer<EnergyValueRegistry>,
if (!Settings.DynamicEnergyValueGeneration.regenerateEnergyValuesWhen.equalsIgnoreCase("Always"))
{
if (md5EnergyValuesFile.exists())
if (Settings.DynamicEnergyValueGeneration.regenerateEnergyValuesWhen.equalsIgnoreCase("When Mods Change"))
{
LogHelper.info("Attempting to load energy values from file: " + md5EnergyValuesFile.getAbsolutePath());
stackValueMap = SerializationHelper.decompressEnergyValueStackMapFromFile(md5EnergyValuesFile);
if (md5EnergyValuesFile.exists())
{
LogHelper.info("Attempting to load energy values from file: " + md5EnergyValuesFile.getAbsolutePath());
stackValueMap = SerializationHelper.decompressEnergyValueStackMapFromFile(md5EnergyValuesFile);
}
}
if (staticEnergyValuesFile.exists())
else if (Settings.DynamicEnergyValueGeneration.regenerateEnergyValuesWhen.equalsIgnoreCase("Never"))
{
LogHelper.info("Attempting to load energy values from file: " + staticEnergyValuesFile.getAbsolutePath());
stackValueMap = SerializationHelper.decompressEnergyValueStackMapFromFile(staticEnergyValuesFile);
if (staticEnergyValuesFile.exists())
{
LogHelper.info("Attempting to load energy values from file: " + staticEnergyValuesFile.getAbsolutePath());
stackValueMap = SerializationHelper.decompressEnergyValueStackMapFromFile(staticEnergyValuesFile);
}
else if (md5EnergyValuesFile.exists())
{
LogHelper.info("Attempting to load energy values from file: " + md5EnergyValuesFile.getAbsolutePath());
stackValueMap = SerializationHelper.decompressEnergyValueStackMapFromFile(md5EnergyValuesFile);
}
}
if (stackValueMap != null)
{
loadFromMap(stackValueMap);
LogHelper.info("Successfully loaded energy values from file");
for (WrappedStack wrappedStack : stackValueMap.keySet())
{
LogHelper.info(String.format("Stack: %s, Value: %s", wrappedStack, stackValueMap.get(wrappedStack)));
}
return false;
return true;
}
else
{

View file

@ -1,20 +1,25 @@
package com.pahimar.ee3.network.message;
import com.google.gson.stream.JsonReader;
import com.pahimar.ee3.api.EnergyValue;
import com.pahimar.ee3.exchange.EnergyValueRegistry;
import com.pahimar.ee3.exchange.EnergyValueStackMapping;
import com.pahimar.ee3.exchange.WrappedStack;
import com.pahimar.ee3.util.CompressionHelper;
import com.pahimar.ee3.util.LogHelper;
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;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.Map;
import java.util.TreeMap;
public class MessageSyncEnergyValues implements IMessage, IMessageHandler<MessageSyncEnergyValues, IMessage>
{
public NBTTagCompound energyValueRegistryNBT;
public String jsonEnergyValueRegistry;
public MessageSyncEnergyValues()
{
@ -22,8 +27,7 @@ public class MessageSyncEnergyValues implements IMessage, IMessageHandler<Messag
public MessageSyncEnergyValues(EnergyValueRegistry energyValueRegistry)
{
energyValueRegistryNBT = new NBTTagCompound();
energyValueRegistry.writeToNBT(energyValueRegistryNBT);
this.jsonEnergyValueRegistry = energyValueRegistry.toJson();
}
/**
@ -34,24 +38,17 @@ public class MessageSyncEnergyValues implements IMessage, IMessageHandler<Messag
@Override
public void fromBytes(ByteBuf buf)
{
byte[] compressedNBT = null;
byte[] compressedBytes = null;
int readableBytes = buf.readInt();
if (readableBytes > 0)
{
compressedNBT = buf.readBytes(readableBytes).array();
compressedBytes = buf.readBytes(readableBytes).array();
}
if (compressedNBT != null)
if (compressedBytes != null)
{
try
{
this.energyValueRegistryNBT = CompressedStreamTools.readCompressed(new ByteArrayInputStream(compressedNBT));
}
catch (IOException e)
{
e.printStackTrace();
}
this.jsonEnergyValueRegistry = CompressionHelper.decompressStringFromByteArray(compressedBytes);
}
}
@ -63,24 +60,17 @@ public class MessageSyncEnergyValues implements IMessage, IMessageHandler<Messag
@Override
public void toBytes(ByteBuf buf)
{
byte[] compressedNBT = null;
byte[] compressedBytes = null;
try
if (jsonEnergyValueRegistry != null)
{
if (energyValueRegistryNBT != null)
{
compressedNBT = CompressedStreamTools.compress(energyValueRegistryNBT);
}
}
catch (IOException e)
{
e.printStackTrace();
compressedBytes = CompressionHelper.compressStringToByteArray(jsonEnergyValueRegistry);
}
if (compressedNBT != null)
if (compressedBytes != null)
{
buf.writeInt(compressedNBT.length);
buf.writeBytes(compressedNBT);
buf.writeInt(compressedBytes.length);
buf.writeBytes(compressedBytes);
}
else
{
@ -99,9 +89,35 @@ public class MessageSyncEnergyValues implements IMessage, IMessageHandler<Messag
@Override
public IMessage onMessage(MessageSyncEnergyValues message, MessageContext ctx)
{
if (message.energyValueRegistryNBT != null)
if (message.jsonEnergyValueRegistry != null)
{
EnergyValueRegistry.getInstance().readFromNBT(message.energyValueRegistryNBT);
Map<WrappedStack, EnergyValue> energyValueStackMap = new TreeMap<WrappedStack, EnergyValue>();
try
{
JsonReader jsonReader = new JsonReader(new StringReader(message.jsonEnergyValueRegistry));
jsonReader.beginArray();
while (jsonReader.hasNext())
{
EnergyValueStackMapping energyValueStackMapping = EnergyValueStackMapping.jsonSerializer.fromJson(jsonReader, EnergyValueStackMapping.class);
if (energyValueStackMapping != null)
{
energyValueStackMap.put(energyValueStackMapping.wrappedStack, energyValueStackMapping.energyValue);
}
}
jsonReader.endArray();
jsonReader.close();
}
catch (IOException e)
{
e.printStackTrace();
}
for (WrappedStack wrappedStack : energyValueStackMap.keySet())
{
LogHelper.info(String.format("Object: %s, Value: %s", wrappedStack, energyValueStackMap.get(wrappedStack)));
}
EnergyValueRegistry.getInstance().loadFromMap(energyValueStackMap);
LogHelper.info("Client successfully received EnergyValues from server");
}
else