Jump to content

1.12 food saturation/heal amount as tooltip


NoHaxJustLegit

Recommended Posts

Hello, I'm trying to add the saturation level and the food heal that a fooditem gives.

@SideOnly(Side.CLIENT)
public class FoodNutrition {

	private static final KeyBinding keyBindSneak = Minecraft.getMinecraft().gameSettings.keyBindSneak;

	@SubscribeEvent
	public void onTooltipDisplayed(ItemTooltipEvent event) {
		if (event.getEntityPlayer() == null) {
			return;
		}
			if (!event.getItemStack().isEmpty() && event.getItemStack().getItem() instanceof ItemFood) {
				Item item = event.getItemStack().getItem();
				ItemStack stack = event.getItemStack();
				final List<String> tooltip = event.getToolTip();
				float heal = ((ItemFood) item).getHealAmount(stack);
				float saturation = ((ItemFood) item).getSaturationModifier(stack);
				System.out.print("\nSat: " + event.getEntityPlayer().getFoodStats().getSaturationLevel() + "\nFood: "
						+ event.getEntityPlayer().getFoodStats().getFoodLevel());
				if (GameSettings.isKeyDown(keyBindSneak)) {
					tooltip.add("&2Food level: &a" + (heal / 2));
					tooltip.add("&2Saturation level: &a"+ saturation);
				} else {
					tooltip.add(Messages.color("Press shift"));
			}
		}
	}
}

The cooked chicken gives Food Level: 6.0 restoring 6 points and its correct and Saturation level gives me 0.6 and the print gives:

Sat: 6.0
Food: 6

is there a better way to get/show how many heal points (not hearts) and saturation points a food gives? I think my only problem is Saturation level which gives me a 0.x number

Edited by NoHaxJustLegit
Link to comment
Share on other sites

12 minutes ago, diesieben07 said:
  • Do not use @SideOnly.
  • Use the TextFormatting class instead of hardcoded color prefixes (which are even broken, you need to use §, not & if you were to use them).
  • You can see how the new food and saturation levels are calculated in FoodStats#addStats.
tooltip.add("§2Saturation level: §a"+ Math.min(5F + heal * saturation * 2.0F, heal));

5F is from foodSaturationLevel inside FoodStats class, is it correct?

Link to comment
Share on other sites

25 minutes ago, diesieben07 said:

No... You need to actually use the value of FoodStats#foodSaturationLevel.

But the value is private

 

Tried this but 

public static final Field saturationLevel = ReflectionHelper.findField(FoodStats.class, "field_75125_b",
			"foodSaturationLevel");
try {
tooltip.add("§2Saturation level: §a" + saturationLevel.getFloat("foodSaturationLevel")));
} catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}

 

 java.lang.IllegalArgumentException: Can not set float field net.minecraft.util.FoodStats.foodSaturationLevel to java.lang.String

 

Edited by NoHaxJustLegit
Link to comment
Share on other sites

14 minutes ago, NoHaxJustLegit said:

But the value is private

Use reflection.

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

8 hours ago, diesieben07 said:

That is not how you handle exceptions. At all. Ever.

 

Why are you passing a string to Field#getFloat?

Just to be sure, you know I want to know how much saturation a food gives right? like AppleSkin does

Example: If a cooked beef gives you 3 saturation points (1.5 as hunger shanks) it should say 1.5

Edited by NoHaxJustLegit
Link to comment
Share on other sites

1 hour ago, diesieben07 said:

Cooked beef gives 8 food level (not saturation), that's 4 hunger-bars. This value can be gotten using ItemFood#getHealAmount(ItemStack). For cooked beef this returns 8.

Saturation is a different concept, it is not shown in the UI directly.

I know, I wanted to add how much saturation a food gives based on 20 food level (max) like AppleSkin does with the yellow food level border when food is held. If it's useless then I'll just add the food level

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.