WorldGen: Difference between revisions

From Vintage Story Wiki
Line 55: Line 55:


== Placing our chest ==
== 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.
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#">
<syntaxhighlight lang="c#">
private void PlaceTreasureChestInFrontOfPlayer(IServerPlayer player, int groupId, CmdArgs args)
private void PlaceTreasureChestInFrontOfPlayer(IServerPlayer player, int groupId, CmdArgs args)
Line 64: Line 64:
}
}
</syntaxhighlight>
</syntaxhighlight>
The first line of code asks the IWorldManagerAPI to get the numeric block id for the block code "chest-south". Next we get the Block class that represents that chest. And finally we call a method called TryPlaceBlockForWorldGen and pass in an IBlockAccessor. There are many implementations of IBlockAccessor and we will cover that in a little more detail later. For now we are using this one. The second argument just calculates the world coordinates for 2 blocks in front of the player.
The first line of code asks the '''IWorldManagerAPI''' to get the numeric block id for the block code "chest-south". Next we get the '''Block''' class that represents that chest. And finally we call a method called '''TryPlaceBlockForWorldGen''' and pass in an IBlockAccessor. There are many implementations of '''IBlockAccessor''' and we will cover that in a little more detail later. For now we are using this one. The second argument just calculates the world coordinates for 2 blocks in front of the player.


Now if you run the mod from Visual Studio by pressing "Start" at the top you should be able to execute the /treasure command in the game. Once you do that you will have an fancy new chest appear in front of you with no items. Well I guess it's time for us to add some items.
Now if you run the mod from Visual Studio by pressing "Start" at the top you should be able to execute the '''/treasure''' command in the game. Once you do that you will have a fancy new chest appear in front of you with no items.  
 
Well I guess it's time for us to add some items.


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

edits