Advertisement
Cypher121

InvComponent

Oct 9th, 2015
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.75 KB | None | 0 0
  1. package com.cout970.magneticraft.util;
  2.  
  3. import net.minecraft.entity.player.EntityPlayer;
  4. import net.minecraft.inventory.IInventory;
  5. import net.minecraft.item.ItemStack;
  6. import net.minecraft.nbt.NBTTagCompound;
  7. import net.minecraft.nbt.NBTTagList;
  8. import net.minecraft.tileentity.TileEntity;
  9.  
  10. public class InventoryComponent implements IInventory {
  11.  
  12.     public TileEntity tile;
  13.     public ItemStack[] inventory;
  14.     public String name;
  15.  
  16.     public InventoryComponent(TileEntity t, int slots, String n) {
  17.         inventory = new ItemStack[slots];
  18.         name = n;
  19.         tile = t;
  20.     }
  21.  
  22.     @Override
  23.     public int getSizeInventory() {
  24.         return inventory.length;
  25.     }
  26.  
  27.     @Override
  28.     public ItemStack getStackInSlot(int var1) {
  29.         return inventory[var1];
  30.     }
  31.  
  32.     @Override
  33.     public ItemStack decrStackSize(int slot, int amount) {
  34.         ItemStack itemStack = getStackInSlot(slot);
  35.         if (itemStack != null) {
  36.             if (itemStack.stackSize <= amount) {
  37.                 setInventorySlotContents(slot, null);
  38.             } else {
  39.                 itemStack = itemStack.splitStack(amount);
  40.                 if (itemStack.stackSize == 0) {
  41.                     setInventorySlotContents(slot, null);
  42.                 }
  43.             }
  44.         }
  45.         return itemStack;
  46.     }
  47.  
  48.     @Override
  49.     public ItemStack getStackInSlotOnClosing(int var1) {
  50.         return inventory[var1];
  51.     }
  52.  
  53.     @Override
  54.     public void setInventorySlotContents(int slot, ItemStack itemStack) {
  55.         inventory[slot] = itemStack;
  56.  
  57.         if (itemStack != null && itemStack.stackSize > this.getInventoryStackLimit()) {
  58.             itemStack.stackSize = this.getInventoryStackLimit();
  59.         }
  60.         markDirty();
  61.     }
  62.  
  63.     @Override
  64.     public String getInventoryName() {
  65.         return name;
  66.     }
  67.  
  68.     @Override
  69.     public boolean hasCustomInventoryName() {
  70.         return false;
  71.     }
  72.  
  73.     @Override
  74.     public int getInventoryStackLimit() {
  75.         return 64;
  76.     }
  77.  
  78.     @Override
  79.     public boolean isUseableByPlayer(EntityPlayer var1) {
  80.         return true;
  81.     }
  82.  
  83.     @Override
  84.     public void openInventory() {
  85.     }
  86.  
  87.     @Override
  88.     public void closeInventory() {
  89.     }
  90.  
  91.     @Override
  92.     public boolean isItemValidForSlot(int var1, ItemStack var2) {
  93.         return true;
  94.     }
  95.  
  96.     public void readFromNBT(NBTTagCompound nbtTagCompound) {
  97.         readFromNBT(nbtTagCompound, "Inventory");
  98.     }
  99.  
  100.     public void writeToNBT(NBTTagCompound nbtTagCompound) {
  101.         writeToNBT(nbtTagCompound, "Inventory");
  102.     }
  103.  
  104.     public void readFromNBT(NBTTagCompound nbtTagCompound, String name) {
  105.  
  106.         NBTTagList tagList = nbtTagCompound.getTagList(name, 10);
  107.         inventory = new ItemStack[this.getSizeInventory()];
  108.         for (int i = 0; i < tagList.tagCount(); ++i) {
  109.             NBTTagCompound tagCompound = tagList.getCompoundTagAt(i);
  110.             byte slot = tagCompound.getByte("Slot");
  111.             if (slot >= 0 && slot < inventory.length) {
  112.                 setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(tagCompound));
  113.             }
  114.         }
  115.     }
  116.  
  117.     public void writeToNBT(NBTTagCompound nbtTagCompound, String name) {
  118.  
  119.         NBTTagList list = new NBTTagList();
  120.         for (int currentIndex = 0; currentIndex < inventory.length; ++currentIndex) {
  121.             if (inventory[currentIndex] != null) {
  122.                 NBTTagCompound nbt = new NBTTagCompound();
  123.                 nbt.setByte("Slot", (byte) currentIndex);
  124.                 inventory[currentIndex].writeToNBT(nbt);
  125.                 list.appendTag(nbt);
  126.             }
  127.         }
  128.         nbtTagCompound.setTag(name, list);
  129.     }
  130.  
  131.     @Override
  132.     public void markDirty() {
  133.         tile.markDirty();
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement