Althonos Posted October 5, 2014 Share Posted October 5, 2014 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] Quote Link to comment Share on other sites More sharing options...
Althonos Posted October 5, 2014 Author Share Posted October 5, 2014 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? Quote Link to comment Share on other sites More sharing options...
Althonos Posted October 5, 2014 Author Share Posted October 5, 2014 So it says 20 ticks is equivalent to one second, unless the computer is slower, then ticks happen at a slower rate. Is there a way to guarantee one second will pass? When twenty ticks isn't guaranteed to be one second. Quote Link to comment Share on other sites More sharing options...
Althonos Posted October 5, 2014 Author Share Posted October 5, 2014 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); } } } } } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.