WorldGen: Difference between revisions

From Vintage Story Wiki
no edit summary
No edit summary
Line 52: Line 52:
</syntaxhighlight>
</syntaxhighlight>
This method will now be called when the user types /treasure command in the chat window. Wasn't that easy!!! Now we need to write the code to place our chest in that method.
This method will now be called when the user types /treasure command in the chat window. Wasn't that easy!!! Now we need to write the code to place our chest in that method.
== Placing our chest ==
The first thing we need to do is figure out how to tell the API that we want a chest and not grass or stone or some other block. Every Block has a numerical ID that gets assigned when the server starts but this ID may change. Luckily there is a property of the Block class that identifies it and does not change. This is the Code property. Block codes can be found in Vintagestory\assets\blocktypes in json files. The one for chest is Vintagestory\assets\blocktypes\wood\generic\chest.json. If you open that file you will see at the very top the code property is set to "chest". We also need to append the type of the shape that basically tells the system which way the chest is facing. So for simplicity we are going to pick south. So the resulting block code we will be using is "chest-south". Ok lets see some code.
<syntaxhighlight lang="c#">
private void PlaceTreasureChestInFrontOfPlayer(IServerPlayer player, int groupId, CmdArgs args)
{
    ushort blockID = api.WorldManager.GetBlockId("chest-south");
    Block chest = api.WorldManager.GetBlockType(blockID);
    chest.TryPlaceBlockForWorldGen(api.World.BlockAccessor, player.Entity.Pos.HorizontalAheadCopy(2).AsBlockPos, BlockFacing.UP);
}
</syntaxhighlight>


== Hooking in to the world gen API ==
== Hooking in to the world gen API ==
256

edits