Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MinefusInventoryPlayer implements IInventory
- {
- /** The name your custom inventory will display in the GUI, possibly just "Inventory" */
- private String name;
- /** The key used to store and retrieve the inventory from NBT */
- private final String tagName = "CustomInvTag";
- /** Define the inventory size here for easy reference */
- // This is also the place to define which slot is which if you have different types,
- // for example SLOT_SHIELD = 0, SLOT_AMULET = 1;
- public static final int INV_SIZE = 64;
- /** Inventory's size must be same as number of slots you add to the Container class */
- private ItemStack[] inventory = new ItemStack[INV_SIZE];
- public MinefusInventoryPlayer()
- {
- }
- @Override
- public int getSizeInventory()
- {
- return inventory.length;
- }
- @Override
- public ItemStack getStackInSlot(int slot)
- {
- return inventory[slot];
- }
- @Override
- public ItemStack decrStackSize(int slot, int amount)
- {
- ItemStack stack = getStackInSlot(slot);
- if (stack != null)
- {
- if (stack.stackSize > amount)
- {
- stack = stack.splitStack(amount);
- if (stack.stackSize == 0)
- {
- setInventorySlotContents(slot, null);
- }
- }
- else
- {
- setInventorySlotContents(slot, null);
- }
- this.markDirty();;
- }
- return stack;
- }
- @Override
- public ItemStack getStackInSlotOnClosing(int slot)
- {
- ItemStack stack = getStackInSlot(slot);
- if (stack != null)
- {
- setInventorySlotContents(slot, null);
- }
- return stack;
- }
- @Override
- public void setInventorySlotContents(int slot, ItemStack itemstack)
- {
- this.inventory[slot] = itemstack;
- if (itemstack != null && itemstack.stackSize > this.getInventoryStackLimit())
- {
- itemstack.stackSize = this.getInventoryStackLimit();
- }
- this.markDirty();
- }
- @Override
- public String getInventoryName()
- {
- return this.hasCustomInventoryName() ? this.name : "key.minefusinventory.desc";
- }
- public MinefusInventoryPlayer getMinefusInventory(){
- return this.getMinefusInventory();
- }
- @Override
- public boolean hasCustomInventoryName()
- {
- return this.name != null && this.name.length() > 0;
- }
- /**
- * Our custom slots are similar to armor - only one item per slot
- */
- @Override
- public int getInventoryStackLimit()
- {
- return 64;
- }
- @Override
- public void markDirty()
- {
- for (int i = 0; i < this.getSizeInventory(); ++i)
- {
- if (this.getStackInSlot(i) != null && this.getStackInSlot(i).stackSize == 0)
- this.setInventorySlotContents(i, null);
- }
- }
- @Override
- public boolean isUseableByPlayer(EntityPlayer entityplayer)
- {
- return true;
- }
- /**
- * This method doesn't seem to do what it claims to do, as
- * items can still be left-clicked and placed in the inventory
- * even when this returns false
- */
- @Override
- public boolean isItemValidForSlot(int slot, ItemStack itemstack)
- {
- return true;
- }
- public void writeToNBT(NBTTagCompound compound)
- {
- NBTTagList items = new NBTTagList();
- for (int i = 0; i < getSizeInventory(); ++i)
- {
- if (getStackInSlot(i) != null)
- {
- NBTTagCompound item = new NBTTagCompound();
- item.setByte("Slot", (byte) i);
- getStackInSlot(i).writeToNBT(item);
- items.appendTag(item);
- }
- }
- // We're storing our items in a custom tag list using our 'tagName' from above
- // to prevent potential conflicts
- compound.setTag(tagName, items);
- }
- public void readFromNBT(NBTTagCompound compound) {
- NBTTagList items = compound.getTagList(tagName, compound.getId());
- for (int i = 0; i < items.tagCount(); ++i) {
- NBTTagCompound item = items.getCompoundTagAt(i);
- byte slot = item.getByte("Slot");
- if (slot >= 0 && slot < getSizeInventory()) {
- inventory[slot] = ItemStack.loadItemStackFromNBT(item);
- }
- }
- }
- @Override
- public void openInventory() {
- }
- @Override
- public void closeInventory() {
- }
- public void setCustomGuiName(String name)
- {
- this.name = name;
- }
- public int getFirstEmptyStack(){
- for (int i = 0; i < this.inventory.length; ++i)
- {
- if (this.inventory[i] == null)
- {
- return i;
- }
- }
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment