summaryrefslogtreecommitdiff
path: root/api.lua
blob: f3af43219a07f44c8bdf79190d5fef4252bfe066 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
 -- FOOD MOD
-- A mod written by rubenwardy that adds
-- food to the minetest game
-- =====================================
-- >> food/api.lua
-- The supporting api for the mod
-- =====================================

-- Boilerplate to support localized strings if intllib mod is installed.
S = 0
if (intllib) then
	dofile(minetest.get_modpath("intllib").."/intllib.lua")
	S = intllib.Getter(minetest.get_current_modname())
else
	S = function ( s ) return s end
end

food = {
	modules = {},
	disabled_modules = {},
	debug = true,
	version = 2.3
}

-- Checks for external content, and adds support
function food.support(group, item)
	if type(group) == "table" then
		for i = 1, #group do
			food.support(group[i], item)
		end
		return
	end
	if type(item) == "table" then
		for i = 1, #item do
			food.support(group, item[i])
		end
		return
	end

	local idx = string.find(item, ":")
	if idx <= 1 then
		error("[Food Error] food.support - error in item name ('" .. item .. "')")
	end
	mod = string.sub(item, 1, idx - 1)

	if not minetest.get_modpath(mod) then
		if food.debug then
			print("[Food Debug] Mod '"..mod.."' is not installed")
		end
		return
	end

	local data = minetest.registered_items[item]
	if not data then
		print("[Food Warning] Item '"..item.."' not found")
		return
	end


	food.disable(group)

	-- Add group
	g = {}
	if data.groups then
		for k, v in pairs(data.groups) do
			g[k] = v
		end
	end
	g["food_"..group] = 1
	minetest.override_item(item, {groups = g})
end

function food.disable(name)
	food.disabled_modules[name] = true
end

-- Adds a module
function food.module(name, func, ingred)
	if food.disabled_modules[name] then
		return
	end
	if ingred then
		for name, def in pairs(minetest.registered_items) do
			local g = def.groups and def.groups["food_"..name] or 0
			if g > 0 then
				print("cancelled")
				return
			end
		end
	
		if food.debug then
			print("[Food Debug] Registering " .. name .. " fallback definition")
		end
	elseif food.debug then
		print("[Food Debug] Module " .. name)
	end
	func()
end

-- Checks for hunger mods to register food on
function food.item_eat(amt)
	if minetest.get_modpath("diet") then
		return diet.item_eat(amt)
	elseif minetest.get_modpath("hud") then
		return hud.item_eat(amt)
	else
		return minetest.item_eat(amt)
	end
end

-- Registers craft item or node depending on settings
function food.register(name, data, mod)
	if (minetest.setting_getbool("food_use_2d") or (mod ~= nil and minetest.setting_getbool("food_"..mod.."_use_2d"))) then
		minetest.register_craftitem(name,{
			description = data.description,
			inventory_image = data.inventory_image,
			groups = data.groups,
			on_use = data.on_use
		})
	else
		local newdata = {
			description = data.description,
			tiles = data.tiles,
			groups = data.groups,
			on_use = data.on_use,
			walkable = false,
			sunlight_propagates = true,
			drawtype = "nodebox",
			paramtype = "light",
			node_box = data.node_box
		}
		if (minetest.setting_getbool("food_2d_inv_image")) then
			newdata.inventory_image = data.inventory_image
		end
		minetest.register_node(name,newdata)
	end
end

-- Allows for overriding in the future
function food.craft(craft)
	minetest.register_craft(craft)
end