Jump to content

[1.7.10] [probably Solved]Hunger Bar Updates Each Tick But doesn't save


Thornack

Recommended Posts

Hi Everyone,

 

I am trying to implement a stamina bar that replenishes over time and is used up while using an item (I have the item part figured out where on right click the item adds exhaustion to the player using the player.addExhaustion(); method). I have decided to use Minecraft's Default Hunger Bar to achieve this stamina bar effect but have run into problems.

 

My event class updates the bar each tick increasing the food level (you can see the gui update more meat stick things are added each tick until it is full) but this isnt saved for some reason where the next tick the hunger level goes back to the level it was before. This is made apparent when hunger reaches 0 and the bar updates until it shows it is full and then becomes 0 the next tick.

 

Question 1) does anyone know what my mistake is and why the food level doesnt seem to be saved? The class is below

 

My event class

 

 

import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent.PlayerTickEvent;
import cpw.mods.fml.common.gameevent.TickEvent.WorldTickEvent;

public class EventPlayerHungerUpdate {

private static int tick = 0;

@SubscribeEvent
public void incrimentHungerBar(WorldTickEvent event) {
	tick++;
	int divisor = 20;

	if ((tick % divisor == 0) && !event.world.isRemote) {
		EntityPlayer player = Minecraft.getMinecraft().thePlayer;
		// System.out.println("Adding Food Each Tick");
		if ((!(player == null)) && (player.getFoodStats().getFoodLevel() < 20)) {
			player.getFoodStats().setFoodLevel(player.getFoodStats().getFoodLevel() + 1);
		}
	}
}

}

 

 

Question 2) Is there a way to change the maximum hunger level that is allowed (currently this is 20) but lets say I want to increase it to 21 or 22 or 23 depending on the players exp for example.and If so How would you do it?

 

Question 3) Is there a way to change the default Hunger GUI? and If so How?

 

Question 4) If it isnt possible to change the maximum amount of hunger level or the GUI texture for the hunger level does anyone know how to create a custom hunger bar and could you point me in the direction to create one.

 

Any help/input is appreciated

 

Link to comment
Share on other sites

When I try

@SubscribeEvent
public void incrimentHungerBar(PlayerTickEvent event) {
	tick++;
	int divisor = 20;

	if ((tick % divisor == 0) && !event.player.worldObj.isRemote) {
		EntityPlayer player = Minecraft.getMinecraft().thePlayer;
		// System.out.println("Adding Food Each Tick");
		if ((!(player == null)) && (player.getFoodStats().getFoodLevel() < 20)) {
			player.getFoodStats().setFoodLevel(player.getFoodStats().getFoodLevel() + 1);
		}
	}
}

 

The same thing happens

 

and if i add @SideOnly(Side.SERVER) and remove the && !event.player.worldObj.isRemote bit the method doesnt seem to be called at all

Link to comment
Share on other sites

y u use client player?

EntityPlayer player = Minecraft.getMinecraft().thePlayer;

 

You've used the player from the event already, so why not use it there?

 

Also player shouldn't be null, so you can remove the null check.

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Link to comment
Share on other sites

@SubscribeEvent
public void incrimentHungerBar(PlayerTickEvent event) {
	tick++;
	int divisor = 20;

	if ((tick % divisor == 0)) {
		// System.out.println("Adding Food Each Tick");
		if ((!(event.player == null)) && (event.player.getFoodStats().getFoodLevel() < 20)) {
			event.player.getFoodStats().setFoodLevel(event.player.getFoodStats().getFoodLevel() + 1);
		}
	}
}

}

 

Seems to work but there is a weird issue, when I switch from creative mode to survival mode in game the hunger bar saves. when I log out while in survival mode and log back in the saving breaks. Does anyone know why.

 

[EDIT] Also sometimes it saves properly and other times it does not

Link to comment
Share on other sites

This seems to work buut the ticking doesnt happen often, I want it to update the hunger bar once/sec and it doesnt since the server ticks dont seem to almost ever occur. Is this frame rate dependant the way it is. And how could I get the correct ticks

public class EventPlayerHungerUpdate {

private static int tick = 0;

@SubscribeEvent
public void incrimentHungerBar(PlayerTickEvent event) {
	tick++;
	int divisor = 20;
	World world = event.player.worldObj;
	if ((tick % divisor == 0 && event.phase == event.phase.END && !world.isRemote)) {
		System.out.println("Adding Food Each Tick");
		if ((!(event.player == null)) && (event.player.getFoodStats().getFoodLevel() < 20)) {
			event.player.getFoodStats().setFoodLevel(event.player.getFoodStats().getFoodLevel() + 1);
		}
	}
}

}

Link to comment
Share on other sites

It seems to work perfectly now


public class EventPlayerHungerUpdate {

private  int tick ;

@SubscribeEvent
public void incrimentHungerBar(PlayerTickEvent event) {
	tick = event.player.ticksExisted ++;
	int divisor = 20;
	World world = event.player.worldObj;
	if ((tick % divisor == 0 && event.phase == event.phase.END && !world.isRemote)) {
		System.out.println("Adding Food Each Tick");
		if ((!(event.player == null)) && (event.player.getFoodStats().getFoodLevel() < 20)) {
			event.player.getFoodStats().setFoodLevel(event.player.getFoodStats().getFoodLevel() + 1);
		}
	}
}

}

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.

Announcements



×
×
  • Create New...

Important Information

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