Skip to content

IConfigurableFoodItem

Kye edited this page Sep 19, 2022 · 2 revisions

IConfigurableFoodItem

The IConfigurableFoodItem is an Interface implementation. It allows you to make your Food Items an Overridable Food Item to allow either:

  • Other Modders
  • Server Owners

To customize Food Nutrition Values for your items.

The implementation of the IConfigurableFoodItem can be used on Any Housing Item.

Below is the Implementation of the IConfigurableFoodItem:


Implementation


The implementation uses a specific data structure and is setup in a specific way for it to be able to work.

Working with the implementation will be confusing to start with as it is not a normal implementation of the Food Nutrition Values setup. (Okay its pretty close)

private static readonly FoodItemModel defaults = new(
    typeof(Item), 
    "Random Item", 
    calories: 300, 
    carbs: 2, 
    fat: 5, 
    protein: 7, 
    vitamins: 1,
    shelflife: 24
);

Here is each component broken down and explained:


typeof(Item)

This is The item you are connecting the Resolver too.


"Some Item"

This is The Items Display Name


Calories

This is the Calorie Value your Food Item will Give


Carbs

This is the Carbs Nutrition Value


Fat

This is the Fat Nutrition Value


Protein

This is the Protein Nutrition Value


Vitamins

This is the Vitamins Nutrition Value


Shelflife

This is how long the food will last (This value is entered in hours, the resolver will handle the conversion for you)


Setting It Up


These are the values that are required when using this on the Your Food Items.

Anything Marked with an Asterixis (*) is Required (Attributes Not Included)

    public partial class RandomItem : FoodItem, IConfigurableFoodItem
    {
        public override LocString DisplayNamePlural => Localizer.DoStr("Random Items");
        public override LocString DisplayDescription => Localizer.DoStr("Descrpition");

        private static readonly FoodItemModel defaults = new(
          typeof(RandomItem), *
          "Random", *
          calories: 300, *
          carbs: 2, *
          fat: 5, *
          protein: 7, *
          vitamins: 1, *
          shelflife: 24 *
        );

        public override float Calories => EMFoodItemResolver.Obj.ResolveCalories(this); *
        public override Nutrients Nutrition => EMFoodItemResolver.Obj.ResolveNutrients(this); *
        protected override int BaseShelfLife                  => EMFoodItemResolver.Obj.ResolveShelfLife(this); *
        static RandomItem() => EMFoodItemResolver.AddDefaults(defaults);
    }

And thats it! you can now override the food nutrition values with other mods and the EM Configure Plugin (Built In)

Clone this wiki locally