// -----------------------------------------------------------------------
//
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
//
// -----------------------------------------------------------------------
namespace MUnique.OpenMU.GameLogic.PlayerActions.ItemConsumeActions;
///
/// Base class of an item consumption handler.
///
public abstract class BaseConsumeHandlerPlugIn : IItemConsumeHandlerPlugIn
{
///
public abstract ItemIdentifier Key { get; }
///
public virtual async ValueTask ConsumeItemAsync(Player player, Item item, Item? targetItem, FruitUsage fruitUsage)
{
if (!this.CheckPreconditions(player, item))
{
return false;
}
await this.ConsumeSourceItemAsync(player, item).ConfigureAwait(false);
return true;
}
///
/// Consumes the source item.
///
/// The player.
/// The item.
protected async ValueTask ConsumeSourceItemAsync(Player player, Item item)
{
if (item.Durability > 0)
{
item.Durability -= 1;
}
}
///
/// Checks the preconditions.
///
/// The player.
/// The item.
/// True, if preconditions are met.
protected virtual bool CheckPreconditions(Player player, Item item)
{
if (player.PlayerState.CurrentState != PlayerState.EnteredWorld
|| item.Durability == 0)
{
return false;
}
return true;
}
}