Tutorial : How to create extension


Tutorial: Creating an Extension for CustomRewards

In this tutorial, we'll guide you through creating a functional extension for the CustomRewards plugin using Oxide. We'll use the PlaytimeReward extension as an example, which integrates with the PlaytimeTracker plugin. This choice provides a practical scenario where players are rewarded based on their playtime, showcasing how CustomRewards can be integrated with other plugins.

Prerequisites

  • Basic knowledge of Oxide plugin development.

  • Installed CustomRewards plugin.

1. Setting Up Your Extension

Start by creating a new class for your extension:

public class PlaytimeReward : RustPlugin
{
    [PluginReference] 
    Plugin PlaytimeTracker, CustomRewards;
}

2. Interacting with CustomRewards

Fetching Points with Additional Data:

We're using Get_Owner_WithString_API because we want to retrieve not just the points but also an additional string value, which represents the total playtime.

public int GetPointsWithString(ulong userId, out string stringVal)
{
    stringVal = null;

    if (CustomRewards == null)
    {
        Puts("Error : CustomRewards plugin not loaded !!");
        return 0;
    }

    string[] result = CustomRewards.Call<string[]>("Get_Owner_WithString_API", m_configuration.CustomRewardsType, nameof(PlaytimeReward), userId);
    if (result == null)
        return 0;

    int points = int.Parse(result[0]);
    stringVal = result[1];

    return points;
}

Giving Points with Additional Data:

We use Give_Owner_WithString_API because we want to store additional data (the playtime) alongside the points.

public void GiveVotePointsWithString(int points, ulong userId, string objString)
{
    if (CustomRewards == null)
    {
        Puts("Error : CustomRewards plugin not loaded !!");
        return;
    }

    CustomRewards.Call("Give_Owner_WithString_API", m_configuration.CustomRewardsType, nameof(PlaytimeReward), userId, points, objString);
}

3. Core Logic of Your Extension

Fetching Playtime:

This method fetches the playtime of a player:

public double PlaytimeTracker_GetPlayedTime(string type, ulong playerId)
{
    double time = 0;
    object timeObj = PlaytimeTracker.Call<object>("GetPlayTime", playerId.ToString());

    if (timeObj != null)
        time += (double)timeObj;

    if (m_configuration.CheckAfkTime)
    {
        timeObj = PlaytimeTracker.Call<object>("GetAFKTime", playerId.ToString());
        if (timeObj != null)
            time += (double)timeObj;
    }

    return time;
}

Awarding Points Based on Playtime:

This method calculates and awards points based on the player's playtime:

public void PlaytimeTracker_TryGivePointsForPlayedTime(string type, ulong playerId)
{
    var time = PlaytimeTracker_GetPlayedTime(type, playerId);
    string stringVal = null;
    var points = GetPointsWithString(playerId, out stringVal);
    double oldTime = 0;

    if (stringVal != null)
        oldTime = double.Parse(stringVal);

    if (time <= oldTime)
    {
        return;
    }

    stringVal = time.ToString();

    if (time - oldTime < m_configuration.EveryXSeconds)
    {
        return;
    }

    int totalPoints = (int)((time / (double)m_configuration.EveryXSeconds) * (double)m_configuration.PointsEveryXSeconds);
    if (totalPoints < points)
    {
        return;
    }

    var diff = totalPoints - points;
    if (diff > 0)
    {
        GiveVotePointsWithString(totalPoints, playerId, time.ToString());
    }
}

Refreshing Points:

Before showing the UI, refresh the player's points based on their playtime:

private void RefreshPoints(ulong playerId)
{
    if (PlaytimeTracker != null)
    {
        PlaytimeTracker_TryGivePointsForPlayedTime(m_configuration.CustomRewardsType, playerId);
    }
}

4. CustomRewards Hooks

Implement hooks to interact with the CustomRewards plugin:

private void CustomRewards_OnBeforeShowUI(string type, ulong playerId)
{
    if (type != m_configuration.CustomRewardsType)
        return;

    RefreshPoints(playerId);
}

5. Conclusion

By following this tutorial, you can create a functional extension for the CustomRewards plugin using Oxide. This guide focuses on the integration aspect with CustomRewards, ensuring that by the end, you have a complete and operational plugin. The choice of methods (..._WithString_API vs. the simpler versions) is based on the need to store and retrieve additional data, showcasing the flexibility of CustomRewards. Adapt the logic and methods based on your extension's specific requirements. Happy coding!

Last updated