Modding:Adding Block Behavior: Difference between revisions

From Vintage Story Wiki
Line 35: Line 35:
# Check if the block can be placed at this position
# Check if the block can be placed at this position
# Remove the original block
# Remove the original block
# Place the new block using the previous calculate position
# Place the new block using the previously calculated position


<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
     public override bool OnPlayerInteract(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handling)
     public override bool OnPlayerInteract(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handling)
     {
     {
        // Find the target position
         BlockPos pos = blockSel.Position.AddCopy(blockSel.Face.GetOpposite());
         BlockPos pos = blockSel.Position.AddCopy(blockSel.Face.GetOpposite());
        // Can we place the block there?
         if (world.BlockAccessor.GetBlock(pos).IsReplacableBy(block))
         if (world.BlockAccessor.GetBlock(pos).IsReplacableBy(block))
         {
         {
            // Remove the block at the current position and place it at the target position
             world.BlockAccessor.SetBlock(0, blockSel.Position);
             world.BlockAccessor.SetBlock(0, blockSel.Position);
             world.BlockAccessor.SetBlock(block.BlockId, pos);
             world.BlockAccessor.SetBlock(block.BlockId, pos);
         }
         }
        // Notify the game engine other block behaviors that we handled the players interaction with the block. If we would not set the handling field the player would still be able to place blocks if he has them in hands.
         handling = EnumHandling.PreventDefault;
         handling = EnumHandling.PreventDefault;
         return true;
         return true;
     }
     }
</syntaxhighlight>
</syntaxhighlight>
'''Please Note:''' Due to a bug in version 1.4.2 the OnPlayerInteract() and other methods are not being called on the client side, it will be fixed in version 1.4.3. This tutorial assumes you have version 1.4.3 or higher.
----
<syntaxhighlight lang="c#">
handling = EnumHandling.PreventDefault;
</syntaxhighlight>
This is used to prevent all further actions. For example if you hold a block in hand it will no longer place it.


== Register ==
== Register ==
Confirmedusers, Bureaucrats, editor, Administrators
1,778

edits