Catalog News Guides Community For contributors
Everything for CS 1.6 / CSS / CS:GO servers - Plugins | Builds | Models | Maps » Articles » Pawn from Scratch? Is it really possible to write your own server plugin ( 1 full-fledged plugin )

Pawn from Scratch? Is it really possible to write your own server plugin ( 1 full-fledged plugin )

29.04.2023, 19:56adminViews: 509Comments: 1
Pawn from Scratch? Is it really possible to write your own server plugin ( 1 full-fledged plugin )

About this content

Pawn с Нуля? реально ли написать свой плагин для сервера  ( 1 полноценный плагин )
We've already learned how to write a simple plugin, and now it's time to make something more complex and simple at the same time.

Let's write a plugin that adds a tag to chat based on an access flag—a fairly useful and simple plugin, and most importantly, FREE  

We'll have a VIP tag for us and our buddies, and DOG for those who have annoyed us )

#include < amxmodx >

#include < amxmisc >

 

new const TAG_VIP[] = "VIP";

new const TAG_DOG[] = "DOG";

 

public plugin_init() {

    register_plugin("Tag Plugin", "1.0", "MyNickname");

    register_clcmd("mytags", "cmdTags");

}

 

public cmdTags(id) {

    new szTags[128], szTag[32];

 

    // Получаем доступные теги из конфига

    GetConfigString("Tags", "VIP", szTag, sizeof szTag);

    if (strlen(szTag))

        formatex(szTags, sizeof szTags, "%s%s ", szTags, szTag);

 

    GetConfigString("Tags", "DOG", szTag, sizeof szTag);

    if (strlen(szTag))

        formatex(szTags, sizeof szTags, "%s%s ", szTags, szTag);

 

    // Получаем флаги доступа игрока

    new flags = get_user_flags(id);

 

    // Если игрок имеет флаг j, добавляем тег VIP

    if (flags & ADMIN_J) {

        client_print(id, print_console, "You have VIP access.");

        client_print(id, print_chat, "[%s] ", TAG_VIP);

    }

 

        // If the player has the i flag, add the DOG tag

        if (flags & ADMIN_I) {

        client_print(id, print_console, "You have DOG access.");

        client_print(id, print_chat, "[%s] ", TAG_DOG);

    }

 

        // Display the player's available tags in the console

        formatex(szTag, sizeof szTag, "Your available tags: %s", szTags);

        client_print(id, print_console, szTag);

 

        return PLUGIN_CONTINUE;

}

Code explanation:

  1. At the beginning, we include two header files - amxmodx и amxmisc, which are needed for the plugin to work.
  2. Then we declare two constants - TAG_VIP и TAG_DOG, which store the corresponding tags.
  3. Next, we register the plugin and the command /mytags, which will display the list of the player's available tags.
  4. In the function cmdTags we declare several local variables: szTags - a string to which the available tags will be added, szTag - a temporary buffer for retrieving each tag from the config.
  5. Using the function GetConfigString we get the list of available tags from the config and add each one to the string szTags.
  6. Then we get the player's access flags using the function get_user_flags.
  7. If the player has the flag ADMIN_J, then we display a message about it in the console and add the VIP tag to the player's chat.
  8. Similarly, if the player has the flag ADMIN_I, then we display

Don't forget to change the flags and names... After all, this is a lesson, not just copy-pasting my code!!!


Write about the results in the comments, and for convenience, let's make things a little more complicated: I don't like having to recompile the plugin all the time, so let's move everything into a config.  


To do this, we need to make some changes to the plugin.

  1. We need to add a new configuration file "chatvip.cfg" to the "configs" directory.
  2. In the plugin code, we need to replace hard-coded values with values read from the configuration file.
  3. To read values from the configuration file, we need to use functions from the "file" library.
  4. We also need to check that the values were read successfully from the configuration file.

Here's what the updated plugin code will look like:

#include < amxmodx >

#include < amxmisc >

#include < amxstring >

#include < amxfile >

 

new String:tagVIP[MAX_PATH];

new String:tagDOG[MAX_PATH];

 

public plugin_init()

{

    register_plugin("ChatVIP", "1.0", "Author");

 

    if (!load_config())

    {

        log_amx("Failed to load configuration. Plugin will be disabled.");

        plugin_end();

        return;

    }

 

        register_event("SendText", "on_send_text");

}

 

public on_send_text(id, const message[], len, const name[], const prefix[], team)

{

    new access_flags = get_user_flags(id);

 

    if (access_flags & ADMIN_FLAG)

    {

        return PLUGIN_CONTINUE;

    }

 

        if (access_flags & get_flag_bit('j'))

    {

        formatex(tagVIP, sizeof(tagVIP), "[%s] ", chat_tag('VIP'));

        client_print(id, print_chat, "%s%s%s", tagVIP, prefix, message);

        return PLUGIN_HANDLED;

    }

 

        if (access_flags & get_flag_bit('i'))

    {

        formatex(tagDOG, sizeof(tagDOG), "[%s] ", chat_tag('DOG'));

        client_print(id, print_chat, "%s%s%s", tagDOG, prefix, message);

        return PLUGIN_HANDLED;

    }

 

        return PLUGIN_CONTINUE;

}

 

