Default: Expose the formspec getter functions (#1783)

This commit is contained in:
SmallJoker 2017-07-16 14:31:34 +02:00 committed by GitHub
parent 1d5bc15f9a
commit e5189760b3
3 changed files with 74 additions and 32 deletions

View File

@ -592,6 +592,48 @@ Default constants
`default.LIGHT_MAX` The maximum light level (see [Node definition] light_source) `default.LIGHT_MAX` The maximum light level (see [Node definition] light_source)
GUI and formspecs
-----------------
`default.get_hotbar_bg(x, y)`
* Get the hotbar background as string, containing the formspec elements
* x: Horizontal position in the formspec
* y: Vertical position in the formspec
`default.gui_bg`
* Background color formspec element
`default.gui_bg_img`
* Image overlay formspec element for the background to use in formspecs
`default.gui_slots`
* `listcolors` formspec element that is used to format the slots in formspecs
`default.gui_survival_form`
* Entire formspec for the survival inventory
`default.get_chest_formspec(pos)`
* Get the chest formspec using the defined GUI elements
* pos: Location of the node
`default.get_furnace_active_formspec(fuel_percent, item_percent)`
* Get the active furnace formspec using the defined GUI elements
* fuel_percent: Percent of how much the fuel is used
* item_percent: Percent of how much the item is cooked
`default.get_furnace_inactive_formspec()`
* Get the inactive furnace formspec using the defined GUI elements
Player API Player API
---------- ----------

View File

@ -3,9 +3,8 @@
-- Formspecs -- Formspecs
-- --
local function active_formspec(fuel_percent, item_percent) function default.get_furnace_active_formspec(fuel_percent, item_percent)
local formspec = return "size[8,8.5]"..
"size[8,8.5]"..
default.gui_bg.. default.gui_bg..
default.gui_bg_img.. default.gui_bg_img..
default.gui_slots.. default.gui_slots..
@ -25,11 +24,10 @@ local function active_formspec(fuel_percent, item_percent)
"listring[context;fuel]".. "listring[context;fuel]"..
"listring[current_player;main]".. "listring[current_player;main]"..
default.get_hotbar_bg(0, 4.25) default.get_hotbar_bg(0, 4.25)
return formspec
end end
local inactive_formspec = function default.get_furnace_inactive_formspec()
"size[8,8.5]".. return "size[8,8.5]"..
default.gui_bg.. default.gui_bg..
default.gui_bg_img.. default.gui_bg_img..
default.gui_slots.. default.gui_slots..
@ -47,6 +45,7 @@ local inactive_formspec =
"listring[context;fuel]".. "listring[context;fuel]"..
"listring[current_player;main]".. "listring[current_player;main]"..
default.get_hotbar_bg(0, 4.25) default.get_hotbar_bg(0, 4.25)
end
-- --
-- Node callback functions that are the same for active and inactive furnace -- Node callback functions that are the same for active and inactive furnace
@ -190,7 +189,7 @@ local function furnace_node_timer(pos, elapsed)
-- --
-- Update formspec, infotext and node -- Update formspec, infotext and node
-- --
local formspec = inactive_formspec local formspec
local item_state local item_state
local item_percent = 0 local item_percent = 0
if cookable then if cookable then
@ -216,7 +215,7 @@ local function furnace_node_timer(pos, elapsed)
active = "active " active = "active "
local fuel_percent = math.floor(fuel_time / fuel_totaltime * 100) local fuel_percent = math.floor(fuel_time / fuel_totaltime * 100)
fuel_state = fuel_percent .. "%" fuel_state = fuel_percent .. "%"
formspec = active_formspec(fuel_percent, item_percent) formspec = default.get_furnace_active_formspec(fuel_percent, item_percent)
swap_node(pos, "default:furnace_active") swap_node(pos, "default:furnace_active")
-- make sure timer restarts automatically -- make sure timer restarts automatically
result = true result = true
@ -224,12 +223,14 @@ local function furnace_node_timer(pos, elapsed)
if not fuellist[1]:is_empty() then if not fuellist[1]:is_empty() then
fuel_state = "0%" fuel_state = "0%"
end end
formspec = default.get_furnace_inactive_formspec()
swap_node(pos, "default:furnace") swap_node(pos, "default:furnace")
-- stop timer on the inactive furnace -- stop timer on the inactive furnace
minetest.get_node_timer(pos):stop() minetest.get_node_timer(pos):stop()
end end
local infotext = "Furnace " .. active .. "(Item: " .. item_state .. "; Fuel: " .. fuel_state .. ")" local infotext = "Furnace " .. active .. "(Item: " .. item_state ..
"; Fuel: " .. fuel_state .. ")"
-- --
-- Set meta values -- Set meta values
@ -266,7 +267,7 @@ minetest.register_node("default:furnace", {
on_construct = function(pos) on_construct = function(pos)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
meta:set_string("formspec", inactive_formspec) meta:set_string("formspec", default.get_furnace_inactive_formspec())
local inv = meta:get_inventory() local inv = meta:get_inventory()
inv:set_size('src', 1) inv:set_size('src', 1)
inv:set_size('fuel', 1) inv:set_size('fuel', 1)
@ -327,4 +328,3 @@ minetest.register_node("default:furnace_active", {
allow_metadata_inventory_move = allow_metadata_inventory_move, allow_metadata_inventory_move = allow_metadata_inventory_move,
allow_metadata_inventory_take = allow_metadata_inventory_take, allow_metadata_inventory_take = allow_metadata_inventory_take,
}) })

View File

@ -1766,7 +1766,7 @@ minetest.register_node("default:lava_flowing", {
-- Tools / "Advanced" crafting / Non-"natural" -- Tools / "Advanced" crafting / Non-"natural"
-- --
local function get_chest_formspec(pos) function default.get_chest_formspec(pos)
local spos = pos.x .. "," .. pos.y .. "," .. pos.z local spos = pos.x .. "," .. pos.y .. "," .. pos.z
local formspec = local formspec =
"size[8,9]" .. "size[8,9]" ..
@ -1890,7 +1890,7 @@ function default.register_chest(name, d)
end end
minetest.after(0.2, minetest.show_formspec, minetest.after(0.2, minetest.show_formspec,
clicker:get_player_name(), clicker:get_player_name(),
"default:chest", get_chest_formspec(pos)) "default:chest", default.get_chest_formspec(pos))
open_chests[clicker:get_player_name()] = { pos = pos, open_chests[clicker:get_player_name()] = { pos = pos,
sound = def.sound_close, swap = name } sound = def.sound_close, swap = name }
end end
@ -1912,7 +1912,7 @@ function default.register_chest(name, d)
minetest.show_formspec( minetest.show_formspec(
player:get_player_name(), player:get_player_name(),
"default:chest_locked", "default:chest_locked",
get_chest_formspec(pos) default.get_chest_formspec(pos)
) )
end end
def.on_skeleton_key_use = function(pos, player, newsecret) def.on_skeleton_key_use = function(pos, player, newsecret)
@ -1957,7 +1957,7 @@ function default.register_chest(name, d)
end end
minetest.after(0.2, minetest.show_formspec, minetest.after(0.2, minetest.show_formspec,
clicker:get_player_name(), clicker:get_player_name(),
"default:chest", get_chest_formspec(pos)) "default:chest", default.get_chest_formspec(pos))
open_chests[clicker:get_player_name()] = { pos = pos, open_chests[clicker:get_player_name()] = { pos = pos,
sound = def.sound_close, swap = name } sound = def.sound_close, swap = name }
end end