Modding:Adding Block Behavior
Moving
Let's create a behavior which makes a block move on right click.
Setting up
A development workspace is required. Additionally you will need the assets (blocktype, texture and lang file). You can either create your one owns or use those pre-made ones: Moving - No CS File.zip
Creating the behavior
So first of all we need to create the behavior itself, which is a class extending BlockBehavior
class Moving : BlockBehavior
{
public Moving(Block block) : base(block)
{
}
}
This class provides several methods we can override. You can find a full list of a methods by hovering with the mouse of "BlockBehavior" and pressing "F12".
The method bool OnPlayerInteract(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handling)
looks to be ideal for our purpose.
What should it do?
1. Calculate the new position of the block depending on the face the player is looking at 2. Check if the block can be placed at this position 3. Remove the original block 4. Place the new block using the previous calculate position
public override bool OnPlayerInteract(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handling)
{
BlockPos pos = blockSel.Position.AddCopy(blockSel.Face.GetOpposite());
if (world.BlockAccessor.GetBlock(pos).IsReplacableBy(block))
{
world.BlockAccessor.SetBlock(0, blockSel.Position);
world.BlockAccessor.SetBlock(block.BlockId, pos);
}
handling = EnumHandling.PreventDefault;
return true;
}
public override bool OnPlayerInteract(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handling)
{
BlockPos pos = blockSel.Position.AddCopy(blockSel.Face.GetOpposite());
if (world.BlockAccessor.GetBlock(pos).IsReplacableBy(block))
{
world.BlockAccessor.SetBlock(0, blockSel.Position);
world.BlockAccessor.SetBlock(block.BlockId, pos);
}
handling = EnumHandling.PreventDefault;
return true;
}
Register
Testing
Second Behavior (using custom properties)
Still trying to find an idea.