Jump to content

[1.7.10] Trouble with Auto Repair


HappyKiller1O1

Recommended Posts

Hey so, I am trying to set a timer for my auto repair and, I really wish it would work. xD I am using ticks and basically, the int constantly is set back to zero. Here is my code:

public void onUpdate(ItemStack stack, World world, Entity entity, int i, boolean flag) { //3600 = 3 minutes
	fixNBT(stack);

	if(stack.stackTagCompound.getBoolean("AutoRepair")) {
		if(stack.isItemDamaged()) {
			int autoRepairDelay = 0;

			if(autoRepairDelay < 3600)
				autoRepairDelay++;

			LogHelper.logInfo("Delay: " + autoRepairDelay);

			if(autoRepairDelay == 3600) {
				stack.damageItem(-1, (EntityPlayer)entity);

				autoRepairDelay = 0;
			}
		}
	}
}

 

The autoRepairDelay NEEDS to be initialized before I can edit it but, if I set it to zero it will constantly be reset to zero and my number gets stuck going back and forth from one to zero. If I make the int declared in my class it works but, when you have two of the item in your inventory to repair it screws up. Any way to do this?

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

My problem is not setting and reading from NBT, even if it is set to NBT it will still be constantly set to that number.

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

Plus, you don't read it, you just initialise it with 0... Do you think mc will magically read it from nbt?

You do know I have not added any number to NBT yet, right?

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

Plus, you don't read it, you just initialise it with 0... Do you think mc will magically read it from nbt?

You do know I have not added any number to NBT yet, right?

You do know that if you don't, your code is going to do fucknothing, right?

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Yes, I get that. My problem still is I need to make a timer. Even if I set my Integer to a Integer set in the stack's NBT it still gets reset every time. Am I suppose to just use that number and constantly save and load the nbt to raise the number?

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

How about you show us code where you save that variable to the NBT and read it back out again.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Honestly, I know I gotta be doing something wrong. xD

 

public void onUpdate(ItemStack stack, World world, Entity entity, int i, boolean flag) { //3600 = 3 minutes
	fixNBT(stack);

	if(stack.stackTagCompound.getBoolean("AutoRepair")) {
		if(stack.isItemDamaged()) {
			int autoRepairDelay = stack.stackTagCompound.getInteger("RepairDelay");

			if(autoRepairDelay < 3600)
				autoRepairDelay++;

			LogHelper.logInfo("Delay: " + autoRepairDelay); //TODO WORK ON AUTO REPAIR

			if(autoRepairDelay == 3600) {
				stack.damageItem(-1, (EntityPlayer)entity);

				autoRepairDelay = stack.stackTagCompound.getInteger("RepairDelay");
			}
		}
	}
}

public static void fixNBT(ItemStack stack){
	if(stack.stackTagCompound == null) {
		LogHelper.logWarn("[CrewHammer] " + "The ItemStack NBTTagCompound was null. Attempting fix...");

		stack.stackTagCompound = new NBTTagCompound();
	}

	/*if(!stack.stackTagCompound.hasKey("HammerTag")) {
		stack.stackTagCompound.setTag("HammerTag", new NBTTagCompound());
		LogHelper.logWarn("[CrewHammer] " + "The ItemStack did not contain the key 'HammerTag'. Attempting fix...");
	}*/

	if(!stack.stackTagCompound.hasKey("RepairDelay")) {
		LogHelper.logWarn("[CrewHammer] " + "The ItemStack did not contain the key 'RepairDelay'. Attempting fix...");

		stack.stackTagCompound.setInteger("RepairDelay", 0);
	}

	if(!stack.stackTagCompound.hasKey("MiningSize")) {
		LogHelper.logWarn("[CrewHammer] " + "The ItemStack did not contain the key 'MiningSize'. Attempting fix...");

		stack.stackTagCompound.setString("MiningSize", "3x3");
	}

	if(!stack.stackTagCompound.hasKey("AutoSmelt")) {
		LogHelper.logWarn("[CrewHammer] " + "The ItemStack did not contain the key 'AutoSmelt'. Attempting fix...");

		stack.stackTagCompound.setBoolean("AutoSmelt", false);
	}

	if(!stack.stackTagCompound.hasKey("AutoRepair")) {
		LogHelper.logWarn("[CrewHammer] " + "The ItemStack did not contain the key 'AutoRepair'. Attempting fix...");

		stack.stackTagCompound.setBoolean("AutoRepair", false);
	}

	if(!stack.stackTagCompound.hasKey("Has3x3")) {
		LogHelper.logWarn("[CrewHammer] " + "The ItemStack did not contain the key 'Has3x3'. Attempting fix...");

		stack.stackTagCompound.setBoolean("Has3x3", false);
	}

	if(!stack.stackTagCompound.hasKey("Has5x5")) {
		LogHelper.logWarn("[CrewHammer] " + "The ItemStack did not contain the key 'Has5x5'. Attempting fix...");

		stack.stackTagCompound.setBoolean("Has5x5", false);
	}

	if(!stack.stackTagCompound.hasKey("Has7x7")) {
		LogHelper.logWarn("[CrewHammer] " + "The ItemStack did not contain the key 'Has7x7'. Attempting fix...");

		stack.stackTagCompound.setBoolean("Has7x7", false);
	}
}

 

It's set in fixNBT and read in onUpdate. I was thinking about setting a variable to my class and setting it equal to the saved Integer? Not sure if that would work though.

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

Honestly, I know I gotta be doing something wrong. xD

 

public void onUpdate(ItemStack stack, World world, Entity entity, int i, boolean flag) { //3600 = 3 minutes
	fixNBT(stack);

	if(stack.stackTagCompound.getBoolean("AutoRepair")) {
		if(stack.isItemDamaged()) {
			int autoRepairDelay = stack.stackTagCompound.getInteger("RepairDelay");

			if(autoRepairDelay < 3600)
				autoRepairDelay++;
[/quote]
You should save the local variable(autoRepairDelay) into the stackTagCompound, otherwise it would just get lost.
Use NBTTagCompount#setInteger for that.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Link to comment
Share on other sites

Hm, see I understand that but, in changing it to go up and down like a timer; wouldn't I have to constantly save and load from NBT?

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

Yes, you would. In every onUpdate() you will first get NBT integer, if found you increment it and save back to NBT, if not found, you make new one, increment and save it back. That's it. You get -> increment -> save.

 

NBT is nothing but just a big-ass map.

Note that even if that is not very bad design, there are still better ones (more efficient), but require a lot more coding.

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

If I understand you'r issue correctly then all you need to do is declare an integer outside of the onUpdate function

Something like:

private in ticks = 0;
public void onUpdate(ItemStack stack, World world, Entity entity, int i, boolean flag)
{
    if(ticks == 400)//400 is the amount of ticks to wait. 20 ticks = 1 second
    {
        stack.setItemDamage(stack.getItemDamage() + 1);
        ticks = 0;
    }
    else
    {
        ticks++;
    }


}

should work just fine!

Hope this helped  ;)

Link to comment
Share on other sites

Sbeagin - you might know Java, but in this case you wrote something that is not even remotely correct for Minecraft/Forge coding.

Never, ever, forever you should declare ANY fields inside Item that are not shared between all Items.

 

Item is merely an instance of something that represents item, ItemStack is the actual "thing" that you hold, and only ItemStack can hold data (NBT).

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

If I understand you'r issue correctly then all you need to do is declare an integer outside of the onUpdate function

Something like:

private in ticks = 0;
public void onUpdate(ItemStack stack, World world, Entity entity, int i, boolean flag)
{
    if(ticks == 400)//400 is the amount of ticks to wait. 20 ticks = 1 second
    {
        stack.setItemDamage(stack.getItemDamage() + 1);
        ticks = 0;
    }
    else
    {
        ticks++;
    }


}

should work just fine!

Hope this helped  ;)

 

Thanks for attempting to help but, like Ernio said: declaring a field in the item class would be for every one of those Items. That would mean that, for one: the tick count would be shared between all of that Item. And secondly, you could not repair two of the item in your inventory. :P

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

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.