Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.happykiller.weightlimit.player.inventory;
- import com.happykiller.weightlimit.items.ItemBackpack;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.inventory.IInventory;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.nbt.NBTTagList;
- import net.minecraft.util.ChatComponentText;
- import net.minecraft.util.IChatComponent;
- public class InventoryWeightLimit implements IInventory {
- private final String name = "Inventory";
- private final String tagName = "WLInvTag";
- public static final int INV_SIZE = 1;
- private final int SLOT_BACKPACK = 0;
- ItemStack[] inventory = new ItemStack[INV_SIZE];
- public InventoryWeightLimit() {}
- public String getName() {
- return name;
- }
- public boolean hasCustomName() {
- return name.length() > 0;
- }
- public IChatComponent getDisplayName() {
- return new ChatComponentText(name);
- }
- public int getSizeInventory() {
- return inventory.length;
- }
- public ItemStack getStackInSlot(int slot) {
- return inventory[slot];
- }
- 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;
- }
- public ItemStack getStackInSlotOnClosing(int slot) {
- ItemStack stack = getStackInSlot(slot);
- if(stack != null) {
- setInventorySlotContents(slot, null);
- }
- return stack;
- }
- public void setInventorySlotContents(int slot, ItemStack stack) {
- this.inventory[slot] = stack;
- if(stack != null && stack.stackSize > this.getInventoryStackLimit()) {
- stack.stackSize = this.getInventoryStackLimit();
- }
- this.markDirty();
- }
- public int getInventoryStackLimit() {
- return 1;
- }
- 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);
- }
- }
- public boolean isUseableByPlayer(EntityPlayer player) {
- return true;
- }
- public void openInventory(EntityPlayer player) {}
- public void closeInventory(EntityPlayer player) {}
- public boolean isItemValidForSlot(int slot, ItemStack stack) {
- if(slot == SLOT_BACKPACK && stack.getItem() instanceof ItemBackpack)
- return true;
- else
- return false;
- }
- public void writeToNBT(NBTTagCompound tag) {
- NBTTagList tagList = new NBTTagList();
- for(int i = 0; i < this.getSizeInventory(); i++) {
- if(this.getStackInSlot(i) != null) {
- NBTTagCompound tag1 = new NBTTagCompound();
- tag1.setByte("Slot", (byte)i);
- this.getStackInSlot(i).writeToNBT(tag1);
- tagList.appendTag(tag1);
- }
- }
- tag.setTag(tagName, tagList);
- }
- public void readFromNBT(NBTTagCompound tag) {
- NBTTagList items = tag.getTagList(tagName, tag.getId());
- for(int i = 0; i < items.tagCount(); i++) {
- NBTTagCompound item = items.getCompoundTagAt(i);
- byte slot = item.getByte("Slot");
- if(slot >= 0 && slot < this.getSizeInventory()) {
- this.setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(item));
- }
- }
- }
- public int getField(int id) {
- return 0;
- }
- public void setField(int id, int value) {
- }
- public int getFieldCount() {
- return 0;
- }
- public void clear() {
- for(int i = 0; i < inventory.length; i++) {
- inventory[i] = null;
- }
- }
- }
Add Comment
Please, Sign In to add comment