2017-04-09 18:16:23 +02:00
|
|
|
if not smart_inventory.skins_mod and not smart_inventory.armor_mod then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-02-12 23:28:21 +01:00
|
|
|
local filter = smart_inventory.filter
|
2017-02-15 11:56:30 +01:00
|
|
|
local cache = smart_inventory.cache
|
2017-03-27 21:50:56 +02:00
|
|
|
local ui_tools = smart_inventory.ui_tools
|
2017-03-15 22:01:03 +01:00
|
|
|
local txt = smart_inventory.txt
|
2017-02-14 15:58:33 +01:00
|
|
|
local creative = minetest.setting_getbool("creative_mode")
|
2017-02-12 23:28:21 +01:00
|
|
|
|
2017-03-15 22:01:03 +01:00
|
|
|
local armor_type = {}
|
|
|
|
if smart_inventory.armor_mod then
|
|
|
|
for _,v in ipairs(armor.elements) do
|
2017-04-17 00:51:39 +02:00
|
|
|
armor_type["armor:"..v] = true
|
2017-03-15 22:01:03 +01:00
|
|
|
end
|
|
|
|
end
|
2017-03-13 23:20:21 +01:00
|
|
|
|
2017-02-12 23:28:21 +01:00
|
|
|
local function update_grid(state, listname)
|
|
|
|
-- Update the users inventory grid
|
|
|
|
local list = {}
|
|
|
|
state.param["armor_"..listname.."_list"] = list
|
|
|
|
local name = state.location.rootState.location.player
|
|
|
|
local inventory = minetest.get_player_by_name(name):get_inventory()
|
|
|
|
local invlist = inventory:get_list(listname)
|
|
|
|
|
|
|
|
for stack_index, stack in ipairs(invlist) do
|
2017-03-12 00:20:12 +01:00
|
|
|
local itemdef = stack:get_definition()
|
2017-02-12 23:28:21 +01:00
|
|
|
local is_armor = false
|
2017-03-15 22:01:03 +01:00
|
|
|
if itemdef and cache.citems[itemdef.name] then
|
|
|
|
for groupname, groupinfo in pairs(cache.citems[itemdef.name].cgroups) do
|
2017-03-27 22:29:22 +02:00
|
|
|
if armor_type[groupname] then
|
2017-03-15 22:01:03 +01:00
|
|
|
local wear = stack:get_wear()
|
2017-04-02 20:47:27 +02:00
|
|
|
|
|
|
|
local entry = {
|
2017-03-15 22:01:03 +01:00
|
|
|
itemdef = itemdef,
|
|
|
|
stack_index = stack_index,
|
|
|
|
-- buttons_grid related
|
2017-04-02 20:47:27 +02:00
|
|
|
item = itemdef.name,
|
2017-03-15 22:01:03 +01:00
|
|
|
is_button = true
|
2017-04-02 20:47:27 +02:00
|
|
|
}
|
|
|
|
if wear > 0 then
|
|
|
|
entry.text = tostring(math.floor((1 - wear / 65535) * 100 + 0.5)).." %"
|
|
|
|
end
|
|
|
|
table.insert(list, entry)
|
2017-03-15 22:01:03 +01:00
|
|
|
break
|
|
|
|
end
|
2017-03-12 00:20:12 +01:00
|
|
|
end
|
2017-02-12 23:28:21 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
table.sort(list, function(a,b)
|
|
|
|
return a.item < b.item
|
|
|
|
end)
|
|
|
|
local grid = state:get(listname.."_grid")
|
|
|
|
grid:setList(list)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function update_selected_item(state, listentry)
|
2017-03-13 23:20:21 +01:00
|
|
|
local name = state.location.rootState.location.player
|
2017-03-27 22:29:22 +02:00
|
|
|
state.param.armor_selected_item = listentry or state.param.armor_selected_item
|
|
|
|
listentry = state.param.armor_selected_item
|
2017-02-12 23:28:21 +01:00
|
|
|
if not listentry then
|
|
|
|
return
|
|
|
|
end
|
2017-03-15 22:01:03 +01:00
|
|
|
local i_list = state:get("i_list")
|
|
|
|
i_list:clearItems()
|
2017-03-27 21:50:56 +02:00
|
|
|
for _, groupdef in ipairs(ui_tools.get_tight_groups(cache.citems[listentry.itemdef.name].cgroups)) do
|
|
|
|
i_list:addItem(groupdef.group_desc)
|
2017-02-12 23:28:21 +01:00
|
|
|
end
|
2017-02-13 22:26:43 +01:00
|
|
|
state:get("item_name"):setText(listentry.itemdef.description)
|
2017-02-12 23:28:21 +01:00
|
|
|
state:get("item_image"):setImage(listentry.item)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function update_page(state)
|
|
|
|
local name = state.location.rootState.location.player
|
2017-02-13 12:17:00 +01:00
|
|
|
if smart_inventory.armor_mod then
|
2017-02-14 15:58:33 +01:00
|
|
|
if creative == false then
|
|
|
|
update_grid(state, "main")
|
|
|
|
end
|
2017-02-13 12:17:00 +01:00
|
|
|
update_grid(state, "armor")
|
|
|
|
state:get("preview"):setImage(armor.textures[name].preview)
|
|
|
|
state.location.parentState:get("player_button"):setImage(armor.textures[name].preview)
|
2017-03-13 23:20:21 +01:00
|
|
|
local a_list = state:get("a_list")
|
|
|
|
a_list:clearItems()
|
|
|
|
for k, v in pairs(armor.def[name]) do
|
2017-03-15 22:01:03 +01:00
|
|
|
local grouptext
|
2017-03-27 23:00:37 +02:00
|
|
|
if k == "groups" then
|
2017-03-27 21:45:29 +02:00
|
|
|
for gn, gv in pairs(v) do
|
|
|
|
if txt["armor:"..gn] then
|
2017-04-17 00:51:39 +02:00
|
|
|
grouptext = txt["armor:"..gn]
|
2017-03-27 21:45:29 +02:00
|
|
|
else
|
|
|
|
grouptext = "armor:"..gn
|
|
|
|
end
|
|
|
|
if grouptext and gv ~= 0 then
|
|
|
|
a_list:addItem(grouptext..": "..gv)
|
|
|
|
end
|
|
|
|
end
|
2017-03-15 22:01:03 +01:00
|
|
|
else
|
2017-03-27 23:00:37 +02:00
|
|
|
local is_physics = false
|
|
|
|
for _, group in ipairs(armor.physics) do
|
|
|
|
if group == k then
|
|
|
|
is_physics = true
|
|
|
|
break
|
|
|
|
end
|
2017-03-27 21:45:29 +02:00
|
|
|
end
|
2017-03-27 23:00:37 +02:00
|
|
|
if is_physics then
|
2017-04-17 00:51:39 +02:00
|
|
|
if txt["physics:"..k] then
|
|
|
|
grouptext = txt["physics:"..k]
|
2017-03-27 23:00:37 +02:00
|
|
|
else
|
2017-04-17 00:51:39 +02:00
|
|
|
grouptext = "physics:"..k
|
2017-03-27 23:00:37 +02:00
|
|
|
end
|
|
|
|
if grouptext and v ~= 1 then
|
|
|
|
a_list:addItem(grouptext..": "..v)
|
|
|
|
end
|
|
|
|
else
|
2017-04-17 00:51:39 +02:00
|
|
|
if txt["armor:"..k] then
|
|
|
|
grouptext = txt["armor:"..k]
|
2017-03-27 23:00:37 +02:00
|
|
|
else
|
2017-04-17 00:51:39 +02:00
|
|
|
grouptext = "armor:"..k
|
2017-03-27 23:00:37 +02:00
|
|
|
end
|
|
|
|
if grouptext and v ~= 0 then
|
2017-04-02 21:18:40 +02:00
|
|
|
if k == "state" then
|
|
|
|
a_list:addItem(grouptext..": "..tostring(math.floor((1 - v / armor.def[name].count / 65535) * 100 + 0.5)).." %")
|
|
|
|
else
|
|
|
|
a_list:addItem(grouptext..": "..v)
|
|
|
|
end
|
2017-03-27 23:00:37 +02:00
|
|
|
end
|
2017-03-27 21:45:29 +02:00
|
|
|
end
|
2017-03-13 23:20:21 +01:00
|
|
|
end
|
|
|
|
end
|
2017-02-13 12:17:00 +01:00
|
|
|
update_selected_item(state)
|
2017-02-13 22:26:43 +01:00
|
|
|
elseif smart_inventory.skins_mod then
|
2017-02-13 12:17:00 +01:00
|
|
|
state.location.parentState:get("player_button"):setImage(skins.skins[name].."_preview.png")
|
|
|
|
state:get("preview"):setImage(skins.skins[name].."_preview.png")
|
|
|
|
end
|
|
|
|
if smart_inventory.skins_mod then
|
2017-02-13 22:26:43 +01:00
|
|
|
local skin = skins.skins[name]
|
|
|
|
if skin and skins.meta[skin] then
|
|
|
|
state:get("skinname"):setText("Skin name: "..(skins.meta[skin].name or ""))
|
|
|
|
state:get("skinauthor"):setText("Author: "..(skins.meta[skin].author or ""))
|
|
|
|
state:get("skinlicense"):setText("License: "..(skins.meta[skin].license or ""))
|
|
|
|
else
|
|
|
|
state:get("skinname"):setText("")
|
|
|
|
state:get("skinauthor"):setText("")
|
|
|
|
state:get("skinlicense"):setText("")
|
|
|
|
end
|
2017-02-13 12:17:00 +01:00
|
|
|
end
|
2017-02-12 23:28:21 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
local function move_item_to_armor(state, item)
|
|
|
|
local name = state.location.rootState.location.player
|
|
|
|
local inventory = minetest.get_player_by_name(name):get_inventory()
|
2017-03-19 21:31:37 +01:00
|
|
|
|
|
|
|
-- get item to be moved to armor inventory
|
|
|
|
local itemstack, itemname, itemdef
|
2017-02-14 15:58:33 +01:00
|
|
|
if creative == true then
|
2017-03-19 21:31:37 +01:00
|
|
|
itemstack = ItemStack(item.item)
|
|
|
|
itemname = item.item
|
2017-02-14 15:58:33 +01:00
|
|
|
else
|
2017-03-11 23:41:51 +01:00
|
|
|
itemstack = inventory:get_stack("main", item.stack_index)
|
2017-03-19 21:31:37 +01:00
|
|
|
itemname = itemstack:get_name()
|
2017-02-14 15:58:33 +01:00
|
|
|
end
|
2017-03-19 21:31:37 +01:00
|
|
|
itemdef = minetest.registered_items[itemname]
|
|
|
|
local new_groups = {}
|
|
|
|
for _, element in ipairs(armor.elements) do
|
|
|
|
if itemdef.groups["armor_"..element] then
|
|
|
|
new_groups["armor_"..element] = true
|
2017-03-11 23:41:51 +01:00
|
|
|
end
|
2017-03-19 21:31:37 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- remove all items with the same group
|
|
|
|
local removed_items = {}
|
|
|
|
for stack_index, stack in ipairs(inventory:get_list("armor")) do
|
|
|
|
local old_def = stack:get_definition()
|
|
|
|
if old_def then
|
|
|
|
for groupname, groupdef in pairs(old_def.groups) do
|
|
|
|
if new_groups[groupname] then
|
|
|
|
table.insert(removed_items, stack)
|
|
|
|
stack = inventory:set_stack("armor", stack_index, {})
|
2017-03-11 23:41:51 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-03-19 21:31:37 +01:00
|
|
|
end
|
2017-03-11 23:41:51 +01:00
|
|
|
|
2017-03-19 21:31:37 +01:00
|
|
|
-- move the new item to the armor inventory
|
|
|
|
itemstack = inventory:add_item("armor", itemstack)
|
|
|
|
|
|
|
|
-- handle put backs in non-creative to not lost items
|
|
|
|
if creative == false then
|
|
|
|
inventory:set_stack("main", item.stack_index, itemstack)
|
|
|
|
for _, stack in ipairs(removed_items) do
|
|
|
|
stack = inventory:add_item("main", stack)
|
|
|
|
stack = inventory:add_item("armor", stack)
|
2017-03-11 23:41:51 +01:00
|
|
|
end
|
|
|
|
end
|
2017-02-12 23:28:21 +01:00
|
|
|
armor:set_player_armor(minetest.get_player_by_name(name))
|
|
|
|
end
|
|
|
|
|
|
|
|
local function move_item_to_inv(state, item)
|
|
|
|
local name = state.location.rootState.location.player
|
|
|
|
local inventory = minetest.get_player_by_name(name):get_inventory()
|
2017-02-14 15:58:33 +01:00
|
|
|
if creative == true then
|
|
|
|
inventory:set_stack("armor", item.stack_index, {})
|
|
|
|
else
|
|
|
|
local itemstack = inventory:get_stack("armor", item.stack_index)
|
|
|
|
itemstack = inventory:add_item("main", itemstack)
|
|
|
|
inventory:set_stack("armor", item.stack_index, itemstack)
|
|
|
|
end
|
2017-02-12 23:28:21 +01:00
|
|
|
armor:set_player_armor(minetest.get_player_by_name(name))
|
|
|
|
end
|
|
|
|
|
|
|
|
local function player_callback(state)
|
|
|
|
local name = state.location.rootState.location.player
|
2017-02-13 22:26:43 +01:00
|
|
|
state:background(0, 2.3, 6, 4.6, "it_bg", "minimap_overlay_square.png")
|
2017-03-13 23:20:21 +01:00
|
|
|
state:label(2,5,"item_name", "")
|
|
|
|
state:listbox(2.2,2.5,3.2,2.5,"i_list", nil, true)
|
|
|
|
state:item_image(0,3.5,2,2,"item_image","")
|
2017-02-13 22:26:43 +01:00
|
|
|
|
|
|
|
state:background(6.7, 2.3, 6, 4.6, "pl_bg", "minimap_overlay_square.png")
|
|
|
|
state:image(7,3.0,2,4,"preview","")
|
2017-03-13 23:20:21 +01:00
|
|
|
state:listbox(9.2,2.5,3.2,2.5,"a_list", nil, true)
|
2017-02-13 22:26:43 +01:00
|
|
|
state:label(9,5.0,"skinname","")
|
|
|
|
state:label(9,5.5,"skinauthor", "")
|
|
|
|
state:label(9,6.0, "skinlicense", "")
|
2017-02-13 21:09:34 +01:00
|
|
|
|
|
|
|
state:background(0, 0, 20, 1, "top_bg", "halo.png")
|
|
|
|
state:background(0, 8, 20, 2, "bottom_bg", "halo.png")
|
2017-02-13 12:17:00 +01:00
|
|
|
if smart_inventory.armor_mod then
|
|
|
|
local grid_armor = smart_inventory.smartfs_elements.buttons_grid(state, 0, 0, 8, 1, "armor_grid")
|
2017-02-13 21:09:34 +01:00
|
|
|
|
2017-02-13 12:17:00 +01:00
|
|
|
grid_armor:onClick(function(self, state, index, player)
|
2017-03-27 23:22:28 +02:00
|
|
|
if state.param.armor_armor_list[index] then
|
|
|
|
update_selected_item(state, state.param.armor_armor_list[index])
|
|
|
|
move_item_to_inv(state, state.param.armor_armor_list[index])
|
|
|
|
update_page(state)
|
|
|
|
end
|
2017-02-13 12:17:00 +01:00
|
|
|
end)
|
|
|
|
|
2017-02-14 15:58:33 +01:00
|
|
|
local grid_main = smart_inventory.smartfs_elements.buttons_grid(state, 0, 8, 20, 2, "main_grid")
|
2017-02-13 12:17:00 +01:00
|
|
|
grid_main:onClick(function(self, state, index, player)
|
|
|
|
update_selected_item(state, state.param.armor_main_list[index])
|
|
|
|
move_item_to_armor(state, state.param.armor_main_list[index])
|
|
|
|
update_page(state)
|
|
|
|
end)
|
|
|
|
armor:set_player_armor(minetest.get_player_by_name(name))
|
2017-02-14 15:58:33 +01:00
|
|
|
|
|
|
|
if creative == true then
|
|
|
|
-- fill creative list once, not each page update
|
|
|
|
local list = {}
|
2017-03-15 22:01:03 +01:00
|
|
|
local list_dedup = {}
|
|
|
|
for armor_group, _ in pairs(armor_type) do
|
|
|
|
for _, itemdef in pairs(cache.cgroups[armor_group].items) do
|
|
|
|
list_dedup[itemdef.name] = itemdef
|
|
|
|
end
|
|
|
|
end
|
|
|
|
for _, itemdef in pairs(list_dedup) do
|
2017-02-15 11:56:30 +01:00
|
|
|
table.insert(list, {
|
|
|
|
itemdef = itemdef,
|
|
|
|
-- buttons_grid related
|
|
|
|
item = itemdef.name,
|
|
|
|
is_button = true
|
|
|
|
})
|
2017-02-14 15:58:33 +01:00
|
|
|
end
|
|
|
|
table.sort(list, function(a,b)
|
|
|
|
return a.item < b.item
|
|
|
|
end)
|
|
|
|
grid_main:setList(list)
|
|
|
|
state.param.armor_main_list = list
|
|
|
|
end
|
2017-02-13 12:17:00 +01:00
|
|
|
end
|
2017-02-12 23:28:21 +01:00
|
|
|
|
2017-02-13 21:09:34 +01:00
|
|
|
if smart_inventory.skins_mod then
|
|
|
|
-- Skins Grid
|
2017-02-17 16:37:50 +01:00
|
|
|
local grid_skins = smart_inventory.smartfs_elements.buttons_grid(state, 13.1, 1.3, 7 , 7, "skins_grid", 0.87, 1.30)
|
2017-02-13 22:26:43 +01:00
|
|
|
state:background(13, 1, 7 , 7, "bg_skins", "minimap_overlay_square.png")
|
2017-02-13 21:09:34 +01:00
|
|
|
grid_skins:onClick(function(self, state, index, player)
|
2017-02-13 22:26:43 +01:00
|
|
|
local skin = skins.list[index]
|
|
|
|
local player_obj = minetest.get_player_by_name(name)
|
|
|
|
skins.skins[player] = skin
|
2017-02-13 21:09:34 +01:00
|
|
|
skins.file_save = true
|
2017-02-13 22:26:43 +01:00
|
|
|
skins.update_player_skin(player_obj)
|
2017-02-13 21:09:34 +01:00
|
|
|
if smart_inventory.armor_mod then
|
2017-02-13 22:26:43 +01:00
|
|
|
armor.textures[name].skin = skin..".png"
|
|
|
|
armor:set_player_armor(player_obj)
|
2017-02-13 21:09:34 +01:00
|
|
|
end
|
|
|
|
update_page(state)
|
|
|
|
end)
|
2017-02-12 23:28:21 +01:00
|
|
|
|
2017-02-13 21:09:34 +01:00
|
|
|
local skins_grid_data = {}
|
|
|
|
for idx, skin in pairs(skins.list) do
|
|
|
|
table.insert(skins_grid_data, {
|
|
|
|
image = skin.."_preview.png",
|
2017-02-13 22:26:43 +01:00
|
|
|
tooltip = skins.meta[skin].name,
|
2017-02-13 21:09:34 +01:00
|
|
|
is_button = true,
|
|
|
|
size = { w = 0.87, h = 1.30 }
|
|
|
|
})
|
|
|
|
if skin == skins.skins[name] then
|
2017-02-13 22:26:43 +01:00
|
|
|
grid_skins:setFirstVisible(idx - 19) --8x5 (grid size) / 2 -1
|
2017-02-13 21:09:34 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
grid_skins:setList(skins_grid_data)
|
|
|
|
end
|
2017-02-20 17:04:06 +01:00
|
|
|
|
|
|
|
-- not visible update plugin for updates from outsite (API)
|
|
|
|
state:element("code", { name = "update_hook" }):onSubmit(function(self, state)
|
|
|
|
update_page(state)
|
|
|
|
state.location.rootState:show()
|
|
|
|
end)
|
2017-02-12 23:28:21 +01:00
|
|
|
update_page(state)
|
|
|
|
end
|
|
|
|
|
|
|
|
smart_inventory.register_page({
|
|
|
|
name = "player",
|
|
|
|
icon = "player.png",
|
2017-02-13 22:26:43 +01:00
|
|
|
tooltip = "Customize yourself",
|
2017-02-12 23:28:21 +01:00
|
|
|
smartfs_callback = player_callback,
|
|
|
|
sequence = 20,
|
|
|
|
on_button_click = update_page
|
|
|
|
})
|
2017-03-20 08:47:22 +01:00
|
|
|
|
|
|
|
-- register callback in 3d_armor for updates
|
|
|
|
if smart_inventory.armor_mod and armor.register_on_update then
|
2017-04-02 21:08:46 +02:00
|
|
|
|
|
|
|
local function submit_update_hook(player)
|
2017-03-20 08:47:22 +01:00
|
|
|
local name = player:get_player_name()
|
|
|
|
local state = smart_inventory.get_page_state("player", name)
|
|
|
|
if state then
|
|
|
|
state:get("update_hook"):submit()
|
|
|
|
end
|
2017-04-02 21:08:46 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
armor:register_on_update(submit_update_hook)
|
|
|
|
|
|
|
|
-- There is no callback in 3d_armor for wear change in on_hpchange implementation
|
|
|
|
minetest.register_on_player_hpchange(function(player, hp_change)
|
|
|
|
minetest.after(0, submit_update_hook, player)
|
2017-03-20 08:47:22 +01:00
|
|
|
end)
|
|
|
|
end
|