Skip to content

Commit bc6ad16

Browse files
Properly remove custom_data component if we created it in 1.20.5+ protocols (ViaVersion#4229)
1 parent 33aecef commit bc6ad16

File tree

3 files changed

+28
-26
lines changed

3 files changed

+28
-26
lines changed

common/src/main/java/com/viaversion/viaversion/protocols/v1_20_5to1_21/rewriter/BlockItemPacketRewriter1_21.java

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,13 @@ public Item handleItemToClient(final UserConnection connection, final Item item)
136136
super.handleItemToClient(connection, item);
137137
updateItemData(item);
138138

139-
final StructuredDataContainer dataContainer = item.dataContainer();
140-
if (dataContainer.has(StructuredDataKey.RARITY)) {
139+
final StructuredDataContainer data = item.dataContainer();
140+
if (data.has(StructuredDataKey.RARITY)) {
141141
return item;
142142
}
143-
144143
// Change rarity of trident and piglin banner pattern
145144
if (item.identifier() == 1188 || item.identifier() == 1200) {
146-
dataContainer.set(StructuredDataKey.RARITY, 0); // Common
145+
data.set(StructuredDataKey.RARITY, 0); // Common
147146
saveTag(createCustomTag(item), new ByteTag(true), "rarity");
148147
}
149148
return item;
@@ -174,7 +173,16 @@ public Item handleItemToServer(final UserConnection connection, final Item item)
174173

175174
super.handleItemToServer(connection, item);
176175
downgradeItemData(item);
177-
resetRarityValues(item, nbtTagName("rarity"));
176+
177+
final StructuredDataContainer data = item.dataContainer();
178+
final CompoundTag customData = data.get(StructuredDataKey.CUSTOM_DATA);
179+
if (customData == null) {
180+
return item;
181+
}
182+
if (customData.remove(nbtTagName("rarity")) != null) {
183+
data.remove(StructuredDataKey.RARITY);
184+
removeCustomTag(data, customData);
185+
}
178186
return item;
179187
}
180188

@@ -206,21 +214,6 @@ public static void downgradeItemData(final Item item) {
206214
});
207215
}
208216

209-
public static void resetRarityValues(final Item item, final String tagName) {
210-
final StructuredDataContainer dataContainer = item.dataContainer();
211-
final CompoundTag customData = dataContainer.get(StructuredDataKey.CUSTOM_DATA);
212-
if (customData == null) {
213-
return;
214-
}
215-
216-
if (customData.remove(tagName) != null) {
217-
dataContainer.remove(StructuredDataKey.RARITY);
218-
if (customData.isEmpty()) {
219-
dataContainer.remove(StructuredDataKey.CUSTOM_DATA);
220-
}
221-
}
222-
}
223-
224217
private int itemToJubeboxSong(final int id) {
225218
String identifier = Protocol1_20_5To1_21.MAPPINGS.getFullItemMappings().identifier(id);
226219
if (!identifier.contains("music_disc_")) {

common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/rewriter/BlockItemPacketRewriter1_21_2.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -484,9 +484,7 @@ public Item handleItemToServer(final UserConnection connection, final Item item)
484484

485485
if (customData.remove(nbtTagName("remove_custom_name")) != null) {
486486
dataContainer.remove(StructuredDataKey.CUSTOM_NAME);
487-
if (customData.isEmpty()) {
488-
dataContainer.remove(StructuredDataKey.CUSTOM_DATA);
489-
}
487+
removeCustomTag(dataContainer, customData);
490488
}
491489

492490
final IntArrayTag emptyEnchantments = customData.getIntArrayTag(nbtTagName("0_enchants"));
@@ -504,9 +502,7 @@ public Item handleItemToServer(final UserConnection connection, final Item item)
504502
if (customData.remove(nbtTagName("remove_glint")) != null) {
505503
dataContainer.remove(StructuredDataKey.ENCHANTMENT_GLINT_OVERRIDE);
506504
}
507-
if (customData.isEmpty()) {
508-
dataContainer.remove(StructuredDataKey.CUSTOM_DATA);
509-
}
505+
removeCustomTag(dataContainer, customData);
510506
}
511507
return item;
512508
}

common/src/main/java/com/viaversion/viaversion/rewriter/StructuredItemRewriter.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
public class StructuredItemRewriter<C extends ClientboundPacketType, S extends ServerboundPacketType,
4242
T extends Protocol<C, ?, ?, S>> extends ItemRewriter<C, S, T> {
4343

44+
public static final String MARKER_KEY = "VV|custom_data";
45+
4446
public StructuredItemRewriter(
4547
T protocol,
4648
Type<Item> itemType, Type<Item[]> itemArrayType, Type<Item> mappedItemType, Type<Item[]> mappedItemArrayType,
@@ -205,15 +207,18 @@ protected void restoreTextComponents(final Item item) {
205207
// Remove custom name
206208
if (customData.remove(nbtTagName("added_custom_name")) != null) {
207209
data.remove(StructuredDataKey.CUSTOM_NAME);
210+
removeCustomTag(data, customData);
208211
} else {
209212
final Tag customName = removeBackupTag(customData, "custom_name");
210213
if (customName != null) {
211214
data.set(StructuredDataKey.CUSTOM_NAME, customName);
215+
removeCustomTag(data, customData);
212216
}
213217

214218
final Tag itemName = removeBackupTag(customData, "item_name");
215219
if (itemName != null) {
216220
data.set(StructuredDataKey.ITEM_NAME, itemName);
221+
removeCustomTag(data, customData);
217222
}
218223
}
219224
}
@@ -223,6 +228,7 @@ protected CompoundTag createCustomTag(final Item item) {
223228
CompoundTag customData = data.get(StructuredDataKey.CUSTOM_DATA);
224229
if (customData == null) {
225230
customData = new CompoundTag();
231+
customData.putBoolean(MARKER_KEY, true);
226232
data.set(StructuredDataKey.CUSTOM_DATA, customData);
227233
}
228234
return customData;
@@ -239,6 +245,13 @@ protected void saveTag(final CompoundTag customData, final Tag tag, final String
239245
return customData.remove(nbtTagName(tagName));
240246
}
241247

248+
protected void removeCustomTag(final StructuredDataContainer data, final CompoundTag customData) {
249+
// Only remove if we initially added it and only the marker is left
250+
if (customData.contains(MARKER_KEY) && customData.size() == 1) {
251+
data.remove(StructuredDataKey.CUSTOM_DATA);
252+
}
253+
}
254+
242255
@FunctionalInterface
243256
private interface ItemHandler {
244257

0 commit comments

Comments
 (0)