Posted February 18, 201510 yr 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
February 18, 201510 yr Author 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
February 18, 201510 yr 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.
February 18, 201510 yr Author @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
February 18, 201510 yr Author Im stumped by this. Upon further testing it seems that the above only works when you switch from creative to survival mode. If you log off and log back on then it doesnt save again.
February 18, 201510 yr Author 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); } } } }
February 18, 201510 yr Author 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); } } } }
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.