Bucket: Allow buckets to trigger the on_punch of entities and nodes
authorFernando Carmona Varo <ferkiwi@gmail.com>
Sun, 11 Sep 2016 07:55:33 +0000 (09:55 +0200)
committerparamat <mat.gregory@virginmedia.com>
Wed, 2 Nov 2016 12:39:56 +0000 (12:39 +0000)
The purpose of this is to allow mods to be able to interact (e.g. fill up)
an empty bucket when it is used to punch a node that's not a liquid source
or when punching a custom entity (e.g. milking a cow).

game_api.txt
mods/bucket/init.lua

index 6bf51af4847830ceb1ff3f81871fafb5c97dc521..2958bfdcfced1416b2500c98667cd74afbe0065b 100644 (file)
@@ -19,7 +19,6 @@ Bucket API
 
 The bucket API allows registering new types of buckets for non-default liquids.
 
-
        bucket.register_liquid(
                "default:lava_source",   -- name of the source node
                "default:lava_flowing",  -- name of the flowing node
@@ -32,6 +31,9 @@ The bucket API allows registering new types of buckets for non-default liquids.
                                         -- Needed to avoid creating holes in sloping rivers.
        )
 
+The filled bucket item is returned to the player that uses an empty bucket pointing to the given liquid source.
+When punching with an empty bucket pointing to an entity or a non-liquid node, the on_punch of the entity or node will be triggered.
+
 Beds API
 --------
 
index 638ce1f42c1d8b4ca5745865356e8cafcddb2a7c..5076dece3992186b7a679e5b662bdfe72318fe2c 100644 (file)
@@ -115,8 +115,11 @@ minetest.register_craftitem("bucket:bucket_empty", {
        stack_max = 99,
        liquids_pointable = true,
        on_use = function(itemstack, user, pointed_thing)
-               -- Must be pointing to node
-               if pointed_thing.type ~= "node" then
+               if pointed_thing.type == "object" then
+                       pointed_thing.ref:punch(user, 1.0, { full_punch_interval=1.0 }, nil)
+                       return user:get_wielded_item()
+               elseif pointed_thing.type ~= "node" then
+                       -- do nothing if it's neither object nor node
                        return
                end
                -- Check if pointing to a liquid source
@@ -165,6 +168,13 @@ minetest.register_craftitem("bucket:bucket_empty", {
                        end
 
                        return ItemStack(giving_back)
+               else
+                       -- non-liquid nodes will have their on_punch triggered
+                       local node_def = minetest.registered_nodes[node.name]
+                       if node_def then
+                               node_def.on_punch(pointed_thing.under, node, user, pointed_thing)
+                       end
+                       return user:get_wielded_item()
                end
        end,
 })