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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//! V1458 (17w50a+1) — schematic-relevant subset of `V1458.java`.
//!
//! VERSION = MCVersions.V17W50A + 1 = 1457 + 1 = 1458.
//!
//! Ported:
//! * ENTITY structure converter (V1458.java:46-55): for every entity *except*
//! `minecraft:commandblock_minecart`, run `updateCustomName` — a non-empty
//! plain-string `CustomName` becomes a `{"text":…}` text component, an
//! empty/absent one is removed.
//! * ITEM_STACK structure converter (V1458.java:57-83): a plain-string
//! `tag.display.Name` is wrapped into a `{"text":…}` text component. The
//! `LocName` branch is commented out in Java (1.20.5 removed
//! ItemCustomNameToComponentFix), so it is intentionally not ported.
//! * TILE_ENTITY structure converter (V1458.java:85-94): for every tile entity
//! *except* `minecraft:command_block`, run `updateCustomName` on `CustomName`.
//! * ENTITY structure walker (V1458.java:96-101): walk the `Passengers` list as
//! ENTITY, walk `CustomName` as TEXT_COMPONENT, then run
//! ENTITY_EQUIPMENT.convert. This supersedes the V135 entity structure walker
//! by adding the CustomName TEXT_COMPONENT descent.
//! * `named` / `namedInventory` TILE_ENTITY walkers (V1458.java:28-35,128-138)
//! for beacon, banner, brewing_stand, chest, trapped_chest, dispenser,
//! dropper, enchanting_table, furnace, hopper, shulker_box. `named` routes
//! `CustomName` through TEXT_COMPONENT; `namedInventory` also walks `Items`.
//!
//! Skipped (non-schematic): the PLAYER structure converter (V1458.java:39-44) and
//! the PLAYER structure walker (V1458.java:105-126) — PLAYER never appears in a
//! schematic file. The command-block / commandblock_minecart cases that V1458
//! excludes here are handled at V1488 (see `v1488.rs`).
use std::sync::Arc;
use crate::nbt::NbtMap;
use super::super::helpers::create_plain_text_component;
use super::super::registry::RegistryBuilder;
use super::super::types::MapExt;
use super::super::walker::{convert, convert_list, item_lists};
const VERSION: i32 = 1458;
/// `V1458.updateCustomName` (V1458.java:16-26): a non-empty plain-string
/// `CustomName` becomes a JSON text component `{"text":"<name>"}`; an
/// empty/absent one is removed.
fn update_custom_name(data: &mut NbtMap) {
let name = data.get_string("CustomName").unwrap_or("").to_string();
if name.is_empty() {
data.take("CustomName");
} else {
data.set_string("CustomName", create_plain_text_component(&name));
}
}
/// Inverse of `update_custom_name` (and the item `tag.display.Name` wrap): unwrap
/// a single-key plain-text component `{"text": s}` back to the raw legacy string
/// `s`.
///
/// The forward (`ComponentUtils.createPlainTextComponent`) only ever produces
/// `{"text": <raw string>}` from a non-empty legacy `CustomName`/`Name`. That
/// mapping is injective — the single `"text"` value is exactly the original raw
/// string (even if that string itself looked like JSON, it was stored as the
/// string value), so this unwrap is an exact, lossless inverse for any real
/// downgrade. The empty/absent case the forward *removed* leaves nothing to
/// invert (absence stays absence). Anything that is not a single-key `{"text":…}`
/// string component is foreign to the forward output and is left untouched.
fn revert_custom_name(data: &mut NbtMap, path: &str) {
let text = match data.get_string(path).map(str::to_string) {
Some(t) => t,
None => return,
};
let parsed: serde_json::Value = match serde_json::from_str(&text) {
Ok(v) => v,
// Forward only ever emits JSON here; non-JSON is foreign data — leave it.
Err(_) => return,
};
if let serde_json::Value::Object(obj) = &parsed {
if obj.len() == 1 {
if let Some(serde_json::Value::String(s)) = obj.get("text") {
data.set_string(path, s);
}
}
}
}
/// `named(version, id)` (V1458.java:28-30): route `CustomName` through
/// TEXT_COMPONENT for the given tile-entity id.
fn named(reg: &mut RegistryBuilder, id: &'static str) {
reg.tile_entity.add_walker(
VERSION,
0,
id,
Arc::new(|reg, data, from, to| {
convert(reg, ®.text_component, data, "CustomName", from, to);
}),
);
}
/// `namedInventory(version, id)` (V1458.java:32-35): `named` plus a
/// `DataWalkerItemLists("Items")`.
fn named_inventory(reg: &mut RegistryBuilder, id: &'static str) {
named(reg, id);
reg.tile_entity
.add_walker(VERSION, 0, id, item_lists(&["Items"]));
}
pub fn register(reg: &mut RegistryBuilder) {
// ENTITY: updateCustomName for every entity except commandblock_minecart
// (V1458.java:46-55). The commandblock_minecart case is handled at V1488.
reg.entity.add_structure_converter(
VERSION,
0,
Box::new(|data: &mut NbtMap, _from, _to| {
if data.get_string("id") == Some("minecraft:commandblock_minecart") {
return;
}
update_custom_name(data);
}),
);
// Reverse of the ENTITY converter: unwrap the `{"text":…}` CustomName back to
// a raw legacy string. Mirror the same commandblock_minecart exclusion as the
// forward (it never wrapped that entity, so we must not unwrap it either).
reg.entity.add_reverse_converter(
VERSION,
0,
Box::new(|data: &mut NbtMap, _from, _to| {
if data.get_string("id") == Some("minecraft:commandblock_minecart") {
return;
}
revert_custom_name(data, "CustomName");
}),
);
// ITEM_STACK: wrap a plain-string tag.display.Name into a text component
// (V1458.java:57-83). The LocName branch is dead in Java, so it is skipped.
reg.item_stack.add_structure_converter(
VERSION,
0,
Box::new(|data: &mut NbtMap, _from, _to| {
let Some(tag) = data.get_map_mut("tag") else {
return;
};
let Some(display) = tag.get_map_mut("display") else {
return;
};
if let Some(name) = display.get_string("Name").map(str::to_string) {
display.set_string("Name", create_plain_text_component(&name));
}
}),
);
// Reverse of the ITEM_STACK converter: unwrap the `{"text":…}` tag.display.Name
// back to its raw legacy string (same single-key plain-text inverse).
reg.item_stack.add_reverse_converter(
VERSION,
0,
Box::new(|data: &mut NbtMap, _from, _to| {
let Some(tag) = data.get_map_mut("tag") else {
return;
};
let Some(display) = tag.get_map_mut("display") else {
return;
};
revert_custom_name(display, "Name");
}),
);
// TILE_ENTITY: updateCustomName for every tile entity except command_block
// (V1458.java:85-94). The command_block case is handled at V1488.
reg.tile_entity.add_structure_converter(
VERSION,
0,
Box::new(|data: &mut NbtMap, _from, _to| {
if data.get_string("id") == Some("minecraft:command_block") {
return;
}
update_custom_name(data);
}),
);
// Reverse of the TILE_ENTITY converter: unwrap the `{"text":…}` CustomName back
// to a raw legacy string, mirroring the forward's command_block exclusion.
reg.tile_entity.add_reverse_converter(
VERSION,
0,
Box::new(|data: &mut NbtMap, _from, _to| {
if data.get_string("id") == Some("minecraft:command_block") {
return;
}
revert_custom_name(data, "CustomName");
}),
);
// ENTITY structure walker (V1458.java:96-101): Passengers (ENTITY) +
// CustomName (TEXT_COMPONENT) + entity equipment. Supersedes the V135 walker.
reg.entity.add_structure_walker(
VERSION,
0,
Arc::new(|reg, data, from, to| {
convert_list(reg, ®.entity, data, "Passengers", from, to);
convert(reg, ®.text_component, data, "CustomName", from, to);
reg.entity_equipment.convert(reg, data, from, to);
}),
);
// named / namedInventory tile-entity walkers (V1458.java:128-138).
named(reg, "minecraft:beacon");
named(reg, "minecraft:banner");
named_inventory(reg, "minecraft:brewing_stand");
named_inventory(reg, "minecraft:chest");
named_inventory(reg, "minecraft:trapped_chest");
named_inventory(reg, "minecraft:dispenser");
named_inventory(reg, "minecraft:dropper");
named(reg, "minecraft:enchanting_table");
named_inventory(reg, "minecraft:furnace");
named_inventory(reg, "minecraft:hopper");
named_inventory(reg, "minecraft:shulker_box");
}