Event queue
This commit is contained in:
@@ -105,25 +105,19 @@ void scripting::on_world_load(Level* level, BlocksController* blocks) {
|
||||
load_script("world.lua");
|
||||
|
||||
for (auto& pack : scripting::engine->getContentPacks()) {
|
||||
if (state->getglobal(pack.id+".worldopen")) {
|
||||
state->callNoThrow(0);
|
||||
}
|
||||
emit_event(pack.id + ".worldopen");
|
||||
}
|
||||
}
|
||||
|
||||
void scripting::on_world_save() {
|
||||
for (auto& pack : scripting::engine->getContentPacks()) {
|
||||
if (state->getglobal(pack.id+".worldsave")) {
|
||||
state->callNoThrow(0);
|
||||
}
|
||||
emit_event(pack.id + ".worldsave");
|
||||
}
|
||||
}
|
||||
|
||||
void scripting::on_world_quit() {
|
||||
for (auto& pack : scripting::engine->getContentPacks()) {
|
||||
if (state->getglobal(pack.id+".worldquit")) {
|
||||
state->callNoThrow(0);
|
||||
}
|
||||
emit_event(pack.id + ".worldquit");
|
||||
}
|
||||
if (state->getglobal("__scripts_cleanup")) {
|
||||
state->callNoThrow(0);
|
||||
@@ -134,109 +128,97 @@ void scripting::on_world_quit() {
|
||||
}
|
||||
|
||||
void scripting::on_blocks_tick(const Block* block, int tps) {
|
||||
std::string name = block->name+".blockstick";
|
||||
if (state->getglobal(name)) {
|
||||
std::string name = block->name + ".blockstick";
|
||||
emit_event(name, [tps] (lua::LuaState* state) {
|
||||
state->pushinteger(tps);
|
||||
state->callNoThrow(1);
|
||||
}
|
||||
return 1;
|
||||
});
|
||||
}
|
||||
|
||||
void scripting::update_block(const Block* block, int x, int y, int z) {
|
||||
std::string name = block->name+".update";
|
||||
if (state->getglobal(name)) {
|
||||
std::string name = block->name + ".update";
|
||||
emit_event(name, [x, y, z] (lua::LuaState* state) {
|
||||
state->pushivec3(x, y, z);
|
||||
state->callNoThrow(3);
|
||||
}
|
||||
return 3;
|
||||
});
|
||||
}
|
||||
|
||||
void scripting::random_update_block(const Block* block, int x, int y, int z) {
|
||||
std::string name = block->name+".randupdate";
|
||||
if (state->getglobal(name)) {
|
||||
std::string name = block->name + ".randupdate";
|
||||
emit_event(name, [x, y, z] (lua::LuaState* state) {
|
||||
state->pushivec3(x, y, z);
|
||||
state->callNoThrow(3);
|
||||
}
|
||||
return 3;
|
||||
});
|
||||
}
|
||||
|
||||
void scripting::on_block_placed(Player* player, const Block* block, int x, int y, int z) {
|
||||
std::string name = block->name+".placed";
|
||||
if (state->getglobal(name)) {
|
||||
std::string name = block->name + ".placed";
|
||||
emit_event(name, [x, y, z, player] (lua::LuaState* state) {
|
||||
state->pushivec3(x, y, z);
|
||||
state->pushinteger(player->getId());
|
||||
state->callNoThrow(4);
|
||||
}
|
||||
return 4;
|
||||
});
|
||||
}
|
||||
|
||||
void scripting::on_block_broken(Player* player, const Block* block, int x, int y, int z) {
|
||||
std::string name = block->name+".broken";
|
||||
if (state->getglobal(name)) {
|
||||
std::string name = block->name + ".broken";
|
||||
emit_event(name, [x, y, z, player] (lua::LuaState* state) {
|
||||
state->pushivec3(x, y, z);
|
||||
state->pushinteger(player->getId());
|
||||
state->callNoThrow(4);
|
||||
}
|
||||
return 4;
|
||||
});
|
||||
}
|
||||
|
||||
bool scripting::on_block_interact(Player* player, const Block* block, int x, int y, int z) {
|
||||
std::string name = block->name+".interact";
|
||||
if (state->getglobal(name)) {
|
||||
std::string name = block->name + ".interact";
|
||||
return emit_event(name, [x, y, z, player] (lua::LuaState* state) {
|
||||
state->pushivec3(x, y, z);
|
||||
state->pushinteger(player->getId());
|
||||
if (state->callNoThrow(4)) {
|
||||
return state->toboolean(-1);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return 4;
|
||||
});
|
||||
}
|
||||
|
||||
bool scripting::on_item_use(Player* player, const ItemDef* item) {
|
||||
std::string name = item->name+".use";
|
||||
if (state->getglobal(name)) {
|
||||
std::string name = item->name + ".use";
|
||||
return emit_event(name, [player] (lua::LuaState* state) {
|
||||
state->pushinteger(player->getId());
|
||||
if (state->callNoThrow(1)) {
|
||||
return state->toboolean(-1);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return 1;
|
||||
});
|
||||
}
|
||||
|
||||
bool scripting::on_item_use_on_block(Player* player, const ItemDef* item, int x, int y, int z) {
|
||||
std::string name = item->name+".useon";
|
||||
if (state->getglobal(name)) {
|
||||
std::string name = item->name + ".useon";
|
||||
return emit_event(name, [x, y, z, player] (lua::LuaState* state) {
|
||||
state->pushivec3(x, y, z);
|
||||
state->pushinteger(player->getId());
|
||||
if (state->callNoThrow(4)) {
|
||||
return state->toboolean(-1);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return 4;
|
||||
});
|
||||
}
|
||||
|
||||
bool scripting::on_item_break_block(Player* player, const ItemDef* item, int x, int y, int z) {
|
||||
std::string name = item->name+".blockbreakby";
|
||||
if (state->getglobal(name)) {
|
||||
std::string name = item->name + ".blockbreakby";
|
||||
return emit_event(name, [x, y, z, player] (lua::LuaState* state) {
|
||||
state->pushivec3(x, y, z);
|
||||
state->pushinteger(player->getId());
|
||||
if (state->callNoThrow(4)) {
|
||||
return state->toboolean(-1);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return 4;
|
||||
});
|
||||
}
|
||||
|
||||
void scripting::on_ui_open(UiDocument* layout, Inventory* inventory, glm::ivec3 blockcoord) {
|
||||
std::string name = layout->getId()+".open";
|
||||
if (state->getglobal(name)) {
|
||||
std::string name = layout->getId() + ".open";
|
||||
emit_event(name, [inventory, blockcoord] (lua::LuaState* state) {
|
||||
state->pushinteger(inventory == nullptr ? 0 : inventory->getId());
|
||||
state->pushivec3(blockcoord.x, blockcoord.y, blockcoord.z);
|
||||
state->callNoThrow(4);
|
||||
}
|
||||
return 4;
|
||||
});
|
||||
}
|
||||
|
||||
void scripting::on_ui_close(UiDocument* layout, Inventory* inventory) {
|
||||
std::string name = layout->getId()+".close";
|
||||
if (state->getglobal(name)) {
|
||||
state->pushinteger(inventory->getId());
|
||||
state->callNoThrow(1);
|
||||
}
|
||||
std::string name = layout->getId() + ".close";
|
||||
emit_event(name, [inventory] (lua::LuaState* state) {
|
||||
state->pushinteger(inventory == nullptr ? 0 : inventory->getId());
|
||||
return 1;
|
||||
});
|
||||
}
|
||||
|
||||
bool scripting::register_event(int env, const std::string& name, const std::string& id) {
|
||||
@@ -244,17 +226,32 @@ bool scripting::register_event(int env, const std::string& name, const std::stri
|
||||
state->pushglobals();
|
||||
}
|
||||
if (state->getfield(name)) {
|
||||
state->pop();
|
||||
state->getglobal("events");
|
||||
state->getfield("on");
|
||||
state->pushstring(id);
|
||||
state->getfield(name, -4);
|
||||
state->callNoThrow(2);
|
||||
state->pop();
|
||||
|
||||
// remove previous name
|
||||
state->pushnil();
|
||||
state->setfield(name, -3);
|
||||
// add new global name
|
||||
state->setglobal(id);
|
||||
state->pop();
|
||||
state->setfield(name);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool scripting::emit_event(const std::string &name, std::function<int(lua::LuaState *)> args) {
|
||||
state->getglobal("events");
|
||||
state->getfield("emit");
|
||||
state->pushstring(name);
|
||||
state->callNoThrow(args(state) + 1);
|
||||
bool result = state->toboolean(-1);
|
||||
state->pop(2);
|
||||
return result;
|
||||
}
|
||||
|
||||
void scripting::load_block_script(int env, std::string prefix, fs::path file, block_funcs_set& funcsset) {
|
||||
std::string src = files::read_string(file);
|
||||
std::cout << "loading script " << file.u8string() << std::endl;
|
||||
|
||||
Reference in New Issue
Block a user