Skip to content

Commit a7f1cf8

Browse files
sqozznickelpro
authored andcommitted
Add common data API endpoint
This fixes #20
1 parent bcc0575 commit a7f1cf8

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

example.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,10 @@
1818

1919
print(mcd_pe.version)
2020
print(mcd_pe.find_item_or_block('stone'))
21+
22+
# Query common data. E.g. to map the protocol version to a minecraft version
23+
protocol_version = 754 # example protocol version
24+
for version in minecraft_data.common().protocolVersions:
25+
if version["version"] == protocol_version:
26+
print(version["minecraftVersion"]) # 1.16.5
27+
break

minecraft_data/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
import sys
33

4-
from minecraft_data.tools import convert
4+
from minecraft_data.tools import convert, commondata
55

66

77
class mod(sys.modules[__name__].__class__):
@@ -11,5 +11,11 @@ def __call__(self, version, edition = 'pc'):
1111
)
1212
return type(version, (object,), convert(_dir, version, edition))
1313

14+
def common(self, edition = 'pc'):
15+
_dir = os.path.join(
16+
os.path.dirname(__file__), "data/data/"
17+
)
18+
return type('common', (object,), commondata(_dir, edition))
19+
1420

1521
sys.modules[__name__].__class__ = mod

minecraft_data/tools.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ def find_item_or_block(find):
4141
return ret
4242

4343

44+
def commondata(_dir, edition = 'pc'):
45+
ret = {}
46+
common_path = os.path.join(_dir, edition, 'common')
47+
for common_file in os.listdir(common_path):
48+
key = common_file.split('.', 1)[0]
49+
with open(os.path.join(common_path, common_file)) as f:
50+
data = json.load(f)
51+
ret.update({key: data})
52+
return ret
53+
54+
4455
def _grabdata(_dir, datapaths):
4556
data = {}
4657
for category, folder in datapaths.items():

0 commit comments

Comments
 (0)