/*******************************************************************************
* AbyssalCraft
* Copyright (c) 2012 - 2017 Shinoow.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Contributors:
* Shinoow - implementation
******************************************************************************/
package com.shinoow.abyssalcraft.common.blocks.tile;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.inventory.ItemStackHelper;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraft.util.NonNullList;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextComponentTranslation;
public class TileEntityMaterializer extends TileEntity implements ISidedInventory, ITickable {
private static final int[] slotsTop = new int[] {0};
private static final int[] slotsBottom = new int[] {2, 1};
private static final int[] slotsSides = new int[] {1};
/**
* The ItemStacks that hold the items currently being used in the materializer
*/
private NonNullList<ItemStack> materializerItemStacks = NonNullList.<ItemStack>withSize(30, ItemStack.EMPTY);
private String containerName;
/**
* Returns the number of slots in the inventory.
*/
@Override
public int getSizeInventory()
{
return materializerItemStacks.size();
}
/**
* Returns the stack in slot i
*/
@Override
public ItemStack getStackInSlot(int par1)
{
return materializerItemStacks.get(par1);
}
/**
* Removes from an inventory slot (first arg) up to a specified number (second arg) of items and returns them in a
* new stack.
*/
@Override
public ItemStack decrStackSize(int par1, int par2)
{
return ItemStackHelper.getAndSplit(materializerItemStacks, par1, par2);
}
/**
* When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem -
* like when you close a workbench GUI.
*/
@Override
public ItemStack removeStackFromSlot(int par1)
{
return ItemStackHelper.getAndRemove(materializerItemStacks, par1);
}
/**
* Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
*/
@Override
public void setInventorySlotContents(int par1, ItemStack par2ItemStack)
{
materializerItemStacks.set(par1, par2ItemStack);
if (par2ItemStack != null && par2ItemStack.getCount() > getInventoryStackLimit())
par2ItemStack.setCount(getInventoryStackLimit());
}
/**
* Returns the name of the inventory
*/
@Override
public String getName()
{
return hasCustomName() ? containerName : "container.abyssalcraft.materializer";
}
/**
* Returns if the inventory is named
*/
@Override
public boolean hasCustomName()
{
return containerName != null && containerName.length() > 0;
}
@Override
public ITextComponent getDisplayName()
{
return hasCustomName() ? new TextComponentString(getName()) : new TextComponentTranslation(getName(), new Object[0]);
}
public void func_145951_a(String par1)
{
containerName = par1;
}
@Override
public void readFromNBT(NBTTagCompound par1)
{
super.readFromNBT(par1);
par1.getTagList("Items", 10);
materializerItemStacks = NonNullList.<ItemStack>withSize(getSizeInventory(), ItemStack.EMPTY);
ItemStackHelper.loadAllItems(par1, materializerItemStacks);
if (par1.hasKey("CustomName", 8))
containerName = par1.getString("CustomName");
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound par1)
{
super.writeToNBT(par1);
ItemStackHelper.saveAllItems(par1, materializerItemStacks);
if (hasCustomName())
par1.setString("CustomName", containerName);
return par1;
}
/**
* Returns the maximum stack size for a inventory slot.
*/
@Override
public int getInventoryStackLimit()
{
return 64;
}
@Override
public void update()
{
test();
}
private void test()
{
// if (materializerItemStacks[0] != null)
// {
// List<ItemStack> list = MaterializerRecipes.instance().getMaterializationResult(materializerItemStacks[0]);
//
// if(!list.isEmpty())
// for(int i = 0; i < list.size(); i++)
// materializerItemStacks[i+2] = list.get(i);
// } else {
// for(int i = 2; i < materializerItemStacks.length; i++)
// materializerItemStacks[i] = null;
// }
}
/**
* Do not make give this method the name canInteractWith because it clashes with Container
*/
@Override
public boolean isUsableByPlayer(EntityPlayer par1EntityPlayer)
{
return world.getTileEntity(pos) != this ? false : par1EntityPlayer.getDistanceSq(pos.getX() + 0.5D, pos.getY() + 0.5D, pos.getZ() + 0.5D) <= 64.0D;
}
@Override
public void openInventory(EntityPlayer player) {}
@Override
public void closeInventory(EntityPlayer player) {}
/**
* Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot.
*/
@Override
public boolean isItemValidForSlot(int par1, ItemStack par2ItemStack)
{
return false;
}
/**
* Returns an array containing the indices of the slots that can be accessed by automation on the given side of this
* block.
*/
@Override
public int[] getSlotsForFace(EnumFacing face)
{
return face == EnumFacing.DOWN ? slotsBottom : face == EnumFacing.UP ? slotsTop : slotsSides;
}
/**
* Returns true if automation can insert the given item in the given slot from the given side. Args: Slot, item,
* side
*/
@Override
public boolean canInsertItem(int par1, ItemStack par2ItemStack, EnumFacing facing)
{
return false;
}
/**
* Returns true if automation can extract the given item in the given slot from the given side. Args: Slot, item,
* side
*/
@Override
public boolean canExtractItem(int par1, ItemStack par2ItemStack, EnumFacing facing)
{
return false;
}
@Override
public int getField(int id) {
// TODO Auto-generated method stub
return 0;
}
@Override
public void setField(int id, int value) {
// TODO Auto-generated method stub
}
@Override
public int getFieldCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void clear() {
// TODO Auto-generated method stub
}
@Override
public boolean isEmpty()
{
for (ItemStack itemstack : materializerItemStacks)
if (!itemstack.isEmpty())
return false;
return true;
}
}