Skip to content

Commit

Permalink
Add detailed ItemStack information to dropped items
Browse files Browse the repository at this point in the history
  • Loading branch information
juhdanad authored and sofar committed Apr 9, 2017
1 parent c444628 commit 5c52b3c
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions builtin/game/item_entity.lua
Expand Up @@ -66,6 +66,7 @@ core.register_entity(":__builtin:item", {
visual_size = {x = s, y = s},
collisionbox = {-c, -c, -c, c, c, c},
automatic_rotate = math.pi * 0.5,
wield_item = itemstring,
}
self.object:set_properties(prop)
end,
Expand Down
2 changes: 2 additions & 0 deletions src/object_properties.cpp
Expand Up @@ -117,6 +117,7 @@ void ObjectProperties::serialize(std::ostream &os) const
writeARGB8(os, nametag_color);
writeF1000(os, automatic_face_movement_max_rotation_per_sec);
os << serializeString(infotext);
os << serializeString(wield_item);

// Add stuff only at the bottom.
// Never remove anything, because we don't want new versions of this
Expand Down Expand Up @@ -159,6 +160,7 @@ void ObjectProperties::deSerialize(std::istream &is)
nametag_color = readARGB8(is);
automatic_face_movement_max_rotation_per_sec = readF1000(is);
infotext = deSerializeString(is);
wield_item = deSerializeString(is);
}catch(SerializationError &e){}
}
else
Expand Down
2 changes: 2 additions & 0 deletions src/object_properties.h
Expand Up @@ -52,6 +52,8 @@ struct ObjectProperties
video::SColor nametag_color;
f32 automatic_face_movement_max_rotation_per_sec;
std::string infotext;
//! For dropped items, this contains item information.
std::string wield_item;

ObjectProperties();
std::string dump();
Expand Down
8 changes: 7 additions & 1 deletion src/script/common/c_content.cpp
Expand Up @@ -118,7 +118,7 @@ ItemDefinition read_item_definition(lua_State* L,int index,

/******************************************************************************/
void read_object_properties(lua_State *L, int index,
ObjectProperties *prop)
ObjectProperties *prop, IItemDefManager *idef)
{
if(index < 0)
index = lua_gettop(L) + 1 + index;
Expand Down Expand Up @@ -216,6 +216,10 @@ void read_object_properties(lua_State *L, int index,
}
lua_pop(L, 1);
getstringfield(L, -1, "infotext", prop->infotext);
lua_getfield(L, -1, "wield_item");
if (!lua_isnil(L, -1))
prop->wield_item = read_item(L, -1, idef).getItemString();
lua_pop(L, 1);
}

/******************************************************************************/
Expand Down Expand Up @@ -284,6 +288,8 @@ void push_object_properties(lua_State *L, ObjectProperties *prop)
lua_setfield(L, -2, "automatic_face_movement_max_rotation_per_sec");
lua_pushlstring(L, prop->infotext.c_str(), prop->infotext.size());
lua_setfield(L, -2, "infotext");
lua_pushlstring(L, prop->wield_item.c_str(), prop->wield_item.size());
lua_setfield(L, -2, "wield_item");
}

/******************************************************************************/
Expand Down
3 changes: 2 additions & 1 deletion src/script/common/c_content.h
Expand Up @@ -89,7 +89,8 @@ void push_tool_capabilities (lua_State *L,
ItemDefinition read_item_definition (lua_State *L, int index,
ItemDefinition default_def);
void read_object_properties (lua_State *L, int index,
ObjectProperties *prop);
ObjectProperties *prop,
IItemDefManager *idef);
void push_object_properties (lua_State *L,
ObjectProperties *prop);

Expand Down
5 changes: 3 additions & 2 deletions src/script/cpp_api/s_entity.cpp
Expand Up @@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "object_properties.h"
#include "common/c_converter.h"
#include "common/c_content.h"
#include "server.h"

bool ScriptApiEntity::luaentity_Add(u16 id, const char *name)
{
Expand Down Expand Up @@ -187,11 +188,11 @@ void ScriptApiEntity::luaentity_GetProperties(u16 id,
getstringfield(L, -1, "mesh", prop->mesh);

// Deprecated: read object properties directly
read_object_properties(L, -1, prop);
read_object_properties(L, -1, prop, getServer()->idef());

// Read initial_properties
lua_getfield(L, -1, "initial_properties");
read_object_properties(L, -1, prop);
read_object_properties(L, -1, prop, getServer()->idef());
lua_pop(L, 1);
}

Expand Down
2 changes: 1 addition & 1 deletion src/script/lua_api/l_object.cpp
Expand Up @@ -737,7 +737,7 @@ int ObjectRef::l_set_properties(lua_State *L)
ObjectProperties *prop = co->accessObjectProperties();
if (!prop)
return 0;
read_object_properties(L, 2, prop);
read_object_properties(L, 2, prop, getServer(L)->idef());
co->notifyObjectPropertiesModified();
return 0;
}
Expand Down

0 comments on commit 5c52b3c

Please sign in to comment.