//
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
//
namespace MUnique.OpenMU.GameLogic.PlugIns;
using System.ComponentModel.DataAnnotations;
using System.Runtime.InteropServices;
using MUnique.OpenMU.GameLogic.NPC;
using MUnique.OpenMU.PlugIns;
///
/// This plugin increases the monster kill count of the quest state of active quests.
///
[PlugIn]
[Display(Name = nameof(PlugInResources.QuestMonsterKillCountPlugIn_Name), Description = nameof(PlugInResources.QuestMonsterKillCountPlugIn_Description), ResourceType = typeof(PlugInResources))]
[Guid("416C2231-D7FE-414A-9321-26622E262EF5")]
public class QuestMonsterKillCountPlugIn : IAttackableGotKilledPlugIn, ISupportCustomConfiguration, ISupportDefaultCustomConfiguration
{
///
public QuestMonsterKillCountPlugInConfiguration? Configuration { get; set; }
///
/// Is called when an object got killed by another.
///
/// The killed .
/// The killer.
public async ValueTask AttackableGotKilledAsync(IAttackable killed, IAttacker? killer)
{
var configuration = this.Configuration ??= CreateDefaultConfiguration();
if (!(killer is Player player && killed is Monster monster)
|| player.SelectedCharacter?.QuestStates is null)
{
return;
}
foreach (var questState in player.SelectedCharacter.QuestStates)
{
if (questState.ActiveQuest is null)
{
continue;
}
foreach (var killRequirement in questState.ActiveQuest.RequiredMonsterKills.Where(r => object.Equals(r.Monster, monster.Definition)))
{
if (questState.RequirementStates.FirstOrDefault(s => object.Equals(s.Requirement, killRequirement))
is not { } requirementState)
{
requirementState = player.PersistenceContext.CreateNew();
requirementState.Requirement = killRequirement;
questState.RequirementStates.Add(requirementState);
}
requirementState!.KillCount++;
if (killRequirement.MinimumNumber >= requirementState!.KillCount
&& configuration.Message.GetTranslation(player.Culture) is { Length: > 0 } translation)
{
var message = string.Format(
translation,
questState.ActiveQuest.Name.GetTranslation(player.Culture),
monster.Definition.Designation.GetTranslation(player.Culture),
requirementState.KillCount,
killRequirement.MinimumNumber);
await player.ShowBlueMessageAsync(message).ConfigureAwait(false);
}
}
}
}
///
public object CreateDefaultConfig()
{
return CreateDefaultConfiguration();
}
private static QuestMonsterKillCountPlugInConfiguration CreateDefaultConfiguration()
{
return new QuestMonsterKillCountPlugInConfiguration();
}
}