Jump to content

Merging ItemStacks that are stored in an ArrayList


memcallen

Recommended Posts

this code isn't working (as in it crashes with NullPointerException):

 

@Override
public void updateTick(World world,int x,int y,int z,Random rand){
	CrucibleTileEntity te = (CrucibleTileEntity)world.getTileEntity(x, y, z);

	List<EntityItem> items = world.getEntitiesWithinAABB(EntityItem.class, AxisAlignedBB.getBoundingBox(x+0D, y+1D, z+0D, x+1D, y+2D, z+1D));

	ItemStack item=null;

	if(items.size()>0){


		item = items.get(items.size()-1).getEntityItem();
		if(CrucibleRecipeHandler.alloweds.contains(item.getItem())){

			int Counter=0;

			for(ItemStack i : inventory){
				Counter++;
				if(i.isItemEqual(item))
					if(i.isItemEqual(inventory.get(Counter-1))){
						inventory.add(new ItemStack(i.getItem(),i.stackSize+inventory.get(Counter-1).stackSize));
						inventory.remove(Counter-1);
					break;
					}
			}
			Counter=0;


			inventory.add(item);
			items.get(items.size()-1).setDead();
			item = null;
		}

 

inventory is an ArrayList stored outside the function. Yes I  realize I have to use a tileentity etc, this is just for testing purposes.

The proud(ish) developer of Ancients

Link to comment
Share on other sites

I changed it to:

item = items.get(items.size()-1).getEntityItem();
		if(CrucibleRecipeHandler.alloweds.contains(item.getItem())){

			int Counter=0;

			for(ItemStack i : inventory){
				Counter++;
				if(i.isItemEqual(item))
					if(inventory.get(Counter)!=null)
					if(i.isItemEqual(inventory.get(Counter-1))){
						inventory.add(new ItemStack(i.getItem(),i.stackSize+inventory.get(Counter-1).stackSize));
					break;
					}
			}
			if(inventory.get(Counter-1)!=null)
			inventory.remove(Counter-1);
			Counter=0;


			inventory.add(item);
			items.get(items.size()-1).setDead();
			item = null;
		}

it still doesn't work, keep getting crashes on my null check... "if(inventory.get(Counter-1)!=null)"

The proud(ish) developer of Ancients

Link to comment
Share on other sites

Is your inventory ArrayList ever empty for any reason?

 

If so, then it will crash on that line since you're checking the index position of Counter-1, which will be -1 if the counter is not incremented. Now, the counter is only incremented within your augmented for-loop, which won't execute if there are no elements in the inventory ArrayList.

Link to comment
Share on other sites

I'd say you could do a if(inventory != null) check before that get() call.

 

I also see that you do not have brackets for the if-statements, which only works for one-line statements after the if()

--Remember to "Thank you" posts that actually helped you, and if a person seems nice, why not give them an applaud while you're at it--

Link to comment
Share on other sites

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.



×
×
  • Create New...

Important Information

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