WorldGen: Difference between revisions

From Vintage Story Wiki
Line 213: Line 213:
</syntaxhighlight>
</syntaxhighlight>
The first line is just capturing the BlockPos position object so that we can use it in two places. The next is the same as before, just placing the chest in the world. Next we go back to the IBlockAccessor to get the block entity at that position. It's important to call GetBlockEntity here because a chest is an Entity. An Entity is something that has extra behavior attached to it as opposed to a normal block. This method returns an IBlockEntity which is a generic interface for all block entities. However we specifically need a block entity that has an Inventory. Since we know the block entity we just placed is a chest then it's safe to to cast the returned IBlockEntity to an IBlockEntityContainer which is a specialized version of IBlockEntity that provides access to an Inventory. Now that we have that we pass it along to our AddItemStacks method we created earlier and additionally pass in the list of ItemStacks that are created by our MakeItemStacks method that we also created earlier. Now if you run the code again and type /treasure you should have random items in there! Try it several times and you will see them change.
The first line is just capturing the BlockPos position object so that we can use it in two places. The next is the same as before, just placing the chest in the world. Next we go back to the IBlockAccessor to get the block entity at that position. It's important to call GetBlockEntity here because a chest is an Entity. An Entity is something that has extra behavior attached to it as opposed to a normal block. This method returns an IBlockEntity which is a generic interface for all block entities. However we specifically need a block entity that has an Inventory. Since we know the block entity we just placed is a chest then it's safe to to cast the returned IBlockEntity to an IBlockEntityContainer which is a specialized version of IBlockEntity that provides access to an Inventory. Now that we have that we pass it along to our AddItemStacks method we created earlier and additionally pass in the list of ItemStacks that are created by our MakeItemStacks method that we also created earlier. Now if you run the code again and type /treasure you should have random items in there! Try it several times and you will see them change.
That's really cool and all but not real fun for a game play experience. We want the player to find these chests and encourage them to explore the world! So lets plug in to world gen next!


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