Jump to content

Recommended Posts

Posted

I'm trying to make my machine fill buckets if you put them in a certain slot, so I want to check that there is a bucket in the slot, however my if statement crashes with a null pointer exception when I load into the game with my current method.

 

 

It crashes on this line on what I assume is the first check.

if(slotBucketOut.getItem() == Items.bucket && (this.distilledWater - 1000) >= 0)

 

Here is all of my code in that class:

 

 

package roboguy99.foodTech.common.tile;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import roboguy99.foodTech.common.tile.prefab.Tile;

public class TileDistiller extends Tile implements IInventory
{
protected ItemStack[] slot = new ItemStack[4];
private String customName;
private static final int MAX_WATER = 10000;
private static final int PROCESS_TIME = 200;
public int water = 0;
public int distilledWater = 0;
public int processTimeRemaining;
public int timeSpentProcessing;
private int furnaceBurnTime = 0;
private int temperature;

@Override
public void updateEntity()
{
	boolean shouldMarkDirty = false;

	ItemStack slotBucketIn = this.getStackInSlot(0);
	ItemStack slotFuel = this.getStackInSlot(1);
	ItemStack slotSalt = this.getStackInSlot(2);
	ItemStack slotBucketOut = this.getStackInSlot(3);

	if(slotFuel != null && this.getBurnTime(slotFuel.getItem()) > 0 && !this.isBurning())
	{
		slotFuel.stackSize--;
		this.furnaceBurnTime = this.getBurnTime(slotFuel.getItem());
		shouldMarkDirty = true;

		if(slotFuel.stackSize == 0)
		{
			slotFuel = null;
			this.setInventorySlotContents(1, null);
			shouldMarkDirty = true;
		}
	}

	if(slotBucketIn != null && slotBucketIn.getItem() == Items.water_bucket && (this.water + 1000) <= MAX_WATER)
	{
		slotBucketIn = null;
		this.setInventorySlotContents(0, null);

		this.water += 1000;

		this.setInventorySlotContents(0, new ItemStack(Items.bucket));
	}

	if(this.temperature > 100 && this.isBurning() && this.water > 0 && this.distilledWater < MAX_WATER)
	{
		this.water--;
		this.distilledWater++;

		if(this.processTimeRemaining == 0)
		{
			if (slotSalt == null) this.setInventorySlotContents(3, new ItemStack(roboguy99.foodTech.common.item.Items.itemSalt));
			else slotSalt.stackSize++;

			this.processTimeRemaining = TileDistiller.PROCESS_TIME;
		}
	}

	if(slotBucketOut.getItem() == Items.bucket && (this.distilledWater - 1000) >= 0)
	{
		slotBucketOut = null;
		this.setInventorySlotContents(3, null);

		this.distilledWater -= 1000;

		this.setInventorySlotContents(3, new ItemStack(roboguy99.foodTech.common.item.Items.itemBucketDistilledWater));
	}

	this.temperature--;
	this.processTimeRemaining--;
	if(this.furnaceBurnTime > 0) this.furnaceBurnTime--;
	if(this.temperature < 300) this.temperature++;
	if(shouldMarkDirty) this.markDirty();
}

private int getBurnTime(Item item)
{
	if(item == Items.coal) return 1600;
	if(item == Item.getItemFromBlock(Blocks.coal_block)) return 16000;
	return 0;
}

private boolean isBurning()
{
	return this.furnaceBurnTime > 0;
}

@Override
public ItemStack decrStackSize(int i, int j) 
{
		if(this.slot[i] != null)
		{
			ItemStack itemStack;

			if(this.slot[i].stackSize <= j)
			{
				itemStack = this.slot[i];
				this.slot[i] = null;
				return itemStack;
			}
			else
			{
				itemStack = this.slot[i].splitStack(j);

				if(this.slot[i].stackSize == 0)
				{
					this.slot[i] = null;
					this.setInventorySlotContents(i, null);
				}

				return itemStack;
			}
		}

	return null;
}

public ItemStack getStackInSlot(int i) 
{
	return this.slot[i];
}

public ItemStack getStackInSlotOnClosing(int i) 
{
	if(this.slot[i] != null)
	{
		ItemStack itemStack = this.slot[i];
		this.slot[i] = null;
		return itemStack;
	}

	return null;
}

public boolean isItemValidForSlot(int slot, ItemStack itemStack)
{
	return slot == 2 ? false : true;
}

public boolean isUseableByPlayer(EntityPlayer entityPlayer) 
{
	return true;
}

public void setInventorySlotContents(int i, ItemStack itemStack) 
{
	this.slot[i] = itemStack;

	if(itemStack != null && itemStack.stackSize > this.getInventoryStackLimit())
	{
		itemStack.stackSize = this.getInventoryStackLimit();
	}
}

public boolean canExtractItem(int i, ItemStack var2, int j) 
{
	return true;
}

public boolean canInsertItem(int slot, ItemStack itemStack, int side) 
{
	return this.isItemValidForSlot(slot, itemStack);
}

public int[] getAccessibleSlotsFromSide(int i) 
{
	return null;
}

public String getInventoryName() 
{
	return this.hasCustomInventoryName() ? this.customName : "container.threeSlotMachine";
}

public boolean hasCustomInventoryName() 
{
	return this.customName != null && this.customName.length() > 0;
}

public int getInventoryStackLimit() 
{
	return 64;
}

public int getSizeInventory() 
{
	return this.slot.length;
}

public void openInventory() {}
public void closeInventory() {}
}

 

 

 

If anyone can help I'd greatly appreciate it, thanks.

I have no idea what I'm doing.

Posted

The ItemStack will be

null

if the slot is empty. You need to check for that, as you can't call methods on references that are

null

.

 

Ok, would that just be

if(slotBucketOut != null)

I have no idea what I'm doing.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.