From f2aa2c6a986dec47856c49ae5f54fbf3c688e027 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Tue, 31 Jan 2017 19:49:01 +0000 Subject: Add ItemStack key-value meta storage --- src/script/lua_api/l_item.cpp | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) (limited to 'src/script/lua_api/l_item.cpp') diff --git a/src/script/lua_api/l_item.cpp b/src/script/lua_api/l_item.cpp index ff0baea14..f0293bed8 100644 --- a/src/script/lua_api/l_item.cpp +++ b/src/script/lua_api/l_item.cpp @@ -18,6 +18,7 @@ with this program; if not, write to the Free Software Foundation, Inc., */ #include "lua_api/l_item.h" +#include "lua_api/l_itemstackmeta.h" #include "lua_api/l_internal.h" #include "common/c_converter.h" #include "common/c_content.h" @@ -137,16 +138,28 @@ int LuaItemStack::l_set_wear(lua_State *L) return 1; } +// get_meta(self) -> string +int LuaItemStack::l_get_meta(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + LuaItemStack *o = checkobject(L, 1); + ItemStackMetaRef::create(L, &o->m_stack); + return 1; +} + +// DEPRECATED // get_metadata(self) -> string int LuaItemStack::l_get_metadata(lua_State *L) { NO_MAP_LOCK_REQUIRED; LuaItemStack *o = checkobject(L, 1); ItemStack &item = o->m_stack; - lua_pushlstring(L, item.metadata.c_str(), item.metadata.size()); + const std::string &value = item.metadata.getString(""); + lua_pushlstring(L, value.c_str(), value.size()); return 1; } +// DEPRECATED // set_metadata(self, string) int LuaItemStack::l_set_metadata(lua_State *L) { @@ -156,7 +169,7 @@ int LuaItemStack::l_set_metadata(lua_State *L) size_t len = 0; const char *ptr = luaL_checklstring(L, 2, &len); - item.metadata.assign(ptr, len); + item.metadata.setString("", std::string(ptr, len)); lua_pushboolean(L, true); return 1; @@ -211,8 +224,24 @@ int LuaItemStack::l_to_table(lua_State *L) lua_setfield(L, -2, "count"); lua_pushinteger(L, item.wear); lua_setfield(L, -2, "wear"); - lua_pushlstring(L, item.metadata.c_str(), item.metadata.size()); - lua_setfield(L, -2, "metadata"); + + if (item.metadata.size() == 1 && item.metadata.contains("")) { + const std::string &value = item.metadata.getString(""); + lua_pushlstring(L, value.c_str(), value.size()); + lua_setfield(L, -2, "metadata"); + } else { + lua_newtable(L); + const StringMap &fields = item.metadata.getStrings(); + for (StringMap::const_iterator it = fields.begin(); + it != fields.end(); ++it) { + const std::string &name = it->first; + const std::string &value = it->second; + lua_pushlstring(L, name.c_str(), name.size()); + lua_pushlstring(L, value.c_str(), value.size()); + lua_settable(L, -3); + } + lua_setfield(L, -2, "metadata"); + } } return 1; } @@ -443,6 +472,7 @@ const luaL_reg LuaItemStack::methods[] = { luamethod(LuaItemStack, set_count), luamethod(LuaItemStack, get_wear), luamethod(LuaItemStack, set_wear), + luamethod(LuaItemStack, get_meta), luamethod(LuaItemStack, get_metadata), luamethod(LuaItemStack, set_metadata), luamethod(LuaItemStack, clear), -- cgit v1.2.3 From 0680c47d6c7d3e98e2b96b823f8cc9ca76d5e7f8 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Sun, 5 Feb 2017 18:15:46 +0000 Subject: Fix incompatibility of ItemStack.to_table() introduced by stack meta --- src/script/common/c_content.cpp | 15 +++++++++++++++ src/script/lua_api/l_item.cpp | 28 +++++++++++++--------------- 2 files changed, 28 insertions(+), 15 deletions(-) (limited to 'src/script/lua_api/l_item.cpp') diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index 8925b51f4..399aa88d0 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -848,6 +848,21 @@ ItemStack read_item(lua_State* L, int index,Server* srv) istack.metadata.setString("", value); } + lua_getfield(L, index, "meta"); + fieldstable = lua_gettop(L); + if (lua_istable(L, fieldstable)) { + lua_pushnil(L); + while (lua_next(L, fieldstable) != 0) { + // key at index -2 and value at index -1 + std::string key = lua_tostring(L, -2); + size_t value_len; + const char *value_cs = lua_tolstring(L, -1, &value_len); + std::string value(value_cs, value_len); + istack.metadata.setString(name, value); + lua_pop(L, 1); // removes value, keeps key for next iteration + } + } + return istack; } else { throw LuaError("Expecting itemstack, itemstring, table or nil"); diff --git a/src/script/lua_api/l_item.cpp b/src/script/lua_api/l_item.cpp index f0293bed8..9638740e8 100644 --- a/src/script/lua_api/l_item.cpp +++ b/src/script/lua_api/l_item.cpp @@ -225,23 +225,21 @@ int LuaItemStack::l_to_table(lua_State *L) lua_pushinteger(L, item.wear); lua_setfield(L, -2, "wear"); - if (item.metadata.size() == 1 && item.metadata.contains("")) { - const std::string &value = item.metadata.getString(""); + const std::string &metadata_str = item.metadata.getString(""); + lua_pushlstring(L, metadata_str.c_str(), metadata_str.size()); + lua_setfield(L, -2, "metadata"); + + lua_newtable(L); + const StringMap &fields = item.metadata.getStrings(); + for (StringMap::const_iterator it = fields.begin(); + it != fields.end(); ++it) { + const std::string &name = it->first; + const std::string &value = it->second; + lua_pushlstring(L, name.c_str(), name.size()); lua_pushlstring(L, value.c_str(), value.size()); - lua_setfield(L, -2, "metadata"); - } else { - lua_newtable(L); - const StringMap &fields = item.metadata.getStrings(); - for (StringMap::const_iterator it = fields.begin(); - it != fields.end(); ++it) { - const std::string &name = it->first; - const std::string &value = it->second; - lua_pushlstring(L, name.c_str(), name.size()); - lua_pushlstring(L, value.c_str(), value.size()); - lua_settable(L, -3); - } - lua_setfield(L, -2, "metadata"); + lua_settable(L, -3); } + lua_setfield(L, -2, "meta"); } return 1; } -- cgit v1.2.3 From 88df9fb5b6c78df9485e8bf3750e2608bd78e14c Mon Sep 17 00:00:00 2001 From: red-001 Date: Tue, 31 Jan 2017 13:18:52 +0000 Subject: Add `get_wielded_item` --- clientmods/preview/init.lua | 22 ++++++++++++++++++++++ doc/client_lua_api.txt | 6 ++++++ src/gamedef.h | 1 + src/script/clientscripting.cpp | 3 +++ src/script/common/c_content.cpp | 7 ++----- src/script/common/c_content.h | 3 ++- src/script/cpp_api/s_item.cpp | 12 ++++++------ src/script/lua_api/l_base.cpp | 6 ++++++ src/script/lua_api/l_base.h | 3 +++ src/script/lua_api/l_client.cpp | 19 +++++++++++++++++++ src/script/lua_api/l_client.h | 3 ++- src/script/lua_api/l_env.cpp | 2 +- src/script/lua_api/l_inventory.cpp | 10 +++++----- src/script/lua_api/l_item.cpp | 26 +++++++++++++------------- src/script/lua_api/l_object.cpp | 2 +- 15 files changed, 92 insertions(+), 33 deletions(-) (limited to 'src/script/lua_api/l_item.cpp') diff --git a/clientmods/preview/init.lua b/clientmods/preview/init.lua index db4f28350..2ca4594d3 100644 --- a/clientmods/preview/init.lua +++ b/clientmods/preview/init.lua @@ -60,6 +60,28 @@ end) core.register_on_punchnode(function(pos, node) print("The local player punched a node!") + local itemstack = core.get_wielded_item() + --[[ + -- getters + print(dump(itemstack:is_empty())) + print(dump(itemstack:get_name())) + print(dump(itemstack:get_count())) + print(dump(itemstack:get_wear())) + print(dump(itemstack:get_meta())) + print(dump(itemstack:get_metadata())) + print(dump(itemstack:is_known())) + --print(dump(itemstack:get_definition())) + print(dump(itemstack:get_tool_capabilities())) + print(dump(itemstack:to_string())) + print(dump(itemstack:to_table())) + -- setters + print(dump(itemstack:set_name("default:dirt"))) + print(dump(itemstack:set_count("95"))) + print(dump(itemstack:set_wear(934))) + print(dump(itemstack:get_meta())) + print(dump(itemstack:get_metadata())) + --]] + print(dump(itemstack:to_table())) print("pos:" .. dump(pos)) print("node:" .. dump(node)) return false diff --git a/doc/client_lua_api.txt b/doc/client_lua_api.txt index 5ffffe938..7440c4014 100644 --- a/doc/client_lua_api.txt +++ b/doc/client_lua_api.txt @@ -721,6 +721,7 @@ Call these functions only at load time! * `minetest.after(time, func, ...)` * Call the function `func` after `time` seconds, may be fractional * Optional: Variable number of arguments that are passed to `func` + ### Map * `minetest.get_node(pos)` * Returns the node at the given position as table in the format @@ -728,6 +729,11 @@ Call these functions only at load time! for unloaded areas. * `minetest.get_node_or_nil(pos)` * Same as `get_node` but returns `nil` for unloaded areas. + +### Player +* `minetest.get_wielded_item()` + * Returns the itemstack the local player is holding + ### Misc. * `minetest.parse_json(string[, nullvalue])`: returns something * Convert a string containing JSON data into the Lua equivalent diff --git a/src/gamedef.h b/src/gamedef.h index 16b53e24f..593d27e30 100644 --- a/src/gamedef.h +++ b/src/gamedef.h @@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #define GAMEDEF_HEADER #include +#include #include "irrlichttypes.h" class IItemDefManager; diff --git a/src/script/clientscripting.cpp b/src/script/clientscripting.cpp index aaed2865d..c1e308012 100644 --- a/src/script/clientscripting.cpp +++ b/src/script/clientscripting.cpp @@ -25,6 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "lua_api/l_storage.h" #include "lua_api/l_sound.h" #include "lua_api/l_util.h" +#include "lua_api/l_item.h" ClientScripting::ClientScripting(Client *client): ScriptApiBase() @@ -55,4 +56,6 @@ void ClientScripting::InitializeModApi(lua_State *L, int top) ModApiClient::Initialize(L, top); ModApiSound::Initialize(L, top); ModApiStorage::Initialize(L, top); + + LuaItemStack::Register(L); } diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index a963856b7..99e12cd82 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -20,7 +20,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "common/c_converter.h" #include "common/c_types.h" #include "nodedef.h" -#include "itemdef.h" #include "object_properties.h" #include "cpp_api/s_node.h" #include "lua_api/l_object.h" @@ -784,7 +783,7 @@ bool string_to_enum(const EnumString *spec, int &result, } /******************************************************************************/ -ItemStack read_item(lua_State* L, int index,Server* srv) +ItemStack read_item(lua_State* L, int index, IItemDefManager *idef) { if(index < 0) index = lua_gettop(L) + 1 + index; @@ -803,7 +802,6 @@ ItemStack read_item(lua_State* L, int index,Server* srv) { // Convert from itemstring std::string itemstring = lua_tostring(L, index); - IItemDefManager *idef = srv->idef(); try { ItemStack item; @@ -820,7 +818,6 @@ ItemStack read_item(lua_State* L, int index,Server* srv) else if(lua_istable(L, index)) { // Convert from table - IItemDefManager *idef = srv->idef(); std::string name = getstringfield_default(L, index, "name", ""); int count = getintfield_default(L, index, "count", 1); int wear = getintfield_default(L, index, "wear", 0); @@ -1187,7 +1184,7 @@ std::vector read_items(lua_State *L, int index, Server *srv) if (items.size() < (u32) key) { items.resize(key); } - items[key - 1] = read_item(L, -1, srv); + items[key - 1] = read_item(L, -1, srv->idef()); lua_pop(L, 1); } return items; diff --git a/src/script/common/c_content.h b/src/script/common/c_content.h index 9641f5c9e..10cccbb01 100644 --- a/src/script/common/c_content.h +++ b/src/script/common/c_content.h @@ -38,6 +38,7 @@ extern "C" { #include "irrlichttypes_bloated.h" #include "util/string.h" #include "itemgroup.h" +#include "itemdef.h" namespace Json { class Value; } @@ -77,7 +78,7 @@ void push_dig_params (lua_State *L, void push_hit_params (lua_State *L, const HitParams ¶ms); -ItemStack read_item (lua_State *L, int index, Server *srv); +ItemStack read_item (lua_State *L, int index, IItemDefManager *idef); struct TileAnimationParams read_animation_definition(lua_State *L, int index); diff --git a/src/script/cpp_api/s_item.cpp b/src/script/cpp_api/s_item.cpp index 3c84fb8cf..cbb833807 100644 --- a/src/script/cpp_api/s_item.cpp +++ b/src/script/cpp_api/s_item.cpp @@ -47,7 +47,7 @@ bool ScriptApiItem::item_OnDrop(ItemStack &item, PCALL_RES(lua_pcall(L, 3, 1, error_handler)); if (!lua_isnil(L, -1)) { try { - item = read_item(L,-1, getServer()); + item = read_item(L, -1, getServer()->idef()); } catch (LuaError &e) { throw LuaError(std::string(e.what()) + ". item=" + item.name); } @@ -74,7 +74,7 @@ bool ScriptApiItem::item_OnPlace(ItemStack &item, PCALL_RES(lua_pcall(L, 3, 1, error_handler)); if (!lua_isnil(L, -1)) { try { - item = read_item(L,-1, getServer()); + item = read_item(L, -1, getServer()->idef()); } catch (LuaError &e) { throw LuaError(std::string(e.what()) + ". item=" + item.name); } @@ -101,7 +101,7 @@ bool ScriptApiItem::item_OnUse(ItemStack &item, PCALL_RES(lua_pcall(L, 3, 1, error_handler)); if(!lua_isnil(L, -1)) { try { - item = read_item(L,-1, getServer()); + item = read_item(L, -1, getServer()->idef()); } catch (LuaError &e) { throw LuaError(std::string(e.what()) + ". item=" + item.name); } @@ -127,7 +127,7 @@ bool ScriptApiItem::item_OnSecondaryUse(ItemStack &item, ServerActiveObject *use PCALL_RES(lua_pcall(L, 3, 1, error_handler)); if (!lua_isnil(L, -1)) { try { - item = read_item(L, -1, getServer()); + item = read_item(L, -1, getServer()->idef()); } catch (LuaError &e) { throw LuaError(std::string(e.what()) + ". item=" + item.name); } @@ -159,7 +159,7 @@ bool ScriptApiItem::item_OnCraft(ItemStack &item, ServerActiveObject *user, PCALL_RES(lua_pcall(L, 4, 1, error_handler)); if (!lua_isnil(L, -1)) { try { - item = read_item(L,-1, getServer()); + item = read_item(L, -1, getServer()->idef()); } catch (LuaError &e) { throw LuaError(std::string(e.what()) + ". item=" + item.name); } @@ -191,7 +191,7 @@ bool ScriptApiItem::item_CraftPredict(ItemStack &item, ServerActiveObject *user, PCALL_RES(lua_pcall(L, 4, 1, error_handler)); if (!lua_isnil(L, -1)) { try { - item = read_item(L,-1, getServer()); + item = read_item(L, -1, getServer()->idef()); } catch (LuaError &e) { throw LuaError(std::string(e.what()) + ". item=" + item.name); } diff --git a/src/script/lua_api/l_base.cpp b/src/script/lua_api/l_base.cpp index f2703718a..dfe743b72 100644 --- a/src/script/lua_api/l_base.cpp +++ b/src/script/lua_api/l_base.cpp @@ -43,6 +43,12 @@ Client *ModApiBase::getClient(lua_State *L) return getScriptApiBase(L)->getClient(); } #endif + +IGameDef *ModApiBase::getGameDef(lua_State *L) +{ + return getScriptApiBase(L)->getGameDef(); +} + Environment *ModApiBase::getEnv(lua_State *L) { return getScriptApiBase(L)->getEnv(); diff --git a/src/script/lua_api/l_base.h b/src/script/lua_api/l_base.h index dc1b1b226..cd382629d 100644 --- a/src/script/lua_api/l_base.h +++ b/src/script/lua_api/l_base.h @@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "common/c_types.h" #include "common/c_internal.h" +#include "gamedef.h" extern "C" { #include @@ -46,6 +47,8 @@ public: static Client* getClient(lua_State *L); #endif // !SERVER + static IGameDef* getGameDef(lua_State *L); + static Environment* getEnv(lua_State *L); static GUIEngine* getGuiEngine(lua_State *L); // When we are not loading the mod, this function returns "." diff --git a/src/script/lua_api/l_client.cpp b/src/script/lua_api/l_client.cpp index 41e33889c..1673a62ce 100644 --- a/src/script/lua_api/l_client.cpp +++ b/src/script/lua_api/l_client.cpp @@ -25,6 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "gettext.h" #include "common/c_converter.h" #include "common/c_content.h" +#include "lua_api/l_item.h" int ModApiClient::l_get_current_modname(lua_State *L) { @@ -132,6 +133,23 @@ int ModApiClient::l_get_node_or_nil(lua_State *L) return 1; } +int ModApiClient::l_get_wielded_item(lua_State *L) +{ + Client *client = getClient(L); + + Inventory local_inventory(client->idef()); + client->getLocalInventory(local_inventory); + + InventoryList *mlist = local_inventory.getList("main"); + + if (mlist && client->getPlayerItem() < mlist->getSize()) { + LuaItemStack::create(L, mlist->getItem(client->getPlayerItem())); + } else { + LuaItemStack::create(L, ItemStack()); + } + return 1; +} + void ModApiClient::Initialize(lua_State *L, int top) { API_FCT(get_current_modname); @@ -143,4 +161,5 @@ void ModApiClient::Initialize(lua_State *L, int top) API_FCT(gettext); API_FCT(get_node); API_FCT(get_node_or_nil); + API_FCT(get_wielded_item); } diff --git a/src/script/lua_api/l_client.h b/src/script/lua_api/l_client.h index 207a5bca0..def9b48a3 100644 --- a/src/script/lua_api/l_client.h +++ b/src/script/lua_api/l_client.h @@ -53,7 +53,8 @@ private: // get_node_or_nil(pos) static int l_get_node_or_nil(lua_State *L); - + // get_wielded_item() + static int l_get_wielded_item(lua_State *L); public: static void Initialize(lua_State *L, int top); diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index 442c4b99a..14df558d3 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -472,7 +472,7 @@ int ModApiEnvMod::l_add_item(lua_State *L) // pos //v3f pos = checkFloatPos(L, 1); // item - ItemStack item = read_item(L, 2,getServer(L)); + ItemStack item = read_item(L, 2,getServer(L)->idef()); if(item.empty() || !item.isKnown(getServer(L)->idef())) return 0; diff --git a/src/script/lua_api/l_inventory.cpp b/src/script/lua_api/l_inventory.cpp index 38eade609..9a4aa845d 100644 --- a/src/script/lua_api/l_inventory.cpp +++ b/src/script/lua_api/l_inventory.cpp @@ -194,7 +194,7 @@ int InvRef::l_set_stack(lua_State *L) InvRef *ref = checkobject(L, 1); const char *listname = luaL_checkstring(L, 2); int i = luaL_checknumber(L, 3) - 1; - ItemStack newitem = read_item(L, 4, getServer(L)); + ItemStack newitem = read_item(L, 4, getServer(L)->idef()); InventoryList *list = getlist(L, ref, listname); if(list != NULL && i >= 0 && i < (int) list->getSize()){ list->changeItem(i, newitem); @@ -295,7 +295,7 @@ int InvRef::l_add_item(lua_State *L) NO_MAP_LOCK_REQUIRED; InvRef *ref = checkobject(L, 1); const char *listname = luaL_checkstring(L, 2); - ItemStack item = read_item(L, 3, getServer(L)); + ItemStack item = read_item(L, 3, getServer(L)->idef()); InventoryList *list = getlist(L, ref, listname); if(list){ ItemStack leftover = list->addItem(item); @@ -315,7 +315,7 @@ int InvRef::l_room_for_item(lua_State *L) NO_MAP_LOCK_REQUIRED; InvRef *ref = checkobject(L, 1); const char *listname = luaL_checkstring(L, 2); - ItemStack item = read_item(L, 3, getServer(L)); + ItemStack item = read_item(L, 3, getServer(L)->idef()); InventoryList *list = getlist(L, ref, listname); if(list){ lua_pushboolean(L, list->roomForItem(item)); @@ -332,7 +332,7 @@ int InvRef::l_contains_item(lua_State *L) NO_MAP_LOCK_REQUIRED; InvRef *ref = checkobject(L, 1); const char *listname = luaL_checkstring(L, 2); - ItemStack item = read_item(L, 3, getServer(L)); + ItemStack item = read_item(L, 3, getServer(L)->idef()); InventoryList *list = getlist(L, ref, listname); if(list){ lua_pushboolean(L, list->containsItem(item)); @@ -349,7 +349,7 @@ int InvRef::l_remove_item(lua_State *L) NO_MAP_LOCK_REQUIRED; InvRef *ref = checkobject(L, 1); const char *listname = luaL_checkstring(L, 2); - ItemStack item = read_item(L, 3, getServer(L)); + ItemStack item = read_item(L, 3, getServer(L)->idef()); InventoryList *list = getlist(L, ref, listname); if(list){ ItemStack removed = list->removeItem(item); diff --git a/src/script/lua_api/l_item.cpp b/src/script/lua_api/l_item.cpp index 9638740e8..7e6f457e1 100644 --- a/src/script/lua_api/l_item.cpp +++ b/src/script/lua_api/l_item.cpp @@ -190,7 +190,7 @@ int LuaItemStack::l_replace(lua_State *L) { NO_MAP_LOCK_REQUIRED; LuaItemStack *o = checkobject(L, 1); - o->m_stack = read_item(L,2,getServer(L)); + o->m_stack = read_item(L, 2, getGameDef(L)->idef()); lua_pushboolean(L, true); return 1; } @@ -250,7 +250,7 @@ int LuaItemStack::l_get_stack_max(lua_State *L) NO_MAP_LOCK_REQUIRED; LuaItemStack *o = checkobject(L, 1); ItemStack &item = o->m_stack; - lua_pushinteger(L, item.getStackMax(getServer(L)->idef())); + lua_pushinteger(L, item.getStackMax(getGameDef(L)->idef())); return 1; } @@ -260,7 +260,7 @@ int LuaItemStack::l_get_free_space(lua_State *L) NO_MAP_LOCK_REQUIRED; LuaItemStack *o = checkobject(L, 1); ItemStack &item = o->m_stack; - lua_pushinteger(L, item.freeSpace(getServer(L)->idef())); + lua_pushinteger(L, item.freeSpace(getGameDef(L)->idef())); return 1; } @@ -271,7 +271,7 @@ int LuaItemStack::l_is_known(lua_State *L) NO_MAP_LOCK_REQUIRED; LuaItemStack *o = checkobject(L, 1); ItemStack &item = o->m_stack; - bool is_known = item.isKnown(getServer(L)->idef()); + bool is_known = item.isKnown(getGameDef(L)->idef()); lua_pushboolean(L, is_known); return 1; } @@ -307,7 +307,7 @@ int LuaItemStack::l_get_tool_capabilities(lua_State *L) LuaItemStack *o = checkobject(L, 1); ItemStack &item = o->m_stack; const ToolCapabilities &prop = - item.getToolCapabilities(getServer(L)->idef()); + item.getToolCapabilities(getGameDef(L)->idef()); push_tool_capabilities(L, prop); return 1; } @@ -322,7 +322,7 @@ int LuaItemStack::l_add_wear(lua_State *L) LuaItemStack *o = checkobject(L, 1); ItemStack &item = o->m_stack; int amount = lua_tointeger(L, 2); - bool result = item.addWear(amount, getServer(L)->idef()); + bool result = item.addWear(amount, getGameDef(L)->idef()); lua_pushboolean(L, result); return 1; } @@ -334,8 +334,8 @@ int LuaItemStack::l_add_item(lua_State *L) NO_MAP_LOCK_REQUIRED; LuaItemStack *o = checkobject(L, 1); ItemStack &item = o->m_stack; - ItemStack newitem = read_item(L,-1, getServer(L)); - ItemStack leftover = item.addItem(newitem, getServer(L)->idef()); + ItemStack newitem = read_item(L, -1, getGameDef(L)->idef()); + ItemStack leftover = item.addItem(newitem, getGameDef(L)->idef()); create(L, leftover); return 1; } @@ -348,9 +348,9 @@ int LuaItemStack::l_item_fits(lua_State *L) NO_MAP_LOCK_REQUIRED; LuaItemStack *o = checkobject(L, 1); ItemStack &item = o->m_stack; - ItemStack newitem = read_item(L, 2, getServer(L)); + ItemStack newitem = read_item(L, 2, getGameDef(L)->idef()); ItemStack restitem; - bool fits = item.itemFits(newitem, &restitem, getServer(L)->idef()); + bool fits = item.itemFits(newitem, &restitem, getGameDef(L)->idef()); lua_pushboolean(L, fits); // first return value create(L, restitem); // second return value return 2; @@ -407,7 +407,7 @@ ItemStack& LuaItemStack::getItem() int LuaItemStack::create_object(lua_State *L) { NO_MAP_LOCK_REQUIRED; - ItemStack item = read_item(L, 1, getServer(L)); + ItemStack item = read_item(L, 1, getGameDef(L)->idef()); LuaItemStack *o = new LuaItemStack(item); *(void **)(lua_newuserdata(L, sizeof(void *))) = o; luaL_getmetatable(L, className); @@ -596,7 +596,7 @@ int ModApiItemMod::l_get_content_id(lua_State *L) NO_MAP_LOCK_REQUIRED; std::string name = luaL_checkstring(L, 1); - INodeDefManager *ndef = getServer(L)->getNodeDefManager(); + INodeDefManager *ndef = getGameDef(L)->getNodeDefManager(); content_t c = ndef->getId(name); lua_pushinteger(L, c); @@ -609,7 +609,7 @@ int ModApiItemMod::l_get_name_from_content_id(lua_State *L) NO_MAP_LOCK_REQUIRED; content_t c = luaL_checkint(L, 1); - INodeDefManager *ndef = getServer(L)->getNodeDefManager(); + INodeDefManager *ndef = getGameDef(L)->getNodeDefManager(); const char *name = ndef->get(c).name.c_str(); lua_pushstring(L, name); diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index be454ad45..d5681b809 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -364,7 +364,7 @@ int ObjectRef::l_set_wielded_item(lua_State *L) ServerActiveObject *co = getobject(ref); if (co == NULL) return 0; // Do it - ItemStack item = read_item(L, 2, getServer(L)); + ItemStack item = read_item(L, 2, getServer(L)->idef()); bool success = co->setWieldedItem(item); if (success && co->getType() == ACTIVEOBJECT_TYPE_PLAYER) { getServer(L)->SendInventory(((PlayerSAO*)co)); -- cgit v1.2.3 From 41c54830242269de073e4a0c10d1775dfdf6811d Mon Sep 17 00:00:00 2001 From: Loïc Blot Date: Sat, 8 Apr 2017 09:28:37 +0200 Subject: Replace luaL_reg with luaL_Reg as recent LuaJIT dropped the Lua 5.0 compat (#5541) We are bundling Lua5.1 which has same macro --- src/script/lua_api/l_areastore.cpp | 4 ++-- src/script/lua_api/l_areastore.h | 2 +- src/script/lua_api/l_inventory.cpp | 2 +- src/script/lua_api/l_inventory.h | 2 +- src/script/lua_api/l_item.cpp | 2 +- src/script/lua_api/l_item.h | 2 +- src/script/lua_api/l_itemstackmeta.cpp | 2 +- src/script/lua_api/l_itemstackmeta.h | 2 +- src/script/lua_api/l_minimap.cpp | 2 +- src/script/lua_api/l_minimap.h | 2 +- src/script/lua_api/l_nodemeta.cpp | 4 ++-- src/script/lua_api/l_nodemeta.h | 4 ++-- src/script/lua_api/l_nodetimer.cpp | 2 +- src/script/lua_api/l_nodetimer.h | 2 +- src/script/lua_api/l_noise.cpp | 10 +++++----- src/script/lua_api/l_noise.h | 10 +++++----- src/script/lua_api/l_object.cpp | 2 +- src/script/lua_api/l_object.h | 2 +- src/script/lua_api/l_settings.cpp | 2 +- src/script/lua_api/l_settings.h | 2 +- src/script/lua_api/l_storage.cpp | 2 +- src/script/lua_api/l_storage.h | 2 +- src/script/lua_api/l_vmanip.cpp | 2 +- src/script/lua_api/l_vmanip.h | 2 +- 24 files changed, 35 insertions(+), 35 deletions(-) (limited to 'src/script/lua_api/l_item.cpp') diff --git a/src/script/lua_api/l_areastore.cpp b/src/script/lua_api/l_areastore.cpp index 09a5c78f9..b81985a7f 100644 --- a/src/script/lua_api/l_areastore.cpp +++ b/src/script/lua_api/l_areastore.cpp @@ -74,7 +74,7 @@ static inline void push_areas(lua_State *L, const std::vector &areas, static int deserialization_helper(lua_State *L, AreaStore *as, std::istream &is) { - try { + try { as->deserialize(is); } catch (const SerializationError &e) { lua_pushboolean(L, false); @@ -380,7 +380,7 @@ void LuaAreaStore::Register(lua_State *L) } const char LuaAreaStore::className[] = "AreaStore"; -const luaL_reg LuaAreaStore::methods[] = { +const luaL_Reg LuaAreaStore::methods[] = { luamethod(LuaAreaStore, get_area), luamethod(LuaAreaStore, get_areas_for_pos), luamethod(LuaAreaStore, get_areas_in_area), diff --git a/src/script/lua_api/l_areastore.h b/src/script/lua_api/l_areastore.h index 7dea08df4..8292e7712 100644 --- a/src/script/lua_api/l_areastore.h +++ b/src/script/lua_api/l_areastore.h @@ -28,7 +28,7 @@ class LuaAreaStore : public ModApiBase { private: static const char className[]; - static const luaL_reg methods[]; + static const luaL_Reg methods[]; static int gc_object(lua_State *L); diff --git a/src/script/lua_api/l_inventory.cpp b/src/script/lua_api/l_inventory.cpp index 9a4aa845d..f5e76a7b6 100644 --- a/src/script/lua_api/l_inventory.cpp +++ b/src/script/lua_api/l_inventory.cpp @@ -463,7 +463,7 @@ void InvRef::Register(lua_State *L) } const char InvRef::className[] = "InvRef"; -const luaL_reg InvRef::methods[] = { +const luaL_Reg InvRef::methods[] = { luamethod(InvRef, is_empty), luamethod(InvRef, get_size), luamethod(InvRef, set_size), diff --git a/src/script/lua_api/l_inventory.h b/src/script/lua_api/l_inventory.h index cc5333965..91d41c0d0 100644 --- a/src/script/lua_api/l_inventory.h +++ b/src/script/lua_api/l_inventory.h @@ -36,7 +36,7 @@ private: InventoryLocation m_loc; static const char className[]; - static const luaL_reg methods[]; + static const luaL_Reg methods[]; static InvRef *checkobject(lua_State *L, int narg); diff --git a/src/script/lua_api/l_item.cpp b/src/script/lua_api/l_item.cpp index 7e6f457e1..19b5b0955 100644 --- a/src/script/lua_api/l_item.cpp +++ b/src/script/lua_api/l_item.cpp @@ -462,7 +462,7 @@ void LuaItemStack::Register(lua_State *L) } const char LuaItemStack::className[] = "ItemStack"; -const luaL_reg LuaItemStack::methods[] = { +const luaL_Reg LuaItemStack::methods[] = { luamethod(LuaItemStack, is_empty), luamethod(LuaItemStack, get_name), luamethod(LuaItemStack, set_name), diff --git a/src/script/lua_api/l_item.h b/src/script/lua_api/l_item.h index 1ba5d79e0..b4efaefc8 100644 --- a/src/script/lua_api/l_item.h +++ b/src/script/lua_api/l_item.h @@ -28,7 +28,7 @@ private: ItemStack m_stack; static const char className[]; - static const luaL_reg methods[]; + static const luaL_Reg methods[]; // Exported functions diff --git a/src/script/lua_api/l_itemstackmeta.cpp b/src/script/lua_api/l_itemstackmeta.cpp index 304a7cdf3..efdd77b51 100644 --- a/src/script/lua_api/l_itemstackmeta.cpp +++ b/src/script/lua_api/l_itemstackmeta.cpp @@ -102,7 +102,7 @@ void ItemStackMetaRef::Register(lua_State *L) } const char ItemStackMetaRef::className[] = "ItemStackMetaRef"; -const luaL_reg ItemStackMetaRef::methods[] = { +const luaL_Reg ItemStackMetaRef::methods[] = { luamethod(MetaDataRef, get_string), luamethod(MetaDataRef, set_string), luamethod(MetaDataRef, get_int), diff --git a/src/script/lua_api/l_itemstackmeta.h b/src/script/lua_api/l_itemstackmeta.h index 6f9b2016c..4ef64a91e 100644 --- a/src/script/lua_api/l_itemstackmeta.h +++ b/src/script/lua_api/l_itemstackmeta.h @@ -31,7 +31,7 @@ private: ItemStack *istack; static const char className[]; - static const luaL_reg methods[]; + static const luaL_Reg methods[]; static ItemStackMetaRef *checkobject(lua_State *L, int narg); diff --git a/src/script/lua_api/l_minimap.cpp b/src/script/lua_api/l_minimap.cpp index 182894f4f..c68602909 100644 --- a/src/script/lua_api/l_minimap.cpp +++ b/src/script/lua_api/l_minimap.cpp @@ -201,7 +201,7 @@ void LuaMinimap::Register(lua_State *L) } const char LuaMinimap::className[] = "Minimap"; -const luaL_reg LuaMinimap::methods[] = { +const luaL_Reg LuaMinimap::methods[] = { luamethod(LuaMinimap, show), luamethod(LuaMinimap, hide), luamethod(LuaMinimap, get_pos), diff --git a/src/script/lua_api/l_minimap.h b/src/script/lua_api/l_minimap.h index 9a299b4fd..8be72b8e7 100644 --- a/src/script/lua_api/l_minimap.h +++ b/src/script/lua_api/l_minimap.h @@ -28,7 +28,7 @@ class LuaMinimap : public ModApiBase { private: static const char className[]; - static const luaL_reg methods[]; + static const luaL_Reg methods[]; // garbage collector static int gc_object(lua_State *L); diff --git a/src/script/lua_api/l_nodemeta.cpp b/src/script/lua_api/l_nodemeta.cpp index 4368a8c50..55d11fc13 100644 --- a/src/script/lua_api/l_nodemeta.cpp +++ b/src/script/lua_api/l_nodemeta.cpp @@ -215,7 +215,7 @@ void NodeMetaRef::Register(lua_State *L) } -const luaL_reg NodeMetaRef::methodsServer[] = { +const luaL_Reg NodeMetaRef::methodsServer[] = { luamethod(MetaDataRef, get_string), luamethod(MetaDataRef, set_string), luamethod(MetaDataRef, get_int), @@ -237,7 +237,7 @@ void NodeMetaRef::RegisterClient(lua_State *L) } -const luaL_reg NodeMetaRef::methodsClient[] = { +const luaL_Reg NodeMetaRef::methodsClient[] = { luamethod(MetaDataRef, get_string), luamethod(MetaDataRef, get_int), luamethod(MetaDataRef, get_float), diff --git a/src/script/lua_api/l_nodemeta.h b/src/script/lua_api/l_nodemeta.h index 6d146416b..2ac028079 100644 --- a/src/script/lua_api/l_nodemeta.h +++ b/src/script/lua_api/l_nodemeta.h @@ -39,8 +39,8 @@ private: bool m_is_local; static const char className[]; - static const luaL_reg methodsServer[]; - static const luaL_reg methodsClient[]; + static const luaL_Reg methodsServer[]; + static const luaL_Reg methodsClient[]; static NodeMetaRef *checkobject(lua_State *L, int narg); diff --git a/src/script/lua_api/l_nodetimer.cpp b/src/script/lua_api/l_nodetimer.cpp index ed11cc58e..17b275c46 100644 --- a/src/script/lua_api/l_nodetimer.cpp +++ b/src/script/lua_api/l_nodetimer.cpp @@ -162,7 +162,7 @@ void NodeTimerRef::Register(lua_State *L) } const char NodeTimerRef::className[] = "NodeTimerRef"; -const luaL_reg NodeTimerRef::methods[] = { +const luaL_Reg NodeTimerRef::methods[] = { luamethod(NodeTimerRef, start), luamethod(NodeTimerRef, set), luamethod(NodeTimerRef, stop), diff --git a/src/script/lua_api/l_nodetimer.h b/src/script/lua_api/l_nodetimer.h index 239112037..ae362d8b3 100644 --- a/src/script/lua_api/l_nodetimer.h +++ b/src/script/lua_api/l_nodetimer.h @@ -32,7 +32,7 @@ private: ServerEnvironment *m_env; static const char className[]; - static const luaL_reg methods[]; + static const luaL_Reg methods[]; static int gc_object(lua_State *L); diff --git a/src/script/lua_api/l_noise.cpp b/src/script/lua_api/l_noise.cpp index e0039371f..e3e76191f 100644 --- a/src/script/lua_api/l_noise.cpp +++ b/src/script/lua_api/l_noise.cpp @@ -135,7 +135,7 @@ void LuaPerlinNoise::Register(lua_State *L) const char LuaPerlinNoise::className[] = "PerlinNoise"; -const luaL_reg LuaPerlinNoise::methods[] = { +const luaL_Reg LuaPerlinNoise::methods[] = { luamethod(LuaPerlinNoise, get2d), luamethod(LuaPerlinNoise, get3d), {0,0} @@ -393,7 +393,7 @@ void LuaPerlinNoiseMap::Register(lua_State *L) const char LuaPerlinNoiseMap::className[] = "PerlinNoiseMap"; -const luaL_reg LuaPerlinNoiseMap::methods[] = { +const luaL_Reg LuaPerlinNoiseMap::methods[] = { luamethod(LuaPerlinNoiseMap, get2dMap), luamethod(LuaPerlinNoiseMap, get2dMap_flat), luamethod(LuaPerlinNoiseMap, calc2dMap), @@ -498,7 +498,7 @@ void LuaPseudoRandom::Register(lua_State *L) const char LuaPseudoRandom::className[] = "PseudoRandom"; -const luaL_reg LuaPseudoRandom::methods[] = { +const luaL_Reg LuaPseudoRandom::methods[] = { luamethod(LuaPseudoRandom, next), {0,0} }; @@ -597,7 +597,7 @@ void LuaPcgRandom::Register(lua_State *L) const char LuaPcgRandom::className[] = "PcgRandom"; -const luaL_reg LuaPcgRandom::methods[] = { +const luaL_Reg LuaPcgRandom::methods[] = { luamethod(LuaPcgRandom, next), luamethod(LuaPcgRandom, rand_normal_dist), {0,0} @@ -711,7 +711,7 @@ void LuaSecureRandom::Register(lua_State *L) } const char LuaSecureRandom::className[] = "SecureRandom"; -const luaL_reg LuaSecureRandom::methods[] = { +const luaL_Reg LuaSecureRandom::methods[] = { luamethod(LuaSecureRandom, next_bytes), {0,0} }; diff --git a/src/script/lua_api/l_noise.h b/src/script/lua_api/l_noise.h index 11ec348bf..f252b5ba2 100644 --- a/src/script/lua_api/l_noise.h +++ b/src/script/lua_api/l_noise.h @@ -32,7 +32,7 @@ class LuaPerlinNoise : public ModApiBase private: NoiseParams np; static const char className[]; - static const luaL_reg methods[]; + static const luaL_Reg methods[]; // Exported functions @@ -64,7 +64,7 @@ class LuaPerlinNoiseMap : public ModApiBase Noise *noise; bool m_is3d; static const char className[]; - static const luaL_reg methods[]; + static const luaL_Reg methods[]; // Exported functions @@ -103,7 +103,7 @@ private: PseudoRandom m_pseudo; static const char className[]; - static const luaL_reg methods[]; + static const luaL_Reg methods[]; // Exported functions @@ -134,7 +134,7 @@ private: PcgRandom m_rnd; static const char className[]; - static const luaL_reg methods[]; + static const luaL_Reg methods[]; // Exported functions @@ -169,7 +169,7 @@ class LuaSecureRandom : public ModApiBase private: static const size_t RAND_BUF_SIZE = 2048; static const char className[]; - static const luaL_reg methods[]; + static const luaL_Reg methods[]; u32 m_rand_idx; char m_rand_buf[RAND_BUF_SIZE]; diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index d5681b809..f9d2754e7 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -1823,7 +1823,7 @@ void ObjectRef::Register(lua_State *L) } const char ObjectRef::className[] = "ObjectRef"; -const luaL_reg ObjectRef::methods[] = { +const luaL_Reg ObjectRef::methods[] = { // ServerActiveObject luamethod(ObjectRef, remove), luamethod_aliased(ObjectRef, get_pos, getpos), diff --git a/src/script/lua_api/l_object.h b/src/script/lua_api/l_object.h index 2c9aa559a..b6fc35bc2 100644 --- a/src/script/lua_api/l_object.h +++ b/src/script/lua_api/l_object.h @@ -37,7 +37,7 @@ private: ServerActiveObject *m_object; static const char className[]; - static const luaL_reg methods[]; + static const luaL_Reg methods[]; public: static ObjectRef *checkobject(lua_State *L, int narg); diff --git a/src/script/lua_api/l_settings.cpp b/src/script/lua_api/l_settings.cpp index d3fe03005..809f7d115 100644 --- a/src/script/lua_api/l_settings.cpp +++ b/src/script/lua_api/l_settings.cpp @@ -214,7 +214,7 @@ LuaSettings* LuaSettings::checkobject(lua_State* L, int narg) } const char LuaSettings::className[] = "Settings"; -const luaL_reg LuaSettings::methods[] = { +const luaL_Reg LuaSettings::methods[] = { luamethod(LuaSettings, get), luamethod(LuaSettings, get_bool), luamethod(LuaSettings, set), diff --git a/src/script/lua_api/l_settings.h b/src/script/lua_api/l_settings.h index d5edd32ce..b90f0a8f2 100644 --- a/src/script/lua_api/l_settings.h +++ b/src/script/lua_api/l_settings.h @@ -28,7 +28,7 @@ class LuaSettings : public ModApiBase { private: static const char className[]; - static const luaL_reg methods[]; + static const luaL_Reg methods[]; // garbage collector static int gc_object(lua_State *L); diff --git a/src/script/lua_api/l_storage.cpp b/src/script/lua_api/l_storage.cpp index 867ab9c8d..59906dda5 100644 --- a/src/script/lua_api/l_storage.cpp +++ b/src/script/lua_api/l_storage.cpp @@ -129,7 +129,7 @@ void StorageRef::clearMeta() } const char StorageRef::className[] = "StorageRef"; -const luaL_reg StorageRef::methods[] = { +const luaL_Reg StorageRef::methods[] = { luamethod(MetaDataRef, get_string), luamethod(MetaDataRef, set_string), luamethod(MetaDataRef, get_int), diff --git a/src/script/lua_api/l_storage.h b/src/script/lua_api/l_storage.h index e09b8b391..ec6f8d941 100644 --- a/src/script/lua_api/l_storage.h +++ b/src/script/lua_api/l_storage.h @@ -41,7 +41,7 @@ private: ModMetadata *m_object; static const char className[]; - static const luaL_reg methods[]; + static const luaL_Reg methods[]; virtual Metadata *getmeta(bool auto_create); virtual void clearMeta(); diff --git a/src/script/lua_api/l_vmanip.cpp b/src/script/lua_api/l_vmanip.cpp index 5f129d2af..7316fb200 100644 --- a/src/script/lua_api/l_vmanip.cpp +++ b/src/script/lua_api/l_vmanip.cpp @@ -452,7 +452,7 @@ void LuaVoxelManip::Register(lua_State *L) } const char LuaVoxelManip::className[] = "VoxelManip"; -const luaL_reg LuaVoxelManip::methods[] = { +const luaL_Reg LuaVoxelManip::methods[] = { luamethod(LuaVoxelManip, read_from_map), luamethod(LuaVoxelManip, get_data), luamethod(LuaVoxelManip, set_data), diff --git a/src/script/lua_api/l_vmanip.h b/src/script/lua_api/l_vmanip.h index 65fc0d97a..b6a69f36a 100644 --- a/src/script/lua_api/l_vmanip.h +++ b/src/script/lua_api/l_vmanip.h @@ -38,7 +38,7 @@ private: bool is_mapgen_vm; static const char className[]; - static const luaL_reg methods[]; + static const luaL_Reg methods[]; static int gc_object(lua_State *L); -- cgit v1.2.3 From 83508277ce57720ee833bea125388a99cbe4e20d Mon Sep 17 00:00:00 2001 From: numberZero Date: Wed, 12 Apr 2017 18:51:07 +0400 Subject: Fix inability to change metadata using stack:to_table() (#5547) --- src/script/lua_api/l_item.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/script/lua_api/l_item.cpp') diff --git a/src/script/lua_api/l_item.cpp b/src/script/lua_api/l_item.cpp index 19b5b0955..2d32093a7 100644 --- a/src/script/lua_api/l_item.cpp +++ b/src/script/lua_api/l_item.cpp @@ -234,6 +234,8 @@ int LuaItemStack::l_to_table(lua_State *L) for (StringMap::const_iterator it = fields.begin(); it != fields.end(); ++it) { const std::string &name = it->first; + if (name.empty()) + continue; const std::string &value = it->second; lua_pushlstring(L, name.c_str(), name.size()); lua_pushlstring(L, value.c_str(), value.size()); -- cgit v1.2.3 From f3fe62a0bf9e775b3e6e838f104ab605a2238792 Mon Sep 17 00:00:00 2001 From: Loïc Blot Date: Wed, 19 Apr 2017 23:02:07 +0200 Subject: Fix various copy instead of const ref reported by cppcheck (#5615) * Also remove InventoryList::peekItem unused function * Fix some post increment to preincrement reported by cppcheck --- src/chat.cpp | 2 +- src/chat.h | 6 +++--- src/craftdef.h | 11 +++++++---- src/environment.cpp | 4 ++-- src/exceptions.h | 9 +++------ src/game.cpp | 9 ++------- src/httpfetch.cpp | 5 +++-- src/inventory.cpp | 28 +++++++++------------------- src/inventory.h | 7 ++----- src/script/common/c_content.cpp | 19 +++++++------------ src/script/common/c_content.h | 4 ++-- src/script/lua_api/l_item.cpp | 2 +- src/tool.h | 8 ++++---- 13 files changed, 46 insertions(+), 68 deletions(-) (limited to 'src/script/lua_api/l_item.cpp') diff --git a/src/chat.cpp b/src/chat.cpp index 46555b3dc..de7483e22 100644 --- a/src/chat.cpp +++ b/src/chat.cpp @@ -386,7 +386,7 @@ s32 ChatBuffer::getBottomScrollPos() const -ChatPrompt::ChatPrompt(std::wstring prompt, u32 history_limit): +ChatPrompt::ChatPrompt(const std::wstring &prompt, u32 history_limit): m_prompt(prompt), m_line(L""), m_history(), diff --git a/src/chat.h b/src/chat.h index 11061fd39..5de676a2e 100644 --- a/src/chat.h +++ b/src/chat.h @@ -38,14 +38,14 @@ struct ChatLine // message text EnrichedString text; - ChatLine(std::wstring a_name, std::wstring a_text): + ChatLine(const std::wstring &a_name, const std::wstring &a_text): age(0.0), name(a_name), text(a_text) { } - ChatLine(EnrichedString a_name, EnrichedString a_text): + ChatLine(const EnrichedString &a_name, const EnrichedString &a_text): age(0.0), name(a_name), text(a_text) @@ -148,7 +148,7 @@ private: class ChatPrompt { public: - ChatPrompt(std::wstring prompt, u32 history_limit); + ChatPrompt(const std::wstring &prompt, u32 history_limit); ~ChatPrompt(); // Input character or string diff --git a/src/craftdef.h b/src/craftdef.h index 695ee0c2c..bdd741f7c 100644 --- a/src/craftdef.h +++ b/src/craftdef.h @@ -97,7 +97,7 @@ struct CraftOutput CraftOutput(): item(""), time(0) {} - CraftOutput(std::string item_, float time_): + CraftOutput(const std::string &item_, float time_): item(item_), time(time_) {} std::string dump() const; @@ -124,7 +124,7 @@ struct CraftReplacements CraftReplacements(): pairs() {} - CraftReplacements(std::vector > pairs_): + CraftReplacements(const std::vector > &pairs_): pairs(pairs_) {} std::string dump() const; @@ -359,10 +359,13 @@ public: CraftDefinitionFuel(): recipe(""), hash_inited(false), burntime() {} - CraftDefinitionFuel(std::string recipe_, + CraftDefinitionFuel(const std::string &recipe_, float burntime_, const CraftReplacements &replacements_): - recipe(recipe_), hash_inited(false), burntime(burntime_), replacements(replacements_) + recipe(recipe_), + hash_inited(false), + burntime(burntime_), + replacements(replacements_) {} virtual ~CraftDefinitionFuel(){} diff --git a/src/environment.cpp b/src/environment.cpp index 9c2ea8896..c1aeeec60 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -70,7 +70,7 @@ void Environment::setTimeOfDay(u32 time) { MutexAutoLock lock(this->m_time_lock); if (m_time_of_day > time) - m_day_count++; + ++m_day_count; m_time_of_day = time; m_time_of_day_f = (float)time / 24000.0; } @@ -103,7 +103,7 @@ void Environment::stepTimeOfDay(float dtime) // Sync at overflow if (m_time_of_day + units >= 24000) { sync_f = true; - m_day_count++; + ++m_day_count; } m_time_of_day = (m_time_of_day + units) % 24000; if (sync_f) diff --git a/src/exceptions.h b/src/exceptions.h index 67a2d0df6..1b39c6725 100644 --- a/src/exceptions.h +++ b/src/exceptions.h @@ -27,10 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc., class BaseException : public std::exception { public: - BaseException(const std::string &s) throw() - { - m_s = s; - } + BaseException(const std::string &s) throw(): m_s(s) {} ~BaseException() throw() {} virtual const char * what() const throw() { @@ -122,12 +119,12 @@ public: class ClientStateError : public BaseException { public: - ClientStateError(std::string s): BaseException(s) {} + ClientStateError(const std::string &s): BaseException(s) {} }; class PrngException : public BaseException { public: - PrngException(std::string s): BaseException(s) {} + PrngException(const std::string &s): BaseException(s) {} }; class ModError : public BaseException { diff --git a/src/game.cpp b/src/game.cpp index bcf378e4e..fbc0cf35f 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -127,8 +127,8 @@ struct TextDestPlayerInventory : public TextDest struct LocalFormspecHandler : public TextDest { - LocalFormspecHandler(std::string formname): - m_client(0) + LocalFormspecHandler(const std::string &formname): + m_client(NULL) { m_formname = formname; } @@ -139,11 +139,6 @@ struct LocalFormspecHandler : public TextDest m_formname = formname; } - void gotText(const std::wstring &message) - { - errorstream << "LocalFormspecHandler::gotText old style message received" << std::endl; - } - void gotText(const StringMap &fields) { if (m_formname == "MT_PAUSE_MENU") { diff --git a/src/httpfetch.cpp b/src/httpfetch.cpp index f64c9f717..21400355a 100644 --- a/src/httpfetch.cpp +++ b/src/httpfetch.cpp @@ -207,7 +207,7 @@ public: class HTTPFetchOngoing { public: - HTTPFetchOngoing(HTTPFetchRequest request, CurlHandlePool *pool); + HTTPFetchOngoing(const HTTPFetchRequest &request, CurlHandlePool *pool); ~HTTPFetchOngoing(); CURLcode start(CURLM *multi); @@ -228,7 +228,8 @@ private: }; -HTTPFetchOngoing::HTTPFetchOngoing(HTTPFetchRequest request_, CurlHandlePool *pool_): +HTTPFetchOngoing::HTTPFetchOngoing(const HTTPFetchRequest &request_, + CurlHandlePool *pool_): pool(pool_), curl(NULL), multi(NULL), diff --git a/src/inventory.cpp b/src/inventory.cpp index 6d5b49916..8617f7263 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -46,12 +46,11 @@ static content_t content_translate_from_19_to_internal(content_t c_from) } ItemStack::ItemStack(const std::string &name_, u16 count_, - u16 wear_, IItemDefManager *itemdef) + u16 wear_, IItemDefManager *itemdef) : + name(itemdef->getAlias(name_)), + count(count_), + wear(wear_) { - name = itemdef->getAlias(name_); - count = count_; - wear = wear_; - if (name.empty() || count == 0) clear(); else if (itemdef->get(name).type == ITEM_TOOL) @@ -370,14 +369,13 @@ ItemStack ItemStack::peekItem(u32 peekcount) const Inventory */ -InventoryList::InventoryList(std::string name, u32 size, IItemDefManager *itemdef) +InventoryList::InventoryList(const std::string &name, u32 size, IItemDefManager *itemdef): + m_name(name), + m_size(size), + m_width(0), + m_itemdef(itemdef) { - m_name = name; - m_size = size; - m_width = 0; - m_itemdef = itemdef; clearItems(); - //m_dirty = false; } InventoryList::~InventoryList() @@ -712,14 +710,6 @@ ItemStack InventoryList::takeItem(u32 i, u32 takecount) return taken; } -ItemStack InventoryList::peekItem(u32 i, u32 peekcount) const -{ - if(i >= m_items.size()) - return ItemStack(); - - return m_items[i].peekItem(peekcount); -} - void InventoryList::moveItemSomewhere(u32 i, InventoryList *dest, u32 count) { // Take item from source list diff --git a/src/inventory.h b/src/inventory.h index fe1639728..a9fef3b05 100644 --- a/src/inventory.h +++ b/src/inventory.h @@ -173,7 +173,7 @@ struct ItemStack class InventoryList { public: - InventoryList(std::string name, u32 size, IItemDefManager *itemdef); + InventoryList(const std::string &name, u32 size, IItemDefManager *itemdef); ~InventoryList(); void clearItems(); void setSize(u32 newsize); @@ -239,9 +239,6 @@ public: // Returns empty item if couldn't take any. ItemStack takeItem(u32 i, u32 takecount); - // Similar to takeItem, but keeps the slot intact. - ItemStack peekItem(u32 i, u32 peekcount) const; - // Move an item to a different list (or a different stack in the same list) // count is the maximum number of items to move (0 for everything) // returns number of moved items @@ -254,8 +251,8 @@ public: private: std::vector m_items; - u32 m_size, m_width; std::string m_name; + u32 m_size, m_width; IItemDefManager *m_itemdef; }; diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index bcae874b9..8dfb851e6 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -43,15 +43,12 @@ struct EnumString es_TileAnimationType[] = }; /******************************************************************************/ -ItemDefinition read_item_definition(lua_State* L,int index, - ItemDefinition default_def) +void read_item_definition(lua_State* L, int index, + const ItemDefinition &default_def, ItemDefinition &def) { - if(index < 0) + if (index < 0) index = lua_gettop(L) + 1 + index; - // Read the item definition - ItemDefinition def = default_def; - def.type = (ItemType)getenumfield(L, index, "type", es_ItemType, ITEM_NONE); getstringfield(L, index, "name", def.name); @@ -118,8 +115,6 @@ ItemDefinition read_item_definition(lua_State* L,int index, // "" = no prediction getstringfield(L, index, "node_placement_prediction", def.node_placement_prediction); - - return def; } /******************************************************************************/ @@ -873,7 +868,7 @@ void push_tool_capabilities(lua_State *L, lua_newtable(L); // For each groupcap for (ToolGCMap::const_iterator i = toolcap.groupcaps.begin(); - i != toolcap.groupcaps.end(); i++) { + i != toolcap.groupcaps.end(); ++i) { // Create groupcap table lua_newtable(L); const std::string &name = i->first; @@ -881,7 +876,7 @@ void push_tool_capabilities(lua_State *L, // Create subtable "times" lua_newtable(L); for (UNORDERED_MAP::const_iterator - i = groupcap.times.begin(); i != groupcap.times.end(); i++) { + i = groupcap.times.begin(); i != groupcap.times.end(); ++i) { lua_pushinteger(L, i->first); lua_pushnumber(L, i->second); lua_settable(L, -3); @@ -900,7 +895,7 @@ void push_tool_capabilities(lua_State *L, lua_newtable(L); // For each damage group for (DamageGroup::const_iterator i = toolcap.damageGroups.begin(); - i != toolcap.damageGroups.end(); i++) { + i != toolcap.damageGroups.end(); ++i) { // Create damage group table lua_pushinteger(L, i->second); lua_setfield(L, -2, i->first.c_str()); @@ -939,7 +934,7 @@ void read_inventory_list(lua_State *L, int tableindex, InventoryList *invlist = inv->addList(name, listsize); int index = 0; for(std::vector::const_iterator - i = items.begin(); i != items.end(); i++){ + i = items.begin(); i != items.end(); ++i){ if(forcesize != -1 && index == forcesize) break; invlist->changeItem(index, *i); diff --git a/src/script/common/c_content.h b/src/script/common/c_content.h index 949b136eb..c701c0384 100644 --- a/src/script/common/c_content.h +++ b/src/script/common/c_content.h @@ -86,8 +86,8 @@ ToolCapabilities read_tool_capabilities (lua_State *L, int table); void push_tool_capabilities (lua_State *L, const ToolCapabilities &prop); -ItemDefinition read_item_definition (lua_State *L, int index, - ItemDefinition default_def); +void read_item_definition (lua_State *L, int index, const ItemDefinition &default_def, + ItemDefinition &def); void read_object_properties (lua_State *L, int index, ObjectProperties *prop, IItemDefManager *idef); diff --git a/src/script/lua_api/l_item.cpp b/src/script/lua_api/l_item.cpp index 2d32093a7..0e4fc4ef0 100644 --- a/src/script/lua_api/l_item.cpp +++ b/src/script/lua_api/l_item.cpp @@ -526,7 +526,7 @@ int ModApiItemMod::l_register_item_raw(lua_State *L) def.node_placement_prediction = "__default"; // Read the item definition - def = read_item_definition(L, table, def); + read_item_definition(L, table, def, def); // Default to having client-side placement prediction for nodes // ("" in item definition sets it off) diff --git a/src/tool.h b/src/tool.h index ebba5b749..f33152355 100644 --- a/src/tool.h +++ b/src/tool.h @@ -63,8 +63,8 @@ struct ToolCapabilities ToolCapabilities( float full_punch_interval_=1.4, int max_drop_level_=1, - ToolGCMap groupcaps_=ToolGCMap(), - DamageGroup damageGroups_=DamageGroup() + const ToolGCMap &groupcaps_ = ToolGCMap(), + const DamageGroup &damageGroups_ = DamageGroup() ): full_punch_interval(full_punch_interval_), max_drop_level(max_drop_level_), @@ -85,8 +85,8 @@ struct DigParams u16 wear; std::string main_group; - DigParams(bool a_diggable=false, float a_time=0, u16 a_wear=0, - std::string a_main_group=""): + DigParams(bool a_diggable = false, float a_time = 0.0f, u16 a_wear = 0, + const std::string &a_main_group = ""): diggable(a_diggable), time(a_time), wear(a_wear), -- cgit v1.2.3