wielded items can now be set using inventory slots

This commit is contained in:
Sokomine 2021-07-26 01:25:29 +02:00
parent 952ed84733
commit 48482ce23f
2 changed files with 91 additions and 67 deletions

View File

@ -187,7 +187,7 @@ yl_speak_up.get_fs_fashion = function(pname)
end
local formspec = {
"formspec_version[3]",
"size[13.4,9.5]",
"size[13.4,15]",
"dropdown[0.3,0.2;4,0.75;set_skin;",
mainlist,
";",
@ -204,10 +204,10 @@ yl_speak_up.get_fs_fashion = function(pname)
";",
capeindex,
"]",
"field[0.3,3.2;4,0.75;set_sword;;",
"label[0.3,3.2;",
textures[4],
"]",
"field[9.1,3.2;4,0.75;set_shield;;",
"label[9.1,3.2;",
textures[3],
"]",
"field_close_on_enter[set_sword;false]",
@ -228,7 +228,17 @@ yl_speak_up.get_fs_fashion = function(pname)
preview,
"]",
"button_exit[0.3,8.4;3,0.75;button_cancel;"..button_cancel.."]",
"button[10.1,8.4;3,0.75;button_save;Save]"
"button[10.1,8.4;3,0.75;button_save;Save]",
"list[current_player;main;1.8,10;8,4;]",
-- set wielded items
"label[0.3,9.7;Wield\nleft:]",
"label[12.0,9.7;Wield\nright:]",
"list[detached:yl_speak_up_player_"..tostring(pname)..";wield;0.3,10.5;1,1;]",
"list[detached:yl_speak_up_player_"..tostring(pname)..";wield;12.0,10.5;1,1;1]",
"button[0.3,11.7;1,0.6;button_wield_left;Set]",
"button[12.0,11.7;1,0.6;button_wield_right;Set]",
"tooltip[button_wield_left;Set and store what your NPC shall wield in its left hand.]",
"tooltip[button_wield_right;Set and store what your NPC shall wield in its right hand.]",
}
return table.concat(formspec, "")
end
@ -241,75 +251,73 @@ yl_speak_up.input_fashion = function(player, formname, fields)
local pname = player:get_player_name()
local textures = yl_speak_up.speak_to[pname].textures
if fields then
-- catch ESC as well
if fields.quit or fields.button_cancel or fields.button_exit then
-- is the player editing this npc? then we need to go back to the edit menu
if( yl_speak_up.edit_mode[pname]
and yl_speak_up.edit_mode[pname] == yl_speak_up.speak_to[pname].n_id) then
yl_speak_up.show_fs(player, "talk", {n_id = yl_speak_up.speak_to[pname].n_id,
d_id = yl_speak_up.speak_to[pname].d_id})
return
end
yl_speak_up.speak_to[pname] = nil
return
end
local n_id = yl_speak_up.speak_to[pname].n_id
local n_id = yl_speak_up.speak_to[pname].n_id
-- is the player editing this npc? if not: abort
if(not(yl_speak_up.edit_mode[pname])
or (yl_speak_up.edit_mode[pname] ~= n_id)) then
return ""
end
-- only change cape if there really is a diffrent one selected
if fields.set_cape and fields.set_cape ~= textures[1] then
-- only set the cape if it is part of the list of allowed capes
local capetable, capelist, capeindex = get_npc_capes(textures[1])
for _, v in pairs(capetable) do
if(v == fields.set_cape) then
textures[1] = fields.set_cape
-- catch ESC as well
if(not(fields) or (fields.quit or fields.button_cancel or fields.button_exit)) then
yl_speak_up.show_fs(player, "talk", {n_id = yl_speak_up.speak_to[pname].n_id,
d_id = yl_speak_up.speak_to[pname].d_id})
return
elseif(fields.button_wield_left
or fields.button_wield_right) then
local trade_inv = minetest.get_inventory({type="detached", name="yl_speak_up_player_"..pname})
local left_stack = trade_inv:get_stack("wield", 1)
local right_stack = trade_inv:get_stack("wield", 2)
if(left_stack and left_stack:get_name() and fields.button_wield_left) then
textures[4] = yl_speak_up.get_wield_texture(left_stack:get_name())
yl_speak_up.log_change(pname, n_id,
"(fashion) cape changed to "..tostring(fields.set_cape)..".")
-- we have found it
break
end
end
end
-- only change the skin if there really is a diffrent new one selected
if fields.set_skin and fields.set_skin ~= textures[2] then
-- only set the skin if it is part of the list of allowed skins
local maintable, mainlist, mainindex = get_npc_skins(textures[2])
for _, v in pairs(maintable) do
if(v == fields.set_skin) then
textures[2] = fields.set_skin
yl_speak_up.log_change(pname, n_id,
"(fashion) skin changed to "..tostring(fields.set_skin)..".")
-- we have found it
break
end
end
end
if fields.set_shield then
textures[3] = fields.set_shield
yl_speak_up.log_change(pname, n_id,
"(fashion) shield changed to "..tostring(fields.set_shield)..".")
end
if fields.set_sword then
textures[4] = fields.set_sword
yl_speak_up.log_change(pname, n_id,
"(fashion) sword changed to "..tostring(fields.set_sword)..".")
end
end
if(right_stack and right_stack:get_name() and fields.button_wield_right) then
textures[3] = yl_speak_up.get_wield_texture(right_stack:get_name())
yl_speak_up.log_change(pname, n_id,
"(fashion) shield changed to "..tostring(fields.set_shield)..".")
end
if fields.button_save then
local obj = yl_speak_up.speak_to[pname].obj
if obj ~= nil and obj:get_luaentity() ~= nil then
-- save textures
yl_speak_up.speak_to[pname].skins = textures2skin(textures)
set_textures(obj, textures)
yl_speak_up.log_change(pname, n_id,
-- only change cape if there really is a diffrent one selected
elseif(fields.set_cape and fields.set_cape ~= textures[1]) then
-- only set the cape if it is part of the list of allowed capes
local capetable, capelist, capeindex = get_npc_capes(textures[1])
for _, v in pairs(capetable) do
if(v == fields.set_cape) then
textures[1] = fields.set_cape
y_speak_up.log_change(pname, n_id,
"(fashion) cape changed to "..tostring(fields.set_cape)..".")
-- we have found it
break
end
end
-- only change the skin if there really is a diffrent new one selected
elseif(fields.set_skin and fields.set_skin ~= textures[2]) then
-- only set the skin if it is part of the list of allowed skins
local maintable, mainlist, mainindex = get_npc_skins(textures[2])
for _, v in pairs(maintable) do
if(v == fields.set_skin) then
textures[2] = fields.set_skin
yl_speak_up.log_change(pname, n_id,
"(fashion) skin changed to "..tostring(fields.set_skin)..".")
-- we have found it
break
end
end
elseif fields.button_save then
local obj = yl_speak_up.speak_to[pname].obj
if obj ~= nil and obj:get_luaentity() ~= nil then
-- save textures
yl_speak_up.speak_to[pname].skins = textures2skin(textures)
set_textures(obj, textures)
yl_speak_up.log_change(pname, n_id,
"(fashion) saved changes.")
end
end
--yl_speak_up.speak_to[pname].textures = textures
end
end
end
yl_speak_up.show_fs(player, "fashion")
end
@ -350,3 +358,17 @@ yl_speak_up.update_nametag = function(self)
end
end
end
-- inspired/derived from the wieldview mod in 3darmor
yl_speak_up.get_wield_texture = function(item)
if(not(item) or not(minetest.registered_items[ item ])) then
return "3d_armor_trans.png"
end
local def = minetest.registered_items[ item ]
if(def.inventory_image ~= "") then
return def.inventory_image
elseif(def.tiles and type(def.tiles[1]) == "string" and def.tiles[1] ~= "") then
return minetest.inventorycube(def.tiles[1])
end
return "3d_armor_trans.png"
end

View File

@ -788,4 +788,6 @@ minetest.register_on_joinplayer(function(player, last_login)
-- for setting up actions
trade_inv:set_size("npc_gives", 1)
trade_inv:set_size("npc_wants", 1)
-- for setting wielded items (left and right)
trade_inv:set_size("wield", 2)
end)