Use a standard wagon inventory formspec

This new formspec also allows access to the wagon properties. Once
whitelisted in the wagon properties, other players can access its
inventory.

Note on 'useless use' of OO: I tried passing just the wagon ID and
avoiding using the `self` object in order to bypass the need to look up
the lua entitie out of the list, but it ended up retrieving nil data.
The best way to solve this overhead might be to wait for some kind of
better way upstream in minetest's lua API to get entities, or to keep a
central record of entities. Either way, the solution is outside the
scope of this commit.
master
Blockhead 2020-03-13 13:24:11 +11:00
parent fb837a449a
commit 2da11c5a49
4 changed files with 29 additions and 20 deletions

View File

@ -101,7 +101,9 @@ advtrains.register_wagon(name, prototype, description, inventory_image)
get_inventory_formspec = function(self, player_name, inventory_name)
return "<a formspec>"
end,
^- Function that should return the formspec to be displayed when <player> requests to open the wagon's inventory
^- Function that should return the formspec to be displayed when <player> requests to open the wagon's inventory.
^- advtrains.standard_inventory_formspec can be used for ordinary wagons with inventories to show
^- both the inventory grid and a 'Wagon properties' button.
^- Use "list["..inventory_name..";<list_name>;<X>,<Y>;<W>,<H>;<Start>]" to display a wagon's inventory list.
custom_on_step = function(self, dtime) end

View File

@ -777,7 +777,7 @@ function wagon:show_wagon_properties(pname)
]]
local data = advtrains.wagons[self.id]
local form="size[5,5]"
form = form .. "field[0.5,1;4,1;whitelist;Allow these players to drive your wagon:;"..(data.whitelist or "").."]"
form = form .. "field[0.5,1;4,1;whitelist;Allow these players to access your wagon:;"..(data.whitelist or "").."]"
--seat groups access lists were here
form=form.."button_exit[0.5,3;4,1;save;"..attrans("Save wagon properties").."]"
minetest.show_formspec(pname, "advtrains_prop_"..self.id, form)
@ -1010,6 +1010,20 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
end
end
end
uid=string.match(formname, "^advtrains_inv_(.+)$")
if uid then
local pname=player:get_player_name()
local data = advtrains.wagons[uid]
if fields.prop and data.owner==pname then
for _,wagon in pairs(minetest.luaentities) do
if wagon.is_wagon and wagon.initialized and wagon.id==uid then
minetest.chat_send_player(player:get_player_name(), string.format("Opening wagon props from inv... (id=%s)", uid))
wagon:show_wagon_properties(pname)
--wagon:handle_bordcom_fields(player:get_player_name(), formname, fields)
end
end
end
end
end)
end)
function wagon:seating_from_key_helper(pname, fields, no)
@ -1167,6 +1181,14 @@ function advtrains.get_wagon_prototype(data)
return wt, advtrains.wagon_prototypes[wt]
end
function advtrains.standard_inventory_formspec(self, pname, invname)
return "size[8,11]"..
"list["..invname..";box;0,0;8,3;]"..
"button_exit[0,9;4,1;prop;"..attrans("Wagon properties").."]"..
"list[current_player;main;0,5;8,4;]"..
"listring[]"
end
function advtrains.register_wagon(sysname_p, prototype, desc, inv_img, nincreative)
local sysname = sysname_p
if not string.match(sysname, ":") then

View File

@ -90,12 +90,7 @@ advtrains.register_wagon("wagon_tank", {
collisionbox = {-1.0,-0.5,-1.0, 1.0,2.5,1.0},
drops={"default:steelblock 4"},
has_inventory = true,
get_inventory_formspec = function(self, pname, invname)
return "size[8,11]"..
"list["..invname..";box;0,0;8,3;]"..
"list[current_player;main;0,5;8,4;]"..
"listring[]"
end,
get_inventory_formspec = advtrains.standard_inventory_formspec,
inventory_list_sizes = {
box=8*3,
},
@ -111,12 +106,7 @@ advtrains.register_wagon("wagon_wood", {
collisionbox = {-1.0,-0.5,-1.0, 1.0,2.5,1.0},
drops={"default:steelblock 4"},
has_inventory = true,
get_inventory_formspec = function(self, pname, invname)
return "size[8,11]"..
"list["..invname..";box;0,0;8,3;]"..
"list[current_player;main;0,5;8,4;]"..
"listring[]"
end,
get_inventory_formspec = advtrains.standard_inventory_formspec,
inventory_list_sizes = {
box=8*3,
},

View File

@ -216,12 +216,7 @@ advtrains.register_wagon("wagon_box", {
collisionbox = {-1.0,-0.5,-1.0, 1.0,2.5,1.0},
drops={"default:steelblock 1"},
has_inventory = true,
get_inventory_formspec = function(self, pname, invname)
return "size[8,11]"..
"list["..invname..";box;0,0;8,3;]"..
"list[current_player;main;0,5;8,4;]"..
"listring[]"
end,
get_inventory_formspec = advtrains.standard_inventory_formspec,
inventory_list_sizes = {
box=8*3,
},