diff --git a/src/main/java/com/pahimar/ee3/client/gui/inventory/GuiAlchemicalBag.java b/src/main/java/com/pahimar/ee3/client/gui/inventory/GuiAlchemicalBag.java index 85fcc404..5a0cb6dc 100644 --- a/src/main/java/com/pahimar/ee3/client/gui/inventory/GuiAlchemicalBag.java +++ b/src/main/java/com/pahimar/ee3/client/gui/inventory/GuiAlchemicalBag.java @@ -7,7 +7,7 @@ import com.pahimar.ee3.lib.Strings; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import net.minecraft.client.gui.inventory.GuiContainer; -import net.minecraft.entity.player.InventoryPlayer; +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import org.lwjgl.opengl.GL11; @@ -22,9 +22,9 @@ import org.lwjgl.opengl.GL11; public class GuiAlchemicalBag extends GuiContainer { - public GuiAlchemicalBag(InventoryPlayer inventoryPlayer, InventoryAlchemicalBag inventoryAlchemicalBag) + public GuiAlchemicalBag(EntityPlayer entityPlayer, InventoryAlchemicalBag inventoryAlchemicalBag) { - super(new ContainerAlchemicalBag(inventoryPlayer, inventoryAlchemicalBag)); + super(new ContainerAlchemicalBag(entityPlayer, inventoryAlchemicalBag)); } @Override diff --git a/src/main/java/com/pahimar/ee3/client/handler/ItemTooltipEventHandler.java b/src/main/java/com/pahimar/ee3/client/handler/ItemTooltipEventHandler.java index 6ef72b62..307d3141 100644 --- a/src/main/java/com/pahimar/ee3/client/handler/ItemTooltipEventHandler.java +++ b/src/main/java/com/pahimar/ee3/client/handler/ItemTooltipEventHandler.java @@ -3,6 +3,7 @@ package com.pahimar.ee3.client.handler; import com.pahimar.ee3.api.WrappedStack; import com.pahimar.ee3.emc.EmcRegistry; import com.pahimar.ee3.emc.EmcValue; +import com.pahimar.ee3.item.ItemAlchemicalBag; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import net.minecraftforge.event.EventPriority; @@ -50,5 +51,10 @@ public class ItemTooltipEventHandler event.toolTip.add("No EMC value"); } } + + if (event.itemStack.getItem() instanceof ItemAlchemicalBag && event.itemStack.getTagCompound() != null) + { + event.toolTip.add("NBT: " + event.itemStack.getTagCompound().toString()); + } } } diff --git a/src/main/java/com/pahimar/ee3/handler/CraftingHandler.java b/src/main/java/com/pahimar/ee3/handler/CraftingHandler.java index 30532f0a..4ca90523 100644 --- a/src/main/java/com/pahimar/ee3/handler/CraftingHandler.java +++ b/src/main/java/com/pahimar/ee3/handler/CraftingHandler.java @@ -15,8 +15,6 @@ import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.CraftingManager; -import java.util.UUID; - /** * Equivalent-Exchange-3 *
@@ -53,9 +51,7 @@ public class CraftingHandler implements ICraftingHandler // Set the UUID on an Alchemical Bag when picked up from crafting if (itemStack.getItem() instanceof ItemAlchemicalBag) { - UUID itemUUID = UUID.randomUUID(); - ItemStackNBTHelper.setLong(itemStack, Strings.NBT_ITEM_UUID_MOST_SIG, itemUUID.getMostSignificantBits()); - ItemStackNBTHelper.setLong(itemStack, Strings.NBT_ITEM_UUID_LEAST_SIG, itemUUID.getLeastSignificantBits()); + ItemStackNBTHelper.setUUID(itemStack); } } } diff --git a/src/main/java/com/pahimar/ee3/helper/ItemStackNBTHelper.java b/src/main/java/com/pahimar/ee3/helper/ItemStackNBTHelper.java index b37bb2e3..e5568e16 100644 --- a/src/main/java/com/pahimar/ee3/helper/ItemStackNBTHelper.java +++ b/src/main/java/com/pahimar/ee3/helper/ItemStackNBTHelper.java @@ -1,8 +1,11 @@ package com.pahimar.ee3.helper; +import com.pahimar.ee3.lib.Strings; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; +import java.util.UUID; + /** * Equivalent-Exchange-3 * @@ -21,7 +24,6 @@ public class ItemStackNBTHelper */ private static void initNBTTagCompound(ItemStack itemStack) { - if (itemStack.stackTagCompound == null) { itemStack.setTagCompound(new NBTTagCompound()); @@ -35,17 +37,33 @@ public class ItemStackNBTHelper public static void removeTag(ItemStack itemStack, String keyName) { - if (itemStack.stackTagCompound != null) { itemStack.stackTagCompound.removeTag(keyName); } } + public static boolean hasUUID(ItemStack itemStack) + { + return hasTag(itemStack, Strings.NBT_ITEM_UUID_MOST_SIG) && hasTag(itemStack, Strings.NBT_ITEM_UUID_LEAST_SIG); + } + + public static void setUUID(ItemStack itemStack) + { + initNBTTagCompound(itemStack); + + // Set a UUID on the Alchemical Bag, if one doesn't exist already + if (!hasTag(itemStack, Strings.NBT_ITEM_UUID_MOST_SIG) && !hasTag(itemStack, Strings.NBT_ITEM_UUID_LEAST_SIG)) + { + UUID itemUUID = UUID.randomUUID(); + setLong(itemStack, Strings.NBT_ITEM_UUID_MOST_SIG, itemUUID.getMostSignificantBits()); + setLong(itemStack, Strings.NBT_ITEM_UUID_LEAST_SIG, itemUUID.getLeastSignificantBits()); + } + } + // String public static String getString(ItemStack itemStack, String keyName) { - initNBTTagCompound(itemStack); if (!itemStack.stackTagCompound.hasKey(keyName)) @@ -58,7 +76,6 @@ public class ItemStackNBTHelper public static void setString(ItemStack itemStack, String keyName, String keyValue) { - initNBTTagCompound(itemStack); itemStack.stackTagCompound.setString(keyName, keyValue); @@ -67,7 +84,6 @@ public class ItemStackNBTHelper // boolean public static boolean getBoolean(ItemStack itemStack, String keyName) { - initNBTTagCompound(itemStack); if (!itemStack.stackTagCompound.hasKey(keyName)) @@ -80,7 +96,6 @@ public class ItemStackNBTHelper public static void setBoolean(ItemStack itemStack, String keyName, boolean keyValue) { - initNBTTagCompound(itemStack); itemStack.stackTagCompound.setBoolean(keyName, keyValue); @@ -89,7 +104,6 @@ public class ItemStackNBTHelper // byte public static byte getByte(ItemStack itemStack, String keyName) { - initNBTTagCompound(itemStack); if (!itemStack.stackTagCompound.hasKey(keyName)) @@ -102,7 +116,6 @@ public class ItemStackNBTHelper public static void setByte(ItemStack itemStack, String keyName, byte keyValue) { - initNBTTagCompound(itemStack); itemStack.stackTagCompound.setByte(keyName, keyValue); @@ -111,7 +124,6 @@ public class ItemStackNBTHelper // short public static short getShort(ItemStack itemStack, String keyName) { - initNBTTagCompound(itemStack); if (!itemStack.stackTagCompound.hasKey(keyName)) @@ -124,7 +136,6 @@ public class ItemStackNBTHelper public static void setShort(ItemStack itemStack, String keyName, short keyValue) { - initNBTTagCompound(itemStack); itemStack.stackTagCompound.setShort(keyName, keyValue); @@ -133,7 +144,6 @@ public class ItemStackNBTHelper // int public static int getInt(ItemStack itemStack, String keyName) { - initNBTTagCompound(itemStack); if (!itemStack.stackTagCompound.hasKey(keyName)) @@ -146,7 +156,6 @@ public class ItemStackNBTHelper public static void setInteger(ItemStack itemStack, String keyName, int keyValue) { - initNBTTagCompound(itemStack); itemStack.stackTagCompound.setInteger(keyName, keyValue); @@ -187,7 +196,6 @@ public class ItemStackNBTHelper public static void setFloat(ItemStack itemStack, String keyName, float keyValue) { - initNBTTagCompound(itemStack); itemStack.stackTagCompound.setFloat(keyName, keyValue); @@ -196,7 +204,6 @@ public class ItemStackNBTHelper // double public static double getDouble(ItemStack itemStack, String keyName) { - initNBTTagCompound(itemStack); if (!itemStack.stackTagCompound.hasKey(keyName)) @@ -209,7 +216,6 @@ public class ItemStackNBTHelper public static void setDouble(ItemStack itemStack, String keyName, double keyValue) { - initNBTTagCompound(itemStack); itemStack.stackTagCompound.setDouble(keyName, keyValue); diff --git a/src/main/java/com/pahimar/ee3/inventory/ContainerAlchemicalBag.java b/src/main/java/com/pahimar/ee3/inventory/ContainerAlchemicalBag.java index 66d8dc0d..1172edf4 100644 --- a/src/main/java/com/pahimar/ee3/inventory/ContainerAlchemicalBag.java +++ b/src/main/java/com/pahimar/ee3/inventory/ContainerAlchemicalBag.java @@ -16,6 +16,9 @@ import net.minecraft.item.ItemStack; */ public class ContainerAlchemicalBag extends Container { + private final EntityPlayer entityPlayer; + private final InventoryAlchemicalBag inventoryAlchemicalBag; + private int bagInventoryRows; private int bagInventoryColumns; @@ -35,8 +38,11 @@ public class ContainerAlchemicalBag extends Container private final int PLAYER_INVENTORY_ROWS = 3; private final int PLAYER_INVENTORY_COLUMNS = 9; - public ContainerAlchemicalBag(InventoryPlayer inventoryPlayer, InventoryAlchemicalBag inventoryAlchemicalBag) + public ContainerAlchemicalBag(EntityPlayer entityPlayer, InventoryAlchemicalBag inventoryAlchemicalBag) { + this.entityPlayer = entityPlayer; + this.inventoryAlchemicalBag = inventoryAlchemicalBag; + // if (alchemicalBag.getItemDamage() == 0) // { // bagInventoryRows = SMALL_BAG_INVENTORY_ROWS; @@ -118,13 +124,14 @@ public class ContainerAlchemicalBag extends Container } @Override - public void onContainerClosed(EntityPlayer player) + public void onContainerClosed(EntityPlayer entityPlayer) { - super.onContainerClosed(player); + super.onContainerClosed(entityPlayer); - if (!player.worldObj.isRemote) + if (!entityPlayer.worldObj.isRemote) { - InventoryPlayer invPlayer = player.inventory; + // We can probably do this better now considering the InventoryAlchemicalBag has a findParent method + InventoryPlayer invPlayer = entityPlayer.inventory; for (ItemStack itemStack : invPlayer.mainInventory) { if (itemStack != null) @@ -135,6 +142,8 @@ public class ContainerAlchemicalBag extends Container } } } + + saveInventory(entityPlayer); } } @@ -144,4 +153,9 @@ public class ContainerAlchemicalBag extends Container // TODO return null; } + + public void saveInventory(EntityPlayer entityPlayer) + { + inventoryAlchemicalBag.onGuiSaved(entityPlayer); + } } diff --git a/src/main/java/com/pahimar/ee3/inventory/InventoryAlchemicalBag.java b/src/main/java/com/pahimar/ee3/inventory/InventoryAlchemicalBag.java index 964909be..c0152d53 100644 --- a/src/main/java/com/pahimar/ee3/inventory/InventoryAlchemicalBag.java +++ b/src/main/java/com/pahimar/ee3/inventory/InventoryAlchemicalBag.java @@ -1,5 +1,6 @@ package com.pahimar.ee3.inventory; +import com.pahimar.ee3.helper.ItemStackNBTHelper; import com.pahimar.ee3.lib.Strings; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; @@ -7,6 +8,8 @@ import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; +import java.util.UUID; + public class InventoryAlchemicalBag implements IInventory, INBTTaggable { public ItemStack parentItemStack; @@ -21,13 +24,56 @@ public class InventoryAlchemicalBag implements IInventory, INBTTaggable readFromNBT(itemStack.getTagCompound()); } + public void onGuiSaved(EntityPlayer entityPlayer) + { + parentItemStack = findParentItemStack(entityPlayer); + + if (parentItemStack != null) + { + save(); + } + } + + public ItemStack findParentItemStack(EntityPlayer entityPlayer) + { + if (ItemStackNBTHelper.hasUUID(parentItemStack)) + { + UUID parentItemStackUUID = new UUID(parentItemStack.getTagCompound().getLong(Strings.NBT_ITEM_UUID_MOST_SIG), parentItemStack.getTagCompound().getLong(Strings.NBT_ITEM_UUID_LEAST_SIG)); + for (int i = 0; i < entityPlayer.inventory.getSizeInventory(); i++) + { + ItemStack itemStack = entityPlayer.inventory.getStackInSlot(i); + + if (ItemStackNBTHelper.hasUUID(itemStack)) + { + if (itemStack.getTagCompound().getLong(Strings.NBT_ITEM_UUID_MOST_SIG) == parentItemStackUUID.getMostSignificantBits() && itemStack.getTagCompound().getLong(Strings.NBT_ITEM_UUID_LEAST_SIG) == parentItemStackUUID.getLeastSignificantBits()) + { + return itemStack; + } + } + } + } + + return null; + } + + public boolean matchesUUID(UUID uuid) + { + return ItemStackNBTHelper.hasUUID(parentItemStack) && parentItemStack.getTagCompound().getLong(Strings.NBT_ITEM_UUID_LEAST_SIG) == uuid.getLeastSignificantBits() && parentItemStack.getTagCompound().getLong(Strings.NBT_ITEM_UUID_MOST_SIG) == uuid.getMostSignificantBits(); + } + public void save() { NBTTagCompound nbtTagCompound = parentItemStack.getTagCompound(); + if (nbtTagCompound == null) { nbtTagCompound = new NBTTagCompound(); + + UUID uuid = UUID.randomUUID(); + nbtTagCompound.setLong(Strings.NBT_ITEM_UUID_MOST_SIG, uuid.getMostSignificantBits()); + nbtTagCompound.setLong(Strings.NBT_ITEM_UUID_LEAST_SIG, uuid.getLeastSignificantBits()); } + writeToNBT(nbtTagCompound); parentItemStack.setTagCompound(nbtTagCompound); } @@ -193,7 +239,6 @@ public class InventoryAlchemicalBag implements IInventory, INBTTaggable public String getCustomName() { - return customName; } } diff --git a/src/main/java/com/pahimar/ee3/inventory/SlotAlchemicalBag.java b/src/main/java/com/pahimar/ee3/inventory/SlotAlchemicalBag.java new file mode 100644 index 00000000..4575c19b --- /dev/null +++ b/src/main/java/com/pahimar/ee3/inventory/SlotAlchemicalBag.java @@ -0,0 +1,26 @@ +package com.pahimar.ee3.inventory; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.IInventory; +import net.minecraft.inventory.Slot; +import net.minecraft.item.ItemStack; + +public class SlotAlchemicalBag extends Slot +{ + private final EntityPlayer entityPlayer; + private ContainerAlchemicalBag containerAlchemicalBag; + + public SlotAlchemicalBag(EntityPlayer entityPlayer, ContainerAlchemicalBag containerAlchemicalBag, IInventory inventory, int x, int y, int z) + { + super(inventory, x, y, z); + this.entityPlayer = entityPlayer; + this.containerAlchemicalBag = containerAlchemicalBag; + } + + @Override + public void onSlotChange(ItemStack itemStack1, ItemStack itemStack2) + { + super.onSlotChange(itemStack1, itemStack2); + containerAlchemicalBag.saveInventory(entityPlayer); + } +} diff --git a/src/main/java/com/pahimar/ee3/item/ItemAlchemicalBag.java b/src/main/java/com/pahimar/ee3/item/ItemAlchemicalBag.java index da30d289..79a973e6 100644 --- a/src/main/java/com/pahimar/ee3/item/ItemAlchemicalBag.java +++ b/src/main/java/com/pahimar/ee3/item/ItemAlchemicalBag.java @@ -65,11 +65,13 @@ public class ItemAlchemicalBag extends ItemEE implements IDyeable @Override public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer entityPlayer) { -// if (!world.isRemote) -// { + if (!world.isRemote) + { + // Set a UUID on the Alchemical Bag, if one doesn't exist already + ItemStackNBTHelper.setUUID(itemStack); // ItemStackNBTHelper.setBoolean(itemStack, Strings.NBT_ITEM_ALCHEMICAL_BAG_GUI_OPEN, true); // entityPlayer.openGui(EquivalentExchange3.instance, GuiIds.ALCHEMICAL_BAG, entityPlayer.worldObj, (int) entityPlayer.posX, (int) entityPlayer.posY, (int) entityPlayer.posZ); -// } + } if (world.isRemote) {