Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#(Craft.Net.Client)服务器列表异常_C#_Minecraft - Fatal编程技术网

C#(Craft.Net.Client)服务器列表异常

C#(Craft.Net.Client)服务器列表异常,c#,minecraft,C#,Minecraft,我正在使用Craft.Net.Client库,但在尝试使用ServerList.SaveTo(字符串文件)方法时出错:无法读取流结尾以外的内容(EndOfStreamException) ServerList.cs: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using fNbt; namespace Craft.Net.Clien

我正在使用Craft.Net.Client库,但在尝试使用ServerList.SaveTo(字符串文件)方法时出错:无法读取流结尾以外的内容(EndOfStreamException)

ServerList.cs:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using fNbt;

namespace Craft.Net.Client
{
/// <summary>
/// Provides functionality for interacting with
/// the saved vanilla server list.
/// </summary>
public class ServerList
{
    public static string ServersDat
    {
        get
        {
            return Path.Combine(DotMinecraft.GetDotMinecraftPath(), "servers.dat");
        }
    }

    public ServerList()
    {
        Servers = new List<Server>();
    }

    public List<Server> Servers { get; set; }

    public void Save()
    {
        SaveTo(ServersDat);
    }

    public void SaveTo(string file)
    {
        var nbt = new NbtFile(file); // ERROR : Unable to read beyond the end of the  stream (EndOfStreamException)
        nbt.RootTag = new NbtCompound("");
        var list = new NbtList("servers", NbtTagType.Compound);
        foreach (var server in Servers)
        {
            var compound = new NbtCompound();
            compound.Add(new NbtString("name", server.Name));
            compound.Add(new NbtString("ip", server.Ip));
            compound.Add(new NbtByte("hideAddress", (byte)(server.HideAddress ? 1 : 0)));
            compound.Add(new NbtByte("acceptTextures", (byte)(server.AcceptTextures ? 1 : 0)));
            list.Add(compound);
        }
        nbt.RootTag.Add(list);
        nbt.SaveToFile(file, NbtCompression.None);
    }

    public static ServerList Load()
    {
        return LoadFrom(ServersDat);
    }

    public static ServerList LoadFrom(string file)
    {
        var list = new ServerList();
        var nbt = new NbtFile(file);
        foreach (NbtCompound server in nbt.RootTag["servers"] as NbtList)
        {
            var entry = new Server();
            if (server.Contains("name"))
                entry.Name = server["name"].StringValue;
            if (server.Contains("ip"))
                entry.Ip = server["ip"].StringValue;
            if (server.Contains("hideAddress"))
                entry.HideAddress = server["hideAddress"].ByteValue == 1;
            if (server.Contains("acceptTextures"))
                entry.AcceptTextures = server["acceptTextures"].ByteValue == 1;
            list.Servers.Add(entry);
        }
        return list;
    }

    public class Server
    {
        public string Name { get; set; }
        public string Ip { get; set; }
        public bool HideAddress { get; set; }
        public bool AcceptTextures { get; set; }

        public override string ToString()
        {
            return Name;
        }
    }
}
我注意到的一件事是,我只在servers.dat为空时收到错误,但如果它已经保存了服务器,则不会收到错误

有人能帮我吗

提前感谢,

编辑:由于steveg89,下面的解决方案解决了问题(更新SaveTo方法):


我注意到他们的NbtFile构造函数试图从文件中读取。它假设文件的格式已经正确。这意味着您必须创建并保存它。他们的API应该为您处理这个问题,但是没有,所以试试这个

if (!File.Exists(RuntimeInfo.getMinecraftDir() + @"\servers.dat"))
{
   NbtFile nbt = new NbtFile();
   nbt.SaveToFile(RuntimeInfo.getMinecraftDir() + @"\servers.dat", Nbt.Compression.None);
}
我认为更聪明的方法是更正ServerList的
SaveTo
方法。这种方法意味着您不必在代码中检查它,但它确实意味着您将使用自己风格的Craft.Net。我想这样做:

public void SaveTo(string file)
        {
            NbtFile nbt;
            if( File.Exists( file ) )
            {
              nbt = new NbtFile(file);
            }
            else
            {
              nbt = new NbtFile();
            }
            nbt.RootTag = new NbtCompound("");
            var list = new NbtList("servers", NbtTagType.Compound);
            foreach (var server in Servers)
            {
                var compound = new NbtCompound();
                compound.Add(new NbtString("name", server.Name));
                compound.Add(new NbtString("ip", server.Ip));
                compound.Add(new NbtByte("hideAddress", (byte)(server.HideAddress ? 1 : 0)));
                compound.Add(new NbtByte("acceptTextures", (byte)(server.AcceptTextures ? 1 : 0)));
                list.Add(compound);
            }
            nbt.RootTag.Add(list);
            nbt.SaveToFile(file, NbtCompression.None);
        }

我用一套完全不同的方法对解决方案进行了大量更新。让我知道其中一个是否有效。我将这两个代码组合在一起:在创建新的NbtFile时,我在第二个代码中设置了第一个代码中的条件说明,这是有效的!最后非常感谢D
if (!File.Exists(RuntimeInfo.getMinecraftDir() + @"\servers.dat"))
{
   NbtFile nbt = new NbtFile();
   nbt.SaveToFile(RuntimeInfo.getMinecraftDir() + @"\servers.dat", Nbt.Compression.None);
}
public void SaveTo(string file)
        {
            NbtFile nbt;
            if( File.Exists( file ) )
            {
              nbt = new NbtFile(file);
            }
            else
            {
              nbt = new NbtFile();
            }
            nbt.RootTag = new NbtCompound("");
            var list = new NbtList("servers", NbtTagType.Compound);
            foreach (var server in Servers)
            {
                var compound = new NbtCompound();
                compound.Add(new NbtString("name", server.Name));
                compound.Add(new NbtString("ip", server.Ip));
                compound.Add(new NbtByte("hideAddress", (byte)(server.HideAddress ? 1 : 0)));
                compound.Add(new NbtByte("acceptTextures", (byte)(server.AcceptTextures ? 1 : 0)));
                list.Add(compound);
            }
            nbt.RootTag.Add(list);
            nbt.SaveToFile(file, NbtCompression.None);
        }