All basic functions

This commit is contained in:
nyje 2019-07-30 17:18:57 +00:00
parent 0ae3d735a3
commit aebe9e2fc1
4 changed files with 118 additions and 47 deletions

View File

@ -13,7 +13,9 @@ end
minetest.register_on_player_receive_fields(function(player, bot_key, fields)
--print(dump( fields))
local bot_data = vbots.bot_info[bot_key]
-- Bot main formspec
if bot_data then
print("Main Bot formspec received:")
local inv=minetest.get_inventory({type="node", pos=bot_data.pos})
local meta = minetest.get_meta(bot_data.pos)
local meta_bot_key = meta:get_string("key")
@ -71,5 +73,88 @@ minetest.register_on_player_receive_fields(function(player, bot_key, fields)
minetest.after(0.2, vbots.show_formspec, player, bot_data.pos)
end
end
else
local form_parts = string.split(bot_key,",")
local data = mod_storage:to_table()
local bot_list = {}
for n,d in pairs(data.fields) do
bot_list[#bot_list+1] = n
end
if #form_parts == 2 and form_parts[1] == "loadbot" then
print("Load Bot formspec received")
local bot_data = vbots.bot_info[form_parts[2]]
local pos=bot_data.pos
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
minetest.close_formspec(player:get_player_name(), bot_key)
if fields.delete then
vbots.load(pos,player,"delete")
end
if fields.rename then
vbots.load(pos,player,"rename")
end
if fields.saved then
local bot_name = bot_list[tonumber(string.split(fields.saved,":")[2])]
print('Loadbot '..bot_name)
local inv_list = minetest.deserialize(data.fields[bot_name])
local inv_involved = {}
if inv_list then
for _,v in pairs(inv_list) do
local parts = string.split(v," ")
if #parts == 3 then
inv_involved[parts[1]]=true
end
end
-- print(dump(inv_involved))
local size
for i,_ in pairs(inv_involved) do
size = inv:get_size(i)
for a=1,size do
inv:set_stack(i,a, "")
end
end
for _,v in pairs(inv_list) do
local parts = string.split(v," ")
if #parts == 3 then
inv:add_item(parts[1],parts[2].." "..parts[3])
end
end
end
end
elseif #form_parts == 2 and form_parts[1] == "delete" then
print("Delete Bot formspec received")
local bot_data = vbots.bot_info[form_parts[2]]
local pos=bot_data.pos
minetest.close_formspec(player:get_player_name(), bot_key)
if fields.saved then
local bot_name = bot_list[tonumber(string.split(fields.saved,":")[2])]
data.fields[bot_name]=nil
mod_storage:from_table(data)
end
elseif #form_parts == 2 and form_parts[1] == "rename" then
print("Rename Bot formspec received")
local bot_data = vbots.bot_info[form_parts[2]]
local pos=bot_data.pos
minetest.close_formspec(player:get_player_name(), bot_key)
if fields.saved then
local bot_name = bot_list[tonumber(string.split(fields.saved,":")[2])]
vbots.load(pos,player,"renameto_"..bot_name)
end
elseif #form_parts == 2 and form_parts[1] == "renameto" then
print("Renameto formspec received")
local bot_data = vbots.bot_info[form_parts[2]]
local pos=bot_data.pos
minetest.close_formspec(player:get_player_name(), bot_key)
local oldname = fields.oldname
local newname = fields.newname
if newname and oldname then
local hold = data.fields[oldname]
data.fields[oldname] = nil
data.fields[newname] = hold
mod_storage:from_table(data)
print("renamed "..oldname.." to "..newname)
end
end
end
end)

View File

@ -16,7 +16,7 @@ local trashInv = minetest.create_detached_inventory(
end
})
trashInv:set_size("main", 1)
local mod_storage = minetest.get_mod_storage()
mod_storage = minetest.get_mod_storage()
vbots.save = function(pos)
@ -34,60 +34,46 @@ vbots.save = function(pos)
mod_storage:set_string(botname,minetest.serialize(inv_list))
end
vbots.load = function(pos,player)
vbots.load = function(pos,player,mode)
local meta = minetest.get_meta(pos)
local key = meta:get_string("key")
local data = mod_storage:to_table().fields
local bot_list = ""
for n,d in pairs(data) do
bot_list = bot_list..n..","
end
bot_list = bot_list:sub(1,#bot_list-1)
local formspec = "size[5,9;]"..
"textlist[0,0;5,9;saved;"..bot_list.."]"
local formname = "loadbot,"..pos.x..","..pos.y..","..pos.z
minetest.show_formspec(player:get_player_name(), formname, formspec)
local formspec
local formname
if not mode then
formspec = "size[5,9]"..
"image_button_exit[4,0;1,1;vbots_gui_delete.png;delete;]"..
"tooltip[4,0;1,1;delete]"..
"image_button_exit[4,1;1,1;vbots_gui_rename.png;rename;]"..
"tooltip[4,1;1,1;rename]"..
"textlist[0,0;4,9;saved;"..bot_list.."]"
formname = "loadbot,"..key
elseif mode == "delete" then
formspec = "size[4,9]no_prepend[]"..
"bgcolor[#F00]"..
"textlist[0,0;4,9;saved;"..bot_list.."]"
formname = "delete,"..key
elseif mode == "rename" then
formspec = "size[4,9]no_prepend[]"..
"bgcolor[#0F0]"..
"textlist[0,0;4,9;saved;"..bot_list.."]"
formname = "rename,"..key
elseif mode:sub(1,8) == "renameto" then
local fromname = mode:sub(10)
formspec = "size[5,5]no_prepend[]"..
"bgcolor[#00F]"..
"field[0,0;5,2;oldname;Old Name;"..fromname.."]"..
"field[0,1;5,4;newname;New Name;]"
formname = "renameto,"..key
end
minetest.after(0.2, minetest.show_formspec, player:get_player_name(), formname, formspec)
end
minetest.register_on_player_receive_fields(function(player, key, fields)
local form_pos = string.split(key,",")
if #form_pos == 4 and form_pos[1] == "loadbot" then
local pos={ x = form_pos[2], y = form_pos[3], z = form_pos[4] }
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local data = mod_storage:to_table().fields
local bot_list = {}
for n,d in pairs(data) do
bot_list[#bot_list+1] = n
end
minetest.close_formspec(player:get_player_name(), key)
if fields.saved then
local bot_name = bot_list[tonumber(string.split(fields.saved,":")[2])]
local inv_list = minetest.deserialize(data[bot_name])
local inv_involved = {}
if inv_list then
for _,v in pairs(inv_list) do
local parts = string.split(v," ")
if #parts == 3 then
inv_involved[parts[1]]=true
end
end
-- print(dump(inv_involved))
for i,_ in pairs(inv_involved) do
size = inv:get_size(i)
for a=1,size do
inv:set_stack(i,a, "")
end
end
for _,v in pairs(inv_list) do
local parts = string.split(v," ")
if #parts == 3 then
inv:add_item(parts[1],parts[2].." "..parts[3])
end
end
end
end
end
end)
-------------------------------------
-- Generate 32 bit key for formspec identification

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB