rename events + fix

This commit is contained in:
Xertis 2026-04-08 21:44:17 +03:00
parent 518d5d5ae4
commit acf8112644
6 changed files with 35 additions and 46 deletions

View file

@ -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. Called on block RMB click interaction. Prevents block placing if **true** returned.
```lua ```lua
function on_entity_spawned(uid) function on_entity_spawn(uid)
``` ```
Called after an entity is spawned. Called after an entity is spawned.
```lua ```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) ### Chunk Events (world.lua)

View file

@ -157,16 +157,16 @@ function on_block_interact(blockid, x, y, z, playerid) -> bool
Вызывается при нажатии на блок ПКМ. Предотвращает установку блоков, если возвращает `true` Вызывается при нажатии на блок ПКМ. Предотвращает установку блоков, если возвращает `true`
```lua ```lua
function on_entity_spawned(uid) function on_entity_spawn(uid)
``` ```
Вызывается после спавна сущности Вызывается после спавна сущности
```lua ```lua
function on_entity_despawned(uid) function on_entity_despawn(uid)
``` ```
Вызывается после удаления сущности, на момент вызова события сущность с переданным uid уже удалена Вызывается перед удалением сущности
```lua ```lua
function on_player_tick(playerid: int, tps: int) function on_player_tick(playerid: int, tps: int)

View file

@ -127,8 +127,8 @@ struct WorldFuncsSet {
bool onchunkremove; bool onchunkremove;
bool oninventoryopen; bool oninventoryopen;
bool oninventoryclosed; bool oninventoryclosed;
bool onentityspawned; bool onentityspawn;
bool onentitydespawned; bool onentitydespawn;
}; };
class ContentPackRuntime { class ContentPackRuntime {

View file

@ -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) { bool scripting::on_item_use(Player* player, const ItemDef& item) {
std::string name = item.name + ".use"; std::string name = item.name + ".use";
return lua::emit_event( return lua::emit_event(
@ -809,10 +781,10 @@ void scripting::load_world_script(
register_event(env, "on_inventory_open", prefix + ":.inventoryopen"); register_event(env, "on_inventory_open", prefix + ":.inventoryopen");
funcsset.oninventoryclosed = funcsset.oninventoryclosed =
register_event(env, "on_inventory_closed", prefix + ":.inventoryclosed"); register_event(env, "on_inventory_closed", prefix + ":.inventoryclosed");
funcsset.onentityspawned = funcsset.onentityspawn =
register_event(env, "on_entity_spawned", prefix + ":.entityspawned"); register_event(env, "on_entity_spawn", prefix + ":.entityspawn");
funcsset.onentitydespawned = funcsset.onentitydespawn =
register_event(env, "on_entity_despawned", prefix + ":.entitydespawned"); register_event(env, "on_entity_despawn", prefix + ":.entitydespawn");
} }
void scripting::load_content_script( void scripting::load_content_script(

View file

@ -112,9 +112,6 @@ namespace scripting {
void on_player_tick(Player* player, int tps); 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 /// @brief Called on RMB click with the item selected
/// @return true if prevents default action /// @return true if prevents default action
bool on_item_use(Player* player, const ItemDef& item); bool on_item_use(Player* player, const ItemDef& item);

View file

@ -6,6 +6,7 @@
#include "objects/Entity.hpp" #include "objects/Entity.hpp"
#include "objects/Player.hpp" #include "objects/Player.hpp"
#include "util/stringutil.hpp" #include "util/stringutil.hpp"
#include "content/ContentControl.hpp"
using namespace scripting; using namespace scripting;
@ -130,7 +131,16 @@ void scripting::on_entity_spawn(
create_component(L, -1, *component, args, saved); 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( static void process_entity_callback(
@ -172,14 +182,24 @@ void scripting::on_entity_despawn(const Entity& entity) {
process_entity_callback( process_entity_callback(
entity, "on_despawn", &EntityFuncsSet::on_despawn, nullptr entity, "on_despawn", &EntityFuncsSet::on_despawn, nullptr
); );
auto L = lua::get_main_state(); auto L = lua::get_main_state();
entityid_t uid = entity.getUID(); 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::get_from(L, "stdcomp", "remove_Entity", true);
lua::pushinteger(L, uid); lua::pushinteger(L, uid);
lua::call(L, 1, 0); lua::call(L, 1, 0);
scripting::on_entity_despawned(uid);
} }
void scripting::on_entity_grounded(const Entity& entity, float force) { void scripting::on_entity_grounded(const Entity& entity, float force) {