Skip to content

Integrating food items with AppleCore

squeek502 edited this page Sep 20, 2014 · 17 revisions

If you want your food to integrate with AppleCore, do not call FoodStats.addStats(int, float) when it is eaten. Use the ItemStack-aware FoodStats.func_151686_a(ItemFood, ItemStack) method instead.

ItemFood integration checklist

If your food extends ItemFood and uses the standard Minecraft ItemFood methods, your food will integrate with AppleCore without issue.

  • Have your item extend ItemFood.
  • Implement the 1.7 ItemStack-aware hunger and saturation getters (func_150905_g for hunger value and func_150906_h for the saturation modifier).
  • Use the default ItemFood.onEaten implementation or make sure that your overridden version still calls FoodStats.func_151686_a(ItemFood, ItemStack) (the ItemStack-aware version of FoodStats.addStats).

That's it. Your food will integrate properly with AppleCore if you're able to check off all of the above.

Non-standard food integration

If your food does not extend ItemFood, then you can implement AppleCore's IEdible interface and use ItemFoodProxy to call FoodStats.func_151686_a(ItemFood, ItemStack) when the food is eaten.

Here is a partial example implementation (see applecore.example.ItemNonStandardFood for the full implementation):

@Optional.Interface(iface = "squeek.applecore.api.food.IEdible", modid = "AppleCore")
public class ItemNonStandardFood extends Item implements IEdible
{
	@Optional.Method(modid = "AppleCore")
	@Override
	public FoodValues getFoodValues(ItemStack itemStack)
	{
		return new FoodValues(1, 1f);
	}

	@Override
	public ItemStack onEaten(ItemStack itemStack, World world, EntityPlayer player)
	{
		--itemStack.stackSize;

		if (Loader.isModLoaded("AppleCore"))
		{
			// one possible compatible method
			player.getFoodStats().func_151686_a(new ItemFoodProxy(this), itemStack);

			// another possible compatible method:
			// new ItemFoodProxy(this).onEaten(itemStack, player);
		}
		else
		{
			// this method is not compatible with AppleCore
			player.getFoodStats().addStats(1, 1f);
		}

		return itemStack;
	}
}

Note that the above example degrades nicely: if AppleCore is not loaded, it will still function as expected.

Making a food item inedible

Foods are considered inedible, and therefore will return false when passed to AppleCoreAPI.accessor.isFood if the Item implementation does not return EnumAction.eat or EnumAction.drink from Item.getItemUseAction.

public class ItemFoodEdibleSometimes extends ItemFood
{
	@Override
	public EnumAction getItemUseAction(ItemStack itemStack)
	{
		return someCondition(itemStack) ? EnumAction.eat : EnumAction.none;
	}
}