Add support for unified inventory and skins

This commit is contained in:
stujones11 2014-04-13 17:41:19 +01:00
parent cf2786a818
commit 8457220413
31 changed files with 167 additions and 70 deletions

@ -9,17 +9,40 @@ armor = {
player_hp = {}, player_hp = {},
elements = {"head", "torso", "legs", "feet"}, elements = {"head", "torso", "legs", "feet"},
physics = {"jump","speed","gravity"}, physics = {"jump","speed","gravity"},
formspec = "size[8,8.5]button[0,0;2,0.5;main;Back]" formspec = "size[8,8.5]list[detached:player_name_armor;armor;0,1;2,3;]"
.."list[current_player;main;0,4.5;8,4;]" .."image[2,0.75;2,4;armor_preview]"
.."list[detached:player_name_armor;armor_head;3,0;1,1;]" .."list[current_player;main;0,4.5;8,4;]"
.."list[detached:player_name_armor;armor_torso;3,1;1,1;]" .."list[current_player;craft;4,1;3,3;]"
.."list[detached:player_name_armor;armor_legs;3,2;1,1;]" .."list[current_player;craftpreview;7,2;1,1;]",
.."list[detached:player_name_armor;armor_feet;3,3;1,1;]",
textures = {}, textures = {},
default_skin = "character.png", default_skin = "character",
} }
-- armor.def - Added by BlockMen for HUD integration if inventory_plus then
armor.formspec = "size[8,8.5]button[0,0;2,0.5;main;Back]"
.."list[detached:player_name_armor;armor;0,1;2,3;]"
.."image[2.5,0.75;2,4;armor_preview]"
.."label[5,1;Level: armor_level]"
.."label[5,1.5;Heal: armor_heal]"
.."list[current_player;main;0,4.5;8,4;]"
elseif unified_inventory then
unified_inventory.register_button("armor", {
type = "image",
image = "inventory_plus_armor.png",
})
unified_inventory.register_page("armor", {
get_formspec = function(player)
local name = player:get_player_name()
local formspec = "background[0.06,0.99;7.92,7.52;3d_armor_ui_form.png]"
.."label[0,0;Armor]"
.."list[detached:"..name.."_armor;armor;0,1;2,3;]"
.."image[2.5,0.75;2,4;"..armor.textures[name].preview.."]"
.."label[5,1;Level: "..armor.def[name].level.."]"
.."label[5,1.5;Heal: "..armor.def[name].heal.."]"
return {formspec=formspec}
end,
})
end
armor.def = { armor.def = {
state = 0, state = 0,
@ -48,40 +71,58 @@ armor.set_player_armor = function(self, player)
local player_inv = player:get_inventory() local player_inv = player:get_inventory()
local armor_texture = "3d_armor_trans.png" local armor_texture = "3d_armor_trans.png"
local armor_level = 0 local armor_level = 0
local armor_heal = 0
local state = 0 local state = 0
local items = 0 local items = 0
local textures = {}
local elements = {} local elements = {}
local textures = {}
local physics_o = {speed=1,gravity=1,jump=1} local physics_o = {speed=1,gravity=1,jump=1}
for i, v in ipairs(self.elements) do local material = {type=nil, count=1}
local stack = player_inv:get_stack("armor_"..v, 1) local preview = armor:get_player_skin(name).."_preview.png"
local level = stack:get_definition().groups["armor_"..v] for _,v in ipairs(self.elements) do
elements[v] = false
end
for i=1, 6 do
local stack = player_inv:get_stack("armor", i)
local item = stack:get_name() local item = stack:get_name()
elements[i] = string.match(item, "%:.+_(.+)$") if stack:get_count() == 1 then
if level then local def = stack:get_definition()
table.insert(textures, item:gsub("%:", "_")..".png") for k, v in pairs(elements) do
armor_level = armor_level + level if v == false then
state = state + stack:get_wear() local level = def.groups["armor_"..k]
items = items + 1 if level then
end local texture = item:gsub("%:", "_")
table.insert(textures, texture..".png")
for kk,vv in ipairs(self.physics) do preview = preview.."^"..texture.."_preview.png"
local o_value = stack:get_definition().groups["physics_"..vv] armor_level = armor_level + level
if ( o_value ~= nil ) then state = state + stack:get_wear()
physics_o[vv] = physics_o[vv] + o_value items = items + 1
local heal = def.groups["armor_heal"] or 0
armor_heal = armor_heal + heal
for kk,vv in ipairs(self.physics) do
local o_value = def.groups["physics_"..vv]
if o_value then
physics_o[vv] = physics_o[vv] + o_value
end
end
local mat = string.match(item, "%:.+_(.+)$")
if material.type then
if material.type == mat then
material.count = material.count + 1
end
else
material.type = mat
end
elements[k] = true
end
end
end end
end end
end end
player:set_physics_override(physics_o)
if minetest.get_modpath("shields") then if minetest.get_modpath("shields") then
armor_level = armor_level * 0.9 armor_level = armor_level * 0.9
end end
if elements[1] == elements[2] and if material.type and material.count == #self.elements then
elements[1] == elements[3] and
elements[1] == elements[4] then
armor_level = armor_level * 1.1 armor_level = armor_level * 1.1
end end
if #textures > 0 then if #textures > 0 then
@ -93,9 +134,13 @@ armor.set_player_armor = function(self, player)
armor_groups.fleshy = 100 - armor_level armor_groups.fleshy = 100 - armor_level
end end
player:set_armor_groups(armor_groups) player:set_armor_groups(armor_groups)
player:set_physics_override(physics_o)
self.textures[name].armor = armor_texture self.textures[name].armor = armor_texture
self.textures[name].preview = preview
self.def[name].state = state self.def[name].state = state
self.def[name].count = items self.def[name].count = items
self.def[name].level = armor_level
self.def[name].heal = armor_heal
self:update_player_visuals(player) self:update_player_visuals(player)
end end
@ -117,15 +162,15 @@ armor.update_armor = function(self, player)
local heal_max = 0 local heal_max = 0
local state = 0 local state = 0
local items = 0 local items = 0
for _,v in ipairs(self.elements) do for i=1, 6 do
local stack = armor_inv:get_stack("armor_"..v, 1) local stack = player_inv:get_stack("armor", i)
if stack:get_count() > 0 then if stack:get_count() > 0 then
local use = stack:get_definition().groups["armor_use"] or 0 local use = stack:get_definition().groups["armor_use"] or 0
local heal = stack:get_definition().groups["armor_heal"] or 0 local heal = stack:get_definition().groups["armor_heal"] or 0
local item = stack:get_name() local item = stack:get_name()
stack:add_wear(use) stack:add_wear(use)
armor_inv:set_stack("armor_"..v, 1, stack) armor_inv:set_stack("armor", i, stack)
player_inv:set_stack("armor_"..v, 1, stack) player_inv:set_stack("armor", i, stack)
state = state + stack:get_wear() state = state + stack:get_wear()
items = items + 1 items = items + 1
if stack:get_count() == 0 then if stack:get_count() == 0 then
@ -148,6 +193,33 @@ armor.update_armor = function(self, player)
self.player_hp[name] = hp self.player_hp[name] = hp
end end
armor.get_player_skin = function(self, name)
local skin = nil
if skins then
skin = skins.skins[name]
elseif u_skins then
skin = u_skins.u_skins[name]
end
return skin or armor.default_skin
end
armor.update_inventory = function(self, player)
local name = player:get_player_name()
if unified_inventory then
unified_inventory.set_inventory_formspec(player, "armor")
elseif inventory_plus then
local formspec = armor.formspec:gsub("player_name", name)
formspec = formspec:gsub("armor_preview", armor.textures[name].preview)
formspec = formspec:gsub("armor_level", armor.def[name].level)
formspec = formspec:gsub("armor_heal", armor.def[name].heal)
inventory_plus.set_inventory_formspec(player, formspec)
else
local formspec = armor.formspec:gsub("player_name", name)
formspec = formspec:gsub("armor_preview", armor.textures[name].preview)
player:set_inventory_formspec(formspec)
end
end
-- Register Player Model -- Register Player Model
default.player_register_model("3d_armor_character.x", { default.player_register_model("3d_armor_character.x", {
@ -172,15 +244,15 @@ default.player_register_model("3d_armor_character.x", {
minetest.register_on_player_receive_fields(function(player, formname, fields) minetest.register_on_player_receive_fields(function(player, formname, fields)
local name = player:get_player_name() local name = player:get_player_name()
if fields.armor then if fields.armor then
local formspec = armor.formspec:gsub("player_name", name) armor:update_inventory(player)
inventory_plus.set_inventory_formspec(player, formspec)
return return
end end
for field, _ in pairs(fields) do for field, _ in pairs(fields) do
if string.find(field, "^skins_set_") then if string.find(field, "skins_set_") then
minetest.after(0, function(player) minetest.after(0, function(player)
armor.textures[name].skin = skins.skins[name]..".png" local skin = armor:get_player_skin(name)
armor:update_player_visuals(player) armor.textures[name].skin = skin..".png"
armor:set_player_armor(player)
end, player) end, player)
end end
end end
@ -188,52 +260,77 @@ end)
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
default.player_set_model(player, "3d_armor_character.x") default.player_set_model(player, "3d_armor_character.x")
inventory_plus.register_button(player,"armor", "Armor")
local player_inv = player:get_inventory()
local name = player:get_player_name() local name = player:get_player_name()
local player_inv = player:get_inventory()
local armor_inv = minetest.create_detached_inventory(name.."_armor",{ local armor_inv = minetest.create_detached_inventory(name.."_armor",{
on_put = function(inv, listname, index, stack, player) on_put = function(inv, listname, index, stack, player)
player:get_inventory():set_stack(listname, index, stack) player:get_inventory():set_stack(listname, index, stack)
armor:set_player_armor(player) armor:set_player_armor(player)
armor:update_inventory(player)
end, end,
on_take = function(inv, listname, index, stack, player) on_take = function(inv, listname, index, stack, player)
player:get_inventory():set_stack(listname, index, nil) player:get_inventory():set_stack(listname, index, nil)
armor:set_player_armor(player) armor:set_player_armor(player)
armor:update_inventory(player)
end,
on_move = function(inv, from_list, from_index, to_list, to_index, count, player)
local plaver_inv = player:get_inventory()
local stack = inv:get_stack(to_list, to_index)
player_inv:set_stack(to_list, to_index, stack)
player_inv:set_stack(from_list, from_index, nil)
armor:set_player_armor(player)
armor:update_inventory(player)
end, end,
allow_put = function(inv, listname, index, stack, player) allow_put = function(inv, listname, index, stack, player)
if inv:is_empty(listname) then return 1
return 1
end
return 0
end, end,
allow_take = function(inv, listname, index, stack, player) allow_take = function(inv, listname, index, stack, player)
return stack:get_count() return stack:get_count()
end, end,
allow_move = function(inv, from_list, from_index, to_list, to_index, count, player) allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
return 0 return count
end, end,
}) })
for _,v in ipairs(armor.elements) do if inventory_plus then
local list = "armor_"..v inventory_plus.register_button(player,"armor", "Armor")
player_inv:set_size(list, 1)
armor_inv:set_size(list, 1)
armor_inv:set_stack(list, 1, player_inv:get_stack(list, 1))
end end
armor_inv:set_size("armor", 6)
player_inv:set_size("armor", 6)
for i=1, 6 do
local stack = player_inv:get_stack("armor", i)
armor_inv:set_stack("armor", i, stack)
end
-- Legacy support, import player's armor from old inventory format
for _,v in pairs(armor.elements) do
local list = "armor_"..v
armor_inv:add_item("armor", player_inv:get_stack(list, 1))
player_inv:set_stack(list, 1, nil)
end
armor.player_hp[name] = 0 armor.player_hp[name] = 0
armor.def[name] = { armor.def[name] = {
state = 0, state = 0,
count = 0, count = 0,
level = 0,
heal = 0,
} }
armor.textures[name] = { armor.textures[name] = {
skin = armor.default_skin, skin = armor.default_skin..".png",
armor = "3d_armor_trans.png", armor = "3d_armor_trans.png",
wielditem = "3d_armor_trans.png", wielditem = "3d_armor_trans.png",
preview = armor.default_skin.."_preview.png",
} }
if minetest.get_modpath("skins") then if minetest.get_modpath("skins") then
local skin = skins.skins[name] local skin = skins.skins[name]
if skin and skins.get_type(skin) == skins.type.MODEL then if skin and skins.get_type(skin) == skins.type.MODEL then
armor.textures[name].skin = skin..".png" armor.textures[name].skin = skin..".png"
end end
elseif minetest.get_modpath("u_skins") then
local skin = u_skins.u_skins[name]
if skin and u_skins.get_type(skin) == u_skins.type.MODEL then
armor.textures[name].skin = skin..".png"
end
end end
if minetest.get_modpath("player_textures") then if minetest.get_modpath("player_textures") then
local filename = minetest.get_modpath("player_textures").."/textures/player_"..name local filename = minetest.get_modpath("player_textures").."/textures/player_"..name
@ -245,6 +342,9 @@ minetest.register_on_joinplayer(function(player)
end end
minetest.after(1, function(player) minetest.after(1, function(player)
armor:set_player_armor(player) armor:set_player_armor(player)
if inventory_plus == nil and unified_inventory == nil then
armor:update_inventory(player)
end
end, player) end, player)
end) end)
@ -257,11 +357,11 @@ if minetest.get_modpath("bones") then
local drop = {} local drop = {}
local player_inv = player:get_inventory() local player_inv = player:get_inventory()
local armor_inv = minetest.get_inventory({type="detached", name=name.."_armor"}) local armor_inv = minetest.get_inventory({type="detached", name=name.."_armor"})
for _,v in ipairs(armor.elements) do for i=1, player_inv:get_size("armor") do
local list = "armor_"..v local stack = armor_inv:get_stack("armor", i)
table.insert(drop, player_inv:get_stack(list, 1)) table.insert(drop, stack)
armor_inv:set_stack(list, 1, nil) armor_inv:set_stack("armor", i, nil)
player_inv:set_stack(list, 1, nil) player_inv:set_stack("armor", i, nil)
end end
armor:set_player_armor(player) armor:set_player_armor(player)
minetest.after(1, function() --TODO: Make delay configurable minetest.after(1, function() --TODO: Make delay configurable
@ -271,9 +371,9 @@ if minetest.get_modpath("bones") then
local owner = meta:get_string("owner") local owner = meta:get_string("owner")
local inv = meta:get_inventory() local inv = meta:get_inventory()
if name == owner then if name == owner then
for _,list in ipairs(drop) do for _,stack in ipairs(drop) do
if inv:room_for_item("main", list) then if inv:room_for_item("main", stack) then
inv:add_item("main", list) inv:add_item("main", stack)
end end
end end
end end

@ -1,3 +1,4 @@
default default
inventory_plus inventory_plus?
unified_inventory?

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

@ -4,7 +4,9 @@ Modpack - 3d Armor [0.4.1]
[mod] Visible Player Armor [3d_armor] [mod] Visible Player Armor [3d_armor]
------------------------------------- -------------------------------------
depends: default, inventory_plus depends: default
recommends: inventory_plus or unified_inventory (use only one)
Adds craftable armor that is visible to other players. Each armor item worn contributes to Adds craftable armor that is visible to other players. Each armor item worn contributes to
a player's armor group level making them less vulnerable to weapons. a player's armor group level making them less vulnerable to weapons.

@ -63,12 +63,6 @@ end
minetest.after(0, function() minetest.after(0, function()
table.insert(armor.elements, "shield") table.insert(armor.elements, "shield")
armor.formspec = "size[8,8.5]button[0,0;2,0.5;main;Back]"
.."list[current_player;main;0,4.5;8,4;]"
.."list[detached:player_name_armor;armor_head;3,0;1,1;]"
.."list[detached:player_name_armor;armor_torso;3,1;1,1;]"
.."list[detached:player_name_armor;armor_legs;3,2;1,1;]"
.."list[detached:player_name_armor;armor_feet;3,3;1,1;]"
.."list[detached:player_name_armor;armor_shield;4,1;1,1;]"
end) end)

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB