united_inventory support added

master
Noodlemire 2020-07-15 21:40:51 -05:00
parent 9b4cdc1996
commit 1245c9e207
7 changed files with 107 additions and 25 deletions

View File

@ -1 +1,15 @@
fixed compatability issue where other mods wouldn't be able to use minetest.register_allow_player_inventory_action
v1.1.0:
unified_inventory:
+Added support for unified_inventory.
Now, in survival mode, research and duplication pages are available.
Look for the gear icon for the research page, and the 3 blocks icon for the duplication page.
translations:
+Added support for translations
+Added French translation
(Both courtesy of louisroyer)
fixed:
-Duplication menu's paging not working correctly if nothing has been researched (courtesy of louisroyer)
-compatability issue where other mods wouldn't be able to use minetest.register_allow_player_inventory_action
-removed "init trash" message when a player first joins

View File

@ -32,7 +32,6 @@ local F = minetest.formspec_escape
function rnd.duplication.init(player)
player:get_inventory():set_size("duplication", 32)
player:get_inventory():set_size("trash", 1)
minetest.log("init trash")
end
@ -75,7 +74,7 @@ local function spairs(t)
end
--This function creates the duplication menu.
local function duplication_formspec(player, page)
local function duplication_formspec(player, page, unified)
local pname = player:get_player_name()
--Get the player's inventory to look through later.
@ -120,17 +119,25 @@ local function duplication_formspec(player, page)
----The page number label's text is stored here because it is used more than once.
local pageString = tostring(rnd.duplication.currentPage[pname]).."/"..tostring(math.max(math.ceil(countComplete(pname) / 32), 1))
--Show a different inventory layout based on if the unified_inventory is open or not
local base_inv = rnd.base_inv_formspec
local btn_height = 4.05
if unified then
base_inv = rnd.base_unified_formspec
btn_height = 3.7
end
--With all of the above information, the formspec can be formed here.
--Note that every time information changes, the menu is reformed.
--Also, note the extra listring to trash, which enables deletion of items in the survival inventory via shift-clicking.
return rnd.base_inv_formspec..
"button[1.5,4;1,1;frst;<<]"..
"button[2.5,4;1,1;prev;<]"..
"button[4.5,4;1,1;next;>]"..
"button[5.5,4;1,1;last;>>]"..
"label[3.7,4;"..F(S("Page")).."]"..
"label["..tostring(3.9 - 0.05 * pageString:len())..",4.25;"..pageString.."]"..
"list[current_player;duplication;0,0;8,4;]"..
return base_inv..
"button[1.5,"..btn_height..";1,1;frst;<<]"..
"button[2.5,"..btn_height..";1,1;prev;<]"..
"button[4.5,"..btn_height..";1,1;next;>]"..
"button[5.5,"..btn_height..";1,1;last;>>]"..
"label[3.7,"..(btn_height+0.05)..";"..F(S("Page")).."]"..
"label["..tostring(3.9 - 0.05 * pageString:len())..","..(btn_height+0.3)..";"..pageString.."]"..
"list[current_player;duplication;0,-0.1;8,4;]"..
"listring[current_player;duplication]"..
"listring[current_player;main]"..
"listring[current_player;trash]"
@ -165,7 +172,7 @@ end)
--This function is defined like this because it is used in two different places, one for sfinv and one that is used for /duplicate.
local function on_player_receive_fields_duplication(player, formname, fields, context)
--If one of the four buttons were pressed...
if formname == "duplication" and (fields["frst"] or fields["prev"] or fields["next"] or fields["last"]) then
if ((unified_inventory and formname == "") or formname == "duplication") and (fields["frst"] or fields["prev"] or fields["next"] or fields["last"]) then
local pname = player:get_player_name()
local page = rnd.duplication.currentPage[pname]
@ -185,7 +192,10 @@ local function on_player_receive_fields_duplication(player, formname, fields, co
--When a page changes, the duplication menu has to be reformed.
--A different method must be used if the sfinv inventory is open, so the tabs still work after the reformation.
if sfinv and context then
if unified_inventory and formname == "" then
rnd.duplication.currentPage[pname] = page
unified_inventory.set_inventory_formspec(player, "duplication")
elseif sfinv and context then
rnd.duplication.currentPage[pname] = page
sfinv.set_player_inventory_formspec(player)
else
@ -212,8 +222,29 @@ minetest.register_chatcommand("duplicate", {
end,
})
--When unified_inventory is active, the duplication tab is defined here, using several previously defined functions.
if unified_inventory then
unified_inventory.register_page("duplication", {
get_formspec = function(player)
return {
formspec = duplication_formspec(player, rnd.duplication.currentPage[player:get_player_name()], true),
draw_inventory = false,
}
end
})
unified_inventory.register_button("duplication", {
type = "image",
image = "rnd_button_duplication_page.png",
tooltip = S("Duplicate"),
condition = function(player)
return not unified_inventory.is_creative(player)
end
})
--When sfinv is active, the duplication tab is defined here, using several previously defined functions.
if sfinv then
elseif sfinv then
sfinv.register_page("duplication", {
title = S("Duplicate"),

View File

@ -38,6 +38,11 @@ rnd.base_inv_formspec = "size[8,9.1]"..
"list[current_player;main;0,6.35;8,3;8]"..
hotbar_bg
if unified_inventory then
rnd.base_unified_formspec =
"list[current_player;main;0,4.5;8,4;]"
end
--A custom API file that I find a little more convenient than the usual mod storage API.

View File

@ -1,4 +1,4 @@
name = rnd
description = Research N' Duplicate: Obtain and research enough of a specific item to unlock the ability to infinitely duplicate it. In other words, it's an "earned" creative mode.
optional_depends = creative, default, sfinv
optional_depends = creative, default, sfinv, unified_inventory
min_minetest_version = 5.2.0

View File

@ -178,7 +178,7 @@ end
--This function creates the research menu.
local function research_formspec(player)
local function research_formspec(player, unified)
--Get the player's inventory to look through later.
local inv = player:get_inventory()
@ -206,13 +206,19 @@ local function research_formspec(player)
--The progress label's text is stored here because it is used more than once.
local progString = "("..item.count.."/"..item.goal..")"
--Show a different inventory layout based on if the unified_inventory is open or not
local base_inv = rnd.base_inv_formspec
if unified then
base_inv = rnd.base_unified_formspec
end
--With all of the above information, the formspec can be formed here.
--Note that every time information changes, the menu is reformed.
return rnd.base_inv_formspec..
"label["..tostring(4 - 0.05 * item.name:len())..",1.75;"..item.name.."]"..
"label["..tostring(3.9 - 0.05 * progString:len())..",2;"..progString.."]"..
"button[3,2.5;2,1;research;"..F(S("Research")).."]"..
"list[current_player;research;0,3.5;8,1;]"..
return base_inv..
"label["..tostring(4 - 0.05 * item.name:len())..",0.75;"..item.name.."]"..
"label["..tostring(3.9 - 0.05 * progString:len())..",1;"..progString.."]"..
"button[3,1.5;2,1;research;"..F(S("Research")).."]"..
"list[current_player;research;0,2.5;8,1;]"..
"listring[]"
end
@ -249,7 +255,9 @@ minetest.register_on_player_inventory_action(function(player, action, inventory,
local pname = player:get_player_name()
--A different method must be used if the sfinv inventory is open, so the tabs still work after the reformation.
if sfinv and sfinv.get_or_create_context(player).page == "research" then
if (unified_inventory and not normal_research_menu_active[pname]) then
unified_inventory.set_inventory_formspec(player, "research")
elseif sfinv and sfinv.get_or_create_context(player).page == "research" then
sfinv.set_player_inventory_formspec(player)
--Check first that the /research menu is open. If it is, update it.
elseif normal_research_menu_active[pname] then
@ -263,7 +271,7 @@ end)
local function on_player_receive_fields_research(player, formname, fields, context)
local pname = player:get_player_name()
if formname == "research" and fields["research"] then
if fields["research"] and ((unified_inventory and formname == "") or (formname == "research")) then
--Get the player's inventory to look through later.
local inv = player:get_inventory()
@ -293,7 +301,10 @@ local function on_player_receive_fields_research(player, formname, fields, conte
--Its placement here, before the research inventory is cleared, is important.
--This makes the research label "lag", so if all of the items are removed by research,
--The player will still see the total research progress for that item, rather than the usual (0/X)
if sfinv and context then
--However, unified_inventory is too aggressive for this trick to work. Sorry.
if unified_inventory and formname == "" then
unified_inventory.set_inventory_formspec(player, "research")
elseif sfinv and context then
sfinv.set_player_inventory_formspec(player)
else
minetest.show_formspec(pname, "research", research_formspec(player))
@ -350,8 +361,29 @@ minetest.register_chatcommand("research", {
end,
})
--When unified_inventory is active, the research tab is defined here, using several previously defined functions.
if unified_inventory then
unified_inventory.register_page("research", {
get_formspec = function(player)
return {
formspec = research_formspec(player, true),
draw_inventory = false,
}
end
})
unified_inventory.register_button("research", {
type = "image",
image = "rnd_button_research_page.png",
tooltip = S("Research"),
condition = function(player)
return not unified_inventory.is_creative(player)
end
})
--When sfinv is active, the research tab is defined here, using several previously defined functions.
if sfinv then
elseif sfinv then
sfinv.register_page("research", {
title = S("Research"),

Binary file not shown.

After

Width:  |  Height:  |  Size: 375 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 308 B