Guest User

InventoryWeightLimit

a guest
Dec 26th, 2015
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.64 KB | None | 0 0
  1. package com.happykiller.weightlimit.player.inventory;
  2.  
  3. import com.happykiller.weightlimit.items.ItemBackpack;
  4.  
  5. import net.minecraft.entity.player.EntityPlayer;
  6. import net.minecraft.inventory.IInventory;
  7. import net.minecraft.item.ItemStack;
  8. import net.minecraft.nbt.NBTTagCompound;
  9. import net.minecraft.nbt.NBTTagList;
  10. import net.minecraft.util.ChatComponentText;
  11. import net.minecraft.util.IChatComponent;
  12.  
  13. public class InventoryWeightLimit implements IInventory {
  14.    
  15.     private final String name = "Inventory";
  16.    
  17.     private final String tagName = "WLInvTag";
  18.    
  19.     public static final int INV_SIZE = 1;
  20.    
  21.     private final int SLOT_BACKPACK = 0;
  22.    
  23.     ItemStack[] inventory = new ItemStack[INV_SIZE];
  24.    
  25.     public InventoryWeightLimit() {}
  26.  
  27.     public String getName() {
  28.         return name;
  29.     }
  30.  
  31.     public boolean hasCustomName() {
  32.         return name.length() > 0;
  33.     }
  34.  
  35.     public IChatComponent getDisplayName() {
  36.         return new ChatComponentText(name);
  37.     }
  38.  
  39.     public int getSizeInventory() {
  40.         return inventory.length;
  41.     }
  42.  
  43.     public ItemStack getStackInSlot(int slot) {
  44.         return inventory[slot];
  45.     }
  46.  
  47.     public ItemStack decrStackSize(int slot, int amount) {
  48.         ItemStack stack = getStackInSlot(slot);
  49.        
  50.         if(stack != null) {
  51.             if(stack.stackSize > amount) {
  52.                 stack = stack.splitStack(amount);
  53.                
  54.                 if(stack.stackSize == 0) {
  55.                     setInventorySlotContents(slot, null);
  56.                 }
  57.             }else {
  58.                 setInventorySlotContents(slot, null);
  59.             }
  60.            
  61.             this.markDirty();
  62.         }
  63.        
  64.         return stack;
  65.     }
  66.  
  67.     public ItemStack getStackInSlotOnClosing(int slot) {
  68.        
  69.         ItemStack stack = getStackInSlot(slot);
  70.        
  71.         if(stack != null) {
  72.             setInventorySlotContents(slot, null);
  73.         }
  74.        
  75.         return stack;
  76.     }
  77.  
  78.     public void setInventorySlotContents(int slot, ItemStack stack) {
  79.         this.inventory[slot] = stack;
  80.        
  81.         if(stack != null && stack.stackSize > this.getInventoryStackLimit()) {
  82.             stack.stackSize = this.getInventoryStackLimit();
  83.         }
  84.        
  85.         this.markDirty();
  86.     }
  87.  
  88.     public int getInventoryStackLimit() {
  89.         return 1;
  90.     }
  91.  
  92.     public void markDirty() {
  93.         for(int i  = 0; i < this.getSizeInventory(); i++) {
  94.             if(this.getStackInSlot(i) != null && this.getStackInSlot(i).stackSize == 0)
  95.                 this.setInventorySlotContents(i, null);
  96.         }
  97.     }
  98.  
  99.     public boolean isUseableByPlayer(EntityPlayer player) {
  100.         return true;
  101.     }
  102.  
  103.     public void openInventory(EntityPlayer player) {}
  104.  
  105.     public void closeInventory(EntityPlayer player) {}
  106.  
  107.     public boolean isItemValidForSlot(int slot, ItemStack stack) {
  108.         if(slot == SLOT_BACKPACK && stack.getItem() instanceof ItemBackpack)
  109.             return true;
  110.         else
  111.             return false;
  112.     }
  113.    
  114.     public void writeToNBT(NBTTagCompound tag) {
  115.         NBTTagList tagList = new NBTTagList();
  116.        
  117.         for(int i = 0; i < this.getSizeInventory(); i++) {
  118.             if(this.getStackInSlot(i) != null) {
  119.                 NBTTagCompound tag1 = new NBTTagCompound();
  120.                
  121.                 tag1.setByte("Slot", (byte)i);
  122.                 this.getStackInSlot(i).writeToNBT(tag1);
  123.                
  124.                 tagList.appendTag(tag1);
  125.             }
  126.         }
  127.        
  128.         tag.setTag(tagName, tagList);
  129.     }
  130.    
  131.     public void readFromNBT(NBTTagCompound tag) {
  132.         NBTTagList items = tag.getTagList(tagName, tag.getId());
  133.        
  134.         for(int i = 0; i < items.tagCount(); i++) {
  135.             NBTTagCompound item = items.getCompoundTagAt(i);
  136.            
  137.             byte slot = item.getByte("Slot");
  138.            
  139.             if(slot >= 0 && slot < this.getSizeInventory()) {
  140.                 this.setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(item));
  141.             }
  142.         }
  143.     }
  144.  
  145.     public int getField(int id) {
  146.         return 0;
  147.     }
  148.  
  149.     public void setField(int id, int value) {
  150.        
  151.     }
  152.  
  153.     public int getFieldCount() {
  154.         return 0;
  155.     }
  156.  
  157.     public void clear() {
  158.         for(int i = 0; i < inventory.length; i++) {
  159.             inventory[i] = null;
  160.         }
  161.     }
  162. }
Add Comment
Please, Sign In to add comment