if it possible to get baseplayer who use table on c#
From this method
OnItemResearchEnd
Code:void OnItemResearchEnd(ResearchTable table, float chance) { Puts("OnItemResearchEnd works!"); return chance; }
Getting BasePlayer from OnItemResearchEnd?
Discussion in 'Rust Development' started by PoJa, Oct 14, 2015.
-
Were you able to solve it since your post is marked as "Solved"?
If yes, could you tell me how? -
Wulf Community Admin
As far as I know, table.user.
-
Private field?
-
Wulf Community Admin
"table" is what you pass in via OnItemResearchEnd. If you look in a decompiler, you can see that table.user would be the BasePlayer. -
I mean that user is private field. We can't get this from outside.
-
Wulf Community Admin
Try "table.inventory.playerOwner" and see if that works. -
Returns nothing from .playerOwner. So, I think getting BasePlayer in this hook is not possible for now.
-
Wulf Community Admin
I'm sure there is a way. -
You can access private fields via reflection. I'm at work right now so I don't have a working example, but check LimeBench, I use reflection in it to access buildingprivs for the player. That field might be null all the time though, not the first time I would see a named variable in this code that has no use.
Another way, assuming the research table functions as a Container, is to loop through active players and find which one is looting the table in question. I don't remember the name of the property or function but you can obtain the currently open container for a player. If the container == the tables container then you know you caught your fish. I use this in MoegicBox and possibly Salvager, check these out. -
Quick example:
Code:void OnItemResearchEnd(ResearchTable table, float chance) { var userField = typeof(ResearchTable).GetField("user", (BindingFlags.Instance | BindingFlags.NonPublic)); var player = userField.GetValue(table) as BasePlayer; } -
i find table position and close player position
-
not reliable.. you could have 3 guys standing next to it, which are you going to chose?
[DOUBLEPOST=1444885242][/DOUBLEPOST]Code:private static BasePlayer GetPlayerFromContainer(ItemContainer container, Item item) => item.GetOwnerPlayer() ?? BasePlayer.activePlayerList.FirstOrDefault(p => p.inventory.loot.IsLooting() && p.inventory.loot.entitySource == container.entityOwner); -
As Deicide666ra explained before, and Chilli posted a snippet, the reliable way to do this is by getting the value of the private user field through reflection. Here is another example on getting the player and some more info:
The above example would output something like:Code:using System.Reflection;namespace Oxide.Plugins { [Info("Research Table Example", "Mughisi", 1.0)] class ResearchTableExample : RustPlugin { private FieldInfo user = typeof(ResearchTable).GetField("user", BindingFlags.NonPublic | BindingFlags.Instance); void OnItemResearchEnd(ResearchTable table, float chance) { var player = user.GetValue(table) as BasePlayer; var status = chance < table.GetSuccessChance(table.GetResearchItem(), table.inventory.GetSlot(1)); Puts($"{player.displayName} just finished researching {table.GetResearchItem().info.displayName.english} and {(status ? "succeeded" : "failed")}."); } } }
"Mughisi just finished researching Silencer and failed."
"Mughisi just finished researching Snow Jacked - Wood Camo and succeeded."
