Skip to content

Instantly share code, notes, and snippets.

@virtyvoid
Last active November 20, 2016 23:15
Show Gist options
  • Save virtyvoid/10909f6d4fb4bda7b3a3eb0efe6f34c2 to your computer and use it in GitHub Desktop.
Save virtyvoid/10909f6d4fb4bda7b3a3eb0efe6f34c2 to your computer and use it in GitHub Desktop.
using System.Reflection;
using Oxide.Core.Plugins;
using Oxide.Game.Rust;
using ProtoBuf;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("AutoCupboard", "DefaultPlayer", "1.1")]
public class AutoCupboard : RustPlugin
{
private static readonly MethodInfo UpdateAllPlayersMethod = typeof (BuildingPrivlidge).GetMethod("UpdateAllPlayers", BindingFlags.Instance | BindingFlags.NonPublic);
[PluginReference]
private Plugin Friends;
private static void Authorize(BuildingPrivlidge cupboard, ulong userid)
{
var player = RustCore.FindPlayerById(userid);
cupboard.authorizedPlayers.RemoveAll(pni => pni.userid == userid);
cupboard.authorizedPlayers.Add(new PlayerNameID { userid = userid, username = player ? player.displayName : string.Empty });
}
private void OnEntityBuilt(Planner plan, GameObject entity)
{
var cupboard = entity.GetComponent<BuildingPrivlidge>();
if (!cupboard)
return;
try
{
var userid = plan.GetOwnerPlayer().userID;
Authorize(cupboard, userid);
var friends = Friends?.Call<ulong[]>("GetFriends", userid);
if (friends == null)
return;
foreach (var friend in friends)
Authorize(cupboard, friend);
}
finally
{
UpdateAllPlayersMethod.Invoke(cupboard, null);
cupboard.SendNetworkUpdate();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment