Placing blocks in lua
#1
I'm new to lua, and I was wondering if there was any way to place a specific block in a specific place after the block previously in that place was broken?
Reply
Thanks given by:
#2
A plugin can be written to do that.

There is a hook named HOOK_PLAYER_BROKEN_BLOCK with that you can detect what block and where it has been broken. Then you can place a new one immediately or after a specific time.
That should be easy, if you have any problems / questions just ask  Smile
Reply
Thanks given by: yoboykai
#3
I'm still a little confused about hoe to place a block with lua, as I said I'm very new to lua and developing plugins.
Reply
Thanks given by:
#4
The function that will be called for the hook, contains the param Player. With it you can get the world the player is in:

Code:
Player:GetWorld():SetBlock(Player:GetPosX(), Player:GetPosY(), Player:GetPosZ(), E_BLOCK_CAKE, 0)

API:
Player
World
World:SetBlock()
E_BLOCK_CAKE

If you want I could write a example plugin, that would place a block if a player breaks a block.
Reply
Thanks given by: yoboykai
#5
If you could it would be much appreciated.
Reply
Thanks given by:
#6
1) Create a folder named PlaceBlocks inside of folder Plugins
2) Save the content into the file PlaceBlocks.lua
3) Add the plugin name to the section Plugins in settings.ini

Code:
function Initialize(a_Plugin)
    a_Plugin:SetName( "PlaceBlocks" )
    a_Plugin:SetVersion( 1 )
    
    cPluginManager:AddHook(cPluginManager.HOOK_PLAYER_BROKEN_BLOCK, OnPlayerBrokenBlock)
    
    return true
end

function OnPlayerBrokenBlock(Player, BlockX, BlockY, BlockZ, BlockFace, BlockType, BlockMeta)
    if BlockType == E_BLOCK_GRASS then
        Player:GetWorld():SetBlock(BlockX, BlockY, BlockZ, E_BLOCK_STONE, 0)
    end
end
This plugin example replaces the grass blocks that the player breaks with an stone block.
Reply
Thanks given by: yoboykai
#7
Thank you, this clears so much up for me.
Reply
Thanks given by:
#8
Sorry if i'm annoying you but, how would I change all blocks of stone for example to a different block after a set amount of time, and repeatedly to this?
Reply
Thanks given by:
#9
Do you know the area where you want to replace the blocks?
Reply
Thanks given by:
#10
Yes, but there will be multiple separate areas and new areas being added.
Reply
Thanks given by:




Users browsing this thread: 5 Guest(s)