Hi,
At first, I speak french, so I'm sorry for my bad english...
I'm creating a small mod and I want to create a double output furnace.
I tried the code below, it works fine, but if I try to take an item out of my custom furnace while another one is "burning", my game crashs and says me
java.lang.NullPointerException: Ticking block entity
at lonia.nextgen.tileentities.machinecellextractor.TileEntityCellExtractor.hasValidOutputs(TileEntityCellExtractor.java:278)
at lonia.nextgen.tileentities.machinecellextractor.TileEntityCellExtractor.canSmelt(TileEntityCellExtractor.java:204)
at lonia.nextgen.tileentities.machinecellextractor.TileEntityCellExtractor.updateEntity(TileEntityCellExtractor.java:211)
at net.minecraft.world.World.updateEntities(World.java:2160)
at net.minecraft.world.WorldServer.updateEntities(WorldServer.java:515)
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:703)
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:614)
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:485)
at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:752)
TileEntity Class
package lonia.nextgen.tileentities.machinecellextractor;
import lonia.nextgen.common.ModItems;
import lonia.nextgen.tileentities.machinecube.MachineCubeRecipes;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
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;
public class TileEntityCellExtractor extends TileEntity implements IInventory
{
private ItemStack[] contents = new ItemStack[3];
private int workingTime = 0;
private int workingTimeNeeded = 200;
private ItemStack skinsheep = new ItemStack(ModItems.itemSkinPiece,1,1);
private ItemStack cellsheep = new ItemStack(ModItems.itemCell, 1, 1);
private ItemStack emptyskin = new ItemStack(ModItems.itemSkinPiece, 1, 0);
ItemStack itemstack1 = null;
ItemStack itemstack2 = null;
public void writeToNBT(NBTTagCompound compound)
{
super.writeToNBT(compound);
NBTTagList nbttaglist = new NBTTagList();
for (int i = 0; i < this.contents.length; i++)
{
if (this.contents[i] != null)
{
NBTTagCompound nbttagcompound1 = new NBTTagCompound();
nbttagcompound1.setByte("Slot", (byte)i);
this.contents[i].writeToNBT(nbttagcompound1);
nbttaglist.appendTag(nbttagcompound1);
}
}
compound.setTag("Items", nbttaglist);
compound.setShort("workingTime", (short)this.workingTime);
compound.setShort("workingTimeNeeded", (short)this.workingTimeNeeded);
}
public void readFromNBT(NBTTagCompound compound)
{
super.readFromNBT(compound);
NBTTagList nbttaglist = compound.getTagList("Items", 10);
this.contents = new ItemStack[this.getSizeInventory()];
for (int i = 0; i < nbttaglist.tagCount(); i++)
{
NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
int j = nbttagcompound1.getByte("Slot") & 255;
if (j >= 0 && j < this.contents.length)
{
this.contents[j] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
}
}
this.workingTime = compound.getShort("workingTime");
this.workingTimeNeeded = compound.getShort("workingTimeNeeded");
}
@Override
public int getSizeInventory()
{
return this.contents.length;
}
@Override
public ItemStack getStackInSlot(int slotIndex)
{
return this.contents[slotIndex];
}
@Override
public ItemStack decrStackSize(int slotIndex, int amount) {
if (this.contents[slotIndex] != null)
{
ItemStack itemstack;
if (this.contents[slotIndex].stackSize <= amount)
{
itemstack = this.contents[slotIndex];
this.contents[slotIndex] = null;
this.markDirty();
return itemstack;
}
else
{
itemstack = this.contents[slotIndex].splitStack(amount);
if (this.contents[slotIndex].stackSize == 0)
{
this.contents[slotIndex] = null;
}
this.markDirty();
return itemstack;
}
}
else
{
return null;
}
}
@Override
public ItemStack getStackInSlotOnClosing(int slotIndex) {
if (this.contents[slotIndex] != null)
{
ItemStack itemstack = this.contents[slotIndex];
this.contents[slotIndex] = null;
return itemstack;
}
else
{
return null;
}
}
@Override
public void setInventorySlotContents(int slotIndex, ItemStack stack) {
this.contents[slotIndex] = stack;
if (stack != null && stack.stackSize > this.getInventoryStackLimit())
{
stack.stackSize = this.getInventoryStackLimit();
}
this.markDirty();
}
@Override
public String getInventoryName() {
return "tile.machineCellExtractor";
}
@Override
public boolean hasCustomInventoryName() {
return false;
}
@Override
public int getInventoryStackLimit() {
return 64;
}
@Override
public boolean isUseableByPlayer(EntityPlayer player) {
return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : player.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;
}
@Override
public void openInventory() {
}
@Override
public void closeInventory() {
}
@Override
public boolean isItemValidForSlot(int slot, ItemStack stack) {
return slot == 3 ? false : true;
}
public boolean isBurning()
{
return this.workingTime > 0;
}
public boolean canSmelt()
{
if (this.contents[0] == null)
{
return false;
}
else
{
if (this.contents[0].isItemEqual(skinsheep)) {
itemstack1 = cellsheep;
itemstack2 = emptyskin;
}
if (itemstack1 == null) return false;
if (itemstack2 == null) return false;
return this.hasValidOutputs();
}
}
public void updateEntity()
{
if(this.isBurning() && this.canSmelt())
{
this.workingTime++;
}
if(this.canSmelt() && !this.isBurning())
{
this.workingTime = 1;
}
if (this.canSmelt() && this.workingTime == this.workingTimeNeeded)
{
this.smeltItem();
this.workingTime = 0;
}
if(!this.canSmelt())
{
this.workingTime = 0;
}
}
public void smeltItem()
{
if (this.canSmelt())
{
if (this.contents[1] == null)
{
this.contents[1] = itemstack1.copy();
}
else if (this.contents[1].getItem() == itemstack1.getItem())
{
this.contents[1].stackSize += itemstack1.stackSize;
}
if (this.contents[2] == null)
{
this.contents[2] = itemstack2.copy();
}
else if (this.contents[2].getItem() == itemstack2.getItem())
{
this.contents[2].stackSize += itemstack2.stackSize;
}
this.contents[0].stackSize--;
if (this.contents[0].stackSize <= 0)
{
this.contents[0] = null;
}
}
}
public boolean hasValidOutputs()
{
if (itemstack1 == null || itemstack2 == null) return false;
if (this.contents[1] == null && this.contents[2] == null) return true;
if (!this.contents[1].isItemEqual(itemstack1)) return false;
if (!this.contents[2].isItemEqual(itemstack2)) return false;
int result1 = contents[1].stackSize + itemstack1.stackSize;
int result2 = contents[2].stackSize + itemstack2.stackSize;
return (result2 <= getInventoryStackLimit() && result2 <= this.contents[2].getMaxStackSize()) || (result1 <= getInventoryStackLimit() && result1 <= this.contents[1].getMaxStackSize());
}
}
Thank you for your help.