A shop
A coin shop: players earn coins by playing (every minute online, every kill) and spend them on items in a window. The coins show on the HUD and are kept across restarts.
- Put the prices in a config class, so the server owner can change them in
plugins/config/CoinShop.json. - Keep each player’s coins in a data class, saved in
plugins/data/CoinShop.json. - Pay coins with a timer (
Every) and inOnPlayerDeath, for the killer. - Add a
/shopcommand and a button in the inventory screen that runs it. Each item of the window runs/shop <number>. - On a click, check the coins again (they may have changed since the window opened), take them and give the item.
using RustLegacy;
public class CoinShopOffer{ public string Item { get; set; } = ""; public int Amount { get; set; } = 1; public int Price { get; set; }}
public class CoinShopConfig{ public int CoinsPerMinute { get; set; } = 2; public int CoinsPerKill { get; set; } = 10; public List<CoinShopOffer> Offers { get; set; } = new List<CoinShopOffer> { new CoinShopOffer { Item = "Bandage", Amount = 5, Price = 10 }, new CoinShopOffer { Item = "Small Medkit", Amount = 2, Price = 25 }, new CoinShopOffer { Item = "Hatchet", Amount = 1, Price = 30 }, new CoinShopOffer { Item = "Revolver", Amount = 1, Price = 120 }, new CoinShopOffer { Item = "9mm Ammo", Amount = 30, Price = 50 }, };}
public class CoinShopData{ /// <summary>SteamID → coins.</summary> public Dictionary<string, int> Coins { get; set; } = new Dictionary<string, int>();}
public class CoinShop : RustPlugin{ public override string Description => "Coins earned by playing and by kills, spent in a shop. /shop or the Shop button";
CoinShopConfig _config; CoinShopData _data;
public override void OnLoaded() { _config = LoadConfig<CoinShopConfig>(); _data = LoadData<CoinShopData>(); AddCommand( "shop", OnShop, "Spend your coins", false, "[item number]" ); foreach ( var p in Players ) Welcome( p ); Every( 60f, () => { foreach ( var p in Players ) if ( !p.IsDead ) Add( p, _config.CoinsPerMinute ); SaveData( _data ); } ); }
public override void OnUnloaded() => SaveData( _data );
public override void OnPlayerConnected( RustPlayer player ) => Welcome( player );
void Welcome( RustPlayer player ) { SetButton( player, "shop", "Shop", "/shop" ); Hud( player ); }
public override void OnPlayerDeath( RustPlayer player, RustLegacy.DamageInfo info ) { var killer = info.Attacker != null ? info.Attacker.Components.GetInAncestorsOrSelf<RustPlayer>() : null; if ( killer != null && killer != player ) Add( killer, _config.CoinsPerKill ); }
int Coins( RustPlayer player ) => _data.Coins.ContainsKey( player.SteamId ) ? _data.Coins[player.SteamId] : 0;
void Add( RustPlayer player, int amount ) { _data.Coins[player.SteamId] = Coins( player ) + amount; Hud( player ); }
void Hud( RustPlayer player ) => SetHudText( player, "coins", $"<b>{Coins( player )}</b> coins" );
void OnShop( ChatCommand cmd ) { int pick = 0; if ( cmd.Args.Length > 0 && int.TryParse( cmd.Args[0], out pick ) && pick >= 1 && pick <= _config.Offers.Count ) Buy( cmd.Player, _config.Offers[pick - 1] ); ShowMenu( cmd.Player, Window( cmd.Player ) ); }
void Buy( RustPlayer player, CoinShopOffer offer ) { if ( Coins( player ) < offer.Price ) { player.Notify( $"You need {offer.Price} coins." ); return; } Add( player, -offer.Price ); Give( player, offer.Item, offer.Amount ); player.Notify( $"Bought {offer.Amount} {offer.Item}" ); }
PluginMenu Window( RustPlayer player ) { var menu = new PluginMenu { Title = "Shop", Text = $"You have <b>{Coins( player )}</b> coins.", KeepOpen = true }; for ( var i = 0; i < _config.Offers.Count; i++ ) { var o = _config.Offers[i]; var label = o.Amount > 1 ? $"{o.Amount} × {o.Item}" : o.Item; menu.Buttons.Add( new PluginMenuButton { Label = $"{label} · {o.Price} coins", Item = o.Item, Command = $"/shop {i + 1}" } ); } return menu; }}