1. Hello,

    I am trying to Reference the Economics plugin.

    The current API is

    Code:
    void Set(ulong playerId, double money)
    So when users gather resources I want them to receive economics money like so...

    Code:
    void OnDispenserGather(ResourceDispenser dispenser, BaseEntity entity, Item item)
    {
        Set(playerId, 1)
    }
    But I am unsure how to reference another plugin and put it's methods into scope.
     
  2. Code:
    [PluginReference]
            private Plugin Economics;void OnDispenserGather(ResourceDispenser dispenser, BaseEntity entity, Item item)
    {
        if (!entity.ToPlayer()) return;
        Economics?.CallHook("Set", entity.ToPlayer().userID, 1);
    }
    I wasn't really paying attention at first, this should get you the right ID
     
    Last edited by a moderator: Dec 12, 2016
  3. Awesome, You the man!! :D
    [DOUBLEPOST=1481567479][/DOUBLEPOST]
    Sorry... Is there an easy way to find a playerId? Economics takes the following...

    Code:
    private void Set(ulong playerId, double money)
            {
                economicsData[playerId] = money >= 0 ? money : 0;
                changed = true;
            }
     
  4. I updated it lol, just realized it wouldn't work the way you had it :p
     
  5. Awesome, It's going to take some time learning the API for oxide and knowing what each thing does, I would never of guessed to use entity.ToPlayer().

    Is BaseEntity entity handling who is actually hitting the dispenser?
     
  6. That should give you the player that mined the resource, I've never really used that hook for anything ;)
     
  7. I just realised I am using Set which will set the money to that amount and not increase so I need to use GetPlayerMoney and then += 1

    Code:
    double GetPlayerMoney(ulong playerId)
    Using CallHooks how do I return the value it receives to an int or double?
     
  8. I'd probably use something like
    Code:
    double money = (double)Economics?.CallHook("GetPlayerMoney", entity.ToPlayer().userID);
     
  9. You don't need to cast it, you can do something like this:
    Code:
    var money = Economics?.CallHook<double>("GetPlayerMoney", entity.ToPlayer().userID) ?? 0;
    
    I'm not 100% sure but I think if that returned a null (plugin not existing?) with your code that it would give you an error when trying to cast it to a double. With the code I posted above, if it's a null, it just returns 0 (as specified by the "?? 0").
     
  10. At the minute it seems to be working, I will go with what works xD and then alter it as I understand more...

    This looks like an awful way of doing things... The idea of the counter is so the chat doesn't get spammed and item.ToString() looks for what we are gathering, I feel like there must be a better way?

    But... it works

    Code:
    void OnDispenserGather(ResourceDispenser dispenser, BaseEntity entity, Item item)
            {
                string name = item.ToString();
                         if (!entity.ToPlayer()) return;
                double money = (double)Economics?.CallHook("GetPlayerMoney", entity.ToPlayer().userID);
                Economics?.CallHook("Set", entity.ToPlayer().userID, money +=1);
                counter++;            if (counter == 3)
                {
                    if (name.Contains("wood"))
                        SendReply(entity.ToPlayer(), "You have received {0} coin's' for gathering Wood", 3);
                        counter = 0;
                    if (name.Contains("stones"))
                        SendReply(entity.ToPlayer(), "You have received {0} coin's' for gathering Stone", 3);
                        counter = 0;
                    if (name.Contains("metal.ore"))
                        SendReply(entity.ToPlayer(), "You have received {0} coin's' for gathering Metal Ore", 3);
                        counter = 0;
                    if (name.Contains("hq.metal"))
                        SendReply(entity.ToPlayer(), "You have received {0} coin's' for gathering HQ Metal", 3);
                        counter = 0;
                    if (name.Contains("sulfur"))
                        SendReply(entity.ToPlayer(), "You have received {0} coin's' for gathering Sulfur", 3);
                        counter = 0;
              }
    Also so many if statements are bad so I might turn that to a switch statement?
     
  11. That looks like it would trigger every 3rd time anything is gathered... Like if I gather twice, then you gather once, you get the coins
     
  12. Ooooo not good! Do I need to add BasePlayer player into it?
     
  13. Probably track gathers via hash/dictionary per player, first solution that comes to mind
     
  14. So keep a count of how many hits a player has done in a dictionary and then give money to that specific playerID?
     
  15. Code:
    //Make this global
    private Dictionary<BasePlayer,int> gatherTracking= new Dictionary<BasePlayer,int>();
    Code:
    void OnDispenserGather(ResourceDispenser dispenser, BaseEntity entity, Item item)
            {
                string name = item.ToString();
                        if (!entity.ToPlayer()) return;
                if (!gatherTracking.ContainsKey(entity.ToPlayer()))
                    gatherTracking.Add(entity.ToPlayer(),0);
                int counter = 0;
                double money = (double)Economics?.CallHook("GetPlayerMoney", entity.ToPlayer().userID);
                Economics?.CallHook("Set", entity.ToPlayer().userID, money +=1);
                if (gatherTracking.TryGetValue(entity.ToPlayer(), out counter))
                    gatherTracking[entity.ToPlayer()] += 1;            if (counter >= 3)
                {
                    if (name.Contains("wood"))
                    {
                        SendReply(entity.ToPlayer(), "You have received {0} coin's' for gathering Wood", 3);
                        gatherTracking[entity.ToPlayer()] = 0;
                    }
                    else if (name.Contains("stones"))
                    {
                        SendReply(entity.ToPlayer(), "You have received {0} coin's' for gathering Stone", 3);
                        gatherTracking[entity.ToPlayer()] = 0;
                    }
                    else if (name.Contains("metal.ore"))
                    {
                        SendReply(entity.ToPlayer(), "You have received {0} coin's' for gathering Metal Ore", 3);
                        gatherTracking[entity.ToPlayer()] = 0;
                    }
                    else if (name.Contains("hq.metal"))
                    {
                        SendReply(entity.ToPlayer(), "You have received {0} coin's' for gathering HQ Metal", 3);
                        gatherTracking[entity.ToPlayer()] = 0;
                    }
                    else if (name.Contains("sulfur"))
                    {
                        SendReply(entity.ToPlayer(), "You have received {0} coin's' for gathering Sulfur", 3);
                        gatherTracking[entity.ToPlayer()] = 0;
                    }
              }
     
  16. Would counter have to be changed to gatherTracking too? Or is 'out counter' doing that already?
     
  17. out counter is updating it just for your check
     
  18. Nice one, Thank you for the help it seems to be working fine and I have commented my code for future reference ;)

    Just one last question... If there are multiple players gathering at once could using one "double money =" cause an issue or does the server process it fast enough to not cause any issues?

    The same goes for the counter, Would using one counter cause an issue with multiple users overwriting it, Do you think a dictionary counter would be a better option?
     
  19. No. The server uses single-threaded model.
    Moreover, "double money ..." is a local variable.
     
  20. Ahhh so when someone gathers the server allocates that variable for that user that is calling the hook, right?