Jump to content
  • Sign Up
  • 0
HighTide

How can I give players awards with custom commands

Question

I am trying to make a secret command for my server that gives an award when you use it (you will only be able to use it once). I can't figure out how to A; have it check if you have the award before trying to give it to you again and B; if you don't have the award, give it to you. I can't figure it out as all I can figure out how to do at the moment is p.Message() and p.PlayerActions.ChangeLevel() or whatever. Someone please help :D. I am using MCGalaxy for my server FYI

Share this post


Link to post

5 answers to this question

Recommended Posts

  • 3

First, you can use the source code for reference. Here is the PlayerAwards class: https://github.com/ClassiCube/MCGalaxy/blob/master/MCGalaxy/Modules/Awards/PlayerAwards.cs

To use anything from the PlayerAwards class, you will have to add "using MCGalaxy.Modules.Awards;" at the top of your code file.

Any method or field that is marked as "public" can be used by plugins and commands. You can use "PlayerAwards.Give" to give an award to a player, but this won't show a global chat message. If you want to show a global chat message, you should have console use the award give command. You can do that like this:

const string myAward = "My Secret Award";
Command.Find("award").Use(Player.Console, "give "+p.name+" "+myAward);

If you want to check if the player has the award already, you will have to get the list of awards for the player and then check if that list has the award inside of it. You can do that like this:

bool hasAwardAlready = false;
List<string> currentAwards = PlayerAwards.Get(p.name);
if (currentAwards.CaselessContains(myAward)) { hasAwardAlready = true; }

As a side note, you can shorten the above code like this:

bool hasAwardAlready = PlayerAwards.Get(p.name).CaselessContains(myAward);

Here is an example of everything so far put together. Remember you still need "using MCGalaxy.Modules.Awards;" at the top of the file.

public override void Use(Player p, string message)
{
    const string myAward = "My Secret Award";
    
    bool hasAwardAlready = PlayerAwards.Get(p.name).CaselessContains(myAward);

    if (hasAwardAlready) {
        p.Message("You already found the secret!");
        return;
        // When we write return, that will quit running the command so that the code below doesn't run
    }
    
    //Give the player the award if they haven't already gotten it
    Command.Find("award").Use(Player.Console, "give "+p.name+" "+myAward);
}


 

Share this post


Link to post
  • 0
4 hours ago, Goodly said:

First, you can use the source code for reference. Here is the PlayerAwards class: https://github.com/ClassiCube/MCGalaxy/blob/master/MCGalaxy/Modules/Awards/PlayerAwards.cs

To use anything from the PlayerAwards class, you will have to add "using MCGalaxy.Modules.Awards;" at the top of your code file.

Any method or field that is marked as "public" can be used by plugins and commands. You can use "PlayerAwards.Give" to give an award to a player, but this won't show a global chat message. If you want to show a global chat message, you should have console use the award give command. You can do that like this:


const string myAward = "My Secret Award";
Command.Find("award").Use(Player.Console, "give "+p.name+" "+myAward);

If you want to check if the player has the award already, you will have to get the list of awards for the player and then check if that list has the award inside of it. You can do that like this:


bool hasAwardAlready = false;
List<string> currentAwards = PlayerAwards.Get(p.name);
if (currentAwards.CaselessContains(myAward)) { hasAwardAlready = true; }

As a side note, you can shorten the above code like this:


bool hasAwardAlready = PlayerAwards.Get(p.name).CaselessContains(myAward);

Here is an example of everything so far put together. Remember you still need "using MCGalaxy.Modules.Awards;" at the top of the file.


public override void Use(Player p, string message)
{
    const string myAward = "My Secret Award";
    
    bool hasAwardAlready = PlayerAwards.Get(p.name).CaselessContains(myAward);

    if (hasAwardAlready) {
        p.Message("You already found the secret!");
        return;
        // When we write return, that will quit running the command so that the code below doesn't run
    }
    
    //Give the player the award if they haven't already gotten it
    Command.Find("award").Use(Player.Console, "give "+p.name+" "+myAward);
}


 

Thank you 😄

Share this post


Link to post
  • 0
On 4/18/2024 at 9:14 AM, Goodly said:

First, you can use the source code for reference. Here is the PlayerAwards class: https://github.com/ClassiCube/MCGalaxy/blob/master/MCGalaxy/Modules/Awards/PlayerAwards.cs

To use anything from the PlayerAwards class, you will have to add "using MCGalaxy.Modules.Awards;" at the top of your code file.

Any method or field that is marked as "public" can be used by plugins and commands. You can use "PlayerAwards.Give" to give an award to a player, but this won't show a global chat message. If you want to show a global chat message, you should have console use the award give command. You can do that like this:


const string myAward = "My Secret Award";
Command.Find("award").Use(Player.Console, "give "+p.name+" "+myAward);

If you want to check if the player has the award already, you will have to get the list of awards for the player and then check if that list has the award inside of it. You can do that like this:


bool hasAwardAlready = false;
List<string> currentAwards = PlayerAwards.Get(p.name);
if (currentAwards.CaselessContains(myAward)) { hasAwardAlready = true; }

As a side note, you can shorten the above code like this:


bool hasAwardAlready = PlayerAwards.Get(p.name).CaselessContains(myAward);

Here is an example of everything so far put together. Remember you still need "using MCGalaxy.Modules.Awards;" at the top of the file.


public override void Use(Player p, string message)
{
    const string myAward = "My Secret Award";
    
    bool hasAwardAlready = PlayerAwards.Get(p.name).CaselessContains(myAward);

    if (hasAwardAlready) {
        p.Message("You already found the secret!");
        return;
        // When we write return, that will quit running the command so that the code below doesn't run
    }
    
    //Give the player the award if they haven't already gotten it
    Command.Find("award").Use(Player.Console, "give "+p.name+" "+myAward);
}


 

Sorry but when I put it into my script, it gives me the following error: 

(20:42:40) Error #CS0103 on line 39 - The name `PlayerAwards' does not exist in the current context
(20:42:40) Compiling failed. See logs/errors/compiler.log for more detail

Is there a using statement I should add or something?

  • cube 1

Share this post


Link to post
  • 0
8 minutes ago, HighTide said:

Is there a using statement I should add or something?

Yes. As the post you're replying states you need to add

using MCGalaxy.Modules.Awards;

to the top of your file. (Why does Invision not support small code blocks like Markdown or HTML's <code> block?)

Share this post


Link to post
  • 0
Just now, icanttellyou said:

Yes. As the post you're replying states you need to add


using MCGalaxy.Modules.Awards;

to the top of your file. (Why does Invision not support small code blocks like Markdown or HTML's <code> block?)

Thanks for the help 😄

Share this post


Link to post

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

×
×
  • Create New...