Skip to content

Commit

Permalink
(#70) Transfer ownership (#71)
Browse files Browse the repository at this point in the history
* (#70) Transfer ownership

* (#70) Update technic chests infotext

Co-authored-by: SX <sx@minetest>
  • Loading branch information
S-S-X and SX committed Nov 27, 2020
1 parent 5d4c09c commit 9ef7e96
Show file tree
Hide file tree
Showing 7 changed files with 353 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ files["**/nodes/*.lua"] = { ignore = {"212"} }

globals = {
"metatool",
"travelnet",
}

read_globals = {
Expand All @@ -18,6 +17,8 @@ read_globals = {
"dump",
-- Mods
"default",
"areas",
"travelnet",
"pipeworks",
"technic",
"signs",
Expand Down
9 changes: 4 additions & 5 deletions luatool/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ local function find_luatool_stack(player, refstack)
end

-- lua controller / lua tube mem inspection form
metatool.form.register_form(
'luatool:mem_inspector',
function(player, data)
metatool.form.register_form('luatool:mem_inspector', {
on_create = function(player, data)
local raw_mem = minetest.deserialize(data.mem)
local fmt_mem = dump(raw_mem)
return "formspec_version[3]size[10,12;]label[0.1,0.5;" ..
Expand All @@ -50,7 +49,7 @@ metatool.form.register_form(
"button_exit[5,11;5,1;exit;Exit]" ..
"textarea[0,1;10,10;mem;;" .. minetest.formspec_escape(fmt_mem) .. "]"
end,
function(player, fields, data)
on_receive = function(player, fields, data)
if fields.save and fields.quit then
local itemstack = data.itemstack
if not itemstack then
Expand Down Expand Up @@ -81,7 +80,7 @@ metatool.form.register_form(
end
end
end
)
})

tool:ns({
info = function(node, pos, player, itemstack, group)
Expand Down
4 changes: 2 additions & 2 deletions metatool/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,12 @@ function metatool:ns(data)
print('metatool.ns called with invalid arguments')
end

metatool.check_privs = function(player, privs)
function metatool.check_privs(player, privs)
local success,_ = minetest.check_player_privs(player, privs)
return success
end

metatool.is_protected = function(pos, player, privs, no_violation_record)
function metatool.is_protected(pos, player, privs, no_violation_record)
if privs and (metatool.check_privs(player, privs)) then
-- player is allowed to bypass protection checks
return false
Expand Down
151 changes: 146 additions & 5 deletions metatool/formspec.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,144 @@

local S = metatool.S
local formspec_escape = minetest.formspec_escape

metatool.form = {}
local function formspec_content(value, default)
return formspec_escape(value or default or "")
end

local function fmt_rect(x, y, w, h)
if w then
return ("%0.3f,%0.3f;%0.3f,%0.3f"):format(x, y, w, h)
end
return ("%0.3f,%0.3f"):format(x, y)
end

--
-- ATTENTION: metatool.form.Form is unstable and will probably evolve with breaking changes
-- NOTE: Formspec API is not included in stable features and breaking changes will not change major version.
--
local Form = {}
Form.__index = Form
setmetatable(Form, {
__call = function(_, def)
local obj = {
width = def and def.width or 8,
height = def and def.height or 8,
xspacing = def and (def.xspacing or def.spacing) or 0.1,
yspacing = def and (def.yspacing or def.spacing) or 0.1,
elements = def and def.elements or {},
}
obj.formspec = ("formspec_version[3]size[%s;]"):format(fmt_rect(obj.width, obj.height))
setmetatable(obj, Form)
return obj
end
})

function Form:get_rect(x, y, w, h, xcount, xindex, ycount, yindex, paddingtop)
local sx = self.xspacing
local sy = self.yspacing
local cx = xcount or 1
local ix = xindex or 1
local cy = ycount or 1
local iy = yindex or 1
local pt = paddingtop or 0
w = w or ((self.width - ((cx + 1) * sx)) / cx)
h = h or ((self.height - ((cy + 1) * sy)) / cy)
if cx > 1 then
x = (x or 0) + (sx * ix) + (w * (ix - 1))
end
if cy > 1 then
y = (y or 0) + (sy * iy) + (h * (iy - 1))
end
return x or sx, (y or sy) + pt, w, h - pt
end

function Form:raw(element)
-- Add raw formspec element, `element` should be string with valid properly escaped formspec section
table.insert(self.elements, element)
return self
end

-- NOTE: Unstable function signature and `def` format. High probablility for breaking changes soon.
-- Formspec API is not included in stable features and breaking changes will not change major version.
-- def.columns and def.values required
-- everything else is optional
function Form:table(def)
if not def.name or not def.columns or not def.values then
return self
end
-- TODO: formspec_escape, columns as table with extended options
local columns = {}
local values = {}
for i=1,#def.columns do
table.insert(columns, "text")
table.insert(values, def.columns[i])
end
local ccount = #def.columns
for _,v in ipairs(def.values) do
for i=1,ccount do
table.insert(values, v[i] and formspec_escape(v[i]) or "")
end
end
local pt = def.label and 0.4
local background = def.background and (";background=%s"):format(def.background) or ""
local highlight = def.highlight and (";highlight=%s"):format(def.highlight) or ""
local color = def.color and (";color=%s"):format(def.color) or ""
local x, y, w, h = self:get_rect(def.x, def.y, def.w, def.h, def.xcount, def.xidx, def.ycount, def.yidx, pt)
table.insert(self.elements,
(def.label and ("label[%s;%s]"):format(fmt_rect(x + 0.1, y - 0.2), def.label)) ..
("tableoptions[border=false%s%s%s]"):format(background,highlight,color) ..
("tablecolumns[%s]"):format(table.concat(columns, ";")) ..
("table[%s;%s;%s]"):format(fmt_rect(x, y, w, h), def.name, table.concat(values, ","))
)
return self
end

-- NOTE: Unstable function signature and `def` format. High probablility for breaking changes soon.
-- Formspec API is not included in stable features and breaking changes will not change major version.
-- NOTE: Image button feature unstable, property names will most probably change
function Form:button(def)
if not def.name then
return self
end
local label = formspec_content(def.label, def.name)
local x, y, w, h = self:get_rect(def.x, def.y, def.w, def.h, def.xcount, def.xidx, def.ycount, def.yidx)
local properties
if def.texture1 then
local t1 = def.texture1 .. (def.modifier and formspec_escape(def.modifier) or "")
local t2 = def.texture2 and def.texture2 .. (def.modifier and formspec_escape(def.modifier) or "")
properties = ("%s;%s;%s;false;false;%s"):format(t1, def.name, label, t2)
else
properties = ("%s;%s"):format(def.name, label)
end
table.insert(self.elements,
("button%s[%s;%s]"):format(def.exit and "_exit" or "", fmt_rect(x, y, w, h), properties)
)
return self
end

-- NOTE: Unstable function signature and `def` format. High probablility for breaking changes soon.
-- Formspec API is not included in stable features and breaking changes will not change major version.
function Form:field(def)
if not def.name then
return self
end
local label = formspec_content(def.label, def.name)
local default = formspec_content(def.default)
local x, y, w, h = self:get_rect(def.x, def.y, def.w, def.h, def.xcount, def.xidx, def.ycount, def.yidx)
table.insert(self.elements,
("field[%s;%s;%s;%s]"):format(fmt_rect(x, y, w, h), def.name, label, default)
)
return self
end

function Form:render()
return self.formspec .. table.concat(self.elements)
end

metatool.form = {
Form = Form
}

-- container for form handler callback methods
metatool.form.handlers = {}
Expand Down Expand Up @@ -41,7 +178,7 @@ metatool.form.register_global_handler = function()
end

metatool.form.on_receive = function(player, formname, fields)
if metatool.form.handlers[formname] == nil then
if type(metatool.form.handlers[formname]) ~= "table" then
-- form handler does not exist
return
end
Expand Down Expand Up @@ -73,17 +210,21 @@ end

-- on_create can be either string for static formspecs or function for dynamic formspecs.
-- on_receive is function that receives fields when form is submitted, can be nil.
metatool.form.register_form = function(formname, on_create, on_receive)
metatool.form.register_form = function(formname, formdef)
local name = get_formname(formname)
if not name then
print(S("metatool.form.register_form Registration failed, invalid formname: %s", formname))
return
end
if type(formdef) ~= "table" then
print(S("metatool.form.register_form Registration failed, invalid formdef type: %s", type(formdef)))
return
end
metatool.form.register_global_handler()
print(S("metatool.form.register_form Registering form: %s", name))
metatool.form.handlers[name] = {
on_create = on_create,
on_receive = on_receive,
on_create = formdef.on_create,
on_receive = formdef.on_receive,
}
end

Expand Down
23 changes: 23 additions & 0 deletions sharetool/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,27 @@ local tool = metatool:register_tool('sharetool', sharetool)
-- Create namespace containing sharetool runtime data and functions
tool:ns({
shared_account = metatool.settings('sharetool', 'shared_account'),
player_exists = function(player)
return type(player) == "userdata" or (minetest.get_auth_handler().get_auth(player) ~= nil)
end,
set_area_owner = function(self, id, owner, player)
--luacheck: globals areas
if not self.player_exists(owner) then
minetest.chat_send_player(player:get_player_name(), S('Player %s not found.', owner))
return false
end
id = tonumber(id)
if id == nil or not areas.areas[id] then
minetest.chat_send_player(player:get_player_name(), id == nil
and S('Invalid area id.')
or S('Area %d not found from database.', id)
)
return false
end
areas.areas[id].owner = owner
areas:save()
return true
end,
mark_shared = function(meta)
meta:set_int('sharetool_shared_node', 1)
end,
Expand All @@ -75,6 +96,7 @@ tool:ns({
return allowed
end,
set_travelnet_owner = function(self, pos, player, owner)
--luacheck: globals travelnet
owner = owner or self.shared_account
local name = player:get_player_name()
local meta = minetest.get_meta(pos)
Expand Down Expand Up @@ -130,3 +152,4 @@ tool:load_node_definition(dofile(modpath .. '/nodes/book.lua'))
tool:load_node_definition(dofile(modpath .. '/nodes/travelnet.lua'))
tool:load_node_definition(dofile(modpath .. '/nodes/missions.lua'))
tool:load_node_definition(dofile(modpath .. '/nodes/mapserver.lua'))
tool:load_node_definition(dofile(modpath .. '/nodes/any.lua'))
Loading

0 comments on commit 9ef7e96

Please sign in to comment.