Jump to content

[Solved] Can't access variables inside of java.util.Timer


Althonos

Recommended Posts

So in an item class, I'm overriding the onItemUse like so

 

@Override
public boolean onItemUse(ItemStack itemStack,
							EntityPlayer entityPlayer,
							World world, int posX,
							int posY, int posZ, int p_77648_7_,
							float p_77648_8_, float p_77648_9_, float p_77648_10_){

 

 

The issue is, I have a java.util.timer declaration inside of it, and it refuses to allow me to edit the ItemStack itemStack variable since it is not final. However, if it is final, it doesn't let me edit it anyway.

 

The java.util.Timer

 

timer.schedule(new TimerTask() {

				@Override
				public void run() {

					//The stop decrement logic
					if (itemStack.stackTagCompound != null){

						if (itemStack.stackTagCompound.getInteger("secondsLeft") > 0){

							int timeLeft = itemStack.stackTagCompound.getInteger("secondsLeft");

							itemStack.stackTagCompound.setInteger("secondsLeft", timeLeft-1);
						}

						//The stop error logic
						if (!itemStack.stackTagCompound.getBoolean("isErrorReady")){

							itemStack.stackTagCompound.setBoolean("isErrorReady", true);

							System.out.println("Reset ERROR READY");
						}
					}

				}


			}, 1000);

 

 

This is the error I get in the little lightbulb on the left:

Cannot refer to the non-final local variable itemStack defined in an enclosing scope.

 

I found a java forum that had the same issue, however their solution was of no use to me.

http://stackoverflow.com/questions/21485590/the-final-local-variable-cannot-be-assigned-since-it-is-defined-in-an-enclosing

 

There were several solutions to this problem at that page, however none that seemed to allow me to edit the itemStack variable as itself. I have been working on this for a few hours now, decided to ask you guys to see if there's a simple solution I'm missing. Thanks for reading.

[/code]

Link to comment
Share on other sites

Trying to make it so, if the conditions are set up, the itemStack.stackTagCompound has some values changed every second. If Minecraft does things in ticks, how would I do it using ticks? I thought that Minecraft could essentially use any ordinary Java method?

Link to comment
Share on other sites

It works perfectly, many thanks my friend.

 

Solved timer:

 

@Override
public void onUpdate(ItemStack itemStack, World par2World, Entity par3Entity, int par4, boolean par5) {

	if (itemStack.stackTagCompound != null){
		if (!itemStack.stackTagCompound.getBoolean("isReady")
				|| !itemStack.stackTagCompound.getBoolean("isErrorReady")
				|| itemStack.stackTagCompound.getInteger("secondsLeft") > 0){


			//Incrementing ticks passed
			int ticksPassed = itemStack.stackTagCompound.getInteger("ticksPassed");
			itemStack.stackTagCompound.setInteger("ticksPassed", ticksPassed + 1);

			//Checking if a second passed
			if (itemStack.stackTagCompound.getInteger("ticksPassed") >= 20){
				itemStack.stackTagCompound.setInteger("ticksPassed", 0);

				//The time left message logic
				if (itemStack.stackTagCompound.getInteger("secondsLeft") > 0){

					int timeLeft = itemStack.stackTagCompound.getInteger("secondsLeft");

					itemStack.stackTagCompound.setInteger("secondsLeft", timeLeft-1);
				}

				//The error message logic
				if (!itemStack.stackTagCompound.getBoolean("isErrorReady")){

					itemStack.stackTagCompound.setBoolean("isErrorReady", true);
				}

				//Incrementing seconds passed
				int currentTicks = itemStack.stackTagCompound.getInteger("currentTicks");
				itemStack.stackTagCompound.setInteger("currentTicks", currentTicks+1);

				//The is ready logic
				if (!itemStack.stackTagCompound.getBoolean("isReady")){
					if (itemStack.stackTagCompound.getInteger("currentTicks") >= 5){

						itemStack.stackTagCompound.setInteger("currentTicks", 0);
						itemStack.stackTagCompound.setBoolean("isReady", true);
					}
				}
			}
		}
	}
}

 

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.