-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathitems.py
92 lines (71 loc) · 2.86 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
from logging import getLogger
import json
import directories
import os
import shutil
import types
logger = getLogger(__name__)
class ItemType(object):
def __init__(self, id, name, maxdamage=0, damagevalue=0, stacksize=64):
self.id = id
self.name = name
self.maxdamage = maxdamage
self.damagevalue = damagevalue
self.stacksize = stacksize
def __repr__(self):
return "ItemType({0}, '{1}')".format(self.id, self.name)
def __str__(self):
return "ItemType {0}: {1}".format(self.id, self.name)
class Items(object):
items = {}
def __init__(self, filename=None):
itemsdir = os.path.join(directories.getDataDir(), "Items")
if not os.path.exists(itemsdir):
raise Exception("Couldn't find Item Files. Please reinstall MCEdit!")
for file_ in os.listdir(itemsdir):
if os.path.isdir(os.path.join(itemsdir, file_)):
try:
f = open(os.path.join(itemsdir, file_, "items.json"), 'r')
itempack = json.load(f)
itempacknew = {}
for item in itempack:
itempacknew[file_ + ":" + item] = itempack.get(item)
self.items.update(itempacknew)
except Exception, e:
logger.debug('Error while loading items.json: %s'%e)
pass
try:
f = open(os.path.join(itemsdir, file_, "blocks.json"), 'r')
itempack = json.load(f)
itempacknew = {}
for item in itempack:
itempacknew[file_ + ":" + item] = itempack.get(item)
self.items.update(itempacknew)
except Exception, e:
logger.debug('Error while loading blocks.json: %s'%e)
pass
def findItem(self, id=0, damage=None):
try:
item = self.items[id]
except:
item = self.findItemID(id)
if damage <= item["maxdamage"]:
if type(item["name"]) == str or type(item["name"]) == unicode:
return ItemType(id, item["name"], item["maxdamage"], damage, item["stacksize"])
else:
if type(item["name"][damage]) == str or type(item["name"][damage]) == unicode:
return ItemType(id, item["name"][damage], item["maxdamage"], damage, item["stacksize"])
else:
raise ItemNotFound()
else:
raise ItemNotFound()
def findItemID(self, id):
for item in self.items:
itemTemp = self.items[item]
if type(itemTemp) != types.UnicodeType:
if itemTemp["id"] == id:
return self.items[item]
raise ItemNotFound()
class ItemNotFound(KeyError):
pass
items = Items()