From f39b047c0a2e2bce34624847352152ff85bd6699 Mon Sep 17 00:00:00 2001 From: Pahimar Date: Tue, 9 Sep 2014 22:52:46 -0400 Subject: [PATCH] Unfinished work, nothing to see here, got a baby to feed! --- .../ee3/command/CommandSetEnergyValue.java | 5 ++ .../ee3/exchange/EnergyValueRegistry.java | 48 +++++++++++++- .../message/MessageSetEnergyValue.java | 63 +++++++++++++++++++ 3 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/pahimar/ee3/network/message/MessageSetEnergyValue.java diff --git a/src/main/java/com/pahimar/ee3/command/CommandSetEnergyValue.java b/src/main/java/com/pahimar/ee3/command/CommandSetEnergyValue.java index 2cf7d285..ad2a96e8 100644 --- a/src/main/java/com/pahimar/ee3/command/CommandSetEnergyValue.java +++ b/src/main/java/com/pahimar/ee3/command/CommandSetEnergyValue.java @@ -1,5 +1,8 @@ package com.pahimar.ee3.command; +import com.pahimar.ee3.api.EnergyValue; +import com.pahimar.ee3.exchange.EnergyValueRegistry; +import com.pahimar.ee3.exchange.WrappedStack; import com.pahimar.ee3.util.LogHelper; import net.minecraft.command.ICommandSender; import net.minecraft.command.WrongUsageException; @@ -67,6 +70,8 @@ public class CommandSetEnergyValue extends CommandEE return; } } + + EnergyValueRegistry.getInstance().setEnergyValue(new WrappedStack(itemStack), new EnergyValue(energyValue)); } } diff --git a/src/main/java/com/pahimar/ee3/exchange/EnergyValueRegistry.java b/src/main/java/com/pahimar/ee3/exchange/EnergyValueRegistry.java index e6484fdb..ff1c174f 100644 --- a/src/main/java/com/pahimar/ee3/exchange/EnergyValueRegistry.java +++ b/src/main/java/com/pahimar/ee3/exchange/EnergyValueRegistry.java @@ -653,7 +653,8 @@ public class EnergyValueRegistry implements INBTTaggable NBTTagList stackMappingTagList = new NBTTagList(); for (WrappedStack wrappedStack : stackMappings.keySet()) { - if (wrappedStack != null && stackMappings.get(wrappedStack) != null) { + 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))); @@ -662,4 +663,49 @@ public class EnergyValueRegistry implements INBTTaggable } nbtTagCompound.setTag("stackMappingList", stackMappingTagList); } + + public void setEnergyValue(WrappedStack wrappedStack, EnergyValue energyValue) + { + HashMap stackValueMap = new HashMap(); + + /** + * Read stack value mappings from NBTTagCompound + */ + stackValueMap.putAll(stackMappings); + stackValueMap.put(wrappedStack, energyValue); + + ImmutableSortedMap.Builder stackMappingsBuilder = ImmutableSortedMap.naturalOrder(); + stackMappingsBuilder.putAll(stackValueMap); + stackMappings = stackMappingsBuilder.build(); + + /** + * Resolve value stack mappings from the newly loaded stack mappings + */ + SortedMap> tempValueMappings = new TreeMap>(); + + for (WrappedStack stack : stackMappings.keySet()) + { + if (stack != null) + { + EnergyValue value = stackMappings.get(stack); + + if (value != null) + { + if (tempValueMappings.containsKey(value)) + { + if (!(tempValueMappings.get(value).contains(stack))) + { + tempValueMappings.get(value).add(stack); + } + } + else + { + tempValueMappings.put(value, new ArrayList(Arrays.asList(stack))); + } + } + } + } + + valueMappings = ImmutableSortedMap.copyOf(tempValueMappings); + } } diff --git a/src/main/java/com/pahimar/ee3/network/message/MessageSetEnergyValue.java b/src/main/java/com/pahimar/ee3/network/message/MessageSetEnergyValue.java new file mode 100644 index 00000000..d3c8c0b0 --- /dev/null +++ b/src/main/java/com/pahimar/ee3/network/message/MessageSetEnergyValue.java @@ -0,0 +1,63 @@ +package com.pahimar.ee3.network.message; + +import com.pahimar.ee3.api.EnergyValue; +import com.pahimar.ee3.exchange.WrappedStack; +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.IOException; + +public class MessageSetEnergyValue implements IMessage, IMessageHandler +{ + public NBTTagCompound energyValueMappingNBT; + + public MessageSetEnergyValue() + { + } + + public MessageSetEnergyValue(WrappedStack wrappedStack, EnergyValue energyValue) + { + + } + + @Override + public void fromBytes(ByteBuf buf) + { + + } + + @Override + public void toBytes(ByteBuf buf) + { + byte[] compressedNBT = null; + + try + { + compressedNBT = CompressedStreamTools.compress(energyValueMappingNBT); + } + catch (IOException e) + { + e.printStackTrace(); + } + + if (compressedNBT != null) + { + buf.writeInt(compressedNBT.length); + buf.writeBytes(compressedNBT); + } + else + { + buf.writeInt(0); + } + } + + @Override + public IMessage onMessage(MessageSetEnergyValue message, MessageContext ctx) + { + return null; + } +}