generated from your-land/yl_template
Formatting
This commit is contained in:
parent
705ea82e61
commit
89a16a3479
116
api.lua
116
api.lua
@ -1,28 +1,20 @@
|
||||
|
||||
-- get_stage
|
||||
--
|
||||
|
||||
local function get_stage(modname, nodename)
|
||||
|
||||
if (minetest.get_modpath(modname) == nil) then
|
||||
return false, t("modname_does_not_exist")
|
||||
end
|
||||
|
||||
local def = minetest.registered_nodes[modname..":"..nodename]
|
||||
local def = minetest.registered_nodes[modname .. ":" .. nodename]
|
||||
|
||||
if (def == nil) then
|
||||
return false, t("node_does_not_exist")
|
||||
end
|
||||
if (def == nil) then return false, t("node_does_not_exist") end
|
||||
|
||||
local stage = def._stage
|
||||
|
||||
if (stage == nil) then
|
||||
return false, t("node_does_not_have_stage")
|
||||
end
|
||||
if (stage == nil) then return false, t("node_does_not_have_stage") end
|
||||
|
||||
if (type(stage) ~= "table") then
|
||||
return false, t("stage_not_table")
|
||||
end
|
||||
if (type(stage) ~= "table") then return false, t("stage_not_table") end
|
||||
|
||||
if (yl_api_nodestages.is_valid_stage(stage) == false) then
|
||||
return false, t("stage_not_valid")
|
||||
@ -44,9 +36,7 @@ local function create_stagenode(stage)
|
||||
-- variable stage may be string errormessage or table stage
|
||||
local success, stage = yl_api_nodestages.get_valid_copy_of_stage(stage)
|
||||
|
||||
if success == false then
|
||||
return false, stage
|
||||
end
|
||||
if success == false then return false, stage end
|
||||
|
||||
-- MT properties
|
||||
local nodename = stage.stage_name
|
||||
@ -63,9 +53,8 @@ local function create_stagenode(stage)
|
||||
return true
|
||||
end
|
||||
|
||||
function yl_api_nodestages.create_stagenode(stage)
|
||||
return create_stagenode(stage)
|
||||
end
|
||||
function yl_api_nodestages.create_stagenode(stage) return
|
||||
create_stagenode(stage) end
|
||||
|
||||
-- add_stage
|
||||
--
|
||||
@ -75,12 +64,11 @@ local function add_stage(stage)
|
||||
-- variable stage may be string errormessage or table stage
|
||||
local success, stage = yl_api_nodestages.get_valid_copy_of_stage(stage)
|
||||
|
||||
if success == false then
|
||||
return false, stage
|
||||
end
|
||||
if success == false then return false, stage end
|
||||
|
||||
if (stage.overwrite == true) then
|
||||
return false, yl_api_nodestages.t("error_use_overwrite_instead", dump(nodename))
|
||||
return false, yl_api_nodestages.t("error_use_overwrite_instead",
|
||||
dump(nodename))
|
||||
end
|
||||
|
||||
local nodename = stage.stage_name
|
||||
@ -88,19 +76,23 @@ local function add_stage(stage)
|
||||
local target_def = minetest.registered_nodes[nodename]
|
||||
|
||||
if (target_def == nil) then
|
||||
return false, yl_api_nodestages.t("error_item_not_exists", dump(nodename))
|
||||
return false,
|
||||
yl_api_nodestages.t("error_item_not_exists", dump(nodename))
|
||||
end
|
||||
|
||||
if (target_def.on_timer ~= nil) then
|
||||
return false, yl_api_nodestages.t("error_item_has_timer", dump(nodename))
|
||||
return false,
|
||||
yl_api_nodestages.t("error_item_has_timer", dump(nodename))
|
||||
end
|
||||
|
||||
if (target_def.on_construct ~= nil) then
|
||||
return false, yl_api_nodestages.t("error_item_has_construct", dump(nodename))
|
||||
return false,
|
||||
yl_api_nodestages.t("error_item_has_construct", dump(nodename))
|
||||
end
|
||||
|
||||
if (target_def._stage ~= nil) then
|
||||
return false, yl_api_nodestages.t("error_item_has_stage", dump(nodename))
|
||||
return false,
|
||||
yl_api_nodestages.t("error_item_has_stage", dump(nodename))
|
||||
end
|
||||
|
||||
target_def._stage = stage
|
||||
@ -115,9 +107,7 @@ local function add_stage(stage)
|
||||
return true
|
||||
end
|
||||
|
||||
function yl_api_nodestages.add_stage(stage)
|
||||
return add_stage(stage)
|
||||
end
|
||||
function yl_api_nodestages.add_stage(stage) return add_stage(stage) end
|
||||
|
||||
-- overwrite_stage
|
||||
--
|
||||
@ -127,12 +117,11 @@ local function overwrite_stage(stage)
|
||||
-- variable stage may be string errormessage or table stage
|
||||
local success, stage = yl_api_nodestages.get_valid_copy_of_stage(stage)
|
||||
|
||||
if success == false then
|
||||
return false, stage
|
||||
end
|
||||
if success == false then return false, stage end
|
||||
|
||||
if (stage.overwrite ~= true) then
|
||||
return false, yl_api_nodestages.t("error_use_add_instead", dump(nodename))
|
||||
return false,
|
||||
yl_api_nodestages.t("error_use_add_instead", dump(nodename))
|
||||
end
|
||||
|
||||
local nodename = stage.stage_name
|
||||
@ -140,7 +129,8 @@ local function overwrite_stage(stage)
|
||||
local target_def = minetest.registered_nodes[nodename]
|
||||
|
||||
if (target_def == nil) then
|
||||
return false, yl_api_nodestages.t("error_item_not_exists", dump(nodename))
|
||||
return false,
|
||||
yl_api_nodestages.t("error_item_not_exists", dump(nodename))
|
||||
end
|
||||
|
||||
--[[ Since this is the overwrite, we deliberately do not check for existing nodetimers, construct or stages
|
||||
@ -155,7 +145,7 @@ local function overwrite_stage(stage)
|
||||
if (target_def._stage ~= nil) then
|
||||
return false, yl_api_nodestages.t("error_item_has_stage", dump(nodename))
|
||||
end
|
||||
]]--
|
||||
]] --
|
||||
|
||||
-- Cache the values we are going to overwrite
|
||||
|
||||
@ -180,9 +170,7 @@ local function overwrite_stage(stage)
|
||||
return true
|
||||
end
|
||||
|
||||
function yl_api_nodestages.overwrite_stage(stage)
|
||||
return overwrite_stage(stage)
|
||||
end
|
||||
function yl_api_nodestages.overwrite_stage(stage) return overwrite_stage(stage) end
|
||||
|
||||
-- delete_stage
|
||||
--
|
||||
@ -193,28 +181,22 @@ local function delete_stage(modname, nodename)
|
||||
return false, t("modname_does_not_exist")
|
||||
end
|
||||
|
||||
local def = minetest.registered_nodes[modname..":"..nodename]
|
||||
local def = minetest.registered_nodes[modname .. ":" .. nodename]
|
||||
|
||||
if (def == nil) then
|
||||
return false, t("node_does_not_exist")
|
||||
end
|
||||
if (def == nil) then return false, t("node_does_not_exist") end
|
||||
|
||||
local stage = def._stage
|
||||
|
||||
if (stage == nil) then
|
||||
return false, t("node_does_not_have_stage")
|
||||
end
|
||||
if (stage == nil) then return false, t("node_does_not_have_stage") end
|
||||
|
||||
if (type(stage) ~= "table") then
|
||||
return false, t("stage_not_table")
|
||||
end
|
||||
if (type(stage) ~= "table") then return false, t("stage_not_table") end
|
||||
|
||||
def._stage = nil
|
||||
def.on_timer = nil
|
||||
def.on_construct = nil
|
||||
def.on_destruct = nil -- TODO: Do we really need a destruct? If we do, shouldn't we check for it?
|
||||
|
||||
if (minetest.registered_nodes[modname..":"..nodename]._stage ~= nil) then
|
||||
if (minetest.registered_nodes[modname .. ":" .. nodename]._stage ~= nil) then
|
||||
return false, t("stage_not_deleted")
|
||||
end
|
||||
|
||||
@ -233,17 +215,13 @@ local function register_stage(p_stage)
|
||||
-- variable stage may be string errormessage or table stage
|
||||
local success, stage = yl_api_nodestages.get_valid_copy_of_stage(p_stage)
|
||||
|
||||
if (success == false) then
|
||||
return false, stage
|
||||
end
|
||||
if (success == false) then return false, stage end
|
||||
|
||||
-- Create the node
|
||||
local message
|
||||
success, message = yl_api_nodestages.create_stagenode(stage)
|
||||
|
||||
if (success == false) then
|
||||
return false, message
|
||||
end
|
||||
if (success == false) then return false, message end
|
||||
|
||||
-- Add or overwrite the stage
|
||||
if (stage.overwrite == true) then
|
||||
@ -252,23 +230,19 @@ local function register_stage(p_stage)
|
||||
success, message = yl_api_nodestages.add(stage)
|
||||
end
|
||||
|
||||
if (success == false) then
|
||||
return false, message
|
||||
end
|
||||
if (success == false) then return false, message end
|
||||
|
||||
return true, nil
|
||||
end
|
||||
|
||||
function yl_api_nodestages.register_stage(stage)
|
||||
return register_stage(stage)
|
||||
end
|
||||
function yl_api_nodestages.register_stage(stage) return register_stage(stage) end
|
||||
|
||||
-- register_stages
|
||||
--
|
||||
|
||||
local function register_stages(stages)
|
||||
|
||||
local good, bad, total = 0,0,0
|
||||
local good, bad, total = 0, 0, 0
|
||||
local reasons = {}
|
||||
|
||||
for _, stage in ipairs(stages) do
|
||||
@ -297,9 +271,8 @@ local function register_stages(stages)
|
||||
end
|
||||
end
|
||||
|
||||
function yl_api_nodestages.register_stages(stages)
|
||||
return register_stages(stages)
|
||||
end
|
||||
function yl_api_nodestages.register_stages(stages) return
|
||||
register_stages(stages) end
|
||||
|
||||
-- meh
|
||||
|
||||
@ -308,8 +281,8 @@ function yl_api_nodestages.register_stages(stages, overwrite)
|
||||
-- defense
|
||||
|
||||
if ((type(overwrite) ~= "boolean") and (overwrite ~= nil)) then
|
||||
return false,
|
||||
yl_api_nodestages.t("error_not_a_boolean_or_nil", dump(overwrite))
|
||||
return false, yl_api_nodestages.t("error_not_a_boolean_or_nil",
|
||||
dump(overwrite))
|
||||
end
|
||||
|
||||
--[[ modname is now part of the stage and needs to be validated there
|
||||
@ -322,7 +295,8 @@ function yl_api_nodestages.register_stages(stages, overwrite)
|
||||
end
|
||||
|
||||
if (yl_api_nodestages.is_valid(stages) == false) then
|
||||
return false, yl_api_nodestages.t("error_not_a_valid_table", dump(stages))
|
||||
return false,
|
||||
yl_api_nodestages.t("error_not_a_valid_table", dump(stages))
|
||||
end
|
||||
|
||||
-- payload
|
||||
@ -360,7 +334,8 @@ function yl_api_nodestages.register_stages(stages, overwrite)
|
||||
if (((overwrite == false) or (overwrite == nil)) and
|
||||
(minetest.registered_nodes[nodename] ~= nil)) then
|
||||
-- No overwrite, but item exists
|
||||
return false, yl_api_nodestages.t("error_item_exists", dump(nodename))
|
||||
return false,
|
||||
yl_api_nodestages.t("error_item_exists", dump(nodename))
|
||||
end
|
||||
|
||||
-- MT properties
|
||||
@ -384,5 +359,6 @@ function yl_api_nodestages.register_stages(stages, overwrite)
|
||||
num_stages = num_stages + 1
|
||||
end
|
||||
|
||||
return true, yl_api_nodestages.t("success_changed_num_stages", dump(num_stages))
|
||||
return true,
|
||||
yl_api_nodestages.t("success_changed_num_stages", dump(num_stages))
|
||||
end
|
||||
|
15
internal.lua
15
internal.lua
@ -1,4 +1,3 @@
|
||||
|
||||
-- The functions and variables in this file are only for use in the mod itself.
|
||||
-- Those that do real work should be local and wrapped in public functions
|
||||
local function log(text)
|
||||
@ -60,7 +59,9 @@ local function get_target_nodename(node, pos, next_stages)
|
||||
dump(stage_chance[1]), dump(node.name),
|
||||
dump(pos)))
|
||||
return nil
|
||||
elseif ((stage_chance[3] ~= nil) and (type(stage_chance[3].can_set) == "function") and (stage_chance[3].can_set(pos) == false) ) then
|
||||
elseif ((stage_chance[3] ~= nil) and
|
||||
(type(stage_chance[3].can_set) == "function") and
|
||||
(stage_chance[3].can_set(pos) == false)) then
|
||||
return node.name
|
||||
else
|
||||
return stage_chance[1]
|
||||
@ -109,9 +110,7 @@ local function on_timer(pos, elapsed)
|
||||
end
|
||||
until ((timer >= elapsed) or (stage == nil) or (stage.duration == 0))
|
||||
|
||||
if (node.name == target_nodename) then
|
||||
return true
|
||||
end
|
||||
if (node.name == target_nodename) then return true end
|
||||
|
||||
minetest.set_node(pos, {name = target_nodename})
|
||||
|
||||
@ -128,9 +127,7 @@ function yl_api_nodestages.on_timer(pos, elapsed) return on_timer(pos, elapsed)
|
||||
--
|
||||
|
||||
function get_valid_copy_of_stage(stage)
|
||||
if (type(stage) ~= "table") then
|
||||
return false, t("stage_not_table")
|
||||
end
|
||||
if (type(stage) ~= "table") then return false, t("stage_not_table") end
|
||||
|
||||
local stage = table.copy(stage)
|
||||
|
||||
@ -161,7 +158,7 @@ end
|
||||
|
||||
function yl_api_nodestages.is_valid(stages)
|
||||
|
||||
local good, bad, total = 0,0,0
|
||||
local good, bad, total = 0, 0, 0
|
||||
local reasons = {}
|
||||
|
||||
for _, stage in ipairs(stages) do
|
||||
|
Loading…
Reference in New Issue
Block a user