Hello,
I am trying to Reference the Economics plugin.
The current API is
So when users gather resources I want them to receive economics money like so...Code:void Set(ulong playerId, double money)
But I am unsure how to reference another plugin and put it's methods into scope.Code:void OnDispenserGather(ResourceDispenser dispenser, BaseEntity entity, Item item) { Set(playerId, 1) }
Referencing another plugin?
Discussion in 'Rust Development' started by Nathan 3, Dec 12, 2016.
-
Code:
[PluginReference] private Plugin Economics;void OnDispenserGather(ResourceDispenser dispenser, BaseEntity entity, Item item) { if (!entity.ToPlayer()) return; Economics?.CallHook("Set", entity.ToPlayer().userID, 1); }
Last edited by a moderator: Dec 12, 2016 -
[DOUBLEPOST=1481567479][/DOUBLEPOST]
Code:private void Set(ulong playerId, double money) { economicsData[playerId] = money >= 0 ? money : 0; changed = true; }
-
I updated it lol, just realized it wouldn't work the way you had it
-
Is BaseEntity entity handling who is actually hitting the dispenser? -
That should give you the player that mined the resource, I've never really used that hook for anything
-
Code:double GetPlayerMoney(ulong playerId)
-
I'd probably use something like
Code:double money = (double)Economics?.CallHook("GetPlayerMoney", entity.ToPlayer().userID);
-
Code:var money = Economics?.CallHook<double>("GetPlayerMoney", entity.ToPlayer().userID) ?? 0;
-
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; }
-
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
-
-
Probably track gathers via hash/dictionary per player, first solution that comes to mind
-
-
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; } }
-
-
out counter is updating it just for your check
-
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? -
Moreover, "double money ..." is a local variable. -