Modding:Adding Block Behavior: Difference between revisions

From Vintage Story Wiki
no edit summary
No edit summary
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
<languages />
<translate>
__FORCETOC__
__FORCETOC__


Line 9: Line 11:
== Setting up ==
== Setting up ==


A [[Setting up a dev environment|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: [http://wiki.vintagestory.at/images/2/2f/Moving_-_No_CS_File.zip Moving - No CS File.zip]
A [[Setting up your Development Environment|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: [https://wiki.vintagestory.at/images/2/2f/Moving_-_No_CS_File.zip Moving - No CS File.zip]


== Creating the behavior ==
== Creating the behavior ==


So first of all we need to create the behavior itself, which is a class extending BlockBehavior
So first of all we need to create the behavior itself, which is a class extending BlockBehavior
<syntaxhighlight lang="c#">
<syntaxhighlight lang="csharp">
class Moving : BlockBehavior
class Moving : BlockBehavior
{
{
Line 35: Line 37:
# 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 ==
Line 72: Line 71:
         {
         {
             base.Start(api);
             base.Start(api);
             api.RegisterBlockBehavior("Moving", typeof(Moving));
             api.RegisterBlockBehaviorClass("Moving", typeof(Moving));
         }
         }


Line 78: Line 77:
</syntaxhighlight>
</syntaxhighlight>


Now all you need to do is to put the *.cs file in the zip file as well and you are ready to test/ distribute your mod.
== Distribution ==


You can download the example mod [http://wiki.vintagestory.at/images/c/cb/Moving.zip here].
In order to finish everything, open the modtools and type in <code>pack <your mod id></code>. Now you can take the zip file and share it with other people. You can download the example mod [https://wiki.vintagestory.at/images/c/cb/Moving.zip here].


== Testing ==
== Testing ==
Line 175: Line 174:
The block will be pushed two blocks instead of one and the player can pull it by sneaking while right clicking.
The block will be pushed two blocks instead of one and the player can pull it by sneaking while right clicking.


You can download the complete mod [http://wiki.vintagestory.at/images/7/72/AdvancedMoving.zip here].
You can download the complete mod [https://wiki.vintagestory.at/images/7/72/AdvancedMoving.zip here].
 
 
 
{{Navbox/modding|Vintage Story}}
 
 
</translate>
Confirmedusers, editor, Administrators
886

edits