forked from mtcontrib/diet
		
	Fix hud_hunger compatibility (#3)
* Fix hud_hunger compatibility Add hbhunger support * Indent with tabs other than spaces * Fix for the old hud support
This commit is contained in:
		@@ -1,3 +1,5 @@
 | 
			
		||||
hbhunger?
 | 
			
		||||
hud?
 | 
			
		||||
hunger?
 | 
			
		||||
default?
 | 
			
		||||
animalmaterials?
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										60
									
								
								init.lua
									
									
									
									
									
								
							
							
						
						
									
										60
									
								
								init.lua
									
									
									
									
									
								
							@@ -23,8 +23,23 @@ function diet.save()
 | 
			
		||||
	end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function diet.item_eat(max)	
 | 
			
		||||
-- Poison player
 | 
			
		||||
local function poisenp(tick, time, time_left, player)
 | 
			
		||||
	time_left = time_left + tick
 | 
			
		||||
	if time_left < time then
 | 
			
		||||
		minetest.after(tick, poisenp, tick, time, time_left, player)
 | 
			
		||||
	else
 | 
			
		||||
		--reset hud image
 | 
			
		||||
	end
 | 
			
		||||
	if player:get_hp()-1 > 0 then
 | 
			
		||||
		player:set_hp(player:get_hp()-1)
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function diet.item_eat(max, replace_with_item, poisen, heal)
 | 
			
		||||
	return function(itemstack, user, pointed_thing)
 | 
			
		||||
 | 
			
		||||
		-- Process player data
 | 
			
		||||
		local name = user:get_player_name()
 | 
			
		||||
		local player = diet.__player(name)
 | 
			
		||||
@@ -77,12 +92,42 @@ function diet.item_eat(max)
 | 
			
		||||
		end
 | 
			
		||||
 | 
			
		||||
		-- Increase health
 | 
			
		||||
		if minetest.get_modpath("hud") and hud then
 | 
			
		||||
		if minetest.get_modpath("hbhunger") and hbhunger then
 | 
			
		||||
			minetest.sound_play({name = "hbhunger_eat_generic", gain = 1}, {pos=user:getpos(), max_hear_distance = 16})
 | 
			
		||||
 | 
			
		||||
			-- saturation
 | 
			
		||||
			local h = tonumber(hbhunger.hunger[name])
 | 
			
		||||
			h = h + points
 | 
			
		||||
			if h > 30 then h = 30 end
 | 
			
		||||
			hbhunger.hunger[name] = h
 | 
			
		||||
			hbhunger.set_hunger(user)
 | 
			
		||||
 | 
			
		||||
			-- healing
 | 
			
		||||
			local hp = user:get_hp()
 | 
			
		||||
			if hp < 20 and heal then
 | 
			
		||||
				hp = hp + heal
 | 
			
		||||
				if hp > 20 then hp = 20 end
 | 
			
		||||
				user:set_hp(hp)
 | 
			
		||||
			end
 | 
			
		||||
 | 
			
		||||
			 -- Poison
 | 
			
		||||
			if poisen then
 | 
			
		||||
				--set hud-img
 | 
			
		||||
				poisenp(1.0, poisen, 0, user)
 | 
			
		||||
			end
 | 
			
		||||
	 
 | 
			
		||||
		elseif minetest.get_modpath("hunger") and hunger then
 | 
			
		||||
			local h = tonumber(hunger.players[name].lvl)
 | 
			
		||||
			h = h + points
 | 
			
		||||
			hunger.update_hunger(user, h)
 | 
			
		||||
			
 | 
			
		||||
		elseif minetest.get_modpath("hud") and hud and hud.hunger then
 | 
			
		||||
			local h = tonumber(hud.hunger[name])
 | 
			
		||||
			h = h + points
 | 
			
		||||
			if h>30 then h = 30 end
 | 
			
		||||
			if h > 30 then h = 30 end
 | 
			
		||||
			hud.hunger[name] = h
 | 
			
		||||
			hud.save_hunger(user)
 | 
			
		||||
 | 
			
		||||
		else
 | 
			
		||||
			local hp = user:get_hp()		
 | 
			
		||||
			if (hp+points > 20) then
 | 
			
		||||
@@ -99,6 +144,7 @@ function diet.item_eat(max)
 | 
			
		||||
		diet.save()
 | 
			
		||||
 | 
			
		||||
		-- Remove item
 | 
			
		||||
		itemstack:add_item(replace_with_item)
 | 
			
		||||
		itemstack:take_item()
 | 
			
		||||
		return itemstack
 | 
			
		||||
	end
 | 
			
		||||
@@ -128,12 +174,12 @@ function diet.__register_eat(player,food,type)
 | 
			
		||||
	end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local function overwrite(name, amt)
 | 
			
		||||
local function overwrite(name, hunger_change, replace_with_item, poisen, heal)
 | 
			
		||||
	local tab = minetest.registered_items[name]
 | 
			
		||||
	if not tab then
 | 
			
		||||
		return
 | 
			
		||||
	end
 | 
			
		||||
	tab.on_use = diet.item_eat(amt)
 | 
			
		||||
	tab.on_use = diet.item_eat(hunger_change, replace_with_item, poisen, heal)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
diet.__init()
 | 
			
		||||
@@ -223,7 +269,7 @@ end
 | 
			
		||||
if minetest.get_modpath("bushes_classic") then
 | 
			
		||||
	-- bushes_classic mod, as found in the plantlife modpack
 | 
			
		||||
	local berries = {
 | 
			
		||||
	    "strawberry",
 | 
			
		||||
		"strawberry",
 | 
			
		||||
		"blackberry",
 | 
			
		||||
		"blueberry",
 | 
			
		||||
		"raspberry",
 | 
			
		||||
@@ -397,7 +443,7 @@ if minetest.get_modpath("kpgmobs") ~= nil then
 | 
			
		||||
	overwrite("kpgmobs:meat", 6)
 | 
			
		||||
	overwrite("kpgmobs:rat_cooked", 5)
 | 
			
		||||
	overwrite("kpgmobs:med_cooked", 4)
 | 
			
		||||
  	if minetest.get_modpath("bucket") then
 | 
			
		||||
	if minetest.get_modpath("bucket") then
 | 
			
		||||
	   overwrite("kpgmobs:bucket_milk", 4, "bucket:bucket_empty")
 | 
			
		||||
	end
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user