Modding:Adding Block Behavior: Difference between revisions

From Vintage Story Wiki
m
Updated to 1.19.3.
(Add a link to Modding:Behavior_Traversal)
m (Updated to 1.19.3.)
Line 3: Line 3:
<!--T:1-->
<!--T:1-->
__FORCETOC__
__FORCETOC__
{{GameVersion|1.15}}
{{GameVersion|1.19.3}}


= Introduction = <!--T:2-->
= Introduction = <!--T:2-->
Line 38: Line 38:


<!--T:10-->
<!--T:10-->
The method <code>bool OnPlayerInteract(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handling)</code> looks to be ideal for our purpose.
The method <code>bool OnBlockInteractStart(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handling)</code> looks to be ideal for our purpose.


<!--T:11-->
<!--T:11-->
Line 52: Line 52:
<!--T:13-->
<!--T:13-->
<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
     public override bool OnPlayerInteract(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handling)
     public override bool OnBlockInteractStart(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handling)
     {
     {
         // Find the target position
         // Find the target position
         BlockPos pos = blockSel.Position.AddCopy(blockSel.Face.GetOpposite());
         BlockPos pos = blockSel.Position.AddCopy(blockSel.Face.Opposite);


         <!--T:14-->
         <!--T:14-->
Line 178: Line 178:
The next thing we need to change is the interact method itself, so that it takes care of the distance and the pull properties ...
The next thing we need to change is the interact method itself, so that it takes care of the distance and the pull properties ...
<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
         public override bool OnPlayerInteract(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handling)
         public override bool OnBlockInteractStart(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handling)
         {
         {
             BlockPos pos = blockSel.Position.AddCopy(pull && byPlayer.WorldData.EntityControls.Sneak ? blockSel.Face : blockSel.Face.GetOpposite(), distance);
            // Find the target position
             BlockPos pos = blockSel.Position.AddCopy(pull && byPlayer.WorldData.EntityControls.Sneak ? blockSel.Face : blockSel.Face.Opposite, distance);
 
            // 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;
Confirmedusers
536

edits