introzen

Restrict commands by tag

Recommended Posts

Posted (edited)

Hi!

First post, happy to be here! I'm an old SAMP scripter who never really finished anything.

I'm starting a roleplay gamemode in this awesome scripting environment (compared to PAWN) and I'm looking for some guidance so that I can do everything right from the start.

I'm currently going to start creating all admin commands from scratch and I'm wondering if there's any way to tag the commands, storing them as admin specific commands and at the same time setting an admin level restriction to them. My thoughts are then to check for these commands being run from anyone on server and at that point checking if that player is an admin or not, rather than checking this for every single command.

Examples are widely appreciated!
 

Regards,

introzen

Edited by introzen

Share this post


Link to post
Share on other sites

I can understand what you mean here although i'm not sure how you would go about this, I suggest maybe making regions and a seperate class for admin commands... Such as..

 

GTANetwork Side solution:

public class AdminCommands {

#region executive

dosomethinghere

#endregion executive



#region headadmin

dosomethinghere

#endregion headadmin

}

RageMP Side solution:

WIKI:

https://wiki.rage.mp/index.php?title=PlayerCommand 

 

Just insert an if statement in the event.
Kind Regards,
JCurtis

Share this post


Link to post
Share on other sites
Posted (edited)

Maybe a data structure like this would work well for you?

You could make a GUI to add/assign roles and commands (along with what roles a command requires). Could even go further and add individual permissions directly to a person, or multiple permissions per role.

These are the roles themselves

Roles {
    Id,
    Name
}

This table gives people specific roles

UserRoles {
    UserId,
    RoleId
}

 

Then how you store the commands themselves is up to you.

Maybe something like this, which is the commands themselves: 

Commands {
    CommandId,
    Name,
    Description
}

 

Then for assigning which commands work with what roles:

RoleCommands {
    RoleId,
    CommandId
}

 

As for your built in commands, you could have a base set of roles (or permissions would make more sense, then people could give those specific permissions to whatever role/person they'd like) which would be added whenever you bootstrap your addon. Then those with that role would be allowed to use your built in commands.

Edited by ManselD

Share this post


Link to post
Share on other sites
Posted (edited)

I hugely appreciate the effort, although I still think those solutions are to complicated. Surely there must be some other way more efficient.

I'm thinking something like the following. I know this code would not work, but it might show you where I'm heading.

public class AdminCommands : Script
{
	[Command("kick", alvl = 1)] //set optional command parameter alvl to regulate the lowest needed alvl to execute the command.
  	public void CMD_kick(Client client)
    	{
     	//Kick command here.. 
    	}
  
  	[ServerEvent(Event.PlayerCommand)]
  	public void OnPlayerCommand(Client client, string cmd)
  	{
            if(cmd.alvl) > 0) //if the required alvl for the command is more than 0, check if user is admin and what lvl
            {
                if(client.GetData("alvl") < cmd.alvl) return; //if the users alvl is less than the required for this command, return.
            }
  	}
  
}

 

Edited by introzen

Share this post


Link to post
Share on other sites
Posted (edited)

The most efficient and readable way would be to create data-structures, That's why it's in the name to be able to structure data in a way that you would like to. If you're looking to just create something quickly just set a value on the player you are looking for in the command.

Edited by Crossy1998
  • Like 1

Share this post


Link to post
Share on other sites
Posted (edited)

I agree with Crossy.

Your method @introzen would require the use of reflection in order to get what you need working. Using your method, the permissions would be hardcoded into the application. Meaning to change permissions for commands around, you'd have to edit & build the code again.

You could make a new attribute, inherit from the current Command and then overload the constructor with your new parameter.

Inside your OnPlayerCommand method you could get the current Assembly/Class (depends how you want to do it, and if you want this system everywhere in your project). Find all methods and filter them based on if they have your custom attribute. Then ensure that the command itself (which is a property on your inherited attribute) matches the one entered by the user. If it does, check the user's command level against the command itself.

Here's a starting point that you may be interested in: https://stackoverflow.com/questions/5475209/get-class-methods-using-reflection

Edited by ManselD
  • Like 1

Share this post


Link to post
Share on other sites

Hmm, I was going to say that I feel like the skill requirement to achieve this is just not currently where I am right now, but then I read your post over and over again and I think I actually understand what you mean. Please correct me if I'm wrong.

13 minutes ago, ManselD said:

You could make a new attribute, inherit from the current Command and then overload the constructor with your new parameter.

By this, I assume you mean that I would create something that inherits the properties of the regular [Command(str cmd)] attribute, but in which I could add my own parameters. Something like this?
 

namespace myAttribute
{
    [AttributeUsage(
            AttributeTargets.Method,
            AllowMultiple = true,
            Inherited = true)]
    public class Command : Attribute
    {
        private string cmdtext;
        private int alvl;

        public Command(string cmdtext)
        {
            this.cmdtext = cmdtext;
        }

        public Command(string cmdtext, int alvl)
        {
            this.cmdtext = cmdtext;
            this.alvl = alvl;
        }
    }
}

Please point me in the right direction.

The stackoverflow link you posted, I assume it can be useful for when it is time to find all methods under OnPlayerCommand?

Share this post


Link to post
Share on other sites
Posted (edited)

Good work so far man! But instead of inheriting from the base attribute, you could inherit from the current command attribute, so that it still functions as it did previously (in that your method will get called and hooked as a command when the player enters it in chat)

like so: 

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class PermCommand : Attribute {
	private int _level; 
	//We only need one constructor, as we're defaulting level to 0 and passing along the command text to the base attribute.
  	public Command(string cmdText, int level = 0) : base(cmdText) { 
		_level = level;
  	}
}

However that depends how the mod was created. If RageMP doesn't check inheritance, then your method will work fine. You could just have two attributes then. The command attribute as normal on a method, then yours like so:

//Combined attribute using inheritance
[PermCommand("kick", Level = 1)]
public void KickCommand(Client client) {
    //Kick command here 
}

//Or if the API doesn't pickup inheritance, make a separate attribute. Although now I think of it, it feels cleaner this way.
[Command("kick")]
[PermissionLevel(1)]
public void KickCommand(Client client)
{
    //Kick command here
}

Then in your OnPlayerCommand method you'd find your new command, or your custom one depending on which works. Give either a go I suppose. It's entirely up to you.

Edited by ManselD
  • Like 2

Share this post


Link to post
Share on other sites

Shit man, I really get it!

I will try to apply this during the next days. Surely I will run into some issues but now at least I have a basic idea of how I should do it.

Until then, thanks a million!

Share this post


Link to post
Share on other sites

No worries! If you need anything else I'm always open to PMs.

I love C# so am more than happy to help.

Hope it all goes well!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.