protected override async UniTask OnExecuteAsync() { if (Context.Parameters.Length < 1 || Context.Parameters.Length > 2) { throw new CommandWrongUsageException(Context); } string kitName = Context.Parameters[0].ToUpperInvariant(); int kitCooldown = Context.Parameters.Length == 2 ? await Context.Parameters.GetAsync <int>(1) : 0; KitsData kitsData = await m_DataStore.LoadAsync <KitsData>(KitsKey); if (kitsData.Kits.ContainsKey(kitName)) { throw new UserFriendlyException(m_StringLocalizer["kits:spawn:none", new { Kit = kitName }]); } var unturnedUser = Context.Actor as UnturnedUser; // Player clothes are not included in inventory, so we need to collect them manually SerializableItem[] serializableClothes = unturnedUser.Player.Player.clothing.ToSerializableItems(); // IInventoryPage separates items into pages (basically categories) including equipped items // IInventoryItem is a wrapper around items for manipulating inventory // e.g dropping/destroying the item. Also provides access to ItemJar // We are not interacting with either by using SelectMany and Select. This is here for me to keep track var items = unturnedUser.Player.Inventory.SelectMany(page => page.Items.Select(invItem => invItem.Item)); // Since we are checking the length and then later enumerating again this will prevent "multiple enumeration" // https://www.jetbrains.com/help/rider/PossibleMultipleEnumeration.html var serializableItems = new List <SerializableItem>(); // By adding the clothes first, the player will receive them first and equip them serializableItems.AddRange(serializableClothes); serializableItems.AddRange(items.Select(item => new SerializableItem( item.Asset.ItemAssetId, item.State.StateData, item.State.ItemAmount, item.State.ItemDurability, item.State.ItemQuality))); if (serializableItems.Count == 0) { throw new UserFriendlyException(m_StringLocalizer["kits:create:none"]); } kitsData.Kits.Add(kitName, new SerializableKit(serializableItems.ToArray(), kitCooldown)); await m_DataStore.SaveAsync(KitsKey, kitsData); m_PluginAccessor.Instance.RegisterNewKitPermission(kitName); await Context.Actor.PrintMessageAsync(m_StringLocalizer["kits:create:success", new { Kit = kitName }]); }
private async Task LoadFromDisk() { if (await m_Plugin.DataStore.ExistsAsync(c_KitsKey)) { m_Data = await m_Plugin.DataStore.LoadAsync <KitsData>(c_KitsKey) ?? new() { Kits = new() }; } else { m_Data = new() { Kits = new() }; } }
protected override async UniTask OnExecuteAsync() { KitsData kitsData = await m_DataStore.LoadAsync <KitsData>(KitsKey); if (kitsData.Kits.Count == 0) { throw new UserFriendlyException(m_StringLocalizer["kits:none"]); } StringBuilder kitsBuilder = new StringBuilder(); if (Context.Actor.Type == KnownActorTypes.Console) { foreach (var pair in kitsData.Kits) { kitsBuilder.Append($"{pair.Key}, "); } // Remove trailing ", " kitsBuilder.Remove(kitsBuilder.Length - 2, 2); await Context.Actor.PrintMessageAsync(m_StringLocalizer["kits:list", new { List = kitsBuilder.ToString() }]); return; } foreach (var pair in kitsData.Kits) { if (await m_PermissionChecker.CheckPermissionAsync(Context.Actor, $"kits.kit.{pair.Key}") == PermissionGrantResult.Grant || await m_PermissionChecker.CheckPermissionAsync(Context.Actor, $"kits.kit.give.{pair.Key}") == PermissionGrantResult.Grant) { kitsBuilder.Append($"{pair.Key}, "); } } if (kitsBuilder.Length == 0) { throw new UserFriendlyException(m_StringLocalizer["kits:none"]); } // Remove trailing ", " kitsBuilder.Remove(kitsBuilder.Length - 2, 2); await Context.Actor.PrintMessageAsync(m_StringLocalizer["kits:list", new { List = kitsBuilder.ToString() }]); }
protected override async UniTask OnExecuteAsync() { if (Context.Parameters.Length != 1) { throw new CommandWrongUsageException(Context); } string kitName = Context.Parameters[0].ToUpperInvariant(); KitsData kitsData = await m_DataStore.LoadAsync <KitsData>(KitsKey); if (!kitsData.Kits.ContainsKey(kitName)) { throw new UserFriendlyException(m_StringLocalizer["kits:create:exists", new { Kit = kitName }]); } kitsData.Kits.Remove(kitName); await m_DataStore.SaveAsync(KitsKey, kitsData); await Context.Actor.PrintMessageAsync(m_StringLocalizer["kits:removed", new { Kit = kitName }]); }
protected override async UniTask OnExecuteAsync() { if (Context.Parameters.Length < 1 || Context.Parameters.Length > 2) { throw new CommandWrongUsageException(Context); } // If the console is trying to spawn itself a kit, remind to specify a player if (Context.Parameters.Length == 1 && Context.Actor.Type == KnownActorTypes.Console) { throw new CommandWrongUsageException(Context); } string kitName = Context.Parameters[0].ToUpperInvariant(); KitsData kitsData = await m_DataStore.LoadAsync <KitsData>(KitsKey); if (!kitsData.Kits.ContainsKey(kitName)) { throw new UserFriendlyException(m_StringLocalizer["kits:spawn:none", new { Kit = kitName }]); } if (await m_PermissionChecker.CheckPermissionAsync(Context.Actor, $"kits.kit.{kitName}") == PermissionGrantResult.Deny) { throw new UserFriendlyException(m_StringLocalizer["kits:spawn:denied", new { Kit = kitName }]); } if (Context.Parameters.Length == 2 && await m_PermissionChecker.CheckPermissionAsync(Context.Actor, $"kits.kit.give.{kitName}") == PermissionGrantResult.Deny) { throw new UserFriendlyException(m_StringLocalizer["kits:give:deny", new { Kit = kitName }]); } UnturnedUser recipient; if (Context.Parameters.Length == 2) { recipient = await Context.Parameters.GetAsync <UnturnedUser>(1); } else { recipient = Context.Actor as UnturnedUser; } if (recipient == null) { throw new UserFriendlyException(m_StringLocalizer["commands:failed_player", new { Player = Context.Parameters[1] }]); } SerializableKit kit = kitsData.Kits[kitName]; double?cooldown = await m_CooldownManager.OnCooldownAsync(recipient, "kits", kitName, kit.Cooldown); if (cooldown.HasValue) { throw new UserFriendlyException(m_StringLocalizer["kits:spawn:cooldown", new { Kit = kitName, Time = cooldown }]); } foreach (var item in kit.SerializableItems) { await m_ItemSpawner.GiveItemAsync( recipient.Player.Inventory, item.ID, new SerializedItemState(item.Quality, item.Durability, item.Amount, item.State)); } await Context.Actor.PrintMessageAsync(m_StringLocalizer["kits:spawn:success", new { Kit = kitName, Player = recipient.DisplayName }]); }