Advertisement
Cypher121

InvResizable

Oct 9th, 2015
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.66 KB | None | 0 0
  1. package com.cout970.magneticraft.util;
  2.  
  3. import net.minecraft.item.ItemStack;
  4. import net.minecraft.nbt.NBTTagCompound;
  5. import net.minecraft.tileentity.TileEntity;
  6.  
  7. public class InventoryResizable extends InventoryComponent {
  8.     private int maxSlots, curSlots;
  9.     public InventoryResizable(TileEntity t, int slots, String n) {
  10.         super(t, slots, n);
  11.         curSlots = maxSlots = slots;
  12.     }
  13.  
  14.     public void unlock() {
  15.         markDirty();
  16.         curSlots = maxSlots;
  17.     }
  18.  
  19.     public void lock() {
  20.         markDirty();
  21.         for (ItemStack is : inventory) {
  22.             if (is != null) {
  23.                 return;
  24.             }
  25.         }
  26.         curSlots = 0;
  27.     }
  28.  
  29.     public boolean resize(int delta) {
  30.         markDirty();
  31.         int newSlots = curSlots + delta;
  32.         if ((newSlots < 0) || (newSlots > maxSlots)) {
  33.             return false;
  34.         }
  35.  
  36.         if (delta < 0) {
  37.             for (int i = newSlots; i < curSlots; i++) {
  38.                 if (inventory[i] != null) {
  39.                     return false;
  40.                 }
  41.             }
  42.         }
  43.  
  44.         curSlots = newSlots;
  45.         return true;
  46.     }
  47.  
  48.     @Override
  49.     public boolean isItemValidForSlot(int slot, ItemStack is) {
  50.         return (slot < curSlots) && super.isItemValidForSlot(slot, is);
  51.     }
  52.  
  53.     @Override
  54.     public void readFromNBT(NBTTagCompound nbt, String name) {
  55.         super.readFromNBT(nbt, name);
  56.         curSlots = nbt.getInteger(name + "curSlots");
  57.     }
  58.  
  59.     @Override
  60.     public void writeToNBT(NBTTagCompound nbt, String name) {
  61.         super.writeToNBT(nbt, name);
  62.         nbt.setInteger(name + "curSlots", curSlots);
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement