Skip to content

Commit 70c6af3

Browse files
committed
fix: Parse CustomName more adaptively
1 parent 7c3961a commit 70c6af3

File tree

7 files changed

+76
-72
lines changed

7 files changed

+76
-72
lines changed

src/_props.hpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <je2be/uuid.hpp>
66

7+
#include "_java-data-versions.hpp"
78
#include "_pos3.hpp"
89
#include "_rotation.hpp"
910

@@ -300,6 +301,46 @@ static std::u8string GetTextComponent(std::u8string const &in) {
300301
}
301302
}
302303

304+
static inline std::optional<std::u8string> ParseJavaTextComponent(std::shared_ptr<Tag> const &input) {
305+
using namespace std;
306+
using namespace je2be::props;
307+
optional<u8string> text;
308+
if (auto s = dynamic_pointer_cast<StringTag>(input); s) {
309+
text = s->fValue;
310+
} else if (auto c = dynamic_pointer_cast<CompoundTag>(input); c) {
311+
if (auto t = c->string(u8"text"); t) {
312+
return *t;
313+
} else {
314+
return nullopt;
315+
}
316+
}
317+
if (!text) {
318+
return nullopt;
319+
}
320+
return GetTextComponent(*text);
321+
}
322+
323+
static inline std::shared_ptr<Tag> CreateJavaTextComponent(std::u8string const &value, int outputDataVersion) {
324+
if (outputDataVersion >= (int)JavaDataVersions::Snapshot25w02a) {
325+
// 1.21.8
326+
// 1.21.6
327+
// 1.21.5
328+
// 25w07a
329+
// 25w04a
330+
// 25w02a
331+
auto com = Compound();
332+
com->set(u8"text", String(value));
333+
return com;
334+
} else {
335+
// 1.21.4
336+
// 1.20.6
337+
// 1.20
338+
props::Json json;
339+
props::SetJsonString(json, u8"text", value);
340+
return String(props::StringFromJson(json));
341+
}
342+
}
343+
303344
static inline i32 SquashI64ToI32(i64 v) {
304345
if (v < (i64)std::numeric_limits<i32>::min() || (i64)std::numeric_limits<i32>::max() < v) {
305346
return mcfile::XXHash<i32>::Digest(&v, sizeof(v));

src/bedrock-block-entity.cpp

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ class BlockEntity::Impl {
6060
if (!result->fTileEntity->string(u8"CustomName")) {
6161
auto customName = tag.string(u8"CustomName");
6262
if (customName) {
63-
props::Json json;
64-
props::SetJsonString(json, u8"text", *customName);
65-
result->fTileEntity->set(u8"CustomName", props::StringFromJson(json));
63+
result->fTileEntity->set(u8"CustomName", props::CreateJavaTextComponent(*customName, outputDataVersion));
6664
}
6765
}
6866
}
@@ -393,9 +391,7 @@ class BlockEntity::Impl {
393391

394392
auto customName = tagB.string(u8"CustomName", u8"");
395393
if (!customName.empty()) {
396-
props::Json json;
397-
props::SetJsonString(json, u8"text", customName);
398-
t->set(u8"CustomName", props::StringFromJson(json));
394+
t->set(u8"CustomName", props::CreateJavaTextComponent(customName, opt.fOutputDataVersion));
399395
}
400396

401397
Result r;
@@ -781,43 +777,22 @@ class BlockEntity::Impl {
781777
}
782778
for (size_t i = 0; i < 4; i++) {
783779
u8string line = i < linesB.size() ? linesB[i] : u8"";
784-
if (outputDataVersion >= (int)JavaDataVersions::Snapshot25w02a) {
785-
// 1.21.8
786-
// 1.21.6
787-
// 1.21.5
788-
// 25w07a
789-
// 25w04a
790-
// 25w02a
791-
auto text = Compound();
792-
text->set(u8"text", String(line));
793-
messagesJ->push_back(text);
794-
} else {
795-
// 1.21.4
796-
// 1.20.6
797-
// 1.20
798-
if (line.empty()) {
799-
messagesJ->push_back(String(u8R"({"text":""})"));
800-
} else {
801-
props::Json json;
802-
props::SetJsonString(json, u8"text", line);
803-
messagesJ->push_back(String(props::StringFromJson(json)));
804-
}
805-
}
780+
messagesJ->push_back(props::CreateJavaTextComponent(line, outputDataVersion));
806781
}
807782
ret->set(u8"messages", messagesJ);
808783

809784
return ret;
810785
}
811786

812-
static CompoundTagPtr SignTextEmpty() {
787+
static CompoundTagPtr SignTextEmpty(int outputDataVersion) {
813788
auto ret = Compound();
814789
ret->set(u8"color", u8"black");
815790
ret->set(u8"has_glowing_text", Bool(false));
816791
auto messages = List<Tag::Type::String>();
817-
messages->push_back(String(u8R"({"text":""})"));
818-
messages->push_back(String(u8R"({"text":""})"));
819-
messages->push_back(String(u8R"({"text":""})"));
820-
messages->push_back(String(u8R"({"text":""})"));
792+
messages->push_back(props::CreateJavaTextComponent(u8"", outputDataVersion));
793+
messages->push_back(props::CreateJavaTextComponent(u8"", outputDataVersion));
794+
messages->push_back(props::CreateJavaTextComponent(u8"", outputDataVersion));
795+
messages->push_back(props::CreateJavaTextComponent(u8"", outputDataVersion));
821796
ret->set(u8"messages", messages);
822797
return ret;
823798
}
@@ -1127,13 +1102,13 @@ class BlockEntity::Impl {
11271102
auto frontTextJ = SignText(*frontTextB, opt.fOutputDataVersion);
11281103
te->set(u8"front_text", frontTextJ);
11291104
} else {
1130-
te->set(u8"front_text", SignTextEmpty());
1105+
te->set(u8"front_text", SignTextEmpty(opt.fOutputDataVersion));
11311106
}
11321107
if (backTextB) {
11331108
auto backTextJ = SignText(*backTextB, opt.fOutputDataVersion);
11341109
te->set(u8"back_text", backTextJ);
11351110
} else {
1136-
te->set(u8"back_text", SignTextEmpty());
1111+
te->set(u8"back_text", SignTextEmpty(opt.fOutputDataVersion));
11371112
}
11381113
} else {
11391114
auto frontTextJ = Compound();
@@ -1150,14 +1125,12 @@ class BlockEntity::Impl {
11501125
for (int i = 0; i < 4; i++) {
11511126
u8string key = u8"Text" + mcfile::String::ToString(i + 1);
11521127
u8string line = i < lines.size() ? lines[i] : u8"";
1153-
props::Json json;
1154-
props::SetJsonString(json, u8"text", line);
1155-
messagesJ->push_back(String(props::StringFromJson(json)));
1128+
messagesJ->push_back(props::CreateJavaTextComponent(line, opt.fOutputDataVersion));
11561129
}
11571130
frontTextJ->set(u8"messages", messagesJ);
11581131

11591132
te->set(u8"front_text", frontTextJ);
1160-
te->set(u8"back_text", SignTextEmpty());
1133+
te->set(u8"back_text", SignTextEmpty(opt.fOutputDataVersion));
11611134
}
11621135

11631136
CopyBoolValues(tag, *te, {{u8"IsWaxed", u8"is_waxed"}});
@@ -1290,7 +1263,6 @@ class BlockEntity::Impl {
12901263
static std::optional<Result> Null(Pos3i const &pos, mcfile::be::Block const &block, CompoundTag const &tagB, mcfile::je::Block const &blockJ, Context &ctx, Options const &opt) {
12911264
return std::nullopt;
12921265
}
1293-
12941266
#pragma endregion
12951267

12961268
static std::unordered_map<std::u8string_view, Converter> *CreateTable() {

src/bedrock-entity.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,15 +1334,7 @@ class Entity::Impl {
13341334
static void CustomName(CompoundTag const &b, CompoundTag &j, Context &ctx, int dataVersion) {
13351335
auto name = b.string(u8"CustomName");
13361336
if (name) {
1337-
if (dataVersion >= (int)JavaDataVersions::Snapshot25w02a) {
1338-
auto com = Compound();
1339-
com->set(u8"text", String(*name));
1340-
j[u8"CustomName"] = com;
1341-
} else {
1342-
props::Json json;
1343-
props::SetJsonString(json, u8"text", *name);
1344-
j[u8"CustomName"] = String(props::StringFromJson(json));
1345-
}
1337+
j[u8"CustomName"] = props::CreateJavaTextComponent(*name, dataVersion);
13461338
}
13471339
}
13481340

src/bedrock-item.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,7 @@ class Item::Impl {
144144
if (displayB) {
145145
auto displayName = displayB->string(u8"Name");
146146
if (displayName) {
147-
props::Json json;
148-
props::SetJsonString(json, u8"text", *displayName);
149-
java::AppendComponent(itemJ, u8"custom_name", String(props::StringFromJson(json)));
147+
java::AppendComponent(itemJ, u8"custom_name", props::CreateJavaTextComponent(*displayName, dataVersion));
150148
}
151149

152150
if (auto loreB = displayB->listTag(u8"Lore"); loreB) {
@@ -167,9 +165,7 @@ class Item::Impl {
167165
auto customName = tagB->string(u8"CustomName");
168166
auto customNameVisible = tagB->boolean(u8"CustomNameVisible", false);
169167
if (customName && customNameVisible) {
170-
props::Json json;
171-
props::SetJsonString(json, u8"text", *customName);
172-
java::AppendComponent(itemJ, u8"custom_name", String(props::StringFromJson(json)));
168+
java::AppendComponent(itemJ, u8"custom_name", props::CreateJavaTextComponent(*customName, dataVersion));
173169
}
174170

175171
auto enchB = tagB->listTag(u8"ench");
@@ -1109,6 +1105,7 @@ class Item::Impl {
11091105
}
11101106
#pragma endregion
11111107

1108+
#pragma region Utilities
11121109
static std::optional<float> HealthFromBucketTag(CompoundTag const &tagB) {
11131110
auto attributes = tagB.listTag(u8"Attributes");
11141111
if (!attributes) {
@@ -1136,6 +1133,7 @@ class Item::Impl {
11361133
static std::u8string Ns() {
11371134
return u8"minecraft:";
11381135
}
1136+
#pragma endregion
11391137

11401138
static std::unordered_map<std::u8string_view, Converter> *CreateTable() {
11411139
using namespace std;

src/java-tile-entity.cpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -811,11 +811,9 @@ class TileEntity::Impl {
811811
{u8"conditionMet", Bool(conditionMet)},
812812
});
813813
Attach(c, pos, *tag);
814-
auto customNameJ = c->string(u8"CustomName");
815-
if (customNameJ) {
816-
auto text = props::GetTextComponent(*customNameJ);
817-
if (!text.empty()) {
818-
tag->set(u8"CustomName", text);
814+
if (auto customNameJ = c->find(u8"CustomName"); customNameJ != c->end()) {
815+
if (auto text = props::ParseJavaTextComponent(customNameJ->second); text) {
816+
tag->set(u8"CustomName", String(*text));
819817
}
820818
}
821819
return tag;
@@ -1726,11 +1724,9 @@ class TileEntity::Impl {
17261724
tag.set(u8"z", Int(pos.fZ));
17271725

17281726
if (c) {
1729-
auto customNameJ = c->string(u8"CustomName");
1730-
if (customNameJ) {
1731-
auto customNameB = props::GetTextComponent(*customNameJ);
1732-
if (!customNameB.empty()) {
1733-
tag.set(u8"CustomName", customNameB);
1727+
if (auto customNameJ = c->find(u8"CustomName"); customNameJ != c->end()) {
1728+
if (auto customNameB = props::ParseJavaTextComponent(customNameJ->second); customNameB) {
1729+
tag.set(u8"CustomName", String(*customNameB));
17341730
}
17351731
}
17361732
}
@@ -1830,11 +1826,9 @@ class TileEntity::Impl {
18301826
ret->set(u8"z", Int(pos.fZ));
18311827
ret->set(u8"isMovable", Bool(true));
18321828

1833-
auto customName = props::GetJson(tagJ, u8"CustomName");
1834-
if (customName) {
1835-
auto text = GetAsString(*customName, "text");
1836-
if (text) {
1837-
ret->set(u8"CustomName", *text);
1829+
if (auto customName = tagJ.find(u8"CustomName"); customName != tagJ.end()) {
1830+
if (auto text = props::ParseJavaTextComponent(customName->second); text) {
1831+
ret->set(u8"CustomName", String(*text));
18381832
}
18391833
}
18401834
return ret;

src/java/_components.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#pragma once
22

3+
#include "_java-data-versions.hpp"
4+
#include "_props.hpp"
5+
36
namespace je2be {
47
namespace java {
58

src/java/_item.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,12 @@ class Item {
4848
if (auto components = itemJ.compoundTag(u8"components"); components) {
4949
if (auto n = components->string(u8"minecraft:custom_name"); n) {
5050
name = *n;
51-
} else {
52-
return nullopt;
51+
} else if (auto n = components->compoundTag(u8"minecraft:custom_name"); n) {
52+
if (auto text = n->string(u8"text"); text) {
53+
return *text;
54+
} else {
55+
return nullopt;
56+
}
5357
}
5458
} else if (auto tag = itemJ.compoundTag(u8"tag"); tag) {
5559
auto display = tag->compoundTag(u8"display");

0 commit comments

Comments
 (0)