2017-02-22 06:36:35 +01:00
|
|
|
local cache = smart_inventory.cache
|
|
|
|
local ui_tools = smart_inventory.ui_tools
|
|
|
|
|
2017-03-02 11:01:00 +01:00
|
|
|
-----------------------------------------------------
|
|
|
|
-- Item selection action (add to inventory)
|
|
|
|
-----------------------------------------------------
|
2017-02-22 06:36:35 +01:00
|
|
|
local function on_item_select(state, entry)
|
|
|
|
local player = minetest.get_player_by_name(state.location.rootState.location.player)
|
|
|
|
local pinv = player:get_inventory()
|
|
|
|
pinv:add_item("main", entry.item)
|
|
|
|
end
|
|
|
|
|
2017-04-08 22:14:16 +02:00
|
|
|
local function clear_inventory(playername)
|
|
|
|
local inventory = minetest.get_player_by_name(playername):get_inventory()
|
|
|
|
local invsize = inventory:get_size("main")
|
|
|
|
for idx = 1, invsize do
|
|
|
|
inventory:set_stack("main", idx, "")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function save_inventory(playername, slot)
|
|
|
|
local inventory = minetest.get_player_by_name(playername):get_inventory()
|
|
|
|
local list = inventory:get_list("main")
|
|
|
|
local savedata = {}
|
|
|
|
for idx, stack in ipairs(list) do
|
|
|
|
if not stack:is_empty() then
|
|
|
|
savedata[idx] = stack:to_string()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local path = minetest.get_worldpath().."/smart_inventory_creative_"..playername.."_slot_"..tostring(slot)
|
|
|
|
local fd = io.open( path, 'w' )
|
|
|
|
if( fd ) then
|
|
|
|
fd:write( minetest.serialize(savedata))
|
|
|
|
fd:close()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function restore_inventory(playername, slot)
|
|
|
|
local inventory = minetest.get_player_by_name(playername):get_inventory()
|
|
|
|
local savedata
|
|
|
|
local path = minetest.get_worldpath().."/smart_inventory_creative_"..playername.."_slot_"..tostring(slot)
|
|
|
|
local fd = io.open( path, 'r' )
|
|
|
|
if( fd ) then
|
|
|
|
local data = fd:read("*all");
|
|
|
|
fd:close()
|
|
|
|
if data then
|
|
|
|
savedata = minetest.deserialize( data );
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if savedata then
|
|
|
|
local inventory = minetest.get_player_by_name(playername):get_inventory()
|
|
|
|
local invsize = inventory:get_size("main")
|
|
|
|
for idx = 1, invsize do
|
|
|
|
inventory:set_stack("main", idx, savedata[idx])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-03-02 11:01:00 +01:00
|
|
|
-----------------------------------------------------
|
|
|
|
-- Update on group selection change
|
|
|
|
-----------------------------------------------------
|
2017-02-22 06:36:35 +01:00
|
|
|
local function update_group_selection(state, changed_group)
|
|
|
|
local grouped = state.param.creative_grouped_items
|
|
|
|
local groups_sel1 = state:get("groups_sel1")
|
|
|
|
local groups_sel2 = state:get("groups_sel2")
|
|
|
|
local groups_sel3 = state:get("groups_sel3")
|
|
|
|
local grid = state:get("buttons_grid")
|
|
|
|
local outlist
|
|
|
|
|
2017-02-24 12:01:28 +01:00
|
|
|
if state.param.creative_grouped_material_items and
|
|
|
|
next(state.param.creative_grouped_material_items) then
|
|
|
|
local group_info = {}
|
2017-03-09 22:28:33 +01:00
|
|
|
group_info.name = "shape"
|
|
|
|
group_info.cgroup = cache.cgroups["shape"]
|
|
|
|
group_info.group_desc = "#01DF74> "..group_info.cgroup.group_desc
|
2017-02-24 12:01:28 +01:00
|
|
|
group_info.items = state.param.creative_grouped_material_items
|
2017-03-09 22:28:33 +01:00
|
|
|
grouped["shape"] = group_info
|
2017-02-24 12:01:28 +01:00
|
|
|
end
|
|
|
|
|
2017-03-02 11:01:00 +01:00
|
|
|
-- update group 1
|
2017-02-28 15:03:10 +01:00
|
|
|
if changed_group < 1 or not state.param.creative_group_list1 then
|
2017-02-22 06:36:35 +01:00
|
|
|
state.param.creative_group_list1 = ui_tools.update_group_selection(grouped, groups_sel1, state.param.creative_group_list1)
|
|
|
|
end
|
|
|
|
|
|
|
|
local sel_id = groups_sel1:getSelected()
|
2017-03-02 22:01:42 +01:00
|
|
|
if state.param.creative_group_list1[sel_id] == "all"
|
|
|
|
or not state.param.creative_group_list1[sel_id]
|
|
|
|
or not grouped[state.param.creative_group_list1[sel_id]] then
|
2017-02-22 06:36:35 +01:00
|
|
|
outlist = grouped["all"].items
|
|
|
|
groups_sel2:clearItems()
|
|
|
|
groups_sel3:clearItems()
|
|
|
|
else
|
2017-03-02 11:01:00 +01:00
|
|
|
-- update group 2
|
2017-02-22 06:36:35 +01:00
|
|
|
grouped = cache.get_list_grouped(grouped[state.param.creative_group_list1[sel_id]].items)
|
2017-02-28 15:03:10 +01:00
|
|
|
if changed_group < 2 or not state.param.creative_group_list2 then
|
2017-02-22 06:36:35 +01:00
|
|
|
state.param.creative_group_list2 = ui_tools.update_group_selection(grouped, groups_sel2, state.param.creative_group_list2)
|
|
|
|
end
|
|
|
|
|
|
|
|
sel_id = groups_sel2:getSelected()
|
2017-03-02 22:01:42 +01:00
|
|
|
if state.param.creative_group_list2[sel_id] == "all"
|
|
|
|
or not state.param.creative_group_list2[sel_id]
|
|
|
|
or not grouped[state.param.creative_group_list2[sel_id]] then
|
2017-02-22 06:36:35 +01:00
|
|
|
outlist = grouped["all"].items
|
|
|
|
groups_sel3:clearItems()
|
|
|
|
else
|
2017-03-02 11:01:00 +01:00
|
|
|
-- update group 3
|
|
|
|
grouped = cache.get_list_grouped(grouped[state.param.creative_group_list2[sel_id]].items)
|
2017-02-28 15:03:10 +01:00
|
|
|
if changed_group < 3 or not state.param.creative_group_list3 then
|
2017-02-22 06:36:35 +01:00
|
|
|
state.param.creative_group_list3 = ui_tools.update_group_selection(grouped, groups_sel3, state.param.creative_group_list3)
|
|
|
|
end
|
|
|
|
sel_id = groups_sel3:getSelected()
|
|
|
|
outlist = grouped[state.param.creative_group_list3[sel_id]].items
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-02 11:01:00 +01:00
|
|
|
-- update grid list
|
2017-02-22 06:36:35 +01:00
|
|
|
if outlist then
|
|
|
|
table.sort(outlist, function(a,b)
|
|
|
|
return a.item < b.item
|
|
|
|
end)
|
|
|
|
grid:setList(outlist)
|
|
|
|
state.param.creative_outlist = outlist
|
|
|
|
else
|
|
|
|
grid:setList({})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-02 11:01:00 +01:00
|
|
|
-----------------------------------------------------
|
|
|
|
-- Page layout definition
|
|
|
|
-----------------------------------------------------
|
2017-02-22 06:36:35 +01:00
|
|
|
local function creative_callback(state)
|
|
|
|
local player = state.location.rootState.location.player
|
|
|
|
|
2017-03-02 11:01:00 +01:00
|
|
|
-- groups 1-3
|
2017-02-22 06:36:35 +01:00
|
|
|
local group_sel1 = state:listbox(1, 0.15, 5.6, 3, "groups_sel1",nil, false)
|
|
|
|
group_sel1:onClick(function(self, state, player)
|
|
|
|
local selected = self:getSelectedItem()
|
|
|
|
if selected then
|
2017-02-24 23:14:28 +01:00
|
|
|
state:get("groups_sel2"):setSelected(1)
|
|
|
|
state:get("groups_sel3"):setSelected(1)
|
2017-02-22 06:36:35 +01:00
|
|
|
update_group_selection(state, 1)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
local group_sel2 = state:listbox(7, 0.15, 5.6, 3, "groups_sel2",nil, false)
|
|
|
|
group_sel2:onClick(function(self, state, player)
|
|
|
|
local selected = self:getSelectedItem()
|
|
|
|
if selected then
|
2017-02-24 23:14:28 +01:00
|
|
|
state:get("groups_sel3"):setSelected(1)
|
2017-02-22 06:36:35 +01:00
|
|
|
update_group_selection(state, 2)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
local group_sel3 = state:listbox(13, 0.15, 5.6, 3, "groups_sel3",nil, false)
|
|
|
|
group_sel3:onClick(function(self, state, player)
|
|
|
|
local selected = self:getSelectedItem()
|
|
|
|
if selected then
|
|
|
|
update_group_selection(state, 3)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2017-03-02 11:01:00 +01:00
|
|
|
-- functions
|
2017-04-08 21:18:45 +02:00
|
|
|
local searchfield = state:field(3.5, 4.2, 4, 0.5, "search")
|
2017-04-08 21:07:37 +02:00
|
|
|
searchfield:setCloseOnEnter(false)
|
|
|
|
searchfield:onKeyEnter(function(self, state, player)
|
|
|
|
local search_string = self:getText()
|
|
|
|
local filtered_list = ui_tools.search_in_list(state.param.creative_grouped_items_all, search_string)
|
|
|
|
state.param.creative_grouped_items = cache.get_list_grouped(filtered_list)
|
|
|
|
filtered_list = ui_tools.search_in_list(state.param.creative_grouped_items_material_all, search_string)
|
|
|
|
state.param.creative_grouped_material_items = filtered_list
|
|
|
|
update_group_selection(state, 0)
|
2017-03-02 11:01:00 +01:00
|
|
|
end)
|
2017-02-22 06:36:35 +01:00
|
|
|
|
|
|
|
-- craftable items grid
|
2017-04-08 21:18:45 +02:00
|
|
|
state:background(9.2, 3.5, 9.5, 6.5, "buttons_grid_bg", "minimap_overlay_square.png")
|
|
|
|
local grid = smart_inventory.smartfs_elements.buttons_grid(state, 9.55, 3.75, 9.0 , 6.5, "buttons_grid", 0.75,0.75)
|
2017-02-22 06:36:35 +01:00
|
|
|
grid:onClick(function(self, state, index, player)
|
|
|
|
local listentry = state.param.creative_outlist[index]
|
|
|
|
on_item_select(state, listentry)
|
|
|
|
end)
|
|
|
|
|
2017-03-02 11:01:00 +01:00
|
|
|
-- inventory
|
2017-04-08 21:18:45 +02:00
|
|
|
-- state:background(1.4, 8, 8, 1, "inventory_bg", "menu_bg.png")
|
|
|
|
state:inventory(1, 5, 8, 4,"main")
|
2017-02-24 17:34:49 +01:00
|
|
|
ui_tools.create_trash_inv(state, player)
|
2017-04-08 21:18:45 +02:00
|
|
|
state:image(8,9,1,1,"trash_icon","creative_trash_icon.png")
|
2017-02-27 20:36:16 +01:00
|
|
|
state:element("code", {name = "trash_bg_code", code = "listcolors[#00000069;#5A5A5A;#141318;#30434C;#FFF]"})
|
2017-04-08 21:18:45 +02:00
|
|
|
state:inventory(8,9,1,1, "trash"):useDetached(player.."_trash_inv")
|
2017-02-22 06:36:35 +01:00
|
|
|
|
2017-04-08 22:14:16 +02:00
|
|
|
-- trash button
|
2017-04-08 21:25:18 +02:00
|
|
|
local trash_all = state:button(7,9,1,1, "trash_all", "Trash all")
|
|
|
|
trash_all:setImage("creative_trash_icon.png")
|
|
|
|
trash_all:onClick(function(self, state, player)
|
2017-04-08 22:14:16 +02:00
|
|
|
clear_inventory(state.location.rootState.location.player)
|
|
|
|
end)
|
|
|
|
|
|
|
|
-- save/restore buttons
|
|
|
|
state:button(1,9,1,1, "save1", "Save 1"):onClick(function(self, state, player)
|
2017-04-08 21:25:18 +02:00
|
|
|
local name = state.location.rootState.location.player
|
2017-04-08 22:14:16 +02:00
|
|
|
save_inventory(state.location.rootState.location.player, 1)
|
|
|
|
end)
|
|
|
|
state:button(1.9,9,1,1, "save2", "Save 2"):onClick(function(self, state, player)
|
|
|
|
local name = state.location.rootState.location.player
|
|
|
|
save_inventory(state.location.rootState.location.player, 2)
|
2017-04-08 21:25:18 +02:00
|
|
|
end)
|
2017-04-08 22:14:16 +02:00
|
|
|
state:button(2.8,9,1,1, "save3", "Save 3"):onClick(function(self, state, player)
|
|
|
|
local name = state.location.rootState.location.player
|
|
|
|
save_inventory(state.location.rootState.location.player, 3)
|
|
|
|
end)
|
|
|
|
state:button(4,9,1,1, "restore1", "Get 1"):onClick(function(self, state, player)
|
|
|
|
local name = state.location.rootState.location.player
|
|
|
|
restore_inventory(state.location.rootState.location.player, 1)
|
|
|
|
end)
|
|
|
|
state:button(4.9,9,1,1, "restore2", "Get 2"):onClick(function(self, state, player)
|
|
|
|
local name = state.location.rootState.location.player
|
|
|
|
restore_inventory(state.location.rootState.location.player, 2)
|
|
|
|
end)
|
|
|
|
state:button(5.8,9,1,1, "restore3", "Get 3"):onClick(function(self, state, player)
|
|
|
|
restore_inventory(state.location.rootState.location.player, 3)
|
|
|
|
end)
|
|
|
|
|
2017-04-08 21:25:18 +02:00
|
|
|
|
2017-02-22 06:36:35 +01:00
|
|
|
-- fill with data
|
2017-03-01 21:05:16 +01:00
|
|
|
state.param.creative_grouped_items_all, state.param.creative_grouped_items_material_all = cache.get_all_items()
|
2017-02-22 06:36:35 +01:00
|
|
|
state.param.creative_grouped_items = cache.get_list_grouped(state.param.creative_grouped_items_all)
|
2017-02-24 12:01:28 +01:00
|
|
|
state.param.creative_grouped_material_items = state.param.creative_grouped_items_material_all
|
2017-02-22 06:36:35 +01:00
|
|
|
update_group_selection(state, 0)
|
|
|
|
end
|
|
|
|
|
2017-03-02 11:01:00 +01:00
|
|
|
-----------------------------------------------------
|
|
|
|
-- Register page in smart_inventory
|
|
|
|
-----------------------------------------------------
|
2017-02-22 06:36:35 +01:00
|
|
|
smart_inventory.register_page({
|
|
|
|
name = "creative",
|
|
|
|
tooltip = "The creative way to get items",
|
2017-02-24 01:30:26 +01:00
|
|
|
icon = "default_chest_front.png",
|
2017-02-22 06:36:35 +01:00
|
|
|
smartfs_callback = creative_callback,
|
2017-02-23 09:16:06 +01:00
|
|
|
sequence = 15
|
2017-02-22 06:36:35 +01:00
|
|
|
})
|