Modding:World Access: Difference between revisions

From Vintage Story Wiki
no edit summary
No edit summary
(6 intermediate revisions by one other user not shown)
Line 21: Line 21:
</syntaxhighlight>
</syntaxhighlight>


With this you can already start setting or reading blocks using the api object. Please not that mods are loaded before the world is loaded. So you cannot immediately access the world. So depending on what you want to achieve, you have to register to the appropriate event. For some examples in order to do something...
With this you can already start placing or reading blocks using the api object. Please note that mods are loaded before the world is loaded, so you cannot immediately access the world. Depending on what you want to achieve, you have to register to the appropriate event. For some examples in order to do something...
* after the world has loaded you could register to the RunGame phase: <code>api.Event.ServerRunPhase(EnumServerRunPhase.RunGame, OnRunGame);</code>
* after the world has loaded you could register to the RunGame phase: <code>api.Event.ServerRunPhase(EnumServerRunPhase.RunGame, OnRunGame);</code>
* during world generation we'd have to register to the [[WorldGen API|appropriate world gen events]].
* during world generation we'd have to register to the [[WorldGen API|appropriate world gen events]].
* when the player runs a command has to type we can register a command: <code>api.RegisterCommand("test", "a test command", "", OnCommand);</code>
* when the player runs a custom command we need to register one: <code>api.RegisterCommand("test", "a test command", "", OnCommand);</code>
* when a player joins the game: <code>api.Event.PlayerJoin(OnPlayerJoin);</code>
* when a player joins the game: <code>api.Event.PlayerJoin(OnPlayerJoin);</code>


Line 46: Line 46:
* Count the amount log blocks around the player in a 3 block radius
* Count the amount log blocks around the player in a 3 block radius


Now, blocks are always uniquely identified by a number of characters called the block code. If you want to find out a blocks code, a simple way of doing it is to start the game, enable the debug info mode via the command .edi and look at a block in the creative inventory:
Now, blocks are always uniquely identified by a number of characters called the block code. If you want to find out a blocks code, a simple way of doing it is to start the game, enable the debug info mode via the command <code>.edi</code> and look at a block in the creative inventory:


[[File:Blockcode.png]]
[[File:Blockcode.png]]
Line 109: Line 109:
</syntaxhighlight>
</syntaxhighlight>


And the final result:
And the complete code and final result:
 
<syntaxhighlight lang="c#">
        private void OnPlayerJoin(IServerPlayer byPlayer)
        {
            BlockPos plrpos = byPlayer.Entity.Pos.AsBlockPos;
 
            Block firebrickblock = api.World.GetBlock(new AssetLocation("claybricks-fire"));
            ushort blockId = firebrickblock.BlockId;
            api.World.BlockAccessor.SetBlock(blockId, plrpos.DownCopy());
 
            // Check a 3x3x3 area for logs
            int quantityLogs = 0;
            api.World.BlockAccessor.WalkBlocks(
                plrpos.AddCopy(-3, -3, -3),
                plrpos.AddCopy(3, 3, 3),
                (block, pos) => quantityLogs += block.Code.Path.Contains("log") ? 1 : 0
            );
 
            byPlayer.SendMessage(GlobalConstants.GeneralChatGroup, "You have " + quantityLogs + " logs nearby you", EnumChatType.Notification);
        }
</syntaxhighlight>


[[File:SetblockTut.png|500px]]
[[File:SetblockTut.png|500px]]


I hope this taught you the basics of accessing the world.
I hope this taught you the basics of accessing the world. More ways to access blocks and entities will be added here in the future.
 
 
 
{{Navbox/modding|Vintage Story}}
Confirmedusers, Bureaucrats, editor, Administrators
783

edits