Advertisement
Guest User

InventoryCalculatorBasic NBT read write problem :/

a guest
Jan 6th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.27 KB | None | 0 0
  1. package com.limplungs.invcalc.inventories;
  2.  
  3. import net.minecraft.client.Minecraft;
  4. import net.minecraft.entity.player.EntityPlayer;
  5. import net.minecraft.inventory.IInventory;
  6. import net.minecraft.item.ItemStack;
  7. import net.minecraft.nbt.NBTTagCompound;
  8. import net.minecraft.nbt.NBTTagList;
  9.  
  10. public class InventoryCalculatorBasic implements IInventory
  11. {
  12.    
  13.     private ItemStack[] itemslots = new ItemStack[5];
  14.  
  15.     public boolean isOn = false;
  16.     public int battery = 0;
  17.     public int batMax = 200;
  18.    
  19.     public boolean hasBattery = false;
  20.     public boolean hasMirror = false;
  21.     public boolean hasSolar = false;
  22.     public boolean hasClock = false;
  23.    
  24.  
  25.     /**
  26.      * @param itemstack
  27.      *            - the ItemStack to which this inventory belongs
  28.      */
  29.     public InventoryCalculatorBasic(ItemStack stack)
  30.     {
  31.         if (!stack.hasTagCompound())
  32.         {
  33.             stack.setTagCompound(new NBTTagCompound());
  34.         }
  35.  
  36.         readFromNBT(stack.getTagCompound());
  37.     }
  38.  
  39.     @Override
  40.     public int getSizeInventory()
  41.     {
  42.         return itemslots.length;
  43.     }
  44.  
  45.     @Override
  46.     public ItemStack getStackInSlot(int slot)
  47.     {
  48.         return itemslots[slot];
  49.     }
  50.  
  51.     @Override
  52.     public ItemStack decrStackSize(int slot, int count)
  53.     {
  54.         ItemStack itemstack = getStackInSlot(slot);
  55.  
  56.         if (itemstack != null)
  57.         {
  58.             if (itemstack.stackSize <= count)
  59.             {
  60.                 setInventorySlotContents(slot, null);
  61.             }
  62.             else
  63.             {
  64.                 itemstack = itemstack.splitStack(count);
  65.                
  66.                 markDirty();
  67.             }
  68.         }
  69.  
  70.         return itemstack;
  71.     }
  72.  
  73.     @Override
  74.     public ItemStack getStackInSlotOnClosing(int i)
  75.     {
  76.         ItemStack itemstack = getStackInSlot(i);
  77.        
  78.         setInventorySlotContents(i, null);
  79.        
  80.         return itemstack;
  81.     }
  82.  
  83.     @Override
  84.     public void setInventorySlotContents(int i, ItemStack itemstack)
  85.     {
  86.         itemslots[i] = itemstack;
  87.  
  88.         if (itemstack != null && itemstack.stackSize > getInventoryStackLimit())
  89.         {
  90.             itemstack.stackSize = getInventoryStackLimit();
  91.         }
  92.  
  93.         markDirty();
  94.     }
  95.  
  96.     @Override
  97.     public String getInventoryName()
  98.     {
  99.         return "iC-Basic";
  100.     }
  101.  
  102.     @Override
  103.     public boolean hasCustomInventoryName()
  104.     {
  105.         return true;
  106.     }
  107.  
  108.     @Override
  109.     public int getInventoryStackLimit()
  110.     {
  111.         return 64;
  112.     }
  113.  
  114.     @Override
  115.     public boolean isUseableByPlayer(EntityPlayer p_70300_1_)
  116.     {
  117.         return true;
  118.     }
  119.  
  120.     @Override
  121.     public void markDirty()
  122.     {
  123.         for (int i = 0; i < getSizeInventory(); ++i)
  124.         {
  125.             if (getStackInSlot(i) != null && getStackInSlot(i).stackSize == 0)
  126.             {
  127.                 itemslots[i] = null;
  128.             }
  129.         }
  130.        
  131.         writeToNBT(Minecraft.getMinecraft().thePlayer.inventory.getCurrentItem().getTagCompound());
  132.     }
  133.    
  134.     @Override
  135.     public boolean isItemValidForSlot(int slot, ItemStack itemstack)
  136.     {
  137.         return true;
  138.     }
  139.  
  140.     public void writeToNBT(NBTTagCompound compound)
  141.     {
  142.         NBTTagList items = new NBTTagList();
  143.  
  144.         for (int i = 0; i < getSizeInventory(); i++)
  145.         {
  146.             ItemStack stack = this.getStackInSlot(i);
  147.  
  148.             if (stack != null)
  149.             {
  150.                 NBTTagCompound item = new NBTTagCompound();
  151.                 item.setByte("Slot", (byte) i);
  152.                 stack.writeToNBT(item);
  153.                 items.appendTag(item);
  154.             }
  155.         }
  156.  
  157.         compound.setTag("Items", items);
  158.        
  159.         compound.setBoolean("status", this.isOn);
  160.         compound.setInteger("battery", this.battery);
  161.         compound.setInteger("batMax", this.batMax);
  162.        
  163.         compound.setBoolean("hasBattery", this.hasBattery);
  164.         compound.setBoolean("hasMirror", this.hasMirror);
  165.         compound.setBoolean("hasSolar", this.hasSolar);
  166.         compound.setBoolean("hasClock", this.hasClock);
  167.     }
  168.  
  169.     public void readFromNBT(NBTTagCompound compound)
  170.     {
  171.         NBTTagList items = compound.getTagList("Items", compound.getId());
  172.  
  173.         for (int i = 0; i < items.tagCount(); i++)
  174.         {
  175.             NBTTagCompound item = (NBTTagCompound) items.getCompoundTagAt(i);
  176.             byte slot = item.getByte("Slot");
  177.  
  178.             if (slot >= 0 && slot < this.getSizeInventory())
  179.             {
  180.                 this.itemslots[slot] = ItemStack.loadItemStackFromNBT(item);
  181.             }
  182.         }
  183.        
  184.         this.isOn = compound.getBoolean("status");
  185.         this.battery = compound.getInteger("battery");
  186.         this.batMax = compound.getInteger("batMax");
  187.        
  188.         this.hasBattery = compound.getBoolean("hasBattery");
  189.         this.hasMirror = compound.getBoolean("hasMirror");
  190.         this.hasSolar = compound.getBoolean("hasSolar");
  191.         this.hasClock = compound.getBoolean("hasClock");
  192.     }
  193.  
  194.     @Override
  195.     public void openInventory(){}
  196.  
  197.     @Override
  198.     public void closeInventory(){}
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement