From 0288f9d0a91dd628cd3c995cf528691c5875d6e9 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Tue, 21 Feb 2017 15:58:51 +0100 Subject: [PATCH] Add mcl_hunger API: Set and get hunger --- mods/PLAYER/mcl_hunger/API.md | 13 +++++++++++++ mods/PLAYER/mcl_hunger/init.lua | 17 +++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/mods/PLAYER/mcl_hunger/API.md b/mods/PLAYER/mcl_hunger/API.md index 1baf90316e..38a643f3b9 100644 --- a/mods/PLAYER/mcl_hunger/API.md +++ b/mods/PLAYER/mcl_hunger/API.md @@ -3,6 +3,19 @@ This API information is WIP. The mod API is still pretty much unofficial; this mod is mostly seen as standalone for now. This may change in the future development of MineClone 2. Hopefully. +## Hunger level +The hunger level of the player is a whole number between 0 and 20 inclusive. +0 is starving and 20 is full. The hunger level is represented in +the HUD by a statbar with 20 half-icons. + +## Functions +### `mcl_hunger.get_hunger(player)` +Returns the current hunger level of `player` (ObjectRef). + +### `mcl_hunger.set_hunger(player, hunger)` +Sets the hunger level of `player` (ObjectRef) to `hunger` immediately. +`hunger` ***must*** be between 0 and 20 inclusive. + ## Groups Items in group `food=3` will make a drinking sound and no particles. Items in group `food` with any other rating will make an eating sound and particles, diff --git a/mods/PLAYER/mcl_hunger/init.lua b/mods/PLAYER/mcl_hunger/init.lua index cb3a19e926..398254b7ed 100644 --- a/mods/PLAYER/mcl_hunger/init.lua +++ b/mods/PLAYER/mcl_hunger/init.lua @@ -60,6 +60,23 @@ local function update_hud(player) end end +-- API START -- +mcl_hunger.get_hunger = function(player) + local name = player:get_player_name() + return mcl_hunger.hunger[name] +end + +mcl_hunger.set_hunger = function(player, hunger) + local name = player:get_player_name() + mcl_hunger.hunger[name] = hunger + mcl_hunger.set_hunger_raw(player) + update_hud(player) +end + +-- END OF API -- + +-- For internal use only. Don't use the “raw” functions outside of mcl_hunger! + mcl_hunger.get_hunger_raw = function(player) local inv = player:get_inventory() if not inv then return nil end