Skip to content

Integration & Compatibility

Lean's edited this page Jun 12, 2024 · 91 revisions

About

In this section you will know how to make integration with level up mod with your own mod, most of levelup is made with harmony, its highly recomended that you know what it is, in resume harmony is a ingame function patcher to overwrite/change native functions.

Normally you want to make a compatibility with the levelup mod when you need to interact with special functions from levelup, like damage calculation, or your mod explicitly patch some native functions that are not compatible with 2 mods at same time, for example the function ReceiveDamage in Entity class on LevelUP mod overwrite completly this function, and for other mod working on that function the mod will need to make a compatibility patch for that.

A complete example of compatibility with LevelUP is in the RPGDifficulty mod on Overwrite.cs

Checking LevelUP

To check if the LevelUP is present in the server/game context is quitly simple, what you need is just make a foreach in all mods presents in api and check if the "LevelUP" mod exist

public override void Start(ICoreAPI api)
    {
        bool levelUPCompatibility = false;
        api.ModLoader.Mods.Foreach((mod) => { if (mod.FileName == "LevelUP") levelUPCompatibility = true; });
    }

Damage

Adding or removing damage from the level up calculation you need to use the Entity stats to communicate between patches with LevelUP, actually theres 2 options for LevelUP:

  • LevelUP_DamageInteraction_Compatibility_ExtendDamageStart_ReceiveDamage, DamageStart
  • This is used to reduce damage or increase BEFORE the LevelUP levels calculation

  • LevelUP_DamageInteraction_Compatibility_ExtendDamageFinish_ReceiveDamage, DamageFinish
  • This is used to reduce damage or increase AFTER the LevelUP levels Calculation

Lets now create a HarmonyPrefix in the ReceiveDamage function from Entity class, this function is used on LevelUP to handle the damage interactions and performing increased damage

// Overwrite Damage Interaction
[HarmonyPrefix]
[HarmonyPatch(typeof(Entity), "ReceiveDamage")]
public static void ReceiveDamage(Entity __instance, DamageSource damageSource, float damage)
{
  // In this line we will check if theres is no patches for LevelUP
  if (__instance.Stats.GetBlended("LevelUP_DamageInteraction_Compatibility_ExtendDamageFinish_ReceiveDamage") == 0.0f)
  {
    // In this context no mods have added a compatibility so we can proceed to your calculation

    // In this example we are adding 10 damage to the final calculation of LevelUP, also we can use -10 to reduce 10 damage of calculation
    __instance.Stats.Set("LevelUP_DamageInteraction_Compatibility_ExtendDamageFinish_ReceiveDamage", "DamageFinish", 10, true);
  }
  // Otherwises theres already a patch for the LevelUP
  else
  {
    // In this case we get the actual value from other mods
    float entityAdditionalDamage = __instance.Stats.GetBlended("LevelUP_DamageInteraction_Compatibility_ExtendDamageFinish_ReceiveDamage");
    // Now we are setting again with the old value from other mod plus your value 10
    __instance.Stats["LevelUP_DamageInteraction_Compatibility_ExtendDamageFinish_ReceiveDamage"].Set("DamageFinish", entityAdditionalDamage + 10, true);
  }
}

Harvest Drop

Adding or removing harvest drops from the level up calculation you need to use the Entity stats to communicate between patches with LevelUP, actually theres 1 option for LevelUP:

  • LevelUP_BlockInteraction_Compatibility_ExtendHarvestDrop_SetHarvestedKnife, HarvestStart
  • This is the additional/removal value that harvest will receive from LevelUP calculation

Now lets create the harmony patch for the function SetHarvested in EntityBehaviorHarvestable class, this function is called every time a player harvest something

// Overwrite Knife Harvesting
[HarmonyPrefix]
[HarmonyPatch(typeof(EntityBehaviorHarvestable), "SetHarvested")]
public static void SetHarvestedKnifeStart(EntityBehaviorHarvestable __instance, IPlayer byPlayer, float dropQuantityMultiplier = 1f)
  {
    // Checking if not exist any compatibility yet
    if (byPlayer.Entity.Stats.GetBlended("LevelUP_BlockInteraction_Compatibility_ExtendHarvestDrop_SetHarvestedKnife") == 0.0f)
    {
      // Simple create new stats if not exist
      byPlayer.Entity.Stats.Set("LevelUP_BlockInteraction_Compatibility_ExtendHarvestDrop_SetHarvestedKnife", "HarvestStart", dropRateIncrease, true);
    }
    else
    {
      // Some other mod has already created the compatibility, lets get the value
      float entityDropRate = byPlayer.Entity.Stats.GetBlended("LevelUP_BlockInteraction_Compatibility_ExtendHarvestDrop_SetHarvestedKnife");
      // We set now the variable as the: previous additional droprate from other mod plus ours new droprate
      byPlayer.Entity.Stats.Set("LevelUP_BlockInteraction_Compatibility_ExtendHarvestDrop_SetHarvestedKnife", "HarvestStart", entityDropRate + dropRateIncrease, true);
    }
  }

Clone this wiki locally