diff --git a/doc/en/scripting/events.md b/doc/en/scripting/events.md index f475dc1c1..75180edc3 100644 --- a/doc/en/scripting/events.md +++ b/doc/en/scripting/events.md @@ -153,15 +153,15 @@ function on_block_interact(blockid, x, y, z, playerid) -> bool Called on block RMB click interaction. Prevents block placing if **true** returned. ```lua -function on_entity_spawned(uid) +function on_entity_spawn(uid) ``` Called after an entity is spawned. ```lua -function on_entity_despawned(uid) +function on_entity_despawn(uid) ``` -Called after an entity is removed. At the time of the call, the entity with the given uid has already been deleted. +Called before an entity is despawned. ### Chunk Events (world.lua) diff --git a/doc/ru/scripting/events.md b/doc/ru/scripting/events.md index fa20e4030..ff4bfb456 100644 --- a/doc/ru/scripting/events.md +++ b/doc/ru/scripting/events.md @@ -157,16 +157,16 @@ function on_block_interact(blockid, x, y, z, playerid) -> bool Вызывается при нажатии на блок ПКМ. Предотвращает установку блоков, если возвращает `true` ```lua -function on_entity_spawned(uid) +function on_entity_spawn(uid) ``` Вызывается после спавна сущности ```lua -function on_entity_despawned(uid) +function on_entity_despawn(uid) ``` -Вызывается после удаления сущности, на момент вызова события сущность с переданным uid уже удалена +Вызывается перед удалением сущности ```lua function on_player_tick(playerid: int, tps: int) diff --git a/src/content/ContentPack.hpp b/src/content/ContentPack.hpp index bd0ccc79f..06a8cf4dc 100644 --- a/src/content/ContentPack.hpp +++ b/src/content/ContentPack.hpp @@ -127,8 +127,8 @@ struct WorldFuncsSet { bool onchunkremove; bool oninventoryopen; bool oninventoryclosed; - bool onentityspawned; - bool onentitydespawned; + bool onentityspawn; + bool onentitydespawn; }; class ContentPackRuntime { diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index 232c72796..341470c17 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -555,34 +555,6 @@ void scripting::on_player_tick(Player* player, int tps) { } } -void scripting::on_entity_spawned(entityid_t eid) { - auto L = lua::get_main_state(); - for (auto& pack : content_control->getAllContentPacks()) { - lua::emit_event( - L, - pack.id + ":.entityspawned", - [&](lua::State* L) { - lua::pushinteger(L, eid); - return 1; - } - ); - } -} - -void scripting::on_entity_despawned(entityid_t eid) { - auto L = lua::get_main_state(); - for (auto& pack : content_control->getAllContentPacks()) { - lua::emit_event( - L, - pack.id + ":.entitydespawned", - [&](lua::State* L) { - lua::pushinteger(L, eid); - return 1; - } - ); - } -} - bool scripting::on_item_use(Player* player, const ItemDef& item) { std::string name = item.name + ".use"; return lua::emit_event( @@ -809,10 +781,10 @@ void scripting::load_world_script( register_event(env, "on_inventory_open", prefix + ":.inventoryopen"); funcsset.oninventoryclosed = register_event(env, "on_inventory_closed", prefix + ":.inventoryclosed"); - funcsset.onentityspawned = - register_event(env, "on_entity_spawned", prefix + ":.entityspawned"); - funcsset.onentitydespawned = - register_event(env, "on_entity_despawned", prefix + ":.entitydespawned"); + funcsset.onentityspawn = + register_event(env, "on_entity_spawn", prefix + ":.entityspawn"); + funcsset.onentitydespawn = + register_event(env, "on_entity_despawn", prefix + ":.entitydespawn"); } void scripting::load_content_script( diff --git a/src/logic/scripting/scripting.hpp b/src/logic/scripting/scripting.hpp index 3d9737c6a..9d29528b8 100644 --- a/src/logic/scripting/scripting.hpp +++ b/src/logic/scripting/scripting.hpp @@ -112,9 +112,6 @@ namespace scripting { void on_player_tick(Player* player, int tps); - void on_entity_spawned(entityid_t eid); - void on_entity_despawned(entityid_t eid); - /// @brief Called on RMB click with the item selected /// @return true if prevents default action bool on_item_use(Player* player, const ItemDef& item); diff --git a/src/logic/scripting/scripting_entities.cpp b/src/logic/scripting/scripting_entities.cpp index 0be289038..afed77e85 100644 --- a/src/logic/scripting/scripting_entities.cpp +++ b/src/logic/scripting/scripting_entities.cpp @@ -6,6 +6,7 @@ #include "objects/Entity.hpp" #include "objects/Player.hpp" #include "util/stringutil.hpp" +#include "content/ContentControl.hpp" using namespace scripting; @@ -130,7 +131,16 @@ void scripting::on_entity_spawn( create_component(L, -1, *component, args, saved); } - scripting::on_entity_spawned(eid); + for (auto& pack : scripting::content_control->getAllContentPacks()) { + lua::emit_event( + L, + pack.id + ":.entityspawn", + [&](lua::State* L) { + lua::pushinteger(L, eid); + return 1; + } + ); + } } static void process_entity_callback( @@ -172,14 +182,24 @@ void scripting::on_entity_despawn(const Entity& entity) { process_entity_callback( entity, "on_despawn", &EntityFuncsSet::on_despawn, nullptr ); + auto L = lua::get_main_state(); entityid_t uid = entity.getUID(); + for (auto& pack : content_control->getAllContentPacks()) { + lua::emit_event( + L, + pack.id + ":.entitydespawn", + [&](lua::State* L) { + lua::pushinteger(L, uid); + return 1; + } + ); + } + lua::get_from(L, "stdcomp", "remove_Entity", true); lua::pushinteger(L, uid); lua::call(L, 1, 0); - - scripting::on_entity_despawned(uid); } void scripting::on_entity_grounded(const Entity& entity, float force) {