Page 1 of 1

restrict mod to special users

PostPosted: Sun Mar 17, 2013 13:40
by BlindBanana
Hi everyone!

Is there a way to restrict some mod, lets say nuke to only some users, like the server admin?

For example with some addition to the grant command:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
/grant <username> <modname>

PostPosted: Sun Mar 17, 2013 14:24
by PilzAdam
This has to be implemented into the mod. The engine doesnt offer any commands for that, except the privilege system (but the mod has to use it).

PostPosted: Sun Mar 17, 2013 14:49
by BlindBanana
Are there any examples for this? Or can you give me some hints to implement this by myself?
Right now I have no idea how to start.

PostPosted: Sun Mar 17, 2013 15:00
by PilzAdam
You basically register a new privilege and check if a player has this when interacting.
For example if the nuke explodes when the player rightclicks it, then just do:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
minetest.register_node("nuke", {
    ...
    on_rightclick = function(pos, node, player)
        if minetest.check_player_privs(player:get_player_name(), {nuke=true}) then
            code here
        end
    end,
})

Maybe useful:
http://dev.minetest.net/register_privilege
http://dev.minetest.net/check_player_privs
http://dev.minetest.net/register_node#Primary_Callbacks

PostPosted: Sun Mar 17, 2013 17:31
by BlindBanana
Based on the mod nuke version 1.4 from sfan5 http://forum.minetest.net/viewtopic.php?id=638

I modified the init.lua file.

At the verry beginning I register a new privilege (add before the first line):
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
minetest.register_privilege("nuke", {
    description = "Can use TNT from mod nuke",
    give_to_singleplayer = false
})


After this, I modified every register_on_punchnode function to check the newly registerd privilege:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
minetest.register_on_punchnode(function(p, node, puncher)
    if node.name == "nuke:iron_tnt" then
        minetest.env:remove_node(p)
        local p_name = puncher:get_player_name()
        if minetest.check_player_privs(p_name, {nuke=false}) then
            -- player doesn''t have privilege --> cancel explosion
            minetest.chat_send_player(p_name, "privilege nuke required: defuse!")
            nodeupdate(p)
        end
        if minetest.check_player_privs(p_name, {nuke=true}) then
            -- player has privilege --> explode
            minetest.chat_send_player(p_name, "privilege nuke found: explode!")
            spawn_tnt(p, "nuke:iron_tnt")
            nodeupdate(p)
        end
    end
end)


This works.

Nobody has the privilege "nuke" by default (except maybe the serveradmin). The TNT gets defused and removed as it should.

If somebody has the privilege, the TNT explodes. But in this case both messages "privilege nuke required: defuse!" and "privilege nuke found: explode!" are sent to the player. This one thing i don't understand. Only "privilege nuke found: explode!" should be sent to the player.

PostPosted: Mon Mar 18, 2013 22:11
by BlindBanana
Stupid! Just reverse it and use elseif..

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
minetest.register_on_punchnode(function(p, node, puncher)
    if node.name == "nuke:iron_tnt" then
        minetest.env:remove_node(p)
        local p_name = puncher:get_player_name()
        if minetest.check_player_privs(p_name, {nuke=true}) then
            -- player has privilege --> explode
            minetest.chat_send_player(p_name, "privilege nuke found: explode!")
            spawn_tnt(p, "nuke:iron_tnt")
            nodeupdate(p)
        elseif minetest.check_player_privs(p_name, {nuke=false}) then
            -- player doesn''t have privilege --> cancel explosion
            minetest.chat_send_player(p_name, "privilege nuke required: defuse!")
            nodeupdate(p)
        else
            minetest.chat_send_player(p_name, "nuke mod: this should never be seen!")
        end
    end
end)

PostPosted: Mon Mar 18, 2013 22:16
by jojoa1997
That is what I do with my npc mod. You need a priv to place spawners so people can't overload the servers.