Skip to content

Commit 6d1b276

Browse files
committed
Add color and icon Url to the config file
1 parent d9af4a3 commit 6d1b276

4 files changed

Lines changed: 42 additions & 21 deletions

File tree

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
using Microsoft.Extensions.Localization;
1+
using EvolutionPlugins.Universal.Extras.Broadcast;
2+
using Microsoft.Extensions.Configuration;
3+
using Microsoft.Extensions.Localization;
24
using OpenMod.API.Eventing;
35
using OpenMod.API.Users;
4-
using OpenMod.UnityEngine.Extensions;
6+
using OpenMod.Unturned.Locations;
57
using OpenMod.Unturned.Players.Life.Events;
68
using OpenMod.Unturned.Users;
7-
using SDG.Unturned;
8-
using System.Linq;
9+
using System.Drawing;
910
using System.Numerics;
1011
using System.Threading.Tasks;
1112

@@ -16,13 +17,20 @@ public class PlayerDeathEvent : IEventListener<UnturnedPlayerDeathEvent>
1617
private readonly IUnturnedUserDirectory m_UnturnedUserDirectory;
1718
private readonly IUserManager m_UserManager;
1819
private readonly IStringLocalizer m_StringLocalizer;
20+
private readonly IConfiguration m_Configuration;
21+
private readonly IBroadcastManager m_BroadcastManager;
22+
private readonly IUnturnedLocationDirectory m_UnturnedLocationDirectory;
1923

2024
public PlayerDeathEvent(IUnturnedUserDirectory unturnedUserDirectory, IUserManager userManager,
21-
IStringLocalizer stringLocalizer)
25+
IStringLocalizer stringLocalizer, IConfiguration configuration, IBroadcastManager broadcastManager,
26+
IUnturnedLocationDirectory unturnedLocationDirectory)
2227
{
2328
m_UnturnedUserDirectory = unturnedUserDirectory;
2429
m_UserManager = userManager;
2530
m_StringLocalizer = stringLocalizer;
31+
m_Configuration = configuration;
32+
m_BroadcastManager = broadcastManager;
33+
m_UnturnedLocationDirectory = unturnedLocationDirectory;
2634
}
2735

2836
public async Task HandleEventAsync(object? sender, UnturnedPlayerDeathEvent @event)
@@ -35,29 +43,22 @@ public async Task HandleEventAsync(object? sender, UnturnedPlayerDeathEvent @eve
3543

3644
var instigatorUser = m_UnturnedUserDirectory.FindUser(@event.Instigator);
3745

38-
var provider = victimUser.Provider ?? m_UserManager.UserProviders.FirstOrDefault(x => x is UnturnedUserProvider);
39-
if (provider == null)
40-
{
41-
return;
42-
}
46+
var victimPosition = victimUser.Player.Transform.Position;
4347

44-
var deathPositionUVector = @event.DeathPosition.ToUnityVector();
45-
var distance = Vector3.Distance(victimUser.Player.Transform.Position,
48+
var distance = Vector3.Distance(victimPosition,
4649
instigatorUser?.Player.Transform.Position ?? victimUser.Player.Transform.Position);
4750

48-
var nearNode = LevelNodes.nodes.OfType<LocationNode>()
49-
.OrderBy(x => (x.point - deathPositionUVector).sqrMagnitude)
50-
.FirstOrDefault();
51+
var location = m_UnturnedLocationDirectory.GetNearestLocation(victimPosition);
5152

52-
await provider.BroadcastAsync(m_StringLocalizer[$"deathCause:{@event.DeathCause.ToString().ToLower()}", new
53+
await m_BroadcastManager.BroadcastAsync(m_StringLocalizer[$"deathCause:{@event.DeathCause.ToString().ToLower()}", new
5354
{
5455
Victim = victimUser,
5556
Instigator = instigatorUser,
5657
@event.DeathPosition,
5758
Distance = distance,
58-
Node = nearNode?.name, // can smartFormat parse fields?
59+
Node = location?.Name ?? string.Empty,
5960
Limb = m_StringLocalizer[$"limbParse:{@event.Limb.ToString().ToLower()}"].Value
60-
}]);
61+
}], m_Configuration["iconUrl"], ColorTranslator.FromHtml(m_Configuration["color"]));
6162
}
6263
}
6364
}

OpenDeathMessages/EvolutionPlugins.OpenDeathMessages.csproj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,21 @@
2121
<RepositoryType>git</RepositoryType>
2222
<PackageProjectUrl>https://github.com/EvolutionPlugins/OpenDeathMessages</PackageProjectUrl>
2323
</PropertyGroup>
24-
24+
2525
<ItemGroup>
26-
<PackageReference Include="OpenMod.Unturned" Version="3.0.22+d9126b5" />
26+
<PackageReference Include="EvolutionPlugins.Universal.Extras.Unturned" Version="1.0.1" />
27+
<PackageReference Include="OpenMod.Unturned" Version="3.1.3" />
2728
<PackageReference Include="Legacy2CPSWorkaround" Version="1.0.0">
2829
<PrivateAssets>All</PrivateAssets>
2930
</PackageReference>
30-
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies.net461" Version="1.0.0">
31+
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies.net461" Version="1.0.2">
3132
<PrivateAssets>all</PrivateAssets>
3233
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3334
</PackageReference>
3435
</ItemGroup>
3536

3637
<ItemGroup>
38+
<EmbeddedResource Include="config.yaml" />
3739
<EmbeddedResource Include="translations.yaml" />
3840
</ItemGroup>
3941

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using EvolutionPlugins.Universal.Extras.Broadcast;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.DependencyInjection.Extensions;
4+
using OpenMod.API.Ioc;
5+
6+
namespace EvolutionPlugins.OpenDeathMessages
7+
{
8+
public class ServiceConfigurator : IServiceConfigurator
9+
{
10+
public void ConfigureServices(IOpenModServiceConfigurationContext openModStartupContext, IServiceCollection serviceCollection)
11+
{
12+
serviceCollection.TryAddSingleton<IBroadcastManager, BroadcastManager>();
13+
}
14+
}
15+
}

OpenDeathMessages/config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
iconUrl: "https://i.imgur.com/Ld1qy8j.png"
2+
# it can support hex color (#FFFFFF)
3+
color: "white"

0 commit comments

Comments
 (0)