Skip to content

Commit 7375ea9

Browse files
committed
Implement NBT Parsing
1 parent e767da3 commit 7375ea9

3 files changed

Lines changed: 95 additions & 17 deletions

File tree

Exceptions/NBTParsingException.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using SteveSharp.NBT;
2+
3+
namespace SteveSharp.Exceptions;
4+
public class NBTParsingException(NBTTag nbt) : Exception {
5+
public NBTTag NBT { get => nbt; }
6+
public override string Message => $"Could not parse NBT Tag \"{NBT.Name}\". Please check the value type and tag type:\n\tValue Type: {NBT.Value.GetType()}\n\tTag Type: {NBT.Type}\nPlease check if it isn't supported by the NBT Tag parser.";
7+
}

NBT/NBT.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ namespace SteveSharp.NBT;
22

33
public class NBTTag
44
{
5-
public NBTType Type { get; set; } = NBTType.SingleValue;
5+
public NBTType Type { get; }
66
public string Name { get; set; }
77
public object Value { get; set; }
88

9-
public NBTTag(string name, object value)
9+
public NBTTag(string name, object value, NBTType type = NBTType.SingleValue)
1010
{
11+
Type = type;
1112
Name = name;
1213
Value = value;
1314
}

NBT/NBTParser.cs

Lines changed: 85 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,35 @@
1+
using SteveSharp.Exceptions;
2+
13
namespace SteveSharp.NBT;
24

35
public class NBTParser
46
{
7+
public string Encompass(string nbtText) => '{' + nbtText + '}';
8+
public string Encompass(List<string> nbtTexts)
9+
{
10+
string s = "{";
11+
for (int i = 0; i < nbtTexts.Count; i++)
12+
{
13+
s += nbtTexts[i];
14+
if (i < nbtTexts.Count - 1) // if it's not the last item in the list
15+
s += ", ";
16+
}
17+
s += "}";
18+
return s;
19+
}
20+
public string Enlist(string nbts) => '[' + nbts + ']';
21+
public string Enlist(List<string> nbts)
22+
{
23+
string s = "[";
24+
for (int i = 0; i < nbts.Count; i++)
25+
{
26+
s += nbts[i];
27+
if (i < nbts.Count - 1) // if it's not the last item in the list
28+
s += ", ";
29+
}
30+
s += "]";
31+
return s;
32+
}
533
public string ParseValueType(object value)
634
=> value switch
735
{
@@ -13,35 +41,77 @@ public string ParseValueType(object value)
1341
long => value.ToString() + "l",
1442
_ => value.ToString()!
1543
};
44+
1645
public string Parse(NBTTag nbt)
1746
{
18-
string nbtString = "";
47+
string nbtString = nbt.Name + ": ";
1948
switch (nbt.Type)
2049
{
2150
case NBTType.SingleValue:
22-
return nbt.Name + ": " + ParseValueType(nbt.Value);
51+
nbtString += ParseValueType(nbt.Value);
52+
return nbtString;
2353
case NBTType.List:
24-
nbtString = nbt.Name + ": [";
25-
List<object>? list = nbt.Value as List<object>;
54+
dynamic? list;
55+
nbtString += "[";
56+
if (nbt.Value is List<int>)
57+
{
58+
list = nbt.Value as List<int>;
59+
}
60+
else if (nbt.Value is List<double>)
61+
{
62+
list = nbt.Value as List<double>;
63+
}
64+
else if (nbt.Value is List<float>)
65+
{
66+
list = nbt.Value as List<float>;
67+
}
68+
else if (nbt.Value is List<byte>)
69+
{
70+
list = nbt.Value as List<byte>;
71+
}
72+
else if (nbt.Value is List<short>)
73+
{
74+
list = nbt.Value as List<short>;
75+
}
76+
else if (nbt.Value is List<long>)
77+
{
78+
list = nbt.Value as List<long>;
79+
}
80+
else if (nbt.Value is List<char>)
81+
{
82+
list = nbt.Value as List<char>;
83+
}
84+
else if (nbt.Value is List<string>)
85+
{
86+
list = nbt.Value as List<string>;
87+
}
88+
else
89+
{
90+
list = nbt.Value as List<object>;
91+
}
92+
2693
for (int i = 0; i < list!.Count; i++)
2794
{
2895
nbtString += ParseValueType(list[i]);
29-
if (i < list.Count - 1)
96+
if (i < list.Count - 1) // if it's not the last item in the list
3097
nbtString += ", ";
3198
}
32-
return s;
99+
nbtString += "]";
100+
return nbtString;
33101
case NBTType.Compound:
34-
nbtString = nbt.Name + "{";
35-
Dictionary<string, object>? compound = nbt.Value as Dictionary<string, object>;
36-
foreach (var item in compound!)
102+
nbtString += "{";
103+
List<NBTTag>? tags = nbt.Value as List<NBTTag>;
104+
int left = tags!.Count;
105+
for (int i = 0; i < tags.Count; i++)
37106
{
38-
nbtString += nbt.Name + ": ";
39-
nbtString += nbt.Value switch
40-
{
41-
_ => nbtString.ToString()
42-
};
107+
nbtString += Parse(tags[i]);
108+
if (i < tags.Count - 1) // if it's not the last item in the list
109+
nbtString += ", ";
43110
}
111+
nbtString += "}";
112+
return nbtString;
113+
default:
114+
throw new NBTParsingException(nbt);
44115
}
45-
return null!;
46116
}
47117
}

0 commit comments

Comments
 (0)