From 6c607e20828f97f950038dfb86891a74afb6f755 Mon Sep 17 00:00:00 2001 From: Yaman Qalieh Date: Wed, 13 May 2020 07:57:05 -0400 Subject: Allow placing auto-rotating nodes on other nodes in on_rightclick (#9859) --- builtin/common/misc_helpers.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'builtin/common/misc_helpers.lua') diff --git a/builtin/common/misc_helpers.lua b/builtin/common/misc_helpers.lua index 715f89bc4..1a0c71efd 100644 --- a/builtin/common/misc_helpers.lua +++ b/builtin/common/misc_helpers.lua @@ -290,7 +290,8 @@ if INIT == "game" then return end local undef = core.registered_nodes[unode.name] - if undef and undef.on_rightclick then + local sneaking = placer and placer:get_player_control().sneak + if undef and undef.on_rightclick and not sneaking then return undef.on_rightclick(pointed_thing.under, unode, placer, itemstack, pointed_thing) end -- cgit v1.2.3 From 65a6a316d081d3951438bbbcfce74c9c65db407a Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Tue, 26 May 2020 02:11:19 +0200 Subject: Add minetest.is_creative_enabled --- builtin/common/misc_helpers.lua | 10 ++-------- builtin/game/item.lua | 2 +- builtin/game/misc.lua | 6 ++++++ doc/lua_api.txt | 7 +++++++ games/devtest/mods/util_commands/init.lua | 2 +- 5 files changed, 17 insertions(+), 10 deletions(-) (limited to 'builtin/common/misc_helpers.lua') diff --git a/builtin/common/misc_helpers.lua b/builtin/common/misc_helpers.lua index 1a0c71efd..a88adf96d 100644 --- a/builtin/common/misc_helpers.lua +++ b/builtin/common/misc_helpers.lua @@ -345,18 +345,12 @@ if INIT == "game" then --Wrapper for rotate_and_place() to check for sneak and assume Creative mode --implies infinite stacks when performing a 6d rotation. -------------------------------------------------------------------------------- - local creative_mode_cache = core.settings:get_bool("creative_mode") - local function is_creative(name) - return creative_mode_cache or - core.check_player_privs(name, {creative = true}) - end - core.rotate_node = function(itemstack, placer, pointed_thing) local name = placer and placer:get_player_name() or "" local invert_wall = placer and placer:get_player_control().sneak or false return core.rotate_and_place(itemstack, placer, pointed_thing, - is_creative(name), - {invert_wall = invert_wall}, true) + core.is_creative_enabled(name), + {invert_wall = invert_wall}, true) end end diff --git a/builtin/game/item.lua b/builtin/game/item.lua index 3aaa71ef2..f680ce0d4 100644 --- a/builtin/game/item.lua +++ b/builtin/game/item.lua @@ -582,7 +582,7 @@ function core.node_dig(pos, node, digger) wielded = wdef.after_use(wielded, digger, node, dp) or wielded else -- Wear out tool - if not core.settings:get_bool("creative_mode") then + if not core.is_creative_enabled(diggername) then wielded:add_wear(dp.wear) if wielded:get_count() == 0 and wdef.sound and wdef.sound.breaks then core.sound_play(wdef.sound.breaks, { diff --git a/builtin/game/misc.lua b/builtin/game/misc.lua index 0ed11ada0..341e613c2 100644 --- a/builtin/game/misc.lua +++ b/builtin/game/misc.lua @@ -164,6 +164,12 @@ function core.record_protection_violation(pos, name) end end +-- To be overridden by Creative mods + +local creative_mode_cache = core.settings:get_bool("creative_mode") +function core.is_creative_enabled(name) + return creative_mode_cache +end -- Checks if specified volume intersects a protected volume diff --git a/doc/lua_api.txt b/doc/lua_api.txt index c4310aa5b..bb9df5373 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -5475,6 +5475,13 @@ Misc. * `minetest.record_protection_violation(pos, name)` * This function calls functions registered with `minetest.register_on_protection_violation`. +* `minetest.is_creative_enabled(name)`: returns boolean + * Returning `true` means that Creative Mode is enabled for player `name`. + * `name` will be `""` for non-players or if the player is unknown. + * This function should be overridden by Creative Mode-related mods to + implement a per-player Creative Mode. + * By default, this function returns `true` if the setting + `creative_mode` is `true` and `false` otherwise. * `minetest.is_area_protected(pos1, pos2, player_name, interval)` * Returns the position of the first node that `player_name` may not modify in the specified cuboid between `pos1` and `pos2`. diff --git a/games/devtest/mods/util_commands/init.lua b/games/devtest/mods/util_commands/init.lua index ad8d3f9ba..3a0e91a41 100644 --- a/games/devtest/mods/util_commands/init.lua +++ b/games/devtest/mods/util_commands/init.lua @@ -66,7 +66,7 @@ if s_infplace == "true" then elseif s_infplace == "false" then infplace = false else - infplace = minetest.settings:get_bool("creative_mode", false) + infplace = minetest.is_creative_enabled("") end minetest.register_chatcommand("infplace", { -- cgit v1.2.3 From b16f841756ef86e83710ad2fddf2cd5bafdf4bcc Mon Sep 17 00:00:00 2001 From: Paul Ouellette Date: Tue, 9 Jun 2020 13:37:25 -0400 Subject: LuaItemStack: Add __tostring metamethod (#8785) * LuaItemStack: Add __tostring metamethod * Clean up LuaItemStack::checkobject --- builtin/common/misc_helpers.lua | 2 ++ src/inventory.cpp | 29 ++++++++++++++++------------- src/inventory.h | 4 ++-- src/script/lua_api/l_item.cpp | 25 ++++++++++++++++++------- src/script/lua_api/l_item.h | 3 +++ 5 files changed, 41 insertions(+), 22 deletions(-) (limited to 'builtin/common/misc_helpers.lua') diff --git a/builtin/common/misc_helpers.lua b/builtin/common/misc_helpers.lua index a88adf96d..e29a9f422 100644 --- a/builtin/common/misc_helpers.lua +++ b/builtin/common/misc_helpers.lua @@ -20,6 +20,8 @@ local function basic_dump(o) -- dump's output is intended for humans. --elseif tp == "function" then -- return string.format("loadstring(%q)", string.dump(o)) + elseif tp == "userdata" then + return tostring(o) else return string.format("<%s>", tp) end diff --git a/src/inventory.cpp b/src/inventory.cpp index 77ecf5876..349ee503d 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -56,28 +56,31 @@ ItemStack::ItemStack(const std::string &name_, u16 count_, count = 1; } -void ItemStack::serialize(std::ostream &os) const +void ItemStack::serialize(std::ostream &os, bool serialize_meta) const { if (empty()) return; // Check how many parts of the itemstring are needed int parts = 1; - if(count != 1) - parts = 2; - if(wear != 0) - parts = 3; if (!metadata.empty()) parts = 4; + else if (wear != 0) + parts = 3; + else if (count != 1) + parts = 2; - os<= 2) - os<<" "<= 3) - os<<" "<= 2) + os << " " << count; + if (parts >= 3) + os << " " << wear; if (parts >= 4) { os << " "; - metadata.serialize(os); + if (serialize_meta) + metadata.serialize(os); + else + os << ""; } } @@ -240,10 +243,10 @@ void ItemStack::deSerialize(const std::string &str, IItemDefManager *itemdef) deSerialize(is, itemdef); } -std::string ItemStack::getItemString() const +std::string ItemStack::getItemString(bool include_meta) const { std::ostringstream os(std::ios::binary); - serialize(os); + serialize(os, include_meta); return os.str(); } diff --git a/src/inventory.h b/src/inventory.h index 2828d3e5a..67a7859ed 100644 --- a/src/inventory.h +++ b/src/inventory.h @@ -40,13 +40,13 @@ struct ItemStack ~ItemStack() = default; // Serialization - void serialize(std::ostream &os) const; + void serialize(std::ostream &os, bool serialize_meta = true) const; // Deserialization. Pass itemdef unless you don't want aliases resolved. void deSerialize(std::istream &is, IItemDefManager *itemdef = NULL); void deSerialize(const std::string &s, IItemDefManager *itemdef = NULL); // Returns the string used for inventory - std::string getItemString() const; + std::string getItemString(bool include_meta = true) const; // Returns the tooltip std::string getDescription(IItemDefManager *itemdef) const; diff --git a/src/script/lua_api/l_item.cpp b/src/script/lua_api/l_item.cpp index 0a403acbd..d67cab76f 100644 --- a/src/script/lua_api/l_item.cpp +++ b/src/script/lua_api/l_item.cpp @@ -37,6 +37,15 @@ int LuaItemStack::gc_object(lua_State *L) return 0; } +// __tostring metamethod +int LuaItemStack::mt_tostring(lua_State *L) +{ + LuaItemStack *o = checkobject(L, 1); + std::string itemstring = o->m_stack.getItemString(false); + lua_pushfstring(L, "ItemStack(\"%s\")", itemstring.c_str()); + return 1; +} + // is_empty(self) -> true/false int LuaItemStack::l_is_empty(lua_State *L) { @@ -433,12 +442,9 @@ int LuaItemStack::create(lua_State *L, const ItemStack &item) return 1; } -LuaItemStack* LuaItemStack::checkobject(lua_State *L, int narg) +LuaItemStack *LuaItemStack::checkobject(lua_State *L, int narg) { - luaL_checktype(L, narg, LUA_TUSERDATA); - void *ud = luaL_checkudata(L, narg, className); - if(!ud) luaL_typerror(L, narg, className); - return *(LuaItemStack**)ud; // unbox pointer + return *(LuaItemStack **)luaL_checkudata(L, narg, className); } void LuaItemStack::Register(lua_State *L) @@ -448,9 +454,10 @@ void LuaItemStack::Register(lua_State *L) luaL_newmetatable(L, className); int metatable = lua_gettop(L); + // hide metatable from Lua getmetatable() lua_pushliteral(L, "__metatable"); lua_pushvalue(L, methodtable); - lua_settable(L, metatable); // hide metatable from Lua getmetatable() + lua_settable(L, metatable); lua_pushliteral(L, "__index"); lua_pushvalue(L, methodtable); @@ -460,12 +467,16 @@ void LuaItemStack::Register(lua_State *L) lua_pushcfunction(L, gc_object); lua_settable(L, metatable); + lua_pushliteral(L, "__tostring"); + lua_pushcfunction(L, mt_tostring); + lua_settable(L, metatable); + lua_pop(L, 1); // drop metatable luaL_openlib(L, 0, methods, 0); // fill methodtable lua_pop(L, 1); // drop methodtable - // Can be created from Lua (LuaItemStack(itemstack or itemstring or table or nil)) + // Can be created from Lua (ItemStack(itemstack or itemstring or table or nil)) lua_register(L, className, create_object); } diff --git a/src/script/lua_api/l_item.h b/src/script/lua_api/l_item.h index 6fab58045..98744c071 100644 --- a/src/script/lua_api/l_item.h +++ b/src/script/lua_api/l_item.h @@ -34,6 +34,9 @@ private: // garbage collector static int gc_object(lua_State *L); + // __tostring metamethod + static int mt_tostring(lua_State *L); + // is_empty(self) -> true/false static int l_is_empty(lua_State *L); -- cgit v1.2.3