mirror of
				https://github.com/rubenwardy/diet.git
				synced 2025-10-31 06:35:22 +01:00 
			
		
		
		
	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? | hunger? | ||||||
| default? | default? | ||||||
| animalmaterials? | animalmaterials? | ||||||
|   | |||||||
							
								
								
									
										56
									
								
								init.lua
									
									
									
									
									
								
							
							
						
						
									
										56
									
								
								init.lua
									
									
									
									
									
								
							| @@ -23,8 +23,23 @@ function diet.save() | |||||||
| 	end | 	end | ||||||
| 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) | 	return function(itemstack, user, pointed_thing) | ||||||
|  |  | ||||||
| 		-- Process player data | 		-- Process player data | ||||||
| 		local name = user:get_player_name() | 		local name = user:get_player_name() | ||||||
| 		local player = diet.__player(name) | 		local player = diet.__player(name) | ||||||
| @@ -77,12 +92,42 @@ function diet.item_eat(max) | |||||||
| 		end | 		end | ||||||
|  |  | ||||||
| 		-- Increase health | 		-- 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]) | 			local h = tonumber(hud.hunger[name]) | ||||||
| 			h = h + points | 			h = h + points | ||||||
| 			if h>30 then h = 30 end | 			if h > 30 then h = 30 end | ||||||
| 			hud.hunger[name] = h | 			hud.hunger[name] = h | ||||||
| 			hud.save_hunger(user) | 			hud.save_hunger(user) | ||||||
|  |  | ||||||
| 		else | 		else | ||||||
| 			local hp = user:get_hp()		 | 			local hp = user:get_hp()		 | ||||||
| 			if (hp+points > 20) then | 			if (hp+points > 20) then | ||||||
| @@ -99,6 +144,7 @@ function diet.item_eat(max) | |||||||
| 		diet.save() | 		diet.save() | ||||||
|  |  | ||||||
| 		-- Remove item | 		-- Remove item | ||||||
|  | 		itemstack:add_item(replace_with_item) | ||||||
| 		itemstack:take_item() | 		itemstack:take_item() | ||||||
| 		return itemstack | 		return itemstack | ||||||
| 	end | 	end | ||||||
| @@ -128,12 +174,12 @@ function diet.__register_eat(player,food,type) | |||||||
| 	end | 	end | ||||||
| end | end | ||||||
|  |  | ||||||
| local function overwrite(name, amt) | local function overwrite(name, hunger_change, replace_with_item, poisen, heal) | ||||||
| 	local tab = minetest.registered_items[name] | 	local tab = minetest.registered_items[name] | ||||||
| 	if not tab then | 	if not tab then | ||||||
| 		return | 		return | ||||||
| 	end | 	end | ||||||
| 	tab.on_use = diet.item_eat(amt) | 	tab.on_use = diet.item_eat(hunger_change, replace_with_item, poisen, heal) | ||||||
| end | end | ||||||
|  |  | ||||||
| diet.__init() | diet.__init() | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user