Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add on_grant and on_revoke callbacks #4713

Merged
merged 4 commits into from Aug 26, 2017
Merged

Conversation

rubenwardy
Copy link
Member

@rubenwardy rubenwardy commented Nov 2, 2016

minetest.register_privilege("test", {
	on_grant = function(name, grantername)
		local gname = grantername or "<?>"
		print(gname .. " granted " .. name .. " test")
	end,
	on_revoke = function(name, revokername)
		local rname = revokername or "<?>"
		print(rname .. " revoked " .. name .. " test")
	end,
})

The second commit makes it work with mods generally, however the callbacks are called twice when doing /grant - once for when doing set_privs, the other from the /grant function.

USE CASES

  • Allows you to implement hud flag privileges, eg: minimap priv, without polling for priv changes
  • Update Unified Inventory main buttons on priv changes
  • ...

TODO

  • Documentation

@rubenwardy rubenwardy added @ Script API WIP The PR is still being worked on by its author and not ready yet. labels Nov 2, 2016
@Wuzzy2
Copy link
Contributor

Wuzzy2 commented Nov 7, 2016

Add “Update Unified Inventory main buttons” to the list of use cases. :-)

I like the concept (I am generally in favor of new callbacks) and say “Thank you!”.

@rubenwardy
Copy link
Member Author

Currently callbacks being called twice, once with the player name and once without. I don't think it's that bad, and it's hard to fix cleanly. Alternatively, I could do on_player_grant etc and then it would call each callback at most once.

print("Running callback!")
if not core.registered_privileges[priv].on_grant
or core.registered_privileges[priv].on_grant(name, nil) then
for _, func in ipairs(core.registered_on_priv_grant) do
Copy link
Member

Choose a reason for hiding this comment

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

dup code, please factorize

Copy link
Member

@nerzhul nerzhul left a comment

Choose a reason for hiding this comment

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

please fix dup code and reduce code indent with functions and i'm okay

doc/lua_api.txt Outdated
`granter_name` will be nil if the priv was granted by a mod.
* `on_revoke(name, revoker_name)`: Called when taken from player `name` by `revoker_name`.
`revoker_name` will be nil if the priv was revoked by a mod
* Note that the above two callbacks will be called twice if a player is responsible - once with the player name,
Copy link
Member

Choose a reason for hiding this comment

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

Unusual long line. Mind moving more to the second one?

@SmallJoker
Copy link
Member

Tested, works. You've got some debugging leftovers, which also need to be removed before merge.

@rubenwardy
Copy link
Member Author

updated. I think I'm going to split the callback in two - on_grant and on_granted_by_player. This makes the behaviour seem less odd, imo.

Copy link
Member

@nerzhul nerzhul left a comment

Choose a reason for hiding this comment

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

Okay for me

@nerzhul nerzhul merged commit 096ea03 into minetest:master Aug 26, 2017
osjc pushed a commit to osjc/minetest that referenced this pull request Jan 11, 2019
* Add register_on_priv_grant/revoke, and on_grant/revoke to privs. Call from /grant and /revoke

* Call on_grant and on_revoke callbacks from set_privs
osjc pushed a commit to osjc/minetest that referenced this pull request Jan 23, 2019
* Add register_on_priv_grant/revoke, and on_grant/revoke to privs. Call from /grant and /revoke

* Call on_grant and on_revoke callbacks from set_privs
luk3yx pushed a commit to luk3yx/minetest that referenced this pull request Feb 16, 2019
* Add register_on_priv_grant/revoke, and on_grant/revoke to privs. Call from /grant and /revoke

* Call on_grant and on_revoke callbacks from set_privs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants