whitespace and minetest. -> core.

- use same whitespace style throughout and fix some indents.
- change from minetest. to core. namespace.
This commit is contained in:
Luke aka SwissalpS
2024-11-27 22:32:24 +01:00
parent 283b5bec25
commit c8012c7266
6 changed files with 534 additions and 528 deletions

View File

@ -17,9 +17,9 @@ function smartfs.__call(self, name)
end
-- Register forms and elements
function smartfs.create(name,onload)
function smartfs.create(name, onload)
if smartfs._fdef[name] then
error("SmartFS - (Error) Form "..name.." already exists!")
error("SmartFS - (Error) Form " .. name .. " already exists!")
end
if smartfs.loaded and not smartfs._loaded_override then
error("SmartFS - (Error) Forms should be declared while the game loads.")
@ -37,7 +37,7 @@ function smartfs.override_load_checks()
smartfs._loaded_override = true
end
minetest.after(0, function()
core.after(0, function()
smartfs.loaded = true
end)
function smartfs.dynamic(name,player)
@ -45,14 +45,14 @@ function smartfs.dynamic(name,player)
smartfs._dynamic_warned = true
print("SmartFS - (Warning) On the fly forms are being used. May cause bad things to happen")
end
local state = smartfs._makeState_({name=name},player,nil,false)
local state = smartfs._makeState_({ name = name }, player, nil, false)
state.show = state._show_
smartfs.opened[player] = state
return state
end
function smartfs.element(name,data)
if smartfs._edef[name] then
error("SmartFS - (Error) Element type "..name.." already exists!")
error("SmartFS - (Error) Element type " .. name .. " already exists!")
end
smartfs._edef[name] = data
return smartfs._edef[name]
@ -68,8 +68,8 @@ function smartfs.inventory_mod()
end
end
function smartfs.add_to_inventory(form,icon,title)
if unified_inventory then
function smartfs.add_to_inventory(form, icon, title)
unified_inventory.register_button(form.name, {
type = "image",
image = icon,
@ -78,15 +78,15 @@ function smartfs.add_to_inventory(form,icon,title)
get_formspec = function(player, formspec)
local name = player:get_player_name()
local opened = smartfs._show_(form, name, nil, true)
return {formspec = opened:_getFS_(false)}
return { formspec = opened:_getFS_(false) }
end
})
return true
elseif inventory_plus then
minetest.register_on_joinplayer(function(player)
core.register_on_joinplayer(function(player)
inventory_plus.register_button(player, form.name, title)
end)
minetest.register_on_player_receive_fields(function(player, formname, fields)
core.register_on_player_receive_fields(function(player, formname, fields)
if formname == "" and fields[form.name] then
local name = player:get_player_name()
local opened = smartfs._show_(form, name, nil, true)
@ -99,7 +99,7 @@ function smartfs.add_to_inventory(form,icon,title)
end
end
function smartfs._makeState_(form,player,params,is_inv)
function smartfs._makeState_(form, player, params, is_inv)
return {
_ele = {},
def = form,
@ -112,13 +112,13 @@ function smartfs._makeState_(form,player,params,is_inv)
close = function(self)
self.closed = true
end,
size = function(self,w,h)
self._size = {w=w,h=h}
size = function(self, w, h)
self._size = { w = w, h = h }
end,
_getFS_ = function(self,size)
local res = ""
if self._size and size then
res = "size["..self._size.w..","..self._size.h.."]"
res = "size[" .. self._size.w .. "," .. self._size.h .. "]"
end
for key,val in pairs(self._ele) do
res = res .. val:build()
@ -128,25 +128,27 @@ function smartfs._makeState_(form,player,params,is_inv)
_show_ = function(self)
if self.is_inv then
if unified_inventory then
unified_inventory.set_inventory_formspec(minetest.get_player_by_name(self.player), self.def.name)
elseif inventory_plus then
inventory_plus.set_inventory_formspec(minetest.get_player_by_name(self.player), self:_getFS_(true))
unified_inventory.set_inventory_formspec(
core.get_player_by_name(self.player), self.def.name)
inventory_plus.set_inventory_formspec(
core.get_player_by_name(self.player), self:_getFS_(true))
end
else
local res = self:_getFS_(true)
minetest.show_formspec(player,form.name,res)
core.show_formspec(player, form.name, res)
end
end,
load = function(self,file)
local file = io.open(file, "r")
if file then
local table = minetest.deserialize(file:read("*all"))
local table = core.deserialize(file:read("*all"))
if type(table) == "table" then
if table.size then
self._size = table.size
end
for key,val in pairs(table.ele) do
self:element(val.type,val)
self:element(val.type, val)
end
return true
end
@ -154,7 +156,7 @@ function smartfs._makeState_(form,player,params,is_inv)
return false
end,
save = function(self,file)
local res = {ele={}}
local res = { ele = {} }
if self._size then
res.size = self._size
@ -166,65 +168,119 @@ function smartfs._makeState_(form,player,params,is_inv)
local file = io.open(file, "w")
if file then
file:write(minetest.serialize(res))
file:write(core.serialize(res))
file:close()
return true
end
return false
end,
setparam = function(self,key,value)
setparam = function(self, key, value)
if not key then return end
self.param[key] = value
return true
end,
getparam = function(self,key,default)
getparam = function(self, key, default)
if not key then return end
return self.param[key] or default
end,
button = function(self,x,y,w,h,name,text,exitf)
button = function(self, x, y, w, h, name, text, exitf)
if exitf == nil then exitf = false end
return self:element("button",{pos={x=x,y=y},size={w=w,h=h},name=name,value=text,closes=exitf})
return self:element("button", {
pos = { x = x, y = y },
size = { w = w, h = h },
name = name,
value = text,
closes = exitf
})
end,
label = function(self,x,y,name,text)
return self:element("label",{pos={x=x,y=y},name=name,value=text})
label = function(self, x, y, name, text)
return self:element("label", {
pos = { x = x, y = y },
name = name,
value = text
})
end,
toggle = function(self,x,y,w,h,name,list)
return self:element("toggle",{pos={x=x,y=y},size={w=w,h=h},name=name,id=1,list=list})
toggle = function(self, x, y, w, h, name, list)
return self:element("toggle", {
pos = { x = x, y = y },
size = { w = w, h = h },
name = name,
id = 1,
list = list
})
end,
field = function(self,x,y,w,h,name,label)
return self:element("field",{pos={x=x,y=y},size={w=w,h=h},name=name,value="",label=label})
field = function(self, x, y, w, h, name, label)
return self:element("field", {
pos = { x = x, y = y },
size = { w = w, h = h },
name = name,
value = "",
label = label
})
end,
pwdfield = function(self,x,y,w,h,name,label)
local res = self:element("field",{pos={x=x,y=y},size={w=w,h=h},name=name,value="",label=label})
pwdfield = function(self, x, y, w, h, name, label)
local res = self:element("field", {
pos = { x = x, y = y },
size = { w = w, h = h },
name = name,
value = "",
label = label
})
res:isPassword(true)
return res
end,
textarea = function(self,x,y,w,h,name,label)
local res = self:element("field",{pos={x=x,y=y},size={w=w,h=h},name=name,value="",label=label})
textarea = function(self, x, y, w, h, name, label)
local res = self:element("field", {
pos = { x = x, y = y },
size = { w = w, h = h },
name = name,
value = "",
label = label
})
res:isMultiline(true)
return res
end,
image = function(self,x,y,w,h,name,img)
return self:element("image",{pos={x=x,y=y},size={w=w,h=h},name=name,value=img})
image = function(self, x, y, w, h, name, img)
return self:element("image", {
pos = { x = x, y = y },
size = { w = w, h = h },
name = name,
value = img
})
end,
checkbox = function(self,x,y,name,label,selected)
return self:element("checkbox",{pos={x=x,y=y},name=name,value=selected,label=label})
checkbox = function(self, x, y, name, label, selected)
return self:element("checkbox", {
pos = { x = x, y = y },
name = name,
value = selected,
label = label
})
end,
listbox = function(self,x,y,w,h,name,selected,transparent)
return self:element("list", { pos={x=x,y=y}, size={w=w,h=h}, name=name, selected=selected, transparent=transparent })
listbox = function(self, x, y, w, h, name, selected, transparent)
return self:element("list", {
pos = { x = x, y = y },
size = { w = w, h = h },
name = name,
selected = selected,
transparent = transparent
})
end,
inventory = function(self,x,y,w,h,name)
return self:element("inventory", { pos={x=x,y=y}, size={w=w,h=h}, name=name })
inventory = function(self, x, y, w, h, name)
return self:element("inventory", {
pos = { x = x, y = y },
size = { w = w, h = h },
name = name
})
end,
element = function(self,typen,data)
element = function(self, typen, data)
local type = smartfs._edef[typen]
if not type then
error("Element type "..typen.." does not exist!")
error("Element type " .. typen .. " does not exist!")
end
if self._ele[data.name] then
error("Element "..data.name.." already exists")
error("Element " .. data.name .. " already exists")
end
data.type = typen
@ -237,7 +293,7 @@ function smartfs._makeState_(form,player,params,is_inv)
end
}
for key,val in pairs(type) do
for key, val in pairs(type) do
ele[key] = val
end
@ -252,7 +308,7 @@ end
function smartfs._show_(form, player, params, is_inv)
local state = smartfs._makeState_(form, player, params, is_inv)
state.show = state._show_
if form._reg(state)~=false then
if form._reg(state) ~= false then
if not is_inv then
smartfs.opened[player] = state
state:_show_()
@ -264,22 +320,22 @@ function smartfs._show_(form, player, params, is_inv)
end
-- Receive fields from formspec
local function _sfs_recieve_(state,name,fields)
if (fields.quit == "true") then
local function _sfs_recieve_(state, name, fields)
if fields.quit == "true" then
if not state.is_inv then
smartfs.opened[name] = nil
end
return true
end
for key,val in pairs(fields) do
for key, val in pairs(fields) do
if state._ele[key] then
state._ele[key].data.value = val
end
end
for key,val in pairs(state._ele) do
if val.submit then
if (val:submit(fields)==true) then
if val:submit(fields) == true then
return true
end
end
@ -287,7 +343,8 @@ local function _sfs_recieve_(state,name,fields)
if state.closed ~= true then
state:_show_()
else
minetest.show_formspec(name,"","size[5,1]label[0,0;Formspec closing not yet created!]")
core.show_formspec(name, "",
"size[5,1]label[0,0;Formspec closing not yet created!]")
if not state.is_inv then
smartfs.opened[name] = nil
end
@ -295,18 +352,18 @@ local function _sfs_recieve_(state,name,fields)
return true
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
core.register_on_player_receive_fields(function(player, formname, fields)
local name = player:get_player_name()
if smartfs.opened[name] and not smartfs.opened[name].is_inv then
if smartfs.opened[name].def.name == formname then
local state = smartfs.opened[name]
return _sfs_recieve_(state,name,fields)
return _sfs_recieve_(state, name, fields)
else
smartfs.opened[name] = nil
end
elseif smartfs.inv[name] and smartfs.inv[name].is_inv then
local state = smartfs.inv[name]
_sfs_recieve_(state,name,fields)
_sfs_recieve_(state, name, fields)
end
return false
end)
@ -316,41 +373,31 @@ end)
------------------------- ELEMENTS ----------------------------
-----------------------------------------------------------------
smartfs.element("button",{
smartfs.element("button", {
build = function(self)
if self.data.img then
return "image_button["..
self.data.pos.x..","..self.data.pos.y..
";"..
self.data.size.w..","..self.data.size.h..
";"..
self.data.img..
";"..
self.name..
";"..
self.data.value..
"]"
return "image_button["
.. self.data.pos.x .. "," .. self.data.pos.y
.. ";" .. self.data.size.w .. "," ..self.data.size.h
.. ";" .. self.data.img
.. ";" .. self.name
.. ";" .. self.data.value
.. "]"
else
if self.data.closes then
return "button_exit["..
self.data.pos.x..","..self.data.pos.y..
";"..
self.data.size.w..","..self.data.size.h..
";"..
self.name..
";"..
self.data.value..
"]"
return "button_exit["
.. self.data.pos.x .. "," .. self.data.pos.y
.. ";" .. self.data.size.w .. "," ..self.data.size.h
.. ";" .. self.name
.. ";" .. self.data.value
.. "]"
else
return "button["..
self.data.pos.x..","..self.data.pos.y..
";"..
self.data.size.w..","..self.data.size.h..
";"..
self.name..
";"..
self.data.value..
"]"
return "button["
.. self.data.pos.x .. "," .. self.data.pos.y
.. ";" .. self.data.size.w .. "," .. self.data.size.h
.. ";" .. self.name
.. ";" .. self.data.value
.. "]"
end
end
end,
@ -363,52 +410,49 @@ smartfs.element("button",{
return true
end
end,
setPosition = function(self,x,y)
self.data.pos = {x=x,y=y}
setPosition = function(self, x, y)
self.data.pos = { x = x, y = y }
end,
getPosition = function(self,x,y)
return self.data.pos
end,
setSize = function(self,w,h)
self.data.size = {w=w,h=h}
setSize = function(self, w, h)
self.data.size = { w = w, h = h }
end,
getSize = function(self,x,y)
return self.data.size
end,
onClick = function(self,func)
onClick = function(self, func)
self._click = func
end,
click = function(self,func)
click = function(self, func)
self._click = func
end,
setText = function(self,text)
setText = function(self, text)
self.data.value = text
end,
getText = function(self)
return self.data.value
end,
setImage = function(self,image)
setImage = function(self, image)
self.data.img = image
end,
getImage = function(self)
return self.data.img
end,
setClose = function(self,bool)
setClose = function(self, bool)
self.data.closes = bool
end
})
smartfs.element("toggle",{
smartfs.element("toggle", {
build = function(self)
return "button["..
self.data.pos.x..","..self.data.pos.y..
";"..
self.data.size.w..","..self.data.size.h..
";"..
self.name..
";"..
self.data.list[self.data.id]..
"]"
return "button["
.. self.data.pos.x .. "," .. self.data.pos.y
.. ";" .. self.data.size.w .. "," .. self.data.size.h
.. ";" .. self.name
.. ";" .. self.data.list[self.data.id]
.. "]"
end,
submit = function(self,fields)
if fields[self.name] then
@ -421,22 +465,22 @@ smartfs.element("toggle",{
end
end
end,
onToggle = function(self,func)
onToggle = function(self, func)
self._tog = func
end,
setPosition = function(self,x,y)
self.data.pos = {x=x,y=y}
setPosition = function(self, x, y)
self.data.pos = { x = x, y = y }
end,
getPosition = function(self,x,y)
return self.data.pos
end,
setSize = function(self,w,h)
self.data.size = {w=w,h=h}
setSize = function(self, w, h)
self.data.size = { w = w, h = h }
end,
getSize = function(self,x,y)
return self.data.size
end,
setId = function(self,id)
setId = function(self, id)
self.data.id = id
end,
getId = function(self)
@ -447,21 +491,20 @@ smartfs.element("toggle",{
end
})
smartfs.element("label",{
smartfs.element("label", {
build = function(self)
return "label["..
self.data.pos.x..","..self.data.pos.y..
";"..
self.data.value..
"]"
return "label["
.. self.data.pos.x .. "," .. self.data.pos.y
.. ";" .. self.data.value
.. "]"
end,
setPosition = function(self,x,y)
self.data.pos = {x=x,y=y}
setPosition = function(self, x, y)
self.data.pos = { x = x, y = y }
end,
getPosition = function(self,x,y)
return self.data.pos
end,
setText = function(self,text)
setText = function(self, text)
self.data.value = text
end,
getText = function(self)
@ -469,93 +512,80 @@ smartfs.element("label",{
end
})
smartfs.element("field",{
smartfs.element("field", {
build = function(self)
if self.data.ml then
return "textarea["..
self.data.pos.x..","..self.data.pos.y..
";"..
self.data.size.w..","..self.data.size.h..
";"..
self.name..
";"..
self.data.label..
";"..
self.data.value..
"]"
return "textarea["
.. self.data.pos.x .. "," .. self.data.pos.y
.. ";" .. self.data.size.w .. "," .. self.data.size.h
.. ";" .. self.name
.. ";" .. self.data.label
.. ";" .. self.data.value
.. "]"
elseif self.data.pwd then
return "pwdfield["..
self.data.pos.x..","..self.data.pos.y..
";"..
self.data.size.w..","..self.data.size.h..
";"..
self.name..
";"..
self.data.label..
"]"
return "pwdfield["
.. self.data.pos.x .. "," .. self.data.pos.y
.. ";" .. self.data.size.w .. "," .. self.data.size.h
.. ";" .. self.name
.. ";" .. self.data.label
.. "]"
else
return "field["..
self.data.pos.x..","..self.data.pos.y..
";"..
self.data.size.w..","..self.data.size.h..
";"..
self.name..
";"..
self.data.label..
";"..
self.data.value..
"]"
return "field["
.. self.data.pos.x .. "," .. self.data.pos.y
.. ";" .. self.data.size.w .. "," .. self.data.size.h
.. ";" .. self.name
.. ";" .. self.data.label
.. ";" .. self.data.value
.. "]"
end
end,
setPosition = function(self,x,y)
self.data.pos = {x=x,y=y}
setPosition = function(self, x, y)
self.data.pos = { x = x, y = y }
end,
getPosition = function(self,x,y)
return self.data.pos
end,
setSize = function(self,w,h)
self.data.size = {w=w,h=h}
setSize = function(self, w, h)
self.data.size = { w = w, h = h }
end,
getSize = function(self,x,y)
return self.data.size
end,
setText = function(self,text)
setText = function(self, text)
self.data.value = text
end,
getText = function(self)
return self.data.value
end,
isPassword = function(self,bool)
isPassword = function(self, bool)
self.data.pwd = bool
end,
isMultiline = function(self,bool)
isMultiline = function(self, bool)
self.data.ml = bool
end
})
smartfs.element("image",{
smartfs.element("image", {
build = function(self)
return "image["..
self.data.pos.x..","..self.data.pos.y..
";"..
self.data.size.w..","..self.data.size.h..
";"..
self.data.value..
"]"
return "image["
.. self.data.pos.x .. "," .. self.data.pos.y
.. ";" .. self.data.size.w .. "," .. self.data.size.h
.. ";" .. self.data.value
.. "]"
end,
setPosition = function(self,x,y)
self.data.pos = {x=x,y=y}
setPosition = function(self, x, y)
self.data.pos = { x = x, y = y }
end,
getPosition = function(self,x,y)
return self.data.pos
end,
setSize = function(self,w,h)
self.data.size = {w=w,h=h}
setSize = function(self, w, h)
self.data.size = { w = w, h = h }
end,
getSize = function(self,x,y)
return self.data.size
end,
setImage = function(self,text)
setImage = function(self, text)
self.data.value = text
end,
getImage = function(self)
@ -563,7 +593,7 @@ smartfs.element("image",{
end
})
smartfs.element("checkbox",{
smartfs.element("checkbox", {
build = function(self)
if self.data.value then
return "checkbox["..
@ -583,19 +613,19 @@ smartfs.element("checkbox",{
";false]"
end
end,
setPosition = function(self,x,y)
self.data.pos = {x=x,y=y}
setPosition = function(self, x, y)
self.data.pos = { x = x, y = y }
end,
getPosition = function(self,x,y)
return self.data.pos
end,
setSize = function(self,w,h)
self.data.size = {w=w,h=h}
setSize = function(self, w, h)
self.data.size = { w = w, h = h }
end,
getSize = function(self,x,y)
return self.data.size
end,
setText = function(self,text)
setText = function(self, text)
self.data.value = text
end,
getText = function(self)
@ -603,37 +633,33 @@ smartfs.element("checkbox",{
end
})
smartfs.element("list",{
build = function(self)
smartfs.element("list", {
build = function(self)
if not self.data.items then
self.data.items = {}
end
local listformspec = "textlist["..
self.data.pos.x..","..self.data.pos.y..
";"..
self.data.size.w..","..self.data.size.h..
";"..
self.data.name..
";"..
table.concat(self.data.items, ",")..
";"..
tostring(self.data.selected or "")..
";"..
tostring(self.data.transparent or "false").."]"
local listformspec = "textlist["
.. self.data.pos.x .. "," .. self.data.pos.y
.. ";" .. self.data.size.w .. "," .. self.data.size.h
.. ";" .. self.data.name
.. ";" .. table.concat(self.data.items, ",")
.. ";" .. tostring(self.data.selected or "")
.. ";" .. tostring(self.data.transparent or "false")
.. "]"
return listformspec
end,
submit = function(self,fields)
if fields[self.name] then
local _type = string.sub(fields[self.data.name],1,3)
local index = string.sub(fields[self.data.name],5)
if _type == "CHG" and self._click then
self:_click(self.root, index)
elseif _type == "DCL" and self._doubleClick then
self:_doubleClick(self.root, index)
end
end
end,
return listformspec
end,
submit = function(self, fields)
if fields[self.name] then
local _type = string.sub(fields[self.data.name], 1, 3)
local index = string.sub(fields[self.data.name], 5)
if _type == "CHG" and self._click then
self:_click(self.root, index)
elseif _type == "DCL" and self._doubleClick then
self:_doubleClick(self.root, index)
end
end
end,
onClick = function(self, func)
self._click = func
end,
@ -646,14 +672,14 @@ smartfs.element("list",{
doubleclick = function(self, func)
self._doubleClick = func
end,
setPosition = function(self,x,y)
self.data.pos = {x=x,y=y}
setPosition = function(self, x, y)
self.data.pos = { x = x, y = y }
end,
getPosition = function(self,x,y)
return self.data.pos
end,
setSize = function(self,w,h)
self.data.size = {w=w,h=h}
setSize = function(self, w, h)
self.data.size = { w = w, h = h }
end,
getSize = function(self,x,y)
return self.data.size
@ -664,11 +690,11 @@ smartfs.element("list",{
end
table.insert(self.data.items, item)
end,
removeItem = function(self,idx)
removeItem = function(self, idx)
if not self.data.items then
self.data.items = {}
end
table.remove(self.data.items,idx)
table.remove(self.data.items, idx)
end,
popItem = function(self)
if not self.data.items then
@ -680,28 +706,24 @@ smartfs.element("list",{
end
})
smartfs.element("inventory",{
smartfs.element("inventory", {
build = function(self)
return "list["..
(self.data.location or "current_player") ..
";"..
self.name..
";"..
self.data.pos.x..","..self.data.pos.y..
";"..
self.data.size.w..","..self.data.size.h..
";"..
(self.data.index or "") ..
"]"
return "list["
.. (self.data.location or "current_player")
.. ";" .. self.name
.. ";" .. self.data.pos.x .. "," .. self.data.pos.y
.. ";" .. self.data.size.w .. "," .. self.data.size.h
.. ";" .. (self.data.index or "")
.. "]"
end,
setPosition = function(self,x,y)
self.data.pos = {x=x,y=y}
setPosition = function(self, x, y)
self.data.pos = { x = x, y = y }
end,
getPosition = function(self,x,y)
return self.data.pos
end,
setSize = function(self,w,h)
self.data.size = {w=w,h=h}
setSize = function(self, w, h)
self.data.size = { w = w, h = h }
end,
getSize = function(self,x,y)
return self.data.size
@ -712,7 +734,7 @@ smartfs.element("inventory",{
-- "nodemeta:<X>,<Y>,<Z>": Any node metadata
-- "detached:<name>": A detached inventory
-- "context" does not apply to smartfs, since there is no node-metadata as context available
setLocation = function(self,location)
setLocation = function(self, location)
self.data.location = location
end,
getLocation = function(self)
@ -727,7 +749,7 @@ smartfs.element("inventory",{
useDetached = function(self, name)
self.data.location = "detached:" .. name
end,
setIndex = function(self,index)
setIndex = function(self, index)
self.data.index = index
end,
getIndex = function(self)
@ -735,7 +757,7 @@ smartfs.element("inventory",{
end
})
smartfs.element("code",{
smartfs.element("code", {
build = function(self)
if self._build then
self:_build()
@ -743,21 +765,22 @@ smartfs.element("code",{
return self.data.code
end,
submit = function(self,fields)
submit = function(self, fields)
if self._sub then
self:_sub(fields)
end
end,
onSubmit = function(self,func)
onSubmit = function(self, func)
self._sub = func
end,
onBuild = function(self,func)
onBuild = function(self, func)
self._build = func
end,
setCode = function(self,code)
setCode = function(self, code)
self.data.code = code
end,
getCode = function(self)
return self.data.code
end
})