Jump to content

Code Mod: how to start? - or - How to modify the axe without creating a new one?


Recommended Posts

Greetings, dear readers!

I'm starting to try putting some approaches to change details of this amazing game together, hopefully resulting in a neat, well-made mod.
While looking for points to begin my work with / at, I stumbled upon the question of how I should proceed to modify stock game behaviours - possibly by using specialized mono / C# language features, yet avoiding "unsafe" segments.

Let's say I would like to mod the axe for some reason.
To give a more concrete example: I want the axe to cause a drop of wood of random type - so not only the wood type of the tree I hit with it.
As far as my first peek at the code tells me, the code causing the generation of drop belongs to the axe.

public bool OnBlockBrokenWith(IWorldAccessor world, Entity byEntity, ItemSlot itemslot, BlockSelection blockSel, float dropQuantityMultiplier = 1)
	IPlayer byPlayer = null;
	if (byEntity is EntityPlayer) byPlayer = byEntity.World.PlayerByUid(((EntityPlayer)byEntity).PlayerUID);

	/* ... bla bla ... */
	int blocksbroken = 0;

	while (foundPositions.Count > 0)
		BlockPos pos = foundPositions.Pop();

		Block block = world.BlockAccessor.GetBlock(pos);

		/* ... bla bla ... */

		bool isBranchy = block == leavesBranchyBlock;
		bool isLeaves = block == leavesBlock || block.Code.Path == "bambooleaves-grown";

		/* THIS LINE */
		world.BlockAccessor.BreakBlock(pos, byPlayer, isLeaves ? leavesMul : (isBranchy ? leavesBranchyMul : 1));
		/* THIS LINE */
		/* ... bla bla ... */

	/* ... bla bla ... */
	return true;

My eyes noticed "world.BlockAccessor.BreakBlock" to be the point where I possibly would want to put a hook.
Let's say, the approach to alter the blocks of wood before breaking them would be the best one that comes to mind.
So, this would mean taking this single function, adding a few lines, and bringing it back into the play, optimally replacing the old one "in place" - having the same context and scope, code-wise. I can imagine some other ways that could be possible - like de-registering the original axe and creating the modified one as separate item / class that will then be registered as original axe, transparently taking its place. But ..

.. how am I supposed to proceed? Any "best practices" available?

Little background:
I'm not new to programming (regularly do bare metal C / C++ / asm, use Java, SmallTalk, php and truly feel all colours of hate for JS), but I'm quite a beginner in C#. So I'm not sure if I overlooked an obvious technique to easily do that. Please point me to the right direction.

Thanks for any helpful advice. ;)
Have an as great as possible holidays altogether and stay negative.

Edited by Talyas
Small clarification
Link to comment
Share on other sites

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.