1+ using SteveSharp . Exceptions ;
2+
13namespace SteveSharp . NBT ;
24
35public 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