use std::sync::Arc;
use crate::nbt::NbtMap;
use super::engine::{DataType, MCValueType, Walker};
use super::loss;
use super::registry::Registry;
use super::types::{MapExt, ValueExt};
use super::version::EncodedVersion;
pub fn convert(
reg: &Registry,
ty: &DataType,
data: &mut NbtMap,
path: &str,
from: EncodedVersion,
to: EncodedVersion,
) {
if let Some(map) = data.get_map_mut(path) {
let _scope = loss::path_scope(path);
ty.convert(reg, map, from, to);
}
}
pub fn convert_list(
reg: &Registry,
ty: &DataType,
data: &mut NbtMap,
path: &str,
from: EncodedVersion,
to: EncodedVersion,
) {
if let Some(list) = data.get_list_mut(path) {
for (i, el) in list.iter_mut().enumerate() {
if let Some(map) = el.as_compound_mut() {
let _scope = loss::path_scope(format!("{path}[{i}]"));
ty.convert(reg, map, from, to);
}
}
}
}
pub fn convert_list_path(
reg: &Registry,
ty: &DataType,
data: &mut NbtMap,
list_path: &str,
element_path: &str,
from: EncodedVersion,
to: EncodedVersion,
) {
if let Some(list) = data.get_list_mut(list_path) {
for (i, el) in list.iter_mut().enumerate() {
if let Some(map) = el.as_compound_mut() {
if let Some(child) = map.get_map_mut(element_path) {
let _scope = loss::path_scope(format!("{list_path}[{i}].{element_path}"));
ty.convert(reg, child, from, to);
}
}
}
}
}
pub fn convert_value(
ty: &MCValueType,
data: &mut NbtMap,
path: &str,
from: EncodedVersion,
to: EncodedVersion,
) {
if let Some(v) = data.get_mut(path) {
ty.convert(v, from, to);
}
}
pub fn convert_value_list(
ty: &MCValueType,
data: &mut NbtMap,
path: &str,
from: EncodedVersion,
to: EncodedVersion,
) {
if let Some(list) = data.get_list_mut(path) {
for el in list.iter_mut() {
ty.convert(el, from, to);
}
}
}
pub fn item_lists(paths: &'static [&'static str]) -> Walker {
Arc::new(move |reg, data, from, to| {
for p in paths {
convert_list(reg, ®.item_stack, data, p, from, to);
}
})
}
pub fn items(paths: &'static [&'static str]) -> Walker {
Arc::new(move |reg, data, from, to| {
for p in paths {
convert(reg, ®.item_stack, data, p, from, to);
}
})
}
pub fn tile_entities(paths: &'static [&'static str]) -> Walker {
Arc::new(move |reg, data, from, to| {
for p in paths {
convert(reg, ®.tile_entity, data, p, from, to);
}
})
}
pub fn block_names(paths: &'static [&'static str]) -> Walker {
Arc::new(move |reg, data, from, to| {
for p in paths {
convert_value(®.block_name, data, p, from, to);
}
})
}
pub fn item_names(paths: &'static [&'static str]) -> Walker {
Arc::new(move |reg, data, from, to| {
for p in paths {
convert_value(®.item_name, data, p, from, to);
}
})
}