get_flag_bit(flag)

{

    new const config[] = "configs/chatvip.cfg";

    new bit;

 

    if (!read_config_string(config, "VIP", tagVIP, sizeof(tagVIP)))

    {

        log_amx("Failed to read tag for VIP from configuration.");

        return 0;

    }

 

        if (!read_config_string(config, "DOG", tagDOG, sizeof(tagDOG)))

    {

        log_amx("Failed to read tag for DOG from configuration.");

        return 0;

    }

 

        switch (flag)

    {

        case 'j': bit = ADMIN_CHAT; break;

        case 'i': bit = DOG_CHAT; break;

        default: bit = 0; break;

    }

 

        return bit;

}

 

load_config()

{

    new const config[] = "configs/chatvip.cfg";

 

    if (!read_config_string(config, "VIP", tagVIP, sizeof(tagVIP)))

    {

        log_amx("Failed to read tag for VIP from configuration.");

        return false;

    }

 

        if (!read_config_string(config, "DOG", tagDOG, sizeof(tagDOG)))

    {

        log_amx("Failed to read tag for DOG from configuration.");

        return false;

    }

 

        return true;

}

After these changes, you can change the values of the variables "tagVIP" and "tagDOG" in the file "configs/chatvip.cfg", without needing to change

  Compile it and give it a try. Well, it's really easy and simple; I hope it worked for everyone, and don't forget to leave comments—I look forward to hearing from you.

We can make life a little harder for our Dogs on the server and add a sound. I'll add a barking sound; they'll bark as soon as they start typing, so I'll know whether I should read the chat or not...    

Here's the updated plugin code, which adds a tag to a player's chat if they have a specific access flag and plays a sound when a specific tag is typed in chat:

#include < amxmodx >

#include < amxmisc >

#include < hamsandwich >

 

new const MAX_TAG_LEN = 32;

new const MAX_SOUND_LEN = 256;

 

new Handle:configFile;

new char:tagVIP[MAX_TAG_LEN];

new char:tagDOG[MAX_TAG_LEN];

new char:soundDOG[MAX_SOUND_LEN];

 

public plugin_init()

{

    register_plugin("Chat VIP", "1.0", "Author");

 

    configFile = Ham_CreateCfg("configs/chatvip.cfg");

 

    Ham_ReadCfgString(configFile, "VIP", "j", tagVIP, MAX_TAG_LEN);

    Ham_ReadCfgString(configFile, "DOG", "i", tagDOG, MAX_TAG_LEN);

    Ham_ReadCfgString(configFile, "DOG_SOUND", "/cstrike/music/dog.mp3", soundDOG, MAX_SOUND_LEN);

 

    register_clcmd("say", "OnSay", true);

}

 

public OnSay(id, type, const message[], len)

{

    if (type == 0) // Public chat message

    {

        new szTag[MAX_TAG_LEN];

        new szPlayerName[MAX_PLAYER_NAME];

        new szPlayerTag[MAX_TAG_LEN];

        new szPlayerSound[MAX_SOUND_LEN];

 

        GetClientName(id, szPlayerName, sizeof szPlayerName);

        GetClientAuthId(id, szPlayerTag, sizeof szPlayerTag);

 

        // Check for VIP tag

        if (strstr(szPlayerTag, "j"))

        {

            formatex(szTag, sizeof szTag, "[%s]", tagVIP);

            formatex(szPlayerSound, sizeof szPlayerSound, "%s", "");

        }

                // Check for DOG tag

                else if (strstr(szPlayerTag, "i"))

        {

            formatex(szTag, sizeof szTag, "[%s]", tagDOG);

            formatex(szPlayerSound, sizeof szPlayerSound, "%s", soundDOG);

        }

                else

        {

            return PLUGIN_CONTINUE;

        }

 

                ClientPrint(id, print_chat, "%s %s: %s", szTag, szPlayerName, message);

 

                if (szPlayerSound[0] != '')

        {

            client_cmd(id, formatex("spk %s", szPlayerSound));

        }

 

                return PLUGIN_STOP;

    }

 

        return PLUGIN_CONTINUE;

}


Updated configuration file "configs/chatvip.cfg":

"VIP"

{

    "tag" "j"

}

 

"DOG"

{

    "tag" "i"

    "sound" "/cstrike/music/dog.mp3"

}


In this version of the plugin, if a player types the tag "i", then the sound /cstrike/music/dog.mp3 will play every time someone types the tag "i" in chat.

 

Similar content

Comments (1)

Add comment

Leave a comment

  1. Zaur
    Zaur
    2 May 2024 15:16

    Продолжение!!)

New content by email

Subscription with email confirmation. No more than one email per day. Unsubscribe via the link in any email.

Join the community

Register · Reset password