Some more work on alchemical bag inventories

This commit is contained in:
pahimar 2014-03-25 21:59:36 -04:00
parent 5a6279eae9
commit c3dd664629
8 changed files with 127 additions and 32 deletions

View file

@ -7,7 +7,7 @@ import com.pahimar.ee3.lib.Strings;
import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly; import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.gui.inventory.GuiContainer; 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 net.minecraft.item.ItemStack;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
@ -22,9 +22,9 @@ import org.lwjgl.opengl.GL11;
public class GuiAlchemicalBag extends GuiContainer 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 @Override

View file

@ -3,6 +3,7 @@ package com.pahimar.ee3.client.handler;
import com.pahimar.ee3.api.WrappedStack; import com.pahimar.ee3.api.WrappedStack;
import com.pahimar.ee3.emc.EmcRegistry; import com.pahimar.ee3.emc.EmcRegistry;
import com.pahimar.ee3.emc.EmcValue; import com.pahimar.ee3.emc.EmcValue;
import com.pahimar.ee3.item.ItemAlchemicalBag;
import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly; import cpw.mods.fml.relauncher.SideOnly;
import net.minecraftforge.event.EventPriority; import net.minecraftforge.event.EventPriority;
@ -50,5 +51,10 @@ public class ItemTooltipEventHandler
event.toolTip.add("No EMC value"); event.toolTip.add("No EMC value");
} }
} }
if (event.itemStack.getItem() instanceof ItemAlchemicalBag && event.itemStack.getTagCompound() != null)
{
event.toolTip.add("NBT: " + event.itemStack.getTagCompound().toString());
}
} }
} }

View file

@ -15,8 +15,6 @@ import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager; import net.minecraft.item.crafting.CraftingManager;
import java.util.UUID;
/** /**
* Equivalent-Exchange-3 * Equivalent-Exchange-3
* <p/> * <p/>
@ -53,9 +51,7 @@ public class CraftingHandler implements ICraftingHandler
// Set the UUID on an Alchemical Bag when picked up from crafting // Set the UUID on an Alchemical Bag when picked up from crafting
if (itemStack.getItem() instanceof ItemAlchemicalBag) if (itemStack.getItem() instanceof ItemAlchemicalBag)
{ {
UUID itemUUID = UUID.randomUUID(); ItemStackNBTHelper.setUUID(itemStack);
ItemStackNBTHelper.setLong(itemStack, Strings.NBT_ITEM_UUID_MOST_SIG, itemUUID.getMostSignificantBits());
ItemStackNBTHelper.setLong(itemStack, Strings.NBT_ITEM_UUID_LEAST_SIG, itemUUID.getLeastSignificantBits());
} }
} }
} }

View file

@ -1,8 +1,11 @@
package com.pahimar.ee3.helper; package com.pahimar.ee3.helper;
import com.pahimar.ee3.lib.Strings;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import java.util.UUID;
/** /**
* Equivalent-Exchange-3 * Equivalent-Exchange-3
* <p/> * <p/>
@ -21,7 +24,6 @@ public class ItemStackNBTHelper
*/ */
private static void initNBTTagCompound(ItemStack itemStack) private static void initNBTTagCompound(ItemStack itemStack)
{ {
if (itemStack.stackTagCompound == null) if (itemStack.stackTagCompound == null)
{ {
itemStack.setTagCompound(new NBTTagCompound()); itemStack.setTagCompound(new NBTTagCompound());
@ -35,17 +37,33 @@ public class ItemStackNBTHelper
public static void removeTag(ItemStack itemStack, String keyName) public static void removeTag(ItemStack itemStack, String keyName)
{ {
if (itemStack.stackTagCompound != null) if (itemStack.stackTagCompound != null)
{ {
itemStack.stackTagCompound.removeTag(keyName); 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 // String
public static String getString(ItemStack itemStack, String keyName) public static String getString(ItemStack itemStack, String keyName)
{ {
initNBTTagCompound(itemStack); initNBTTagCompound(itemStack);
if (!itemStack.stackTagCompound.hasKey(keyName)) if (!itemStack.stackTagCompound.hasKey(keyName))
@ -58,7 +76,6 @@ public class ItemStackNBTHelper
public static void setString(ItemStack itemStack, String keyName, String keyValue) public static void setString(ItemStack itemStack, String keyName, String keyValue)
{ {
initNBTTagCompound(itemStack); initNBTTagCompound(itemStack);
itemStack.stackTagCompound.setString(keyName, keyValue); itemStack.stackTagCompound.setString(keyName, keyValue);
@ -67,7 +84,6 @@ public class ItemStackNBTHelper
// boolean // boolean
public static boolean getBoolean(ItemStack itemStack, String keyName) public static boolean getBoolean(ItemStack itemStack, String keyName)
{ {
initNBTTagCompound(itemStack); initNBTTagCompound(itemStack);
if (!itemStack.stackTagCompound.hasKey(keyName)) if (!itemStack.stackTagCompound.hasKey(keyName))
@ -80,7 +96,6 @@ public class ItemStackNBTHelper
public static void setBoolean(ItemStack itemStack, String keyName, boolean keyValue) public static void setBoolean(ItemStack itemStack, String keyName, boolean keyValue)
{ {
initNBTTagCompound(itemStack); initNBTTagCompound(itemStack);
itemStack.stackTagCompound.setBoolean(keyName, keyValue); itemStack.stackTagCompound.setBoolean(keyName, keyValue);
@ -89,7 +104,6 @@ public class ItemStackNBTHelper
// byte // byte
public static byte getByte(ItemStack itemStack, String keyName) public static byte getByte(ItemStack itemStack, String keyName)
{ {
initNBTTagCompound(itemStack); initNBTTagCompound(itemStack);
if (!itemStack.stackTagCompound.hasKey(keyName)) if (!itemStack.stackTagCompound.hasKey(keyName))
@ -102,7 +116,6 @@ public class ItemStackNBTHelper
public static void setByte(ItemStack itemStack, String keyName, byte keyValue) public static void setByte(ItemStack itemStack, String keyName, byte keyValue)
{ {
initNBTTagCompound(itemStack); initNBTTagCompound(itemStack);
itemStack.stackTagCompound.setByte(keyName, keyValue); itemStack.stackTagCompound.setByte(keyName, keyValue);
@ -111,7 +124,6 @@ public class ItemStackNBTHelper
// short // short
public static short getShort(ItemStack itemStack, String keyName) public static short getShort(ItemStack itemStack, String keyName)
{ {
initNBTTagCompound(itemStack); initNBTTagCompound(itemStack);
if (!itemStack.stackTagCompound.hasKey(keyName)) if (!itemStack.stackTagCompound.hasKey(keyName))
@ -124,7 +136,6 @@ public class ItemStackNBTHelper
public static void setShort(ItemStack itemStack, String keyName, short keyValue) public static void setShort(ItemStack itemStack, String keyName, short keyValue)
{ {
initNBTTagCompound(itemStack); initNBTTagCompound(itemStack);
itemStack.stackTagCompound.setShort(keyName, keyValue); itemStack.stackTagCompound.setShort(keyName, keyValue);
@ -133,7 +144,6 @@ public class ItemStackNBTHelper
// int // int
public static int getInt(ItemStack itemStack, String keyName) public static int getInt(ItemStack itemStack, String keyName)
{ {
initNBTTagCompound(itemStack); initNBTTagCompound(itemStack);
if (!itemStack.stackTagCompound.hasKey(keyName)) if (!itemStack.stackTagCompound.hasKey(keyName))
@ -146,7 +156,6 @@ public class ItemStackNBTHelper
public static void setInteger(ItemStack itemStack, String keyName, int keyValue) public static void setInteger(ItemStack itemStack, String keyName, int keyValue)
{ {
initNBTTagCompound(itemStack); initNBTTagCompound(itemStack);
itemStack.stackTagCompound.setInteger(keyName, keyValue); itemStack.stackTagCompound.setInteger(keyName, keyValue);
@ -187,7 +196,6 @@ public class ItemStackNBTHelper
public static void setFloat(ItemStack itemStack, String keyName, float keyValue) public static void setFloat(ItemStack itemStack, String keyName, float keyValue)
{ {
initNBTTagCompound(itemStack); initNBTTagCompound(itemStack);
itemStack.stackTagCompound.setFloat(keyName, keyValue); itemStack.stackTagCompound.setFloat(keyName, keyValue);
@ -196,7 +204,6 @@ public class ItemStackNBTHelper
// double // double
public static double getDouble(ItemStack itemStack, String keyName) public static double getDouble(ItemStack itemStack, String keyName)
{ {
initNBTTagCompound(itemStack); initNBTTagCompound(itemStack);
if (!itemStack.stackTagCompound.hasKey(keyName)) if (!itemStack.stackTagCompound.hasKey(keyName))
@ -209,7 +216,6 @@ public class ItemStackNBTHelper
public static void setDouble(ItemStack itemStack, String keyName, double keyValue) public static void setDouble(ItemStack itemStack, String keyName, double keyValue)
{ {
initNBTTagCompound(itemStack); initNBTTagCompound(itemStack);
itemStack.stackTagCompound.setDouble(keyName, keyValue); itemStack.stackTagCompound.setDouble(keyName, keyValue);

View file

@ -16,6 +16,9 @@ import net.minecraft.item.ItemStack;
*/ */
public class ContainerAlchemicalBag extends Container public class ContainerAlchemicalBag extends Container
{ {
private final EntityPlayer entityPlayer;
private final InventoryAlchemicalBag inventoryAlchemicalBag;
private int bagInventoryRows; private int bagInventoryRows;
private int bagInventoryColumns; private int bagInventoryColumns;
@ -35,8 +38,11 @@ public class ContainerAlchemicalBag extends Container
private final int PLAYER_INVENTORY_ROWS = 3; private final int PLAYER_INVENTORY_ROWS = 3;
private final int PLAYER_INVENTORY_COLUMNS = 9; 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) // if (alchemicalBag.getItemDamage() == 0)
// { // {
// bagInventoryRows = SMALL_BAG_INVENTORY_ROWS; // bagInventoryRows = SMALL_BAG_INVENTORY_ROWS;
@ -118,13 +124,14 @@ public class ContainerAlchemicalBag extends Container
} }
@Override @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) for (ItemStack itemStack : invPlayer.mainInventory)
{ {
if (itemStack != null) if (itemStack != null)
@ -135,6 +142,8 @@ public class ContainerAlchemicalBag extends Container
} }
} }
} }
saveInventory(entityPlayer);
} }
} }
@ -144,4 +153,9 @@ public class ContainerAlchemicalBag extends Container
// TODO // TODO
return null; return null;
} }
public void saveInventory(EntityPlayer entityPlayer)
{
inventoryAlchemicalBag.onGuiSaved(entityPlayer);
}
} }

View file

@ -1,5 +1,6 @@
package com.pahimar.ee3.inventory; package com.pahimar.ee3.inventory;
import com.pahimar.ee3.helper.ItemStackNBTHelper;
import com.pahimar.ee3.lib.Strings; import com.pahimar.ee3.lib.Strings;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory; import net.minecraft.inventory.IInventory;
@ -7,6 +8,8 @@ import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList; import net.minecraft.nbt.NBTTagList;
import java.util.UUID;
public class InventoryAlchemicalBag implements IInventory, INBTTaggable public class InventoryAlchemicalBag implements IInventory, INBTTaggable
{ {
public ItemStack parentItemStack; public ItemStack parentItemStack;
@ -21,13 +24,56 @@ public class InventoryAlchemicalBag implements IInventory, INBTTaggable
readFromNBT(itemStack.getTagCompound()); 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() public void save()
{ {
NBTTagCompound nbtTagCompound = parentItemStack.getTagCompound(); NBTTagCompound nbtTagCompound = parentItemStack.getTagCompound();
if (nbtTagCompound == null) if (nbtTagCompound == null)
{ {
nbtTagCompound = new NBTTagCompound(); 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); writeToNBT(nbtTagCompound);
parentItemStack.setTagCompound(nbtTagCompound); parentItemStack.setTagCompound(nbtTagCompound);
} }
@ -193,7 +239,6 @@ public class InventoryAlchemicalBag implements IInventory, INBTTaggable
public String getCustomName() public String getCustomName()
{ {
return customName; return customName;
} }
} }

View file

@ -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);
}
}

View file

@ -65,11 +65,13 @@ public class ItemAlchemicalBag extends ItemEE implements IDyeable
@Override @Override
public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer entityPlayer) 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); // 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); // entityPlayer.openGui(EquivalentExchange3.instance, GuiIds.ALCHEMICAL_BAG, entityPlayer.worldObj, (int) entityPlayer.posX, (int) entityPlayer.posY, (int) entityPlayer.posZ);
// } }
if (world.isRemote) if (world.isRemote)
{ {