public override async Task OnPlayerDeath(PlayerDiedNotification notification, IPluginContext context)
        {
            var serverWorkingDirectory = context.MinecraftServer.WorkingDirectory;

            if (!File.Exists(Path.Combine(serverWorkingDirectory, "usercache.json")))
            {
                await context.Logger.WarningAsync($"Could not find usercache.json in {serverWorkingDirectory}");

                return;
            }
            // We save the game to have latest NBT data to find the death location.
            await context.MinecraftServer.SendCommandWaitForEventAsync("save-all", "saved the game");

            var player = notification.PlayerName.Trim();

            var usercacheJson = await File.ReadAllTextAsync(Path.Combine(serverWorkingDirectory, "usercache.json"));

            var items = JsonSerializer.Deserialize <UserCacheItem[]>(usercacheJson);
            var uuid  = items.FirstOrDefault(x => x.Name == player)?.UUID;

            if (string.IsNullOrWhiteSpace(uuid))
            {
                await context.Logger.WarningAsync($"Could not find {player} in usercache.json");

                return;
            }

            var levelName = context.MinecraftProperties.LevelName;

            if (string.IsNullOrWhiteSpace(levelName))
            {
                return;
            }

            var playerData = Path.Combine(serverWorkingDirectory, levelName, $"playerdata/{uuid}.dat");

            if (!File.Exists(playerData))
            {
                await context.Logger.WarningAsync($"Could not find playerdata with uuid '{uuid}' in {Path.Combine(serverWorkingDirectory, levelName, "playerdata")}");

                return;
            }

            using (var inputStream = File.OpenRead(playerData))
            {
                var nbtData          = NbtConvert.DeserializeObject <MinecraftPlayerData>(inputStream);
                var message          = $"[Death Location] You died at approximately ({nbtData.Coords})";
                var minecraftTellRaw = new MinecraftTellRaw(message, color: MinecraftColor.red);
                await context.MinecraftServer.SendCommandAsync(minecraftTellRaw.ToCommand(notification.PlayerName));

                await context.Logger.InformationAsync($"Sent death location to {player} for coords {nbtData.Coords}");
            }
        }
 public async Task Handle(PlayerDiedNotification notification, CancellationToken cancellationToken)
 {
     foreach (var plugin in _pluginLoader.Enabled)
     {
         try
         {
             await plugin.OnPlayerDeath(notification, _pluginContext);
         }
         catch (Exception ex)
         {
             await _mediator.Publish(new PluginLogNotification
             {
                 Exception = ex,
                 Message   = $"Error in {nameof(plugin.OnPlayerDeath)} for plugin {plugin.Name}"
             });
         }
     }
 }
Пример #3
0
 public async Task PublishPlayerDied(PlayerDiedNotification notification)
 {
     await _playerDiedUpdateChannel.PublishNotification(notification).ConfigureAwait(false);
 }
 public async Task Handle(PlayerDiedNotification notification, CancellationToken cancellationToken)
 {
     await _wonderlandClient.SendMessageAsync($"**{notification.PlayerName}** {notification.DeathMessage}");
 }
 public virtual Task OnPlayerDeath(PlayerDiedNotification notification, IPluginContext context)
 {
     return(Task.CompletedTask);
 }