OnCupboardProtectionCalculated hook change to modify protect minutesSuggestion
can we move void OnCupboardProtectionCalculated(BuildingPrivlidge privilege, float protectedMinutes) to modify the protectMinutes to fool the ui.
I changed this once using Reflection to make it always say 69 hours.  Would be nice to have a simpler way.
Zugzwang
I changed this once using Reflection to make it always say 69 hours.  Would be nice to have a simpler way.

Would you mind giving some example code how this could be accomplish using reflection ?
Thanks.

 

I think the simplest solution here is to just expose the cachedProtectedMinutes field as public so you can change it after it's calculated. I've made a PR. Can probably be merged by next Rust update.

https://github.com/OxideMod/Oxide.Rust/pull/265

Testing this out, the cachedProtectedMinutes is the value that shows in the status bar above the player's health/metabolism, not the amount that shows on the cupboard UI (which seems to be calculated client-side).
kasvoton

Would you mind giving some example code how this could be accomplish using reflection ?
Thanks.

Something like this.  But you're better off waiting for the variable to be exposed as public.

cpm = typeof(BuildingPrivlidge).GetField("cachedProtectedMinutes", BindingFlags.NonPublic | BindingFlags.Instance);
npct = typeof(BuildingPrivlidge).GetField("nextProtectedCalcTime", BindingFlags.NonPublic | BindingFlags.Instance);
cpm.SetValue(toolcupboard, 4140f);
npct.SetValue(toolcupboard, UnityEngine.Time.time + 3024000f);
toolcupboard.SendNetworkUpdate(BasePlayer.NetworkQueue.Update);

Yes @whitethunder thats whati need to modify.  did you change the hook cause the ui is most likley calling CupboardProtectionCalculatso seting the value after the call will not work.
5f1792699e67b.jpg WhiteThunder
I think the simplest solution here is to just expose the cachedProtectedMinutes field as public so you can change it after it's calculated. I've made a PR. Can probably be merged by next Rust update.

https://github.com/OxideMod/Oxide.Rust/pull/265

Testing this out, the cachedProtectedMinutes is the value that shows in the status bar above the player's health/metabolism, not the amount that shows on the cupboard UI (which seems to be calculated client-side).

did you change the hook cause the ui is most likley calling CupboardProtectionCalculat so seting the value after the call will not work. as the ui will have already grabed the value.

No, the hook doesn't need to be changed because, while it is called after the calculation, it is before the method doing the calculation returns the value of that field. That code analysis is how I determined that simply exposing the field should be sufficient.

While testing, I was surprised that the cupboard UI was not updated, but the upkeep UI indicator on the HUD was, so this is evidence that that field only affects the HUD. I also didn't see any other networked fields that appear to correspond to the cupboard UI amount, so the client is likely calculating them based on the cupboard inventory, plus the other networked values that correspond to cvars. Let me know if you have any proof to the contrary and I can take a other look.
ok was just wondering because seting the value when the hooked it called in the hook would be just about as easy to do with a return but both work i guess.

Merged post

just gotto add delay to change it after the hook it called i guess.
No delay needed. I dont have the code in front of me, but it was basically this:
tc.cachedProtectionMinutues = calculatedAmount;
Interface.Oxide.CallHook("OnCupboardProtectionCalculated", tc, tc.cachedProtectionMinutes);
return tc.cachedProtectionMinutes;
ahh i guess i messed where the placement was.. Thanks.
cachedProtectedMinutes is now public so the above recommended approach should work.

You can also use the OnEntitySaved(BuildingPrivlidge, BaseNetworkable.SaveInfo) hook to alter what the user sees independent of the actual value, like below.

private void OnEntitySaved(BuildingPrivlidge buildingPrivilege, BaseNetworkable.SaveInfo saveInfo)
{
    // Always display 69 hours upkeep time on the HUD, regardless of actual upkeep time
    saveInfo.msg.buildingPrivilege.protectedMinutes = 4140;
}​


Changing the displayed upkeep in the tool cupboard UI is possible but trickier since the client performs the calculation based on the items it believes are in the tool cupboard inventory. The way to alter this would be to network to the client that there are additional items in the inventory but outside of the visible slots (so the player doesn't see them). If you want some visible items to not be factored into the calculation, that simply isn't possible without visually hiding them, but you can alter saveInfo.msg.buildingPrivilege.protectedMinutes in the above hook based on the items in the container to cause the client to multiply or divide the displayed protection minutes, but this would require copying the game code for upkeep calculation to predict what the client will calculate.