forked from orphu/mcdungeon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
items.py
341 lines (311 loc) · 9.51 KB
/
items.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import inspect
import sys
import os
import materials
from pymclevel import nbt
_items = {}
_by_id = {}
class ItemInfo (object):
def __init__(self, name, value, data=0, maxstack=64, ench='', p_effect='',
customname='', flag='', flagparam='', lore='', file=''):
self.name = str(name)
self.value = int(value)
self.data = int(data)
self.maxstack = int(maxstack)
self.ench = ench
self.p_effect = p_effect
self.customname = str(customname)
self.flag = str(flag)
self.flagparam = str(flagparam)
self.lore = str(lore)
self.file = str(file)
# Intentionally not printing lore
def __str__(self):
return 'Item: %s, ID: %d, Data: %d, MaxStack: %d,'\
' Ench: %s, PEff: %s, Name: %s, Flag: %s, '\
' FP: %s, File: %s' % (
self.name,
self.value,
self.data,
self.maxstack,
self.ench,
self.p_effect,
self.customname,
self.flag,
self.flagparam,
self.file
)
def LoadItems(filename='items.txt'):
# Try to load items from sys.path[0] if we can,
# otherwise default to the cd.
temp = os.path.join(sys.path[0], filename)
try:
fh = open(temp)
fh.close
filename = temp
except:
pass
items = 0
try:
fh = open(filename)
except IOError as e:
sys.exit(e)
fh.close()
try:
with file(filename) as f:
items_txt = f.read()
except Exception as e:
print "Error reading items file: ", e
for line in items_txt.split("\n"):
try:
line = line.strip()
if len(line) == 0:
continue
if line[0] == "#":
continue
if line.count(',') == 4:
value, name, data, maxstack, flag = line.split(',')
else:
value, name, data, maxstack = line.split(',')
flag = ''
name = name.lower()
_items[name] = ItemInfo(
name,
value,
data,
maxstack,
flag=flag
)
_by_id[int(value)] = ItemInfo(
name,
value,
data,
maxstack,
flag=flag
)
items += 1
except Exception as e:
print "Error reading line:", e
print "Line: ", line
# Now import items from materials
for material, obj in inspect.getmembers(materials):
if (
isinstance(obj, materials.Material) and
obj.name not in _items
):
_items[obj.name] = ItemInfo(
obj.name,
obj.val,
obj.data,
obj.stack,
''
)
_by_id[obj.val] = ItemInfo(
obj.name,
obj.val,
obj.data,
obj.stack,
''
)
items += 1
print 'Loaded', items, 'items.'
def LoadMagicItems(filename='magic_items.txt'):
# Try to load items from sys.path[0] if we can,
# otherwise default to the cd.
temp = os.path.join(sys.path[0], filename)
try:
fh = open(temp)
fh.close
filename = temp
except:
pass
items = 0
try:
fh = open(filename)
except IOError as e:
sys.exit(e)
fh.close()
try:
with file(filename) as f:
items_txt = f.read()
except Exception as e:
print "Error reading items file: ", e
for line in items_txt.split("\n"):
try:
line = line.strip()
if len(line) == 0:
continue
if line[0] == "#":
continue
if line.count(':') == 1:
name, stuff = line.split(':')
lore = ''
else:
name, stuff, lore = line.split(':', 2)
item, ench = stuff.split(',', 1)
customname = name
name = 'magic_%s' % (name.lower())
item = item.lower()
value = _items[item].value
data = _items[item].data
flag = _items[item].flag
flagparam = _items[item].flagparam
_items[name] = ItemInfo(
name,
value,
data=data,
maxstack=1,
ench=ench,
customname=customname,
flag=flag,
flagparam=flagparam, lore=lore
)
# print _items[name]
items += 1
except Exception as e:
print "Error reading line:", e
print "Line: ", line
print 'Loaded', items, 'magic items.'
def LoadPotions(filename='potions.txt'):
# Try to load items from sys.path[0] if we can,
# otherwise default to the cd.
temp = os.path.join(sys.path[0], filename)
try:
fh = open(temp)
fh.close
filename = temp
except:
pass
items = 0
try:
fh = open(filename)
except IOError as e:
sys.exit(e)
fh.close()
try:
with file(filename) as f:
items_txt = f.read()
except Exception as e:
print "Error reading custom potions file: ", e
for line in items_txt.split("\n"):
try:
line = line.strip()
if len(line) == 0:
continue
if line[0] == "#":
continue
# Fill in optional flag param
if line.count(':') == 1:
line = line + ':'
name, stuff, flag = line.split(':')
data, p_effect = stuff.split(',', 1)
# Append section sign and r to name to reset style
customname = u"\u00A7r".encode('utf8')+name
name = (name.lower())
value = _items['water bottle'].value
_items[name] = ItemInfo(name, value, data=data, maxstack=1,
p_effect=p_effect, customname=customname,
flag=flag)
# print _items[name]
items += 1
except Exception as e:
print "Error reading line:", e
print "Line: ", line
print 'Loaded', items, 'custom potions.'
def LoadDyedArmour(filename='dye_colors.txt'):
# Try to load items from sys.path[0] if we can,
# otherwise default to the cd.
temp = os.path.join(sys.path[0], filename)
try:
fh = open(temp)
fh.close
filename = temp
except:
pass
items = 0
try:
fh = open(filename)
except IOError as e:
sys.exit(e)
fh.close()
try:
with file(filename) as f:
color_txt = f.read()
except Exception as e:
print "Error reading dyes file: ", e
# leather armour types
arms = ['leather helmet',
'leather chestplate',
'leather leggings',
'leather boots']
for line in color_txt.split("\n"):
try:
line = line.strip()
if len(line) == 0:
continue
if line[0] == "#":
continue
colorname, colorval = line.split(':')
flag = 'DYED'
flagparam = int(colorval, 16)
for arm in arms:
value = _items[arm].value
name = '%s %s' % (colorname.lower(), _items[arm].name)
_items[name] = ItemInfo(name, value, data=0, maxstack=1,
flag=flag, flagparam=flagparam)
# print _items[name]
items += 1
except Exception as e:
print "Error reading line:", e
print "Line: ", line
print 'Loaded', items, 'dye colors.'
def LoadNBTFiles(dirname='items'):
# Test which path to use. If the path can't be found
# just don't load any items.
if os.path.isdir(os.path.join(sys.path[0], dirname)):
item_path = os.path.join(sys.path[0], dirname)
elif os.path.isdir(dirname):
item_path = dirname
else:
print 'Could not find the NBT items folder!'
return
# Make a list of all the NBT files in the items directory
itemlist = []
for file in os.listdir(item_path):
if (file.endswith(".nbt")):
itemlist.append(file)
items_count = 0
for item in itemlist:
# SomeItem.nbt would be referenced in loot as file_some_item
name = 'file_' + item[:-4].lower()
full_path = os.path.join(item_path, item)
# Load the nbt file and do some basic validation
try:
item_nbt = nbt.load(full_path)
item_nbt['id'] # Throws an error if not set
except:
print item + " is an invalid item! Skipping."
continue # Skip to next item
# If the Count tag exists, use it as our maxstack
try:
stack = item_nbt['Count'].value
except:
stack = 1
_items[name] = ItemInfo(name, 0, maxstack=stack, file=full_path)
# print _items[name]
items_count += 1
print 'Loaded', items_count, 'items from NBT files.'
def byName(name):
try:
return _items[name]
except:
print 'Unknown item:', name
return None
def byID(id):
try:
return _by_id[id]
except:
print 'Unknown item ID:', id
return None
LoadItems()
LoadNBTFiles()