Jump to content

Help Please, C# Code not working?


Recommended Posts

Hi there. And thanks for taking a look.

I completed the trampoline advanced block mod making tutorial successfully. Now, I am trying to go one step further by making a really, really simple mod. But, I'm pulling my hair out because it's not working.

The mod is supposed to replace crafting packed dirt. If you're holding soil and sneak right click to place that on another block of soil, the target block will be exchanged for packed dirt. I thought this would be really, really simple. I *sort of* got it to work using CanPlaceBlock, so I at least know the AssetLocation is valid. 

All of the logs show at the correct times, but it's like the two critical lines don't even execute.
 

                    world.BlockAccessor.ExchangeBlock(world.GetBlock(packedDirt).BlockId, blockSel.Position);
                    world.PlaySoundAt(packSound, byPlayer.Entity);


What am I doing wrong?

Thanks again,

-Nathan

Here's the full code.

using Vintagestory.API.Client;
using Vintagestory.API.Common;
using Vintagestory.API.Common.Entities;
using Vintagestory.API.Config;
using Vintagestory.API.Server;

namespace immersive_packed_dirt
{
    public class immersive_packed_dirtModSystem : ModSystem
    {
        public override void Start(ICoreAPI api)
        {
            base.Start(api);
            api.RegisterBlockBehaviorClass("ImmersivePackedDirtBehavior", typeof(BlockBehaviorImmersivePackedDirt));
            api.Logger.Chat("Registered Soil Mod");
        }

        public class BlockBehaviorImmersivePackedDirt : BlockBehavior
        {
            public BlockBehaviorImmersivePackedDirt(Block block) : base(block) { }

            public AssetLocation packSound = new AssetLocation("game", "thud");
            public AssetLocation packedDirt = new AssetLocation("game", "packeddirt");


            //DOES NOT WORK

            //TO DO: show the player the option to shift right click while holding soil.



            public override bool DoPlaceBlock(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ItemStack byItemStack, ref EnumHandling handling)
            {

                var heldBlock = world.BlockAccessor.GetBlock(byItemStack.Collectible.Code);

                world.Logger.Chat("Detected Place on Soil Block");

                if (byPlayer.Entity.Controls.Sneak && heldBlock.BlockMaterial == EnumBlockMaterial.Soil)
                {
                    world.Logger.Chat("Detected sneaking while holding soil material block");

                    world.BlockAccessor.ExchangeBlock(world.GetBlock(packedDirt).BlockId, blockSel.Position);
                    world.PlaySoundAt(packSound, byPlayer.Entity);

                    world.Logger.Chat("Finished");
                    return true;
                }
                return false;
            }
        }


    }
}

 

Link to comment
Share on other sites

Thank you! That was it. Was driving me nuts.

Although, while I was fixing it, the immensity of my goal dawned on me. This was just the start, I had grand plans to turn all of the grid recipes into either block interactions or one of the other more immersive types of recipes.

Would be pretty cool if the community or devs could turn this whole folder into more immersive game interactions.
Vintagestory\assets\survival\recipes\grid


Sort of like this mod does:
https://mods.vintagestory.at/immersivewoodchopping#showchangelog
 

immersive_packed_dirtModSystem.cs

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.