示例#1
0
        public void PrimitiveTest()
        {
            NbtTag intTag = NbtConvert.MakeTag("derp", 1);

            Assert.IsInstanceOf <NbtInt>(intTag);
            Assert.Equals(intTag.IntValue, 1);
        }
 private static NbtCompoundTag ReadFileAsNBT(string filePath)
 {
     using (var inputStream = File.OpenRead(filePath))
     {
         return(NbtConvert.ParseNbtStream(inputStream));
     }
 }
        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}");
            }
        }