Skip to content

Commit 6b9cbf3

Browse files
Fix some issues in walk_input_nbt (#11)
* Fix some issues in walk_input_nbt * Reformat
1 parent b9cd9b2 commit 6b9cbf3

File tree

1 file changed

+97
-40
lines changed

1 file changed

+97
-40
lines changed

src/amulet/game/translate/_functions/walk_input_nbt.py

Lines changed: 97 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -182,38 +182,41 @@ def run(self, src: SrcData, state: StateData, dst: DstData) -> None:
182182
nbt_path_or_none = state.nbt_path
183183
if nbt_path_or_none is None:
184184
raise RuntimeError("The caller must set the nbt_path attribute")
185-
else:
186-
nbt_path = nbt_path_or_none
185+
nbt_path = nbt_path_or_none
187186

188187
tag_or_none = follow_nbt_path(nbt, nbt_path)
188+
if tag_or_none is None:
189+
raise RuntimeError("The caller must verify that the tag exists")
190+
tag = tag_or_none
189191

190192
nbt_cls = self._nbt_cls
191-
if tag_or_none is None:
192-
pass
193-
elif isinstance(tag_or_none, nbt_cls):
194-
tag = tag_or_none
195-
196-
def run(
197-
keys: Iterable[KeyT],
198-
lut: Mapping[KeyT, WalkInputNBTOptions] | None,
199-
nested_dtype: NBTTagClsT | None,
200-
) -> None:
201-
for key in keys:
202-
if lut is not None and key in lut:
203-
lut[key].run(src, StateData(), dst)
204-
elif self._nested_default is not None:
193+
if isinstance(tag, nbt_cls):
194+
new_type: NBTTagClsT
195+
new_path: NBTPath
196+
if isinstance(tag, CompoundTag):
197+
for key, value in tag.items():
198+
if isinstance(key, bytes):
199+
continue
200+
if self._keys is not None and key in self._keys:
205201
outer_name, outer_type, path = nbt_path
206-
new_type: NBTTagClsT
207-
if nested_dtype is None:
208-
new_type = tag.__class__
209-
else:
210-
new_type = nested_dtype
211-
new_path: NBTPath = path + (
212-
(
213-
key,
214-
new_type,
202+
new_type = type(value)
203+
new_path = path + ((key, new_type),)
204+
self._keys[key].run(
205+
src,
206+
StateData(
207+
state.relative_location,
208+
(
209+
outer_name,
210+
outer_type,
211+
new_path,
212+
),
215213
),
214+
dst,
216215
)
216+
elif self._nested_default is not None:
217+
outer_name, outer_type, path = nbt_path
218+
new_type = type(value)
219+
new_path = path + ((key, new_type),)
217220
self._nested_default.run(
218221
src,
219222
StateData(
@@ -226,28 +229,82 @@ def run(
226229
),
227230
dst,
228231
)
229-
230-
if isinstance(tag, CompoundTag):
231-
run(
232-
[key for key in tag.keys() if isinstance(key, str)],
233-
self._keys,
234-
None,
235-
)
236232
elif isinstance(tag, ListTag):
237-
dtype = NBTLookUp[tag.element_tag_id]
238-
assert dtype is not None
239-
run(range(len(tag)), self._index, dtype)
233+
for i, value in enumerate(tag):
234+
if self._index is not None and i in self._index:
235+
outer_name, outer_type, path = nbt_path
236+
new_type = type(value)
237+
new_path = path + ((i, new_type),)
238+
self._index[i].run(
239+
src,
240+
StateData(
241+
state.relative_location,
242+
(
243+
outer_name,
244+
outer_type,
245+
new_path,
246+
),
247+
),
248+
dst,
249+
)
250+
elif self._nested_default is not None:
251+
outer_name, outer_type, path = nbt_path
252+
new_type = type(value)
253+
new_path = path + ((i, new_type),)
254+
self._nested_default.run(
255+
src,
256+
StateData(
257+
state.relative_location,
258+
(
259+
outer_name,
260+
outer_type,
261+
new_path,
262+
),
263+
),
264+
dst,
265+
)
240266
elif isinstance(tag, AbstractBaseArrayTag):
241-
nested_dtype_: NBTTagClsT
267+
nested_dtype: NBTTagClsT
242268
if isinstance(tag, ByteArrayTag):
243-
nested_dtype_ = ByteTag
269+
nested_dtype = ByteTag
244270
elif isinstance(tag, IntArrayTag):
245-
nested_dtype_ = IntTag
271+
nested_dtype = IntTag
246272
elif isinstance(tag, LongArrayTag):
247-
nested_dtype_ = LongTag
273+
nested_dtype = LongTag
248274
else:
249275
raise TypeError
250-
run(range(len(tag)), self._index, nested_dtype_)
276+
277+
for i, value in enumerate(tag):
278+
if self._index is not None and i in self._index:
279+
outer_name, outer_type, path = nbt_path
280+
new_path = path + ((i, nested_dtype),)
281+
self._index[i].run(
282+
src,
283+
StateData(
284+
state.relative_location,
285+
(
286+
outer_name,
287+
outer_type,
288+
new_path,
289+
),
290+
),
291+
dst,
292+
)
293+
elif self._nested_default is not None:
294+
outer_name, outer_type, path = nbt_path
295+
new_path = path + ((i, nested_dtype),)
296+
self._nested_default.run(
297+
src,
298+
StateData(
299+
state.relative_location,
300+
(
301+
outer_name,
302+
outer_type,
303+
new_path,
304+
),
305+
),
306+
dst,
307+
)
251308
else:
252309
return
253310

0 commit comments

Comments
 (0)