This repository has been archived by the owner on Jul 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unflattening.py
142 lines (120 loc) · 4.54 KB
/
unflattening.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
import sys
import json
from collections import OrderedDict
import templates
def _indent(amount, text: str):
return "\n".join(((" " * amount) + line for line in text.split("\n")))
def flatten_props(props):
return {key: "/".join(val) for key, val in props.items()}
def to_bukkit_material_enum(name):
return name.lower()[10:].upper()
def make_java_class(package="net.glowstone.block.flattening.generated", class_name="GeneratedFlatteningData",
header="THIS FILE IS GENERATED. DO NOT EDIT DIRECTLY."):
class_content = ""
static_content = ""
imports = (
"com.google.common.collect.ImmutableBiMap",
"com.google.common.collect.ImmutableMap",
"com.google.common.collect.Iterators",
"java.io.InputStreamReader",
"java.util.ArrayList",
"java.util.Arrays",
"java.util.Iterator",
"java.util.List",
"java.util.Map",
"org.apache.commons.lang.ArrayUtils",
"org.bukkit.Material",
"org.bukkit.block.BlockFace",
"org.bukkit.configuration.file.YamlConfiguration",
)
# parse the data
data = load_data()
block_faces = OrderedDict()
properties = OrderedDict()
state_base_ids = list()
state_default_ids = list()
mat_ids = OrderedDict()
for block_type in data.keys():
block = data[block_type]
mat = to_bukkit_material_enum(block_type)
if "properties" in block:
props = block["properties"]
properties[mat] = props
if "facing" in props:
block_faces[mat] = props["facing"]
else:
properties[mat] = {}
base_id = block["states"][0]["id"]
mat_ids[mat] = base_id
default_id = None
for state in block["states"]:
if state.get("default", None) is True:
default_id = state["id"]
state_base_ids.append(base_id)
for state in block["states"]:
state_default_ids.append(default_id)
# write the java file
def _props_map(props):
return ", ".join((
"\"{key}\", new PropertyEnum({values})".format(key=key,
values=", ".join(("\"" + value + "\"" for value in values)))
for key, values in props.items()
))
class_content += templates.MATERIAL_PROPERTIES_CLASS
static_content += templates.MATERIAL_PROPERTIES_STATIC.format(
put_statements=_indent(4, "\n".join((
".put(Material.{mat}, "
"new PropertyDefs(propMapOf({props})))".format(
mat=mat,
props=_props_map(props))
for mat, props in properties.items()
)))
)
class_content += templates.DIRECTIONAL_POSSIBLE_FACES_CLASS
static_content += templates.DIRECTIONAL_POSSIBLE_FACES_STATIC.format(
put_statements=_indent(4, "\n".join((
".put(Material.{mat}, Arrays.asList({faces}))".format(
mat=mat,
faces=", ".join(
("BlockFace." + face.upper() for face in
faces)))
for mat, faces in block_faces.items()
)))
)
class_content += templates.STATE_BASE_IDS_CLASS.format(ids_length=len(state_base_ids))
static_content += templates.STATE_BASE_IDS_STATIC
yml_output = templates.STATE_BASE_IDS_YML.format(
base_ids=", ".join(
(str(state) for idx, state in enumerate(state_base_ids))
),
default_ids=", ".join(
(str(state) for idx, state in enumerate(state_default_ids))
)
)
class_content += templates.MATERIAL_ID_MAP_CLASS
static_content += templates.MATERIAL_ID_MAP_STATIC.format(
put_statements=_indent(4, "\n".join((
".put({base_id}, Material.{mat})".format(
base_id=base_id,
mat=mat
)
for mat, base_id in mat_ids.items()
)))
)
# output
return (templates.JAVA_FILE.format(
header=templates.JAVA_HEADER.format(header=header),
package=package,
class_name=class_name,
class_content=_indent(4, class_content),
static_content=_indent(8, static_content),
imports="\n".join(("import " + clazz + ";" for clazz in imports))
), yml_output)
def load_data(path="generated/reports/blocks.json") -> dict:
with open(path) as file:
return json.load(file)
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == "--yml":
print(make_java_class()[1])
else:
print(make_java_class()[0])