1. Hello,

    I tried to edit the Balance class from InfoPanel for Rust | Oxide for it to display the player's RP amount from ServerRewards for Rust | Oxide

    So here are the interesting parts of the Balance class:

    Code:
    [PluginReference] Plugin ServerRewards;public int GetBalance(string PlayerID)
    {             
            var player = RustCore.FindPlayerByIdString(PlayerID);
            if (player == null || !ServerRewards) return 0;
            return (int)ServerRewards?.Call("CheckPoints", player.userID);
    }
    The problem is that the "!ServerRewards" condition is passing, so the function returns 0.

    If I remove the "!ServerRewards" condition, the Call is called and I get tons of errors.

    I don't know what is wrong, any of you could help me please?
     
  2. Wulf

    Wulf Community Admin

    If !ServerRewards passes, that means ServerRewards is installed. What errors are you getting exactly? Is the above code from your plugin I assume? First thing I would say is that you don't need to find the player to get their ID, you already have it from the argument.
    Code:
    [PluginReference] Plugin ServerRewards;public int GetBalance(string id)
    {
            if (!ServerRewards) return 0;
            return (int)ServerRewards.Call("CheckPoints", Convert.ToUInt64(id));
    }
     
  3. Here's the entire class, nothing has been edited outside of it:

    Code:
            #region Balance        private Balance Bala;
            private Timer BalanceUpdater;
            public class Balance
            {
                [PluginReference] Plugin ServerRewards;            public int RefreshRate = 3;            public Balance(int RefreshRate)
                {
                    this.RefreshRate = RefreshRate;
                }            public int GetBalance(string PlayerID)
                {             
                    var player = RustCore.FindPlayerByIdString(PlayerID);
                    if (player == null || !ServerRewards) return 0;
                    return (int)ServerRewards?.Call("CheckPoints", player.userID);
                }            public void Refresh(StoredData storedData, Dictionary<string, Dictionary<string, IPanel>> panels)
                {
                    if (!Settings.CheckPanelAvailability("Balance"))
                        return;                foreach (var panel in panels)
                    {
                        IPanel iPanel;
                        if (!panel.Value.TryGetValue("BalanceText", out iPanel)) continue;
                        var balance = GetBalance(panel.Key);
                        var panelText = (IPanelText)iPanel;
                        if (!balance.Equals(panelText.Content))
                        {
                            panelText.Content = $"{balance}";
                            panelText.Refresh();
                        }
                    }
                }
            }
            #endregion
    If I let the plugin call the "CheckPoints" function from ServerRewards, I get this error:

    Code:
    [Error] Failed to call hook 'OnServerInitialized' on plugin 'InfoPanel v0.9.5' (NullReferenceException: Object reference not set to an instance of an object)
     
  4. Wulf

    Wulf Community Admin

    I added a snippet above for you, not sure if you saw it. Also, are you modifying InfoPanel?
     
  5. I tried the code you gave to me, still returning 0 because of the line

    Code:
    if (!ServerRewards) return 0;
    And still getting the same error as above if I quote it.

    Also, I'm not doing a new plugin, I'm editing the InfoPanel one.
     
  6. Wulf

    Wulf Community Admin

    Are you sure ServerRewards is actually installed and loaded? If it fails there, it generally means it isn't.
     
  7. Yeah, just opened its Store menu in game at the moment.
     
  8. Wulf

    Wulf Community Admin

    Also, pretty sure [PluginReference] Plugin ServerRewards; needs to be in the main namespace, not nested in a method or sub-class.
     
  9. I also thought about that, so I tried placing the line below in the main namespace:

    Code:
    [PluginReference] static Plugin ServerRewards;
    Still the same result.
     
  10. Wulf

    Wulf Community Admin

    [PluginReference] Plugin ServerRewards;
     
  11. Without the "static" I get:

    Code:
    error CS0038: Cannot access a nonstatic member of outer type `Oxide.Plugins.InfoPanel' via nested type `Oxide.Plugins.InfoPanel.Balance'
     
  12. You need the plugin reference in the main namespace. The create a static instance of your plugin. Then call ServerRewards via the instance
    Code:
    [PluginReference] Plugin ServerRewards
    static InfoPanel instance;
    void OnServerInitialized()
    {
       instance = this;
    }// in your class
    instance.ServerRewards?.Call....
     
  13. Singleton very bad style of programming
     
  14. This is working ! Thanks a lot !
     
  15. That is not a singleton.
    Static classes (even singletons!) are completly fine because the plugin class itself already acts as single namespace for all plugin code.
    Don't just randomly parrot things.
     
  16. Static classes is another, Singleton pattern - Wikipedia
    try to build architecture without Singleton, class is an object, which in this should only manage itself.
     
  17. I have my ServerRewards plugin reference in the main namespace, and also create a static instance of info panel.

    Code:
    public double GetBalance(string id)
                {
                    var player = RustCore.FindPlayerByIdString(id);
                    if (player == null) return 0;
                    return (double)instance.ServerRewards?.Call("CheckPoints", Convert.ToUInt64(id));
                }
    But I get this error message:

    Code:
    Failed to call hook 'OnServerInitialized' on plugin 'InfoPanel v0.9.5' (InvalidCastException: Cannot cast from source type to destination type.)
    I guess there is something wrong with the cast issue:

    Code:
    void OnServerInitialized()
    {
       instance = this;
    }
     
  18. did you get this working im also attempting to do this.
     
  19. Code:
    public int GetBalance(string id)
                {
                    var player = RustCore.FindPlayerByIdString(id);
                    if (player == null) return 0;
                    return (int)instance.ServerRewards?.Call("CheckPoints", Convert.ToUInt64(id));
                }
    CheckPoints returns an int, forcing it as a double causes the error.
     
  20. thanks so much man, this is working :)
    [DOUBLEPOST=1484926029][/DOUBLEPOST]
    it is working now