]> git.lizzy.rs Git - minetest.git/commitdiff
Devtest: Add branding iron
authorDesour <vorunbekannt75@web.de>
Fri, 16 Sep 2022 22:09:50 +0000 (00:09 +0200)
committerx2048 <codeforsmile@gmail.com>
Fri, 6 Jan 2023 21:38:35 +0000 (22:38 +0100)
Allows giving names to objects.

LICENSE.txt
games/devtest/mods/testtools/README.md
games/devtest/mods/testtools/init.lua
games/devtest/mods/testtools/textures/testtools_branding_iron.png [new file with mode: 0644]

index 33979893944bfe577af1ee182249f020e48518ba..77228684aba2eda15dfaf9c1a8eb8df042297931 100644 (file)
@@ -76,6 +76,9 @@ SmallJoker:
   textures/base/pack/cdb_clear.png
   textures/base/pack/server_favorite_delete.png (based on server_favorite.png)
 
+DS:
+  games/devtest/mods/testtools/textures/testtools_branding_iron.png
+
 License of Minetest source code
 -------------------------------
 
index 3de2969a740bcf60229c9099c9e92f074146826c..03d826da7f875264b0d5331a5aca1fe25bda46cd 100644 (file)
@@ -120,6 +120,17 @@ Usage:
 * Punch entity to increase visual size
 * Sneak+punch entity to decrease visual size
 
+## Branding Iron
+Give an object a temporary name.
+
+Usage:
+* Punch object: Brand the object
+* Punch air: Brand yourself
+* The name is valid until the object unloads.
+* Devices that accept the returned name also accept "player:<playername>" for players.
+
+Use `testtools.get_branded_object(name)` to get an ObjRef.
+
 ## Note Meta Privatizer
 Sets the 'formspec' and 'infotext' metadata fields of a node
 to private. This means that clients can no longer access these
index 6db5a7cd177ceb792a9409b71eb0ef97538fd9f7..7aa55bdb30de6150da4f07f77461236734d74f9c 100644 (file)
@@ -1,6 +1,8 @@
 local S = minetest.get_translator("testtools")
 local F = minetest.formspec_escape
 
+testtools = {}
+
 dofile(minetest.get_modpath("testtools") .. "/light.lua")
 dofile(minetest.get_modpath("testtools") .. "/privatizer.lua")
 dofile(minetest.get_modpath("testtools") .. "/particles.lua")
@@ -330,6 +332,51 @@ minetest.register_tool("testtools:entity_scaler", {
        end,
 })
 
+
+-- value-weak tables, because we don't want to keep the objrefs of unloaded objects
+local branded_objects = setmetatable({}, {__mode = "v"})
+local next_brand_num = 1
+
+function testtools.get_branded_object(name)
+       if name:sub(1, 7) == "player:" then
+               return minetest.get_player_by_name(name:sub(8))
+       elseif name:sub(1, 4) == "obj:" then
+               return branded_objects[tonumber(name:sub(5)) or 0]
+       end
+       return nil
+end
+
+minetest.register_tool("testtools:branding_iron", {
+       description = S("Branding Iron") .."\n"..
+               S("Give an object a temporary name.") .."\n"..
+               S("Punch object: Brand the object") .."\n"..
+               S("Punch air: Brand yourself") .."\n"..
+               S("The name is valid until the object unloads.") .."\n"..
+               S("Devices that accept the returned name also accept \"player:<playername>\" for players."),
+       inventory_image = "testtools_branding_iron.png",
+       groups = { testtool = 1, disable_repair = 1 },
+       on_use = function(_itemstack, user, pointed_thing)
+               local obj
+               local msg
+               if pointed_thing.type == "object" then
+                       obj = pointed_thing.ref
+                       msg = "You can now refer to this object with: \"@1\""
+               elseif pointed_thing.type == "nothing" then
+                       obj = user
+                       msg = "You can now refer to yourself with: \"@1\""
+               else
+                       return
+               end
+
+               local brand_num = next_brand_num
+               next_brand_num = next_brand_num + 1
+               branded_objects[brand_num] = obj
+
+               minetest.chat_send_player(user:get_player_name(), S(msg, "obj:"..brand_num))
+       end,
+})
+
+
 local selections = {}
 local entity_list
 local function get_entity_list()
diff --git a/games/devtest/mods/testtools/textures/testtools_branding_iron.png b/games/devtest/mods/testtools/textures/testtools_branding_iron.png
new file mode 100644 (file)
index 0000000..40c09b1
Binary files /dev/null and b/games/devtest/mods/testtools/textures/testtools_branding_iron.png differ