/**
* MrCrayfish's Furniture Mod
* Copyright (C) 2016 MrCrayfish (http://www.mrcrayfish.com/)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.mrcrayfish.furniture.tileentity;
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.tileentity.TileEntity;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.IChatComponent;
public class TileEntityBin extends TileEntity implements IInventory
{
private ItemStack[] binContents = new ItemStack[12];
private String customName;
@Override
public int getSizeInventory()
{
return binContents.length;
}
@Override
public ItemStack getStackInSlot(int i)
{
return binContents[i];
}
@Override
public ItemStack decrStackSize(int par1, int par2)
{
if (this.binContents[par1] != null)
{
ItemStack var3;
if (this.binContents[par1].stackSize <= par2)
{
var3 = this.binContents[par1];
this.binContents[par1] = null;
this.markDirty();
return var3;
}
var3 = this.binContents[par1].splitStack(par2);
if (this.binContents[par1].stackSize == 0)
{
this.binContents[par1] = null;
}
this.markDirty();
return var3;
}
return null;
}
@Override
public ItemStack removeStackFromSlot(int par1)
{
if (this.binContents[par1] != null)
{
ItemStack var2 = this.binContents[par1];
this.binContents[par1] = null;
return var2;
}
return null;
}
@Override
public void setInventorySlotContents(int i, ItemStack itemstack)
{
this.binContents[i] = itemstack;
if (itemstack != null && itemstack.stackSize > this.getInventoryStackLimit())
{
itemstack.stackSize = this.getInventoryStackLimit();
}
this.markDirty();
}
@Override
public void readFromNBT(NBTTagCompound par1NBTTagCompound)
{
super.readFromNBT(par1NBTTagCompound);
NBTTagList var2 = (NBTTagList) par1NBTTagCompound.getTag("Items");
this.binContents = new ItemStack[this.getSizeInventory()];
for (int var3 = 0; var3 < var2.tagCount(); ++var3)
{
NBTTagCompound var4 = (NBTTagCompound) var2.getCompoundTagAt(var3);
int var5 = var4.getByte("Slot") & 255;
if (var5 >= 0 && var5 < this.binContents.length)
{
this.binContents[var5] = ItemStack.loadItemStackFromNBT(var4);
}
}
}
@Override
public void writeToNBT(NBTTagCompound par1NBTTagCompound)
{
super.writeToNBT(par1NBTTagCompound);
NBTTagList var2 = new NBTTagList();
for (int var3 = 0; var3 < this.binContents.length; ++var3)
{
if (this.binContents[var3] != null)
{
NBTTagCompound var4 = new NBTTagCompound();
var4.setByte("Slot", (byte) var3);
this.binContents[var3].writeToNBT(var4);
var2.appendTag(var4);
}
}
par1NBTTagCompound.setTag("Items", var2);
}
@Override
public int getInventoryStackLimit()
{
return 64;
}
@Override
public boolean isUseableByPlayer(EntityPlayer entityplayer)
{
return this.worldObj.getTileEntity(pos) != this ? false : entityplayer.getDistanceSq(pos.getX() + 0.5D, pos.getY() + 0.5D, pos.getZ() + 0.5D) <= 64.0D;
}
@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack)
{
return true;
}
@Override
public String getName()
{
return hasCustomName() ? customName : "Bin";
}
@Override
public boolean hasCustomName()
{
return customName != null;
}
@Override
public IChatComponent getDisplayName()
{
return new ChatComponentText(getName());
}
@Override
public void openInventory(EntityPlayer player)
{
worldObj.playSoundEffect(pos.getX(), pos.getY(), pos.getZ(), "cfm:binopen", 0.75F, 1.0F);
}
@Override
public void closeInventory(EntityPlayer player)
{
worldObj.playSoundEffect(pos.getX(), pos.getY(), pos.getZ(), "cfm:binclose", 0.75F, 1.0F);
}
@Override
public int getField(int id)
{
return 0;
}
@Override
public void setField(int id, int value)
{
}
@Override
public int getFieldCount()
{
return 0;
}
@Override
public void clear()
{
for (int i = 0; i < binContents.length; i++)
{
binContents[i] = null;
}
}
}