Modding:Adding Block Behavior: Difference between revisions

From Vintage Story Wiki
No edit summary
No edit summary
Line 1: Line 1:
__FORCETOC__
__FORCETOC__


= Unbreakable =
= Moving =


Let's create a behavior which makes a block unbreakable even in creative mode.
Let's create a behavior which makes a block move on right click.
 
== 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]
 
== Creating the behavior ==
 
So first of all we need to create the behavior itself, which is a class extending BlockBehavior
<syntaxhighlight lang="c#">
class Moving : BlockBehavior
{
    public Moving(Block block) : base(block)
    {
       
    }
}
</syntaxhighlight>
 
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 <code>bool OnPlayerInteract(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handling)</code> 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
 
<syntaxhighlight lang="c#">
    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;
    }
</syntaxhighlight>
 
 
 
<syntaxhighlight lang="c#">
    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;
    }
</syntaxhighlight>


== Creating ==


== Register ==
== Register ==

Revision as of 09:49, 21 August 2017


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.