Posted November 4, 20195 yr 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 November 4, 20195 yr by NoHaxJustLegit
November 4, 20195 yr Author 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?
November 4, 20195 yr Author 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 November 4, 20195 yr by NoHaxJustLegit
November 4, 20195 yr 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.
November 4, 20195 yr Author Just now, DavidM said: Use reflection. Sorry, I edited my last post, I've used it
November 4, 20195 yr Author 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 November 4, 20195 yr by NoHaxJustLegit
November 4, 20195 yr Author 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
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.