Merge branch 'main' into warnings
This commit is contained in:
commit
9ede50045b
@ -136,3 +136,24 @@ function on_hud_close(playerid: int)
|
|||||||
```
|
```
|
||||||
|
|
||||||
Called on world close (before saving)
|
Called on world close (before saving)
|
||||||
|
|
||||||
|
## *events* library
|
||||||
|
|
||||||
|
```lua
|
||||||
|
events.on(code: str, handler: function)
|
||||||
|
```
|
||||||
|
|
||||||
|
Adds an event handler by its code, not limited to the standard ones.
|
||||||
|
|
||||||
|
```lua
|
||||||
|
events.emit(code: str, args...) -> bool
|
||||||
|
```
|
||||||
|
|
||||||
|
Emits an event by code. If the event does not exist, nothing will happen.
|
||||||
|
The existence of an event is determined by the presence of handlers.
|
||||||
|
|
||||||
|
```lua
|
||||||
|
events.remove_by_prefix(packid: str)
|
||||||
|
```
|
||||||
|
|
||||||
|
Removes all events with the prefix `packid:`. When you exit the world, events from all packs are unloaded, including `core:`.
|
||||||
|
|||||||
@ -135,3 +135,24 @@ function on_hud_close(playerid: int)
|
|||||||
```
|
```
|
||||||
|
|
||||||
Вызывается при выходе из мира, перед его сохранением.
|
Вызывается при выходе из мира, перед его сохранением.
|
||||||
|
|
||||||
|
## Библиотека *events*
|
||||||
|
|
||||||
|
```lua
|
||||||
|
events.on(code: str, handler: function)
|
||||||
|
```
|
||||||
|
|
||||||
|
Добавляет обработчик события по его коду, не ограничиваясь стандартными.
|
||||||
|
|
||||||
|
```lua
|
||||||
|
events.emit(code: str, args...) -> bool
|
||||||
|
```
|
||||||
|
|
||||||
|
Генерирует событие по коду. Если событие не существует, ничего не произойдет.
|
||||||
|
Существование события определяется наличием обработчиков.
|
||||||
|
|
||||||
|
```lua
|
||||||
|
events.remove_by_prefix(packid: str)
|
||||||
|
```
|
||||||
|
|
||||||
|
Удаляет все события с префиксом `packid:`. Вы выходе из мира выгружаются события всех паков, включая `core:`.
|
||||||
|
|||||||
@ -9,22 +9,28 @@ function sleep(timesec)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- events
|
------------------------------------------------
|
||||||
|
------------------- Events ---------------------
|
||||||
|
------------------------------------------------
|
||||||
events = {
|
events = {
|
||||||
handlers = {}
|
handlers = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
function events.on(event, func)
|
function events.on(event, func)
|
||||||
-- why an array? length is always = 1
|
if events.handlers[event] == nil then
|
||||||
-- FIXME: temporary fixed
|
events.handlers[event] = {}
|
||||||
events.handlers[event] = {} -- events.handlers[event] or {}
|
end
|
||||||
table.insert(events.handlers[event], func)
|
table.insert(events.handlers[event], func)
|
||||||
end
|
end
|
||||||
|
|
||||||
function events.remove_by_prefix(prefix)
|
function events.remove_by_prefix(prefix)
|
||||||
for name, handlers in pairs(events.handlers) do
|
for name, handlers in pairs(events.handlers) do
|
||||||
if name:sub(1, #prefix) == prefix then
|
local actualname = name
|
||||||
events.handlers[name] = nil
|
if type(name) == 'table' then
|
||||||
|
actualname = name[1]
|
||||||
|
end
|
||||||
|
if actualname:sub(1, #prefix+1) == prefix..':' then
|
||||||
|
events.handlers[actualname] = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -34,11 +40,13 @@ function pack.unload(prefix)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function events.emit(event, ...)
|
function events.emit(event, ...)
|
||||||
result = nil
|
local result = nil
|
||||||
if events.handlers[event] then
|
local handlers = events.handlers[event]
|
||||||
for _, func in ipairs(events.handlers[event]) do
|
if handlers == nil then
|
||||||
result = result or func(...)
|
return nil
|
||||||
end
|
end
|
||||||
|
for _, func in ipairs(handlers) do
|
||||||
|
result = result or func(...)
|
||||||
end
|
end
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|||||||
@ -109,7 +109,8 @@ function string.explode(separator, str, withpattern)
|
|||||||
local current_pos = 1
|
local current_pos = 1
|
||||||
|
|
||||||
for i = 1, string_len(str) do
|
for i = 1, string_len(str) do
|
||||||
local start_pos, end_pos = string_find(str, separator, current_pos, not withpattern)
|
local start_pos, end_pos = string_find(
|
||||||
|
str, separator, current_pos, not withpattern)
|
||||||
if (not start_pos) then break end
|
if (not start_pos) then break end
|
||||||
ret[i] = string_sub(str, current_pos, start_pos - 1)
|
ret[i] = string_sub(str, current_pos, start_pos - 1)
|
||||||
current_pos = end_pos + 1
|
current_pos = end_pos + 1
|
||||||
@ -139,7 +140,7 @@ function string.formatted_time(seconds, format)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function string.replace(str, tofind, toreplace)
|
function string.replace(str, tofind, toreplace)
|
||||||
local tbl = string.Explode(tofind, str)
|
local tbl = string.explode(tofind, str)
|
||||||
if (tbl[1]) then return table.concat(tbl, toreplace) end
|
if (tbl[1]) then return table.concat(tbl, toreplace) end
|
||||||
return str
|
return str
|
||||||
end
|
end
|
||||||
|
|||||||
@ -164,32 +164,32 @@ void scripting::on_world_load(LevelController* controller) {
|
|||||||
|
|
||||||
auto L = lua::get_main_state();
|
auto L = lua::get_main_state();
|
||||||
for (auto& pack : scripting::engine->getContentPacks()) {
|
for (auto& pack : scripting::engine->getContentPacks()) {
|
||||||
lua::emit_event(L, pack.id + ".worldopen");
|
lua::emit_event(L, pack.id + ":.worldopen");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::on_world_tick() {
|
void scripting::on_world_tick() {
|
||||||
auto L = lua::get_main_state();
|
auto L = lua::get_main_state();
|
||||||
for (auto& pack : scripting::engine->getContentPacks()) {
|
for (auto& pack : scripting::engine->getContentPacks()) {
|
||||||
lua::emit_event(L, pack.id + ".worldtick");
|
lua::emit_event(L, pack.id + ":.worldtick");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::on_world_save() {
|
void scripting::on_world_save() {
|
||||||
auto L = lua::get_main_state();
|
auto L = lua::get_main_state();
|
||||||
for (auto& pack : scripting::engine->getContentPacks()) {
|
for (auto& pack : scripting::engine->getContentPacks()) {
|
||||||
lua::emit_event(L, pack.id + ".worldsave");
|
lua::emit_event(L, pack.id + ":.worldsave");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::on_world_quit() {
|
void scripting::on_world_quit() {
|
||||||
auto L = lua::get_main_state();
|
auto L = lua::get_main_state();
|
||||||
for (auto& pack : scripting::engine->getContentPacks()) {
|
for (auto& pack : scripting::engine->getContentPacks()) {
|
||||||
lua::emit_event(L, pack.id + ".worldquit");
|
lua::emit_event(L, pack.id + ":.worldquit");
|
||||||
}
|
}
|
||||||
|
|
||||||
lua::getglobal(L, "pack");
|
lua::getglobal(L, "pack");
|
||||||
for (auto& pack : scripting::engine->getContentPacks()) {
|
for (auto& pack : scripting::engine->getAllContentPacks()) {
|
||||||
lua::getfield(L, "unload");
|
lua::getfield(L, "unload");
|
||||||
lua::pushstring(L, pack.id);
|
lua::pushstring(L, pack.id);
|
||||||
lua::call_nothrow(L, 1);
|
lua::call_nothrow(L, 1);
|
||||||
@ -248,7 +248,7 @@ void scripting::on_block_placed(
|
|||||||
if (pack->worldfuncsset.onblockplaced) {
|
if (pack->worldfuncsset.onblockplaced) {
|
||||||
lua::emit_event(
|
lua::emit_event(
|
||||||
lua::get_main_state(),
|
lua::get_main_state(),
|
||||||
packid + ".blockplaced",
|
packid + ":.blockplaced",
|
||||||
world_event_args
|
world_event_args
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -280,7 +280,7 @@ void scripting::on_block_broken(
|
|||||||
if (pack->worldfuncsset.onblockbroken) {
|
if (pack->worldfuncsset.onblockbroken) {
|
||||||
lua::emit_event(
|
lua::emit_event(
|
||||||
lua::get_main_state(),
|
lua::get_main_state(),
|
||||||
packid + ".blockbroken",
|
packid + ":.blockbroken",
|
||||||
world_event_args
|
world_event_args
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -686,14 +686,14 @@ void scripting::load_world_script(
|
|||||||
int env = *senv;
|
int env = *senv;
|
||||||
lua::pop(lua::get_main_state(), load_script(env, "world", file));
|
lua::pop(lua::get_main_state(), load_script(env, "world", file));
|
||||||
register_event(env, "init", prefix + ".init");
|
register_event(env, "init", prefix + ".init");
|
||||||
register_event(env, "on_world_open", prefix + ".worldopen");
|
register_event(env, "on_world_open", prefix + ":.worldopen");
|
||||||
register_event(env, "on_world_tick", prefix + ".worldtick");
|
register_event(env, "on_world_tick", prefix + ":.worldtick");
|
||||||
register_event(env, "on_world_save", prefix + ".worldsave");
|
register_event(env, "on_world_save", prefix + ":.worldsave");
|
||||||
register_event(env, "on_world_quit", prefix + ".worldquit");
|
register_event(env, "on_world_quit", prefix + ":.worldquit");
|
||||||
funcsset.onblockplaced =
|
funcsset.onblockplaced =
|
||||||
register_event(env, "on_block_placed", prefix + ".blockplaced");
|
register_event(env, "on_block_placed", prefix + ":.blockplaced");
|
||||||
funcsset.onblockbroken =
|
funcsset.onblockbroken =
|
||||||
register_event(env, "on_block_broken", prefix + ".blockbroken");
|
register_event(env, "on_block_broken", prefix + ":.blockbroken");
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::load_layout_script(
|
void scripting::load_layout_script(
|
||||||
|
|||||||
@ -22,7 +22,7 @@ void scripting::on_frontend_init(Hud* hud) {
|
|||||||
for (auto& pack : engine->getContentPacks()) {
|
for (auto& pack : engine->getContentPacks()) {
|
||||||
lua::emit_event(
|
lua::emit_event(
|
||||||
lua::get_main_state(),
|
lua::get_main_state(),
|
||||||
pack.id + ".hudopen",
|
pack.id + ":.hudopen",
|
||||||
[&](lua::State* L) {
|
[&](lua::State* L) {
|
||||||
return lua::pushinteger(L, hud->getPlayer()->getId());
|
return lua::pushinteger(L, hud->getPlayer()->getId());
|
||||||
}
|
}
|
||||||
@ -34,7 +34,7 @@ void scripting::on_frontend_render() {
|
|||||||
for (auto& pack : engine->getContentPacks()) {
|
for (auto& pack : engine->getContentPacks()) {
|
||||||
lua::emit_event(
|
lua::emit_event(
|
||||||
lua::get_main_state(),
|
lua::get_main_state(),
|
||||||
pack.id + ".hudrender",
|
pack.id + ":.hudrender",
|
||||||
[&](lua::State* L) { return 0; }
|
[&](lua::State* L) { return 0; }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -44,7 +44,7 @@ void scripting::on_frontend_close() {
|
|||||||
for (auto& pack : engine->getContentPacks()) {
|
for (auto& pack : engine->getContentPacks()) {
|
||||||
lua::emit_event(
|
lua::emit_event(
|
||||||
lua::get_main_state(),
|
lua::get_main_state(),
|
||||||
pack.id + ".hudclose",
|
pack.id + ":.hudclose",
|
||||||
[&](lua::State* L) {
|
[&](lua::State* L) {
|
||||||
return lua::pushinteger(L, hud->getPlayer()->getId());
|
return lua::pushinteger(L, hud->getPlayer()->getId());
|
||||||
}
|
}
|
||||||
@ -62,8 +62,8 @@ void scripting::load_hud_script(
|
|||||||
|
|
||||||
lua::execute(lua::get_main_state(), env, src, file.u8string());
|
lua::execute(lua::get_main_state(), env, src, file.u8string());
|
||||||
|
|
||||||
register_event(env, "init", packid + ".init");
|
register_event(env, "init", packid + ":.init");
|
||||||
register_event(env, "on_hud_open", packid + ".hudopen");
|
register_event(env, "on_hud_open", packid + ":.hudopen");
|
||||||
register_event(env, "on_hud_render", packid + ".hudrender");
|
register_event(env, "on_hud_render", packid + ":.hudrender");
|
||||||
register_event(env, "on_hud_close", packid + ".hudclose");
|
register_event(env, "on_hud_close", packid + ":.hudclose");
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user