|
| 1 | +using Feli.OpenMod.JoinLeaveMessages.Helpers; |
| 2 | +using Microsoft.Extensions.Configuration; |
| 3 | +using Microsoft.Extensions.Localization; |
| 4 | +using OpenMod.API.Eventing; |
| 5 | +using OpenMod.Unturned.Users.Events; |
| 6 | +using SDG.Unturned; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using UnityEngine; |
| 9 | + |
| 10 | +namespace Feli.OpenMod.JoinLeaveMessages.Events |
| 11 | +{ |
| 12 | + public class EventListener : IEventListener<UnturnedUserConnectedEvent>, IEventListener<UnturnedUserDisconnectedEvent> |
| 13 | + { |
| 14 | + private readonly IConfiguration _configuration; |
| 15 | + private readonly IStringLocalizer _stringLocalizer; |
| 16 | + private readonly IpGeolocationHelper _geolocator; |
| 17 | + |
| 18 | + public EventListener( |
| 19 | + IConfiguration configuration, |
| 20 | + IStringLocalizer stringLocalizer, |
| 21 | + IpGeolocationHelper geolocator) |
| 22 | + { |
| 23 | + _configuration = configuration; |
| 24 | + _stringLocalizer = stringLocalizer; |
| 25 | + _geolocator = geolocator; |
| 26 | + } |
| 27 | + |
| 28 | + public async Task HandleEventAsync(object sender, UnturnedUserConnectedEvent @event) |
| 29 | + { |
| 30 | + if (!_configuration.GetSection("joinMessage:enabled").Get<bool>()) |
| 31 | + return; |
| 32 | + |
| 33 | + var country = string.Empty; |
| 34 | + |
| 35 | + if (_configuration.GetSection("joinMessage:showCountry").Get<bool>()) |
| 36 | + country = await _geolocator.GetCountryFromIpAsync(@event.User.Player.Address.MapToIPv4().ToString()); |
| 37 | + |
| 38 | + var message = _stringLocalizer["join", new |
| 39 | + { |
| 40 | + User = @event.User, |
| 41 | + Country = country |
| 42 | + }]; |
| 43 | + |
| 44 | + ChatManager.serverSendMessage(message, Color.green, mode: EChatMode.GLOBAL, iconURL: _configuration["joinMessage:customIconUrl"], useRichTextFormatting: true); |
| 45 | + } |
| 46 | + |
| 47 | + public Task HandleEventAsync(object sender, UnturnedUserDisconnectedEvent @event) |
| 48 | + { |
| 49 | + if (!_configuration.GetSection("leaveMessage:enabled").Get<bool>()) |
| 50 | + return Task.CompletedTask; |
| 51 | + |
| 52 | + var message = _stringLocalizer["leave", new |
| 53 | + { |
| 54 | + User = @event.User |
| 55 | + }]; |
| 56 | + |
| 57 | + ChatManager.serverSendMessage(message, Color.green, mode: EChatMode.GLOBAL, iconURL: _configuration["leaveMessage:customIconUrl"], useRichTextFormatting: true); |
| 58 | + |
| 59 | + return Task.CompletedTask; |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments