HalestormXV

Untitled

Mar 19th, 2016
43
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.halestormxv.inventory;
  2.  
  3. import com.halestormxv.item.CelestialCraft_items;
  4. import com.halestormxv.item.keyPouch;
  5.  
  6. import net.minecraft.entity.player.EntityPlayer;
  7. import net.minecraft.inventory.IInventory;
  8. import net.minecraft.item.ItemStack;
  9. import net.minecraft.nbt.NBTTagCompound;
  10. import net.minecraft.nbt.NBTTagList;
  11. import net.minecraftforge.common.util.Constants;
  12.  
  13. public class InventoryKeyPouch implements IInventory
  14. {
  15.         private String name = "Celestial Keypouch";
  16.        
  17.         /** Provides NBT Tag Compound to reference */
  18.         private final ItemStack invItem;
  19.  
  20.         /** Defining your inventory size this way is handy */
  21.         public static final int INV_SIZE = 12;
  22.  
  23.         /** Inventory's size must be same as number of slots you add to the Container class */
  24.         private ItemStack[] inventory = new ItemStack[INV_SIZE];
  25.  
  26.         /**
  27.          * @param itemstack - the ItemStack to which this inventory belongs
  28.          */
  29.         public InventoryKeyPouch(ItemStack stack)
  30.         {
  31.             invItem = stack;
  32.  
  33.             // Create a new NBT Tag Compound if one doesn't already exist, or you will crash
  34.             if (!stack.hasTagCompound()) {
  35.                 stack.setTagCompound(new NBTTagCompound());
  36.             }
  37.             // note that it's okay to use stack instead of invItem right there
  38.             // both reference the same memory location, so whatever you change using
  39.             // either reference will change in the other
  40.  
  41.             // Read the inventory contents from NBT
  42.             readFromNBT(stack.getTagCompound());
  43.         }
  44.  
  45.         @Override
  46.         public int getSizeInventory()
  47.         {
  48.             return inventory.length;
  49.         }
  50.  
  51.         @Override
  52.         public ItemStack getStackInSlot(int slot)
  53.         {
  54.             return inventory[slot];
  55.         }
  56.  
  57.         @Override
  58.         public ItemStack decrStackSize(int slot, int amount)
  59.         {
  60.             ItemStack stack = getStackInSlot(slot);
  61.             if(stack != null)
  62.             {
  63.                 if(stack.stackSize > amount)
  64.                 {
  65.                     stack = stack.splitStack(amount);
  66.                     // Don't forget this line or your inventory will not be saved!
  67.                     markDirty();
  68.                 }
  69.                 else
  70.                 {
  71.                     // this method also calls onInventoryChanged, so we don't need to call it again
  72.                     setInventorySlotContents(slot, null);
  73.                 }
  74.             }
  75.             return stack;
  76.         }
  77.  
  78.         @Override
  79.         public ItemStack getStackInSlotOnClosing(int slot)
  80.         {
  81.             ItemStack stack = getStackInSlot(slot);
  82.             setInventorySlotContents(slot, null);
  83.             return stack;
  84.         }
  85.  
  86.         @Override
  87.         public void setInventorySlotContents(int slot, ItemStack stack)
  88.         {
  89.             inventory[slot] = stack;
  90.  
  91.             if (stack != null && stack.stackSize > getInventoryStackLimit())
  92.             {
  93.                 stack.stackSize = getInventoryStackLimit();
  94.             }
  95.  
  96.             // Don't forget this line or your inventory will not be saved!
  97.             markDirty();
  98.         }
  99.  
  100.         // 1.7.2+ renamed to getInventoryName
  101.         @Override
  102.         public String getInventoryName()
  103.         {
  104.             return name;
  105.         }
  106.  
  107.         // 1.7.2+ renamed to hasCustomInventoryName
  108.         @Override
  109.         public boolean hasCustomInventoryName()
  110.         {
  111.             return name.length() > 0;
  112.         }
  113.  
  114.         @Override
  115.         public int getInventoryStackLimit()
  116.         {
  117.             return 64;
  118.         }
  119.  
  120.         /**
  121.          * This is the method that will handle saving the inventory contents, as it is called (or should be called!)
  122.          * anytime the inventory changes. Perfect. Much better than using onUpdate in an Item, as this will also
  123.          * let you change things in your inventory without ever opening a Gui, if you want.
  124.          */
  125.          // 1.7.2+ renamed to markDirty
  126.         @Override
  127.         public void markDirty()
  128.         {
  129.             for (int i = 0; i < getSizeInventory(); ++i)
  130.             {
  131.                 if (getStackInSlot(i) != null && getStackInSlot(i).stackSize == 0) {
  132.                     inventory[i] = null;
  133.                 }
  134.             }
  135.            
  136.             // This line here does the work:       
  137.             writeToNBT(invItem.getTagCompound());
  138.         }
  139.  
  140.         @Override
  141.         public boolean isUseableByPlayer(EntityPlayer entityplayer)
  142.         {
  143.             return true;
  144.         }
  145.  
  146.         // 1.7.2+ renamed to openInventory(EntityPlayer player)
  147.         @Override
  148.         public void openInventory() {}
  149.  
  150.         // 1.7.2+ renamed to closeInventory(EntityPlayer player)
  151.         @Override
  152.         public void closeInventory() {}
  153.  
  154.         /**
  155.          * This method doesn't seem to do what it claims to do, as
  156.          * items can still be left-clicked and placed in the inventory
  157.          * even when this returns false
  158.          */
  159.         @Override
  160.         public boolean isItemValidForSlot(int slot, ItemStack itemstack)
  161.         {
  162.             // Don't want to be able to store the inventory item within itself
  163.             // Bad things will happen, like losing your inventory
  164.             // Actually, this needs a custom Slot to work
  165.             return !(itemstack.getItem() instanceof keyPouch);
  166.         }
  167.  
  168.         /**
  169.          * A custom method to read our inventory from an ItemStack's NBT compound
  170.          */
  171.         public void readFromNBT(NBTTagCompound compound)
  172.         {
  173.             // Gets the custom taglist we wrote to this compound, if any
  174.             // 1.7.2+ change to compound.getTagList("ItemInventory", Constants.NBT.TAG_COMPOUND);
  175.             NBTTagList items = compound.getTagList("ItemInventory", Constants.NBT.TAG_COMPOUND);
  176.  
  177.             for (int i = 0; i < items.tagCount(); ++i)
  178.             {
  179.                 // 1.7.2+ change to items.getCompoundTagAt(i)
  180.                 NBTTagCompound item = (NBTTagCompound) items.getCompoundTagAt(i);
  181.                 int slot = item.getInteger("Slot");
  182.  
  183.                 // Just double-checking that the saved slot index is within our inventory array bounds
  184.                 if (slot >= 0 && slot < getSizeInventory()) {
  185.                     inventory[slot] = ItemStack.loadItemStackFromNBT(item);
  186.                 }
  187.             }
  188.         }
  189.  
  190.         /**
  191.          * A custom method to write our inventory to an ItemStack's NBT compound
  192.          */
  193.         public void writeToNBT(NBTTagCompound tagcompound)
  194.         {
  195.             // Create a new NBT Tag List to store itemstacks as NBT Tags
  196.             NBTTagList items = new NBTTagList();
  197.  
  198.             for (int i = 0; i < getSizeInventory(); ++i)
  199.             {
  200.                 // Only write stacks that contain items
  201.                 if (getStackInSlot(i) != null)
  202.                 {
  203.                     // Make a new NBT Tag Compound to write the itemstack and slot index to
  204.                     NBTTagCompound item = new NBTTagCompound();
  205.                     item.setInteger("Slot", i);
  206.                     // Writes the itemstack in slot(i) to the Tag Compound we just made
  207.                     getStackInSlot(i).writeToNBT(item);
  208.  
  209.                     // add the tag compound to our tag list
  210.                     items.appendTag(item);
  211.                 }
  212.             }
  213.             // Add the TagList to the ItemStack's Tag Compound with the name "ItemInventory"
  214.             tagcompound.setTag("ItemInventory", items);
  215.         }
  216.     }
RAW Paste Data Copied