[LuaCore] New event 'PlayerConnected' added.

This commit is contained in:
Quentin Bazin 2020-05-22 05:34:02 +02:00
parent b789fc3d9b
commit b34480565c
5 changed files with 62 additions and 50 deletions

View File

@ -18,15 +18,15 @@ Adds a listener to a specific type of event.
Example:
```lua
openminer:add_listener(EventType.OnBlockPlaced, function(pos, block, player, world, client, server)
openminer:add_listener(Event.BlockPlaced, function(pos, block, player, world, client, server)
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, block, player, world, client, server)
openminer:add_listener(Event.BlockDigged, function(pos, block, 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)
openminer:add_listener(Event.BlockActivated, function(pos, block, player, world, client, server)
if block:string_id() == "default:portal" then
server:send_chat_message(0, "Swoosh! Changing dimension...", client);
end
@ -35,7 +35,8 @@ end)
Possible events:
- `OnBlockPlaced`: `funcion(pos, block, player, world, client, server)`
- `OnBlockDigged`: `funcion(pos, block, player, world, client, server)`
- `OnBlockActivated`: `function(pos, block, player, world, client, server)`
- `BlockPlaced`: `funcion(pos, block, player, world, client, server)`
- `BlockDigged`: `funcion(pos, block, player, world, client, server)`
- `BlockActivated`: `function(pos, block, player, world, client, server)`
- `PlayerConnected`: `function(pos, player, client, server)`

View File

@ -37,46 +37,54 @@ dofile("trees.lua")
dofile("biomes.lua")
dofile("dimensions.lua")
-- openminer:add_listener(EventType.OnBlockPlaced, function(pos, block, player, world, client, server)
-- server:send_chat_message(0, "Block placed at " .. pos.x .. ";" .. pos.y .. ";" .. pos.z .. " by Client" .. player:client_id(), client);
-- openminer:add_listener(Event.BlockPlaced, function(pos, block, player, world, client, server)
-- 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, block, player, world, client, server)
-- server:send_chat_message(0, "Block digged at " .. pos.x .. ";" .. pos.y .. ";" .. pos.z .. " by Client" .. player:client_id(), client);
-- openminer:add_listener(Event.BlockDigged, function(pos, block, 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)
openminer:add_listener(Event.BlockActivated, function(pos, block, player, world, client, server)
if block:string_id() == "default:portal" then
server:send_chat_message(0, "Swoosh! Changing dimension...", client);
server:send_chat_message(0, "Swoosh! Changing dimension...", client)
end
end)
function init(player)
openminer:add_listener(Event.PlayerConnected, function(pos, player, client, server)
local starting_items = {
{"default:workbench", 1},
{"default:dirt", 64},
{"default:grass", 64},
{"default:stone", 64},
{"default:glass", 64},
{"default:glowstone", 64},
{"default:furnace", 1},
{"default:stone_pickaxe", 1},
{"default:stone_axe", 1},
{"default:oak_wood", 64},
{"default:oak_planks", 64},
{"default:cobblestone", 64},
{"default:stick", 64},
{"default:stone_hoe", 1},
{"default:stone_shovel", 1},
{"default:iron_ore", 64},
{"default:coal", 64},
{"default:iron_ingot", 64},
{"default:gold_ingot", 64},
{"default:diamond", 64}
}
local player_inv = player:inventory()
for _, v in ipairs(starting_items) do
player_inv:add_stack(v[1], v[2])
end
player_inv:add_stack("default:workbench", 1);
player_inv:add_stack("default:dirt", 64);
player_inv:add_stack("default:grass", 64);
player_inv:add_stack("default:stone", 64);
player_inv:add_stack("default:glass", 64);
player_inv:add_stack("default:glowstone", 64);
player_inv:add_stack("default:furnace", 1);
player_inv:add_stack("default:stone_pickaxe", 1);
player_inv:add_stack("default:stone_axe", 1);
player_inv:add_stack("default:oak_wood", 64);
player_inv:add_stack("default:oak_planks", 64);
player_inv:add_stack("default:cobblestone", 64);
player_inv:add_stack("default:stick", 64);
player_inv:add_stack("default:stone_hoe", 1);
player_inv:add_stack("default:stone_shovel", 1);
player_inv:add_stack("default:iron_ore", 64);
player_inv:add_stack("default:coal", 64);
player_inv:add_stack("default:iron_ingot", 64);
player_inv:add_stack("default:gold_ingot", 64);
player_inv:add_stack("default:diamond", 64);
end
server:send_chat_message(0, "Welcome to OpenMiner!", client)
end)
local modpath = mod:path()
@ -175,12 +183,12 @@ mod:entity {
},
on_collision = function(entity, player, server)
mods["default"]:give_item_stack(player, entity:item_stack());
mods["default"]:give_item_stack(player, entity:item_stack())
mods["default"]:despawn_entity(entity)
end,
}
openminer:add_listener(EventType.OnBlockDigged, function(pos, block, player, world, client, server)
openminer:add_listener(Event.BlockDigged, function(pos, block, player, world, client, server)
if ServerConfig.useItemDrops then
mods["default"]:spawn_entity("default:item_drop", {
position = {pos.x + 0.5, pos.y + 0.5, pos.z + 0.5},

View File

@ -35,10 +35,12 @@ 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
lua["Event"] = lua.create_table_with(
"BlockPlaced", LuaEventType::BlockPlaced,
"BlockDigged", LuaEventType::BlockDigged,
"BlockActivated", LuaEventType::BlockActivated,
"PlayerConnected", LuaEventType::PlayerConnected
);
lua["ServerConfig"] = lua.create_table_with(

View File

@ -36,9 +36,11 @@ class Registry;
class ServerModLoader;
enum class LuaEventType {
OnBlockPlaced,
OnBlockDigged,
OnBlockActivated
BlockPlaced,
BlockDigged,
BlockActivated,
PlayerConnected,
};
class LuaCore {

View File

@ -196,8 +196,7 @@ void ServerCommandHandler::setupCallbacks() {
auto &player = m_players.addPlayer(client);
player.setPosition(m_spawnPosition.x, m_spawnPosition.y, m_spawnPosition.z);
// FIXME: Find a better way to give starting items
m_scriptEngine.lua()["init"](player);
m_scriptEngine.luaCore().onEvent(LuaEventType::PlayerConnected, glm::ivec3{m_spawnPosition.x, m_spawnPosition.y, m_spawnPosition.z}, player, client, *this);
Network::Packet invPacket;
invPacket << Network::Command::PlayerInvUpdate << client.id;
@ -266,7 +265,7 @@ void ServerCommandHandler::setupCallbacks() {
world.setBlock(x, y, z, block & 0xffff);
const Block &blockDef = Registry::getInstance().getBlock(block & 0xffff);
m_scriptEngine.luaCore().onEvent(LuaEventType::OnBlockPlaced, glm::ivec3{x, y, z}, blockDef, *player, world, client, *this);
m_scriptEngine.luaCore().onEvent(LuaEventType::BlockPlaced, glm::ivec3{x, y, z}, blockDef, *player, world, client, *this);
Network::Packet answer;
answer << Network::Command::BlockUpdate << x << y << z << block;
@ -286,7 +285,7 @@ void ServerCommandHandler::setupCallbacks() {
const Block &blockDef = Registry::getInstance().getBlock(world.getBlock(x, y, z));
world.setBlock(x, y, z, 0);
m_scriptEngine.luaCore().onEvent(LuaEventType::OnBlockDigged, glm::ivec3{x, y, z}, blockDef, *player, world, client, *this);
m_scriptEngine.luaCore().onEvent(LuaEventType::BlockDigged, glm::ivec3{x, y, z}, blockDef, *player, world, client, *this);
Network::Packet answer;
answer << Network::Command::BlockUpdate << x << y << z << u32(0);
@ -341,7 +340,7 @@ void ServerCommandHandler::setupCallbacks() {
bool hasBeenActivated = block.onBlockActivated({x, y, z}, *player, world, client, *this, screenWidth, screenHeight, guiScale);
if (hasBeenActivated)
m_scriptEngine.luaCore().onEvent(LuaEventType::OnBlockActivated, glm::ivec3{x, y, z}, block, *player, world, client, *this);
m_scriptEngine.luaCore().onEvent(LuaEventType::BlockActivated, glm::ivec3{x, y, z}, block, *player, world, client, *this);
}
else
gkError() << ("Failed to activate block using player " + std::to_string(client.id) + ": Player not found").c_str();