mirror of
https://codeberg.org/tenplus1/bonemeal.git
synced 2024-11-14 04:00:16 +01:00
tidy helper function
This commit is contained in:
parent
9049dbfefa
commit
b9cf957dcc
78
init.lua
78
init.lua
|
@ -340,6 +340,39 @@ local function check_soil(pos, nodename, strength)
|
|||
end
|
||||
|
||||
|
||||
-- helper function
|
||||
local function use_checks(user, pointed_thing)
|
||||
|
||||
-- make sure we use on node
|
||||
if pointed_thing.type ~= "node" then
|
||||
return false
|
||||
end
|
||||
|
||||
-- get position and node info
|
||||
local pos = pointed_thing.under
|
||||
local node = minetest.get_node(pos)
|
||||
local def = minetest.registered_items[node.name]
|
||||
local dirt = def and def.groups
|
||||
|
||||
-- does node exist
|
||||
if not dirt then
|
||||
return false
|
||||
end
|
||||
|
||||
-- if we're using on ground, move position up
|
||||
if dirt.soil or dirt.sand or dirt.can_bonemeal then
|
||||
pos = pointed_thing.above
|
||||
end
|
||||
|
||||
-- check if protected
|
||||
if minetest.is_protected(pos, user:get_player_name()) then
|
||||
return false
|
||||
end
|
||||
|
||||
return node
|
||||
end
|
||||
|
||||
|
||||
-- global functions
|
||||
|
||||
|
||||
|
@ -521,39 +554,6 @@ function bonemeal:on_use(pos, strength, node)
|
|||
end
|
||||
|
||||
|
||||
-- helper function
|
||||
local function find_pos(user, pointed_thing)
|
||||
|
||||
-- make sure we use on node
|
||||
if pointed_thing.type ~= "node" then
|
||||
return false
|
||||
end
|
||||
|
||||
-- get position and node info
|
||||
local pos = pointed_thing.under
|
||||
local node = minetest.get_node(pos)
|
||||
local def = minetest.registered_items[node.name]
|
||||
local dirt = def and def.groups
|
||||
|
||||
-- does node exist
|
||||
if not dirt then
|
||||
return false
|
||||
end
|
||||
|
||||
-- if we're using on ground, move position up
|
||||
if dirt.soil or dirt.sand or dirt.can_bonemeal then
|
||||
pos = pointed_thing.above
|
||||
end
|
||||
|
||||
-- check if protected
|
||||
if minetest.is_protected(pos, user:get_player_name()) then
|
||||
return false
|
||||
end
|
||||
|
||||
return pos, node
|
||||
end
|
||||
|
||||
|
||||
--
|
||||
-- items
|
||||
--
|
||||
|
@ -567,10 +567,10 @@ minetest.register_craftitem("bonemeal:mulch", {
|
|||
on_use = function(itemstack, user, pointed_thing)
|
||||
|
||||
-- use helper function to do checks and return position and node
|
||||
local pos, node = find_pos(user, pointed_thing)
|
||||
local node = use_checks(user, pointed_thing)
|
||||
|
||||
-- call global on_use function with strength of 1
|
||||
if pos and bonemeal:on_use(pointed_thing.under, 1, node) then
|
||||
if node and bonemeal:on_use(pointed_thing.under, 1, node) then
|
||||
|
||||
-- take item if not in creative
|
||||
if not bonemeal.is_creative(user:get_player_name()) then
|
||||
|
@ -591,10 +591,10 @@ minetest.register_craftitem("bonemeal:bonemeal", {
|
|||
on_use = function(itemstack, user, pointed_thing)
|
||||
|
||||
-- use helper function to do checks and return position and node
|
||||
local pos, node = find_pos(user, pointed_thing)
|
||||
local node = use_checks(user, pointed_thing)
|
||||
|
||||
-- call global on_use function with strength of 2
|
||||
if pos and bonemeal:on_use(pointed_thing.under, 2, node) then
|
||||
if node and bonemeal:on_use(pointed_thing.under, 2, node) then
|
||||
|
||||
-- take item if not in creative
|
||||
if not bonemeal.is_creative(user:get_player_name()) then
|
||||
|
@ -615,10 +615,10 @@ minetest.register_craftitem("bonemeal:fertiliser", {
|
|||
on_use = function(itemstack, user, pointed_thing)
|
||||
|
||||
-- use helper function to do checks and return position and node
|
||||
local pos, node = find_pos(user, pointed_thing)
|
||||
local node = use_checks(user, pointed_thing)
|
||||
|
||||
-- call global on_use function with strength of 3
|
||||
if pos and bonemeal:on_use(pointed_thing.under, 3, node) then
|
||||
if node and bonemeal:on_use(pointed_thing.under, 3, node) then
|
||||
|
||||
-- take item if not in creative
|
||||
if not bonemeal.is_creative(user:get_player_name()) then
|
||||
|
|
Loading…
Reference in New Issue
Block a user