Skip to content

Formspec: Show a player inventory using core.show_formspec - #15963

Merged
SmallJoker merged 6 commits into
luanti-org:masterfrom
SmallJoker:pr_15963_show_inventory_formspec
Jun 22, 2025
Merged

SmallJoker merged 6 commits into
luanti-org:masterfrom
SmallJoker:pr_15963_show_inventory_formspec

Conversation

@SmallJoker

@SmallJoker SmallJoker commented Mar 30, 2025 •

Copy link
Copy Markdown
Member

'core.show_formspec' now shows a temporary inventory formspec as if it was opened using the hotkey on client-side.

Fixes luanti-org/minetest_game#3188 as soon both server and client are >= 5.13.0.

To do

This PR is Ready for Review.

How to test

core.register_chatcommand("fs", {
	func = function(name, param)
		local player = core.get_player_by_name(name)
		if not player then return end -- ncurses

		if core.get_player_information(name).protocol_version < (core.protocol_versions["5.13.0"] or 0) then
			return false, "Not supported by client; formspec inputs are not sent to the server"
		end

		local fs = player:get_inventory_formspec()
		core.show_formspec(name, "", "size[8,8]button[4,4;5,1;x;I do nothing]")

		assert(fs == player:get_inventory_formspec(), "formspecs must still match")

		core.after(2, function()
			-- Force update to the regular inventory formspec
			core.chat_send_player(name, "*** PREVIOUS FORMSPEC RESTORED ***")
			player:set_inventory_formspec(fs)
		end)

		core.after(5, function()
			-- Force close
			core.chat_send_player(name, "*** FORCE CLOSE FORMSPEC TEST ***")
			core.show_formspec(name, "", "")
		end)
		return true
	end
})
  1. Open the inventory. Navigate a bit
  2. Use /fs. The formspec must show the same thing that you saw last
  3. Navigate in the formspec before it closes after 5 seconds
  4. Open the inventory using the hotkey. It must show the same formspec that you saw last.

@SmallJoker SmallJoker added Feature ✨ PRs that add or enhance a feature Formspec labels Mar 30, 2025
@Zughy Zughy added the Roadmap: supported by core dev PR not adhering to the roadmap, yet some core dev decided to take care of it label Mar 30, 2025
Comment thread doc/lua_api.md Outdated
@SmallJoker

SmallJoker commented Mar 30, 2025 •

Copy link
Copy Markdown
Member Author

Just realized that my test script misses the point. Updated it to show the advantage, which is in fact inventory mod-independent:

local fs = player:get_inventory_formspec(fs)
core.show_formspec(name, "", fs)

This opens the inventory, although any custom formspec may be provided.

@SmallJoker
SmallJoker force-pushed the pr_15963_show_inventory_formspec branch from 8c11654 to 7cf64b0 Compare May 29, 2025 09:12
Comment thread doc/lua_api.md Outdated
@grorp

grorp commented Jun 10, 2025

Copy link
Copy Markdown
Member

I wonder whether this change is going to be break someone's mod. Currently, you could do core.show_formspec(player:get_player_name(), "", ...) when receiving an event from the inventory formspec to update it temporarily, e.g. to open a dialog. If the user opens the inventory again later, it will still be the inventory formspec set earlier. With this PR, the change will be permanent. Obviously you could just use a different formspec name for the temporary formspec to avoid this.

With grep -P -r 'show_formspec\(.*?, "",' ../games ../mods, I can't see a case in my locally installed content where this would be a problem.

'core.show_formspec' now shows and updates the inventory formspec as if
it was opened using the hotkey on client-side.
@SmallJoker

Copy link
Copy Markdown
Member Author

to update it temporarily, e.g. to open a dialog.

On the other hand: if the formspec looks like an inventory formspec, then it would be weird to revert back to the original formspec when re-opening. e.g. when navigating to a recipe when hitting a node with a special tool.
Mods could address this by sending the same formspec twice: 1x for core.show_formspec and 1x for player:set_inventory_formspec(fs).

@SmallJoker
SmallJoker force-pushed the pr_15963_show_inventory_formspec branch from 7cf64b0 to 0fe91a7 Compare June 10, 2025 17:02
@SmallJoker

Copy link
Copy Markdown
Member Author

Changed the implementation to become popup-like.

@SmallJoker SmallJoker changed the title Formspec: Show/update the player inv using core.show_formspec Formspec: Show a player inventory using core.show_formspec Jun 10, 2025
@SmallJoker
SmallJoker requested a review from grorp June 12, 2025 16:37
@grorp

grorp commented Jun 15, 2025 •

Copy link
Copy Markdown
Member

Changed the implementation to become popup-like.

If I do:

//lua core.show_formspec(core.get_connected_players()[1]:get_player_name(), "", "formspec_version[8]size[3,3]label[0,0.5;Rhododendron]")

then close the formspec and press I to open the regular inventory, I still see the "Rhododendron" formspec. Is that an accident?

@SmallJoker

Copy link
Copy Markdown
Member Author

@grorp I see. Fixed that. Here's a few more commands for testing:

# Open popup
//lua core.after(3, function() core.show_formspec(core.get_connected_players()[1]:get_player_name(), "", "formspec_version[8]size[3,3]label[0,0.5;Rhododendron]") end)

# Force close inventory
//lua core.after(3, function() core.show_formspec(core.get_connected_players()[1]:get_player_name(), "", "") end)

@SmallJoker
SmallJoker force-pushed the pr_15963_show_inventory_formspec branch from d0ab95c to 4b3a809 Compare June 15, 2025 14:54
Comment thread src/client/game_formspec.h Outdated
Comment thread src/client/game_formspec.cpp Outdated
Comment thread src/server.cpp
Comment thread doc/lua_api.md Outdated
Comment thread doc/lua_api.md Outdated

@grorp grorp left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Two nitpicks attached, feel free to change or not.

See also my comment regarding the more explicit documentation, but my approval applies regardless

Comment thread src/client/game.cpp Outdated
Comment thread src/client/game_formspec.cpp Outdated
Comment thread doc/lua_api.md Outdated
Co-authored-by: grorp <gregor.parzefall@posteo.de>
@SmallJoker
SmallJoker merged commit fdc149f into luanti-org:master Jun 22, 2025
DragonWrangler1 pushed a commit to DragonWrangler1/minetest that referenced this pull request Jun 30, 2025
…g#15963)

'core.show_formspec' now shows and updates the inventory formspec as if
it was opened using the hotkey on client-side.
@SmallJoker
SmallJoker deleted the pr_15963_show_inventory_formspec branch July 27, 2025 18:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Feature ✨ PRs that add or enhance a feature Formspec One approval ✅ ◻️ Roadmap: supported by core dev PR not adhering to the roadmap, yet some core dev decided to take care of it

Projects

None yet

Development

Successfully merging this pull request may close these issues.

add sfinv show formspec function

5 participants