Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Player.ResetInventory(List<string>) extension #478

Merged
merged 6 commits into from
Apr 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Exiled.CustomItems/API/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,35 @@ public static class Extensions
SpawnLocation.InsideHid,
};

/// <summary>
/// Resets the player's inventory to the provided list of items and/or customitems names, clearing any items it already possess.
/// </summary>
/// <param name="player">The player to which items will be given.</param>
/// <param name="newItems">The new items that have to be added to the inventory.</param>
/// <param name="displayMessage">Indicates a value whether <see cref="CustomItem.ShowPickedUpMessage"/> will be called when the player receives the <see cref="CustomItem"/> or not.</param>
public static void ResetInventory(this Player player, List<string> newItems, bool displayMessage = false)
{
foreach (Inventory.SyncItemInfo item in player.Inventory.items)
{
if (CustomItem.TryGet(item, out CustomItem customItem))
customItem.InsideInventories.Remove(item.uniq);
}

player.ClearInventory();
iopietro marked this conversation as resolved.
Show resolved Hide resolved

foreach (string item in newItems)
{
if (Enum.TryParse(item, true, out ItemType parsedItem))
{
player.AddItem(parsedItem);
}
else if (!CustomItem.TryGive(player, item, displayMessage))
{
Log.Debug($"\"{item}\" is not a valid item name, nor a custom item name.", CustomItems.Instance.Config.Debug);
}
}
}

/// <summary>
/// Tries to get the <see cref="Transform"/> of the door used for a specific <see cref="SpawnLocation"/>.
/// </summary>
Expand Down