Skip to content

Commit

Permalink
[LuaCore] New event 'OnBlockDigged' added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed May 4, 2020
1 parent f926f4f commit 9f09a0a
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/lua-api-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ openminer:add_listener(EventType.OnBlockPlaced, function(pos, player, world, cli
server:send_chat_message(0, "Block placed at " .. pos.x .. ";" .. pos.y .. ";" .. pos.z .. " by Client" .. player:client_id(), client);
end)

openminer:add_listener(EventType.OnBlockDigged, function(pos, player, world, client, server)
server:send_chat_message(0, "Block digged at " .. pos.x .. ";" .. pos.y .. ";" .. pos.z .. " by Client" .. player:client_id(), client);
end)

openminer:add_listener(EventType.OnBlockActivated, function(pos, block, player, world, client, server)
if block:string_id() == "default:portal" then
server:send_chat_message(0, "Swoosh! Changing dimension...", client);
Expand All @@ -32,5 +36,6 @@ end)
Possible events:

- `OnBlockPlaced`: `funcion(pos, player, world, client, server)`
- `OnBlockDigged`: `funcion(pos, player, world, client, server)`
- `OnBlockActivated`: `function(pos, block, player, world, client, server)`

4 changes: 4 additions & 0 deletions mods/default/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ dofile("dimensions.lua")
-- server:send_chat_message(0, "Block placed at " .. pos.x .. ";" .. pos.y .. ";" .. pos.z .. " by Client" .. player:client_id(), client);
-- end)

-- openminer:add_listener(EventType.OnBlockDigged, function(pos, player, world, client, server)
-- server:send_chat_message(0, "Block digged at " .. pos.x .. ";" .. pos.y .. ";" .. pos.z .. " by Client" .. player:client_id(), client);
-- end)

openminer:add_listener(EventType.OnBlockActivated, function(pos, block, player, world, client, server)
if block:string_id() == "default:portal" then
server:send_chat_message(0, "Swoosh! Changing dimension...", client);
Expand Down
1 change: 1 addition & 0 deletions source/server/lua/LuaCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ void LuaCore::addListener(LuaEventType eventType, const sol::function &listener)
void LuaCore::initUsertype(sol::state &lua) {
lua["EventType"] = lua.create_table_with(
"OnBlockPlaced", LuaEventType::OnBlockPlaced,
"OnBlockDigged", LuaEventType::OnBlockDigged,
"OnBlockActivated", LuaEventType::OnBlockActivated
);

Expand Down
1 change: 1 addition & 0 deletions source/server/lua/LuaCore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class ServerModLoader;

enum class LuaEventType {
OnBlockPlaced,
OnBlockDigged,
OnBlockActivated
};

Expand Down
2 changes: 2 additions & 0 deletions source/server/network/ServerCommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ void ServerCommandHandler::setupCallbacks() {
world.onBlockDigged(x, y, z, Registry::getInstance().getBlock(world.getBlock(x, y, z)), *player);
world.setBlock(x, y, z, 0);

m_scriptEngine.luaCore().onEvent(LuaEventType::OnBlockDigged, glm::ivec3{x, y, z}, *player, world, client, *this);

sf::Packet answer;
answer << Network::Command::BlockUpdate << x << y << z << u32(0);
m_server.sendToAllClients(answer);
Expand Down

0 comments on commit 9f09a0a

Please sign in to comment.