]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/script/lua_api/l_object.cpp
Add ItemStack:get_description() to get tooltip (#8847)
[dragonfireclient.git] / src / script / lua_api / l_object.cpp
index e673778e9d711790174703e65adf4e094b3a5565..be2172f1b853d5cd4a80e37bb5ef4cdce244d43c 100644 (file)
@@ -170,8 +170,8 @@ int ObjectRef::l_punch(lua_State *L)
        ObjectRef *puncher_ref = checkobject(L, 2);
        ServerActiveObject *co = getobject(ref);
        ServerActiveObject *puncher = getobject(puncher_ref);
-       if (co == NULL) return 0;
-       if (puncher == NULL) return 0;
+       if (!co || !puncher)
+               return 0;
        v3f dir;
        if (lua_type(L, 5) != LUA_TTABLE)
                dir = co->getBasePosition() - puncher->getBasePosition();
@@ -183,8 +183,8 @@ int ObjectRef::l_punch(lua_State *L)
        ToolCapabilities toolcap = read_tool_capabilities(L, 4);
        dir.normalize();
 
-       s16 src_original_hp = co->getHP();
-       s16 dst_origin_hp = puncher->getHP();
+       u16 src_original_hp = co->getHP();
+       u16 dst_origin_hp = puncher->getHP();
 
        // Do it
        co->punch(dir, &toolcap, puncher, time_from_last_punch);
@@ -192,7 +192,8 @@ int ObjectRef::l_punch(lua_State *L)
        // If the punched is a player, and its HP changed
        if (src_original_hp != co->getHP() &&
                        co->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
-               getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co, PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, puncher));
+               getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co,
+                               PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, puncher));
        }
 
        // If the puncher is a player, and its HP changed
@@ -257,6 +258,9 @@ int ObjectRef::l_set_hp(lua_State *L)
        if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
                getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co, reason);
 
+       if (reason.hasLuaReference())
+               luaL_unref(L, LUA_REGISTRYINDEX, reason.lua_reference);
+
        // Return
        return 0;
 }
@@ -304,8 +308,9 @@ int ObjectRef::l_get_wield_list(lua_State *L)
        NO_MAP_LOCK_REQUIRED;
        ObjectRef *ref = checkobject(L, 1);
        ServerActiveObject *co = getobject(ref);
-       if (co == NULL) return 0;
-       // Do it
+       if (!co)
+               return 0;
+
        lua_pushstring(L, co->getWieldList().c_str());
        return 1;
 }
@@ -316,8 +321,9 @@ int ObjectRef::l_get_wield_index(lua_State *L)
        NO_MAP_LOCK_REQUIRED;
        ObjectRef *ref = checkobject(L, 1);
        ServerActiveObject *co = getobject(ref);
-       if (co == NULL) return 0;
-       // Do it
+       if (!co)
+               return 0;
+
        lua_pushinteger(L, co->getWieldIndex() + 1);
        return 1;
 }
@@ -328,12 +334,12 @@ int ObjectRef::l_get_wielded_item(lua_State *L)
        NO_MAP_LOCK_REQUIRED;
        ObjectRef *ref = checkobject(L, 1);
        ServerActiveObject *co = getobject(ref);
-       if (co == NULL) {
+       if (!co) {
                // Empty ItemStack
                LuaItemStack::create(L, ItemStack());
                return 1;
        }
-       // Do it
+
        LuaItemStack::create(L, co->getWieldedItem());
        return 1;
 }
@@ -580,6 +586,24 @@ int ObjectRef::l_get_eye_offset(lua_State *L)
        return 2;
 }
 
+// send_mapblock(self, pos)
+int ObjectRef::l_send_mapblock(lua_State *L)
+{
+       NO_MAP_LOCK_REQUIRED;
+       ObjectRef *ref = checkobject(L, 1);
+
+       RemotePlayer *player = getplayer(ref);
+       if (!player)
+               return 0;
+       v3s16 p = read_v3s16(L, 2);
+
+       session_t peer_id = player->getPeerId();
+       bool r = getServer(L)->SendBlock(peer_id, p);
+
+       lua_pushboolean(L, r);
+       return 1;
+}
+
 // set_animation_frame_speed(self, frame_speed)
 int ObjectRef::l_set_animation_frame_speed(lua_State *L)
 {
@@ -728,17 +752,14 @@ int ObjectRef::l_set_properties(lua_State *L)
        NO_MAP_LOCK_REQUIRED;
        ObjectRef *ref = checkobject(L, 1);
        ServerActiveObject *co = getobject(ref);
-       if (co == NULL) return 0;
+       if (!co)
+               return 0;
+
        ObjectProperties *prop = co->accessObjectProperties();
        if (!prop)
                return 0;
-       read_object_properties(L, 2, prop, getServer(L)->idef());
-       if (prop->hp_max < co->getHP()) {
-               PlayerHPChangeReason reason(PlayerHPChangeReason::SET_HP);
-               co->setHP(prop->hp_max, reason);
-               if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
-                       getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co, reason);
-       }
+
+       read_object_properties(L, 2, co, prop, getServer(L)->idef());
        co->notifyObjectPropertiesModified();
        return 0;
 }
@@ -1071,6 +1092,27 @@ int ObjectRef::l_get_player_velocity(lua_State *L)
        return 1;
 }
 
+// add_player_velocity(self, {x=num, y=num, z=num})
+int ObjectRef::l_add_player_velocity(lua_State *L)
+{
+       NO_MAP_LOCK_REQUIRED;
+       ObjectRef *ref = checkobject(L, 1);
+       v3f vel = checkFloatPos(L, 2);
+
+       RemotePlayer *player = getplayer(ref);
+       PlayerSAO *co = getplayersao(ref);
+       if (!player || !co)
+               return 0;
+
+       session_t peer_id = player->getPeerId();
+       if (peer_id == PEER_ID_INEXISTENT)
+               return 0;
+       // Do it
+       co->setMaxSpeedOverride(vel);
+       getServer(L)->SendPlayerSpeed(peer_id, vel);
+       return 0;
+}
+
 // get_look_dir(self)
 int ObjectRef::l_get_look_dir(lua_State *L)
 {
@@ -1236,6 +1278,9 @@ int ObjectRef::l_get_breath(lua_State *L)
 // set_attribute(self, attribute, value)
 int ObjectRef::l_set_attribute(lua_State *L)
 {
+       log_deprecated(L,
+               "Deprecated call to set_attribute, use MetaDataRef methods instead.");
+
        ObjectRef *ref = checkobject(L, 1);
        PlayerSAO* co = getplayersao(ref);
        if (co == NULL)
@@ -1254,6 +1299,9 @@ int ObjectRef::l_set_attribute(lua_State *L)
 // get_attribute(self, attribute)
 int ObjectRef::l_get_attribute(lua_State *L)
 {
+       log_deprecated(L,
+               "Deprecated call to get_attribute, use MetaDataRef methods instead.");
+
        ObjectRef *ref = checkobject(L, 1);
        PlayerSAO* co = getplayersao(ref);
        if (co == NULL)
@@ -1904,6 +1952,7 @@ luaL_Reg ObjectRef::methods[] = {
        luamethod(ObjectRef, is_player_connected),
        luamethod(ObjectRef, get_player_name),
        luamethod(ObjectRef, get_player_velocity),
+       luamethod(ObjectRef, add_player_velocity),
        luamethod(ObjectRef, get_look_dir),
        luamethod(ObjectRef, get_look_pitch),
        luamethod(ObjectRef, get_look_yaw),
@@ -1948,5 +1997,6 @@ luaL_Reg ObjectRef::methods[] = {
        luamethod(ObjectRef, get_local_animation),
        luamethod(ObjectRef, set_eye_offset),
        luamethod(ObjectRef, get_eye_offset),
+       luamethod(ObjectRef, send_mapblock),
        {0,0}
 };