-
Notifications
You must be signed in to change notification settings - Fork 4
Integration & Compatibility
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
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; });
}If you want to change some aspects of the LevelUP and you want to add more damage, harvest drops, you can create a harmony patch using the [HarmonyPrefix], what will happens when using the harmony prefix? you will create a function that will be called before the LevelUP calculations, so you can make all your own calculations and translate it to the LevelUP by creating a communication layer to it.
[HarmonyPatch(typeof(Entity), "ReceiveDamage")]
- LevelUP_DamageInteraction_Compatibility_ExtendDamageStart_ReceiveDamage
-
This is used to reduce damage or increase BEFORE the LevelUP levels calculation
- LevelUP_DamageInteraction_Compatibility_ExtendDamageFinish_ReceiveDamage
-
This is used to reduce damage or increase AFTER the LevelUP levels Calculation
[HarmonyPatch(typeof(EntityBehaviorHarvestable), "SetHarvested")]
- LevelUP_BlockInteraction_Compatibility_ExtendHarvestDrop_SetHarvestedKnife, HarvestStart
-
This is the additional/removal value that harvest will receive from LevelUP 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, in this example every time a player does or receive damage it will be increased by 10
// 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);
}
}
### Increasing Harvest drop rate
Now lets create the harmony patch for the function SetHarvested in EntityBehaviorHarvestable class, this function is called every time a player harvest something, in this example everytime a player harvest something it is increased by 2 (300%)
```csharp
// 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);
}
}
}