Skip to content

Integration & Compatibility

Lean's edited this page Jun 13, 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)
{
        // Check for compatibilities
        if (__instance.Attributes.GetFloat("LevelUP_DamageInteraction_Compatibility_ExtendDamageStart_ReceiveDamage") == 0.0f)
        {
            // Simple create new stats if not exist
            __instance.Attributes.SetFloat("LevelUP_DamageInteraction_Compatibility_ExtendDamageStart_ReceiveDamage", 10);
        }
        else
        {
            // Some other mod has already created the compatibility, lets get the value
            float entityAdditionalDamage = __instance.Attributes.GetFloat("LevelUP_DamageInteraction_Compatibility_ExtendDamageStart_ReceiveDamage");
            // We set now the variable as the: previous additional damage from other mod plus ours new damage
            __instance.Attributes.SetFloat("LevelUP_DamageInteraction_Compatibility_ExtendDamageStart_ReceiveDamage", entityAdditionalDamage + 10);
        }
}

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)
  {
        // Check if player exist
        if (byPlayer != null) return;

        // Get the final droprate
        float dropRateIncrease = 2f;

        // Checking if not exist any compatibility yet
        if (byPlayer.Entity.Attributes.GetFloat("LevelUP_BlockInteraction_Compatibility_ExtendHarvestDrop_SetHarvestedKnife") == 0f)
        {
            // Simple create new stats if not exist
            byPlayer.Entity.Attributes.SetFloat("LevelUP_BlockInteraction_Compatibility_ExtendHarvestDrop_SetHarvestedKnife", dropRateIncrease);
        }
        else
        {
            // Some other mod has already created the compatibility, lets get the value
            float entityDropRate = byPlayer.Entity.Attributes.GetFloat("LevelUP_BlockInteraction_Compatibility_ExtendHarvestDrop_SetHarvestedKnife");
            // We set now the variable as the: previous additional droprate from other mod plus ours new droprate
            byPlayer.Entity.Attributes.SetFloat("LevelUP_BlockInteraction_Compatibility_ExtendHarvestDrop_SetHarvestedKnife", entityDropRate + dropRateIncrease);
        }
    }
  }

Clone this wiki locally