Example Harmony Mod: PlaceBlock()

Harmony Mods ›› Harmony ››
Parent Previous Next

As mentioned in the DMT Harmony Format section, your script will have to follow some rules in order to be initialized. You will also need to define which method or methods you want to intercept and how you want to do that.


Here's an example Nerd Pole mod. The overall goal of this mod is to prevent players from placing blocks when they are jumping or in the air. To do this, we intercept the base Block's class, targeting the PlaceBlock method.


The PlaceBlock() call places the block in the world. Since we want to prevent that from happening, we will use Harmony's Prefix() method.


using Harmony;

using System.Reflection;

using UnityEngine;

using DMT;


[HarmonyPatch(typeof(Block))]

[HarmonyPatch("PlaceBlock")]

public class SphereII_NerdPoll : IHarmony

{

   public void Start()

   {

       Debug.Log(" Loading Patch: " + GetType().ToString());

       var harmony = HarmonyInstance.Create(GetType().ToString());

       harmony.PatchAll(Assembly.GetExecutingAssembly());

   }


   // Returns true for the default PlaceBlock code to execute. If it returns false, it won't execute it at all.

   static bool Prefix(EntityAlive _ea)

   {

       Debug.Log("Prefix PlaceBox");

       EntityPlayerLocal player = _ea as EntityPlayerLocal;

       if(player == null)

           return true;


       if(player.IsGodMode == true)

           return true;


       if(player.IsFlyMode == true)

           return true;


       if(player.IsInElevator())

           return true;


       if(player.IsInWater())

           return true;


       // If you aren't on the ground, don't place the block.

       if(!player.onGround)

           return false;


       return true;

   }

}



Created with the Personal Edition of HelpNDoc: Full-featured Help generator