-
Notifications
You must be signed in to change notification settings - Fork 47
/
items.py
71 lines (56 loc) · 1.85 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
import sys
from minecraft_data.v1_8 import items_list
from spockbot.mcdata.utils import camel_case, find_by
items = {}
items_name = {}
def get_item(item, meta=0, init=True):
ret = None
if isinstance(item, int): # by id
ret = find_by(item, items)
else: # by name
ret = find_by(item, items_name)
if init and ret is not None:
return ret(meta)
return ret
class Item(object):
id = -1
display_name = "Item"
stack_size = 0
name = "item"
variations = {}
def __init__(self, meta=None):
if meta is None:
return
self.metadata = meta
if self.metadata in self.variations:
# TODO: apply other all possible variations
self.display_name = self.variations[self.metadata]["display_name"]
def __str__(self):
return '%s %i:%i' % (self.display_name, self.id,
getattr(self, 'metadata', 0))
def _make_item(item_dict):
cls_name = '%sItem' % camel_case(str(item_dict['name']))
bases = (Item,)
variations = {}
if "variations" in item_dict:
for var in item_dict['variations']:
variations[var['metadata']] = {"display_name": var['displayName']}
attrs = {
'__module__': sys.modules[__name__],
'id': item_dict['id'],
'name': item_dict['name'],
'display_name': item_dict['displayName'],
'stack_size': item_dict['stackSize'],
'variations': variations,
}
cls = type(cls_name, bases, attrs)
assert not hasattr(sys.modules[__name__], cls_name), \
'Item "%s" already registered at %s' % (cls_name, __name__)
setattr(sys.modules[__name__], cls_name, cls)
return cls
def _create_items():
for item in items_list:
cls = _make_item(item)
items[cls.id] = cls
items_name[cls.name] = cls
_create_items()