Jump to content

Set player health and hunger


CountNoobula

Recommended Posts

Hi all!

I am trying to make a stone that regenerates the players health and hunger. So far I have it working, but it doesn't save the data, so on the next update (lose health or hunger) it resets :/

 

I'm using a tick handler to check for the item, then adjusting the player hunger/health. I tried to write the NBT data, that didn't work either.

 

Here is my code for how I am doing it

package com.nightfallstudios.alchemytrader.handlers;

import java.util.EnumSet;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.Item;
import cpw.mods.fml.common.ITickHandler;
import cpw.mods.fml.common.TickType;

public class TickHandler implements ITickHandler {

private int tickCount = 0;

@Override
public void tickStart(EnumSet<TickType> type, Object... tickData) {
}

@Override
public void tickEnd(EnumSet<TickType> type, Object... tickData) {
	if (type.equals(EnumSet.of(TickType.SERVER))) {
		onTickInGame();
	}
}

private void onTickInGame() {
	if (Minecraft.getMinecraft().theWorld != null) {
		for (Object p : Minecraft.getMinecraft().theWorld.playerEntities) {
			EntityPlayer player = (EntityPlayer) p;

			InventoryPlayer inv = player.inventory;
			boolean hungerStone = false, healthStone = false, completeStone = false;
			boolean fireAmulet = false, waterAmulet = false;

			for (int i = 0; i < 9; i++) {
				if (inv.getStackInSlot(i) != null) {
					Item currentItem = inv.getStackInSlot(i).getItem();
					if (currentItem.itemID == ItemHandler.itemHungerStone.itemID) {
						hungerStone = true;
					}
					if(currentItem.itemID == ItemHandler.itemHealthStone.itemID) {
						healthStone = true;
					}
					if(currentItem.itemID == ItemHandler.itemCompleteStone.itemID) {
						completeStone = true;
					}

					if(currentItem.itemID == ItemHandler.itemFireAmulet.itemID) {
						fireAmulet = true;
					}
					if(currentItem.itemID == ItemHandler.itemWaterAmulet.itemID) {
						waterAmulet = true;
					}
				}
			}

			if ((tickCount % 20) == 0) {
				if(completeStone) {
					if (player.getFoodStats().needFood()) {
						player.getFoodStats().setFoodLevel(player.getFoodStats().getFoodLevel() + 1);
					}
					if (player.getHealth() < player.getMaxHealth()) {
						player.setHealth(player.getHealth() + 1);
					}
				} else {
					if (hungerStone) {
						if (player.getFoodStats().needFood()) {
							player.getFoodStats().setFoodLevel(player.getFoodStats().getFoodLevel() + 1);
						}
					}
					if (healthStone) {
						if (player.getHealth() < player.getMaxHealth()) {
							player.setHealth(player.getHealth() + 1);
						}
					}
				}

				if(fireAmulet) {
					if(player.isBurning()) {
						player.extinguish();
					}
				}
				if(waterAmulet) {
				}

				//player.writeEntityToNBT(player.getEntityData()); // i tried this, but it didn't help 
			}

			tickCount++;
		}
	}
}

@Override
public EnumSet<TickType> ticks() {
	return EnumSet.of(TickType.SERVER);
}

@Override
public String getLabel() {
	return "TickHandler.SERVER";
}

}

 

Any help much appreciated!

Link to comment
Share on other sites

  • 1 year later...
Guest
This topic is now closed to further replies.


×
×
  • Create New...

Important Information

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