forked from thgeorgiou/mc-nbt-edit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmc-nbt-edit.py
executable file
·126 lines (113 loc) · 2.99 KB
/
mc-nbt-edit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python
#mc-nbt-edit 0.4 by sakisds <[email protected]>
#Uses the NBT library by Thomas Woolford <[email protected]>
#Based on the NBT specifications by Markus Persson
from nbt import nbt
from nbt.nbt import *
import sys, os, json
def help(): #Prints help
print "mc-nbt-edit 0.4 by sakisds <[email protected]>\n\nUsage: mc-nbt-edit file tag datatype value\n"
print "Possible datatypes: byte, int, float, long, string, short, double.\n Lists are not yet supported.\n\n\nOptions:"
print "--help: Displays this message and then exit."
print "--print: Prints tree inside the given NBT file and then exit."
exit()
def complain(): #Complains on wrong options.
print "Invalid options. Try --help."
exit(1)
def loadfile(filepath): #Loads the NBT file
try:
return NBTFile(filepath,'rb')
except Exception:
print "Could not open file "+sys.argv[1]
exit(1)
def savefile(nbt):
try:
nbt.write_file()
except Exception:
print "Could not save buffer to disk. Chances are discarded."
exit(1)
def settag(name, dtype, value): #Sets wanted tag
if dtype == "byte":
tag = TAG_Byte(name)
tag.value = int(value)
elif dtype == "int":
tag = TAG_Int(name)
tag.value = int(value)
elif dtype == "float":
tag = TAG_Float(name)
tag.value = float(value)
elif dtype == "long":
tag = TAG_Long(name)
tag.value = long(value)
elif dtype == "string":
tag = TAG_String(name)
tag.value = value
elif dtype == "short":
tag = TAG_Short(name)
tag.value = int(value)
elif dtype == "double":
tag = TAG_Double(sys.argv)
tag.value = float(value)
else:
print "Unknown tag data type. "
exit()
tag.name = name
return tag
def printtag(tag):
print tag.name+":"+tag
#Decide about printing help or complaining
printing = False
if len(sys.argv) == 1 :
help()
elif len(sys.argv) == 2:
if sys.argv[1] == "--help":
help()
else:
complain()
elif len(sys.argv) == 3:
if sys.argv[2] == "--print":
printing = True
else:
complain()
elif len(sys.argv) == 4:
if sys.argv[3] == "--print":
printing = True
elif sys.argv[3] != "delete":
complain()
#Load file
nbt = loadfile(sys.argv[1])
#Print if needed
if printing:
if len(sys.argv) == 3:
print nbt.pretty_tree()
elif len(sys.argv) == 4:
path = sys.argv[2].split('.')
item = nbt
while len(path) > 0:
item = item.__getitem__(path[0])
path.pop(0)
print item.pretty_tree()
exit()
#Parse tag
path = sys.argv[2].split('.')
#Do the editing
if len(path) == 1:
tag = settag(path[0], sys.argv[3], sys.argv[4])
nbt.__setitem__(path[0], tag)
elif len(path) == 2:
compound = nbt.__getitem__(path[0])
if sys.argv[3] == "delete":
compound.__delitem__(path[1])
else:
tag = settag(path[1], sys.argv[3], sys.argv[4])
compound.__setitem__(path[1], tag)
elif len(path) == 3:
compound = nbt.__getitem__(path[0])
subcompound = compound.__getitem__(path[1])
if sys.argv[3] == "delete":
subcompound.__delitem__(path[2])
else:
tag = settag(path[2], sys.argv[3], sys.argv[4])
subcompound.__setitem__(path[2], tag)
#Save changes to disk
savefile(nbt)