New Xp Functions

Discussion in 'Rust Development' started by Caffeine, Jul 8, 2016.

  1. I took a look at Rust.Xp.dll in decompiler and wrote these functions that let you tap into the new functionality in addition to the callbacks listed at Oxide API for Rust. Remember to put the following at the top of your code. All credit to Wolf for his help in other threads.

    Code:
    using Rust.Xp; 
    Code:
             float GetCurrentLevel(BasePlayer player)
            {
                return player.xp.CurrentLevel;
            }
      
            float GetEarnedXp(BasePlayer player)
            {
                return player.xp.EarnedXp;
            }
      
            float GetSpentXp(BasePlayer player)
            {
                return player.xp.SpentXp;
            }
      
            float GetUnspentXp(BasePlayer player)
            {
                return player.xp.UnspentXp;
            }
      
            float GetCurrentTime(BasePlayer player)
            {
                return player.xp.CurrentTime;
            }
          
            void ResetXP(BasePlayer player)
            {
                player.xp.Reset();
            }
      
             void AddXP(BasePlayer player, float amount)
             {
                 player.xp.Add(Rust.Xp.Definitions.Cheat, amount);
             }
       
             void SetXP(BasePlayer player, float amount)
             {
                 ResetXP(player);
                 AddXP(player,amount);
             }        bool SpendXp(BasePlayer player,int xpCount, string Category)
            {
                return player.xp.SpendXp(xpCount,Category);
            }
      
            void AddLevel(BasePlayer player, int level)
             {
                 float currentlevel = GetCurrentLevel(player);
                 float leveltoxp = LevelToXp((int)currentlevel+level);
                 ResetXP(player);
                 AddXP(player,leveltoxp);
             }
       
             void SetLevel(BasePlayer player, int level)
             {
                 ResetXP(player);
                 AddXP(player,LevelToXp(level));
             }
       
      
      
            /* Utility functions to return the amounts based on the input */
            float LevelToXp(int level)
            {
                return Rust.Xp.Config.LevelToXp(level);
            }
      
            float XpToLevel(float xp)
            {
                return Rust.Xp.Config.XpToLevel(xp);
            }
    There's more functionality in there than this, but not sure what some of the other stuff does yet.
     
    Last edited by a moderator: Jul 10, 2016
  2. How to up player level? and how to give xp ?
     
  3. You can use AddLevel to add however many to current level, or SetLevel to set it as that level regardless of the current. For Xp can do AddXp or SetXp, I didn't comment because I figured their names would indicate what they did. ResetXp clears all the users XP setting them to level 1.

    So for example if you wanted to increase every awake player on the servers level by 1 you could do

    Code:
    var Active = BasePlayer.activePlayerList as List<BasePlayer>;
    foreach( BasePlayer player in Active )
    {
       AddLevel(player,1);
    }
    Or give them 50 xp
    Code:
    var Active = BasePlayer.activePlayerList as List<BasePlayer>;
    foreach( BasePlayer player in Active )
    {
       AddXp(player,50);
    }
    If you wanted to set all active admins to level 300 you could do

    Code:
    var Active = BasePlayer.activePlayerList as List<BasePlayer>;
    foreach( BasePlayer player in Active )
    {
       if(isAuth(player))
            SetLevel(player,300);
    }/* credit to whoever originally wrote this */
    bool isAuth(BasePlayer player)
            {
                if (player.net.connection != null)
                    if (player.net.connection.authLevel < 1)
                        return false;
                return true;
            }
    
     
    Last edited by a moderator: Jul 8, 2016
  4. Wulf

    Wulf Community Admin

    You don't need the // Reference at the top anymore, it's already referenced in the last Oxide build.
    [DOUBLEPOST=1467985686,1467985516][/DOUBLEPOST]
    This would be the proper way to add a level: player.xp.Add(Definitions.Cheat, Rust.Xp.Config.LevelToXp(#));
     
  5. I'm going to test that real fast, I feel like that's off for level adding, that's why I did a few extra step.
     
  6. Wulf

    Wulf Community Admin

    That's how Rust does it. ;)

    That is basically for leveling to X level by adding the required XP needed.
     
  7. Just tested it, here's why your code is abit off

    [Oxide] 10:30 [Info] [XPTest] level 10: 81 xp
    [Oxide] 10:30 [Info] [XPTest] level 5: 16 xp

    so simply adding level 5 worth of xp to existing level 5 wouldn't be right, have to set to level 10 xp instead 16+16 != 81xp


    Sorry for the block of text before, this should summarize all of it though. My existing AddLevel appears to work right.
     
    Last edited by a moderator: Jul 8, 2016
  8. The LevelToXp method requires the level you want to end up as as the argument, not the amount of levels you wish to add, by doing that you might wish to call a reset or reduce the amount of experience that the player already has:
    Code:
    void AddLevel(BasePlayer player, int levels = 1) => player.xp.Add(Definitions.Cheat, Rust.Xp.Config.LevelToXp((int)player.xp.CurrentLevel + levels) - player.xp.EarnedXp);
     
  9. What? Where did you found this? I have this:
    Screenshot_3.jpg
     
  10. That is what I use, I just use a calculation with methods, fields and input to create the float amount to allow it to be used to add a specific amount of levels :)
     
  11. Your code does the same thing mine does :p just you one lined it and subtracted EarnedXp instead of clearing all of it like i did.
     
  12. I know, was more of a reply to Wulf than to you :p
     
  13. Wulf

    Wulf Community Admin

    I know, that's what I meant it for.
     
  14. What's the name of the variable to get the player level ?
    level = player.xp.CurrentLevel;
    Code:
    if (crafter.xp.CurrentLevel<50) { AddLevel(crafter,1); }
    AddXp(crafter,50);
    My code doesn't work I have this output :
    error CS0103: The name `AddXp' does not exist in the current context
     
    Last edited by a moderator: Jul 8, 2016
  15. That's because 'AddXp' does not exist in the current context :p.
    You should add to your code:
    Code:
    void AddXP(BasePlayer player, float amount)
             {
                 player.xp.Add(Rust.Xp.Definitions.Cheat, amount);
             }
     
  16. Wulf

    Wulf Community Admin

    Or just call it directly: player.xp.Add(Rust.Xp.Definitions.Cheat, amount);
     
  17. Code:
    float currentlevel = crafter.xp.CurrentLevel;
    if (currentlevel<=51) {crafter.xp.Add(Definitions.Cheat, 10);}
    Thk this code work fine
    [DOUBLEPOST=1468003829][/DOUBLEPOST]Ok, now How i can test in cs code if Arg[2] is an int ?
     
  18. Convert.ToInt64(args[2])
     
  19. Wulf

    Wulf Community Admin

    That would convert it to a ulong.
     
  20. Or float.Parse([2]) works I think