Skip to content

Commit

Permalink
Fix short_description fallback order (#10943)
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenwardy committed Feb 17, 2021
1 parent 7832b68 commit a8f6bef
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
4 changes: 0 additions & 4 deletions builtin/game/register.lua
Expand Up @@ -118,10 +118,6 @@ function core.register_item(name, itemdef)
end
itemdef.name = name

-- default short_description to first line of description
itemdef.short_description = itemdef.short_description or
(itemdef.description or ""):gsub("\n.*","")

-- Apply defaults and add to registered_* table
if itemdef.type == "node" then
-- Use the nodebox as selection box if it's not set manually
Expand Down
13 changes: 7 additions & 6 deletions doc/lua_api.txt
Expand Up @@ -6039,18 +6039,18 @@ an itemstring, a table or `nil`.
stack).
* `set_metadata(metadata)`: (DEPRECATED) Returns true.
* `get_description()`: returns the description shown in inventory list tooltips.
* The engine uses the same as this function for item descriptions.
* The engine uses this when showing item descriptions in tooltips.
* Fields for finding the description, in order:
* `description` in item metadata (See [Item Metadata].)
* `description` in item definition
* item name
* `get_short_description()`: returns the short description.
* `get_short_description()`: returns the short description or nil.
* Unlike the description, this does not include new lines.
* The engine uses the same as this function for short item descriptions.
* Fields for finding the short description, in order:
* `short_description` in item metadata (See [Item Metadata].)
* `short_description` in item definition
* first line of the description (See `get_description()`.)
* first line of the description (From item meta or def, see `get_description()`.)
* Returns nil if none of the above are set
* `clear()`: removes all items from the stack, making it empty.
* `replace(item)`: replace the contents of this stack.
* `item` can also be an itemstring or table.
Expand Down Expand Up @@ -7171,8 +7171,9 @@ Used by `minetest.register_node`, `minetest.register_craftitem`, and

short_description = "Steel Axe",
-- Must not contain new lines.
-- Defaults to the first line of description.
-- See also: `get_short_description` in [`ItemStack`]
-- Defaults to nil.
-- Use an [`ItemStack`] to get the short description, eg:
-- ItemStack(itemname):get_short_description()

groups = {},
-- key = name, value = rating; rating = 1..3.
Expand Down
11 changes: 9 additions & 2 deletions games/devtest/mods/unittests/itemdescription.lua
Expand Up @@ -26,15 +26,22 @@ minetest.register_chatcommand("item_description", {
})

function unittests.test_short_desc()
local function get_short_description(item)
return ItemStack(item):get_short_description()
end

local stack = ItemStack("unittests:colorful_pick")
assert(stack:get_short_description() == "Colorful Pickaxe")
assert(stack:get_short_description() == minetest.registered_items["unittests:colorful_pick"].short_description)
assert(get_short_description("unittests:colorful_pick") == "Colorful Pickaxe")
assert(minetest.registered_items["unittests:colorful_pick"].short_description == nil)
assert(stack:get_description() == full_description)
assert(stack:get_description() == minetest.registered_items["unittests:colorful_pick"].description)

stack:get_meta():set_string("description", "Hello World")
assert(stack:get_short_description() == "Colorful Pickaxe")
assert(stack:get_short_description() == "Hello World")
assert(stack:get_description() == "Hello World")
assert(get_short_description(stack) == "Hello World")
assert(get_short_description("unittests:colorful_pick") == "Colorful Pickaxe")

stack:get_meta():set_string("short_description", "Foo Bar")
assert(stack:get_short_description() == "Foo Bar")
Expand Down
6 changes: 4 additions & 2 deletions src/script/common/c_content.cpp
Expand Up @@ -140,8 +140,10 @@ void push_item_definition_full(lua_State *L, const ItemDefinition &i)
lua_setfield(L, -2, "name");
lua_pushstring(L, i.description.c_str());
lua_setfield(L, -2, "description");
lua_pushstring(L, i.short_description.c_str());
lua_setfield(L, -2, "short_description");
if (!i.short_description.empty()) {
lua_pushstring(L, i.short_description.c_str());
lua_setfield(L, -2, "short_description");
}
lua_pushstring(L, type.c_str());
lua_setfield(L, -2, "type");
lua_pushstring(L, i.inventory_image.c_str());
Expand Down

0 comments on commit a8f6bef

Please sign in to comment.