// Give: an item menu like the admin menu's. /give opens it (also a "Give" button in the inventory screen and Server plugin // key 2, J by default): items sorted by category, a search box, one click gives a stack. /give [amount] gives directly. // Admins only by default: set AdminOnly to false in plugins/config/Give.json for a creative server (see also Creative.cs). // Install: copy this file into the server's data folder, plugins/Give.cs. // Script engine rules: test objects with != null, no static fields that change, callbacks with one parameter at most. using RustLegacy; public class GiveConfig { /// Only admins (and the host of a lobby) may give themselves items. public bool AdminOnly { get; set; } = true; /// Largest amount one /give can ask for. public int MaxAmount { get; set; } = 1000; } public class Give : RustPlugin { GiveConfig _config; PluginMenu _menu; public override void OnLoaded() { _config = LoadConfig(); AddCommand( "give", OnGive, "Item menu, or give an item directly", _config.AdminOnly, "[item] [amount]" ); _menu = BuildMenu(); foreach ( var p in Players ) ShowButton( p ); } public override void OnPlayerConnected( RustPlayer player ) => ShowButton( player ); void ShowButton( RustPlayer p ) { if ( !_config.AdminOnly || IsAdmin( p ) ) SetButton( p, "menu", "Give", "/give", 2 ); } /// One button per item, grouped by category: a click runs "/give Item amount" for a full stack. PluginMenu BuildMenu() { var menu = new PluginMenu { Title = "Give items", Text = "Click an item to get a stack. /give in the chat for another amount.", Search = true, KeepOpen = true }; var items = new List(); foreach ( var item in ItemDatabase.Items ) if ( item != null && !string.IsNullOrEmpty( item.Title ) && !item.IsTransient ) items.Add( item ); // plain loops: sorting with LINQ lambdas is fragile in the script engine var categories = new List(); foreach ( var item in items ) if ( !categories.Contains( item.CategoryName ) ) categories.Add( item.CategoryName ); categories.Sort(); foreach ( var category in categories ) { var titles = new List(); foreach ( var item in items ) if ( item.CategoryName == category ) titles.Add( item.Title ); titles.Sort(); foreach ( var title in titles ) { var stack = ItemDatabase.Find( title ).MaxStack; menu.Buttons.Add( new PluginMenuButton { Label = stack > 1 ? $"{title} x{stack}" : title, Item = title, Group = category, Command = $"/give {title} {stack}" } ); } } return menu; } void OnGive( ChatCommand cmd ) { var p = cmd.Player; if ( cmd.Args.Length == 0 ) { ShowMenu( p, _menu ); return; } if ( p.IsDead ) { Reply( p, "Respawn first." ); return; } // the last word is the amount when it is a number: item names have spaces ("Large Wood Storage") var amount = 1; var words = new List( cmd.Args ); string last = words[words.Count - 1]; int n = 0; if ( words.Count > 1 && int.TryParse( last, out n ) ) { amount = n; words.RemoveAt( words.Count - 1 ); } var name = string.Join( " ", words ); var item = ItemDatabase.Find( name ); if ( item == null ) { Reply( p, $"No item called {name}. /give opens the list." ); return; } amount = Math.Clamp( amount, 1, _config.MaxAmount ); Give( p, item.Title, amount ); p.NotifyInventoryGain( $"{amount} x {item.Title}" ); Puts( $"{p.DisplayName} ({p.SteamId}) gave themselves {amount} x {item.Title}" ); } }