Confirmedusers, Bureaucrats, editor, Administrators
1,778
edits
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 | # 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> | ||
== Register == | == Register == |