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;
}
}
}
}