Properly remove custom_data component if we created it in 1.20.5+ protocols (#4229)

This commit is contained in:
EnZaXD 2024-11-01 18:50:54 +01:00 committed by GitHub
parent 33aecef7b3
commit bc6ad16d40
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 28 additions and 26 deletions

View File

@ -136,14 +136,13 @@ public final class BlockItemPacketRewriter1_21 extends StructuredItemRewriter<Cl
super.handleItemToClient(connection, item); super.handleItemToClient(connection, item);
updateItemData(item); updateItemData(item);
final StructuredDataContainer dataContainer = item.dataContainer(); final StructuredDataContainer data = item.dataContainer();
if (dataContainer.has(StructuredDataKey.RARITY)) { if (data.has(StructuredDataKey.RARITY)) {
return item; return item;
} }
// Change rarity of trident and piglin banner pattern // Change rarity of trident and piglin banner pattern
if (item.identifier() == 1188 || item.identifier() == 1200) { if (item.identifier() == 1188 || item.identifier() == 1200) {
dataContainer.set(StructuredDataKey.RARITY, 0); // Common data.set(StructuredDataKey.RARITY, 0); // Common
saveTag(createCustomTag(item), new ByteTag(true), "rarity"); saveTag(createCustomTag(item), new ByteTag(true), "rarity");
} }
return item; return item;
@ -174,7 +173,16 @@ public final class BlockItemPacketRewriter1_21 extends StructuredItemRewriter<Cl
super.handleItemToServer(connection, item); super.handleItemToServer(connection, item);
downgradeItemData(item); downgradeItemData(item);
resetRarityValues(item, nbtTagName("rarity"));
final StructuredDataContainer data = item.dataContainer();
final CompoundTag customData = data.get(StructuredDataKey.CUSTOM_DATA);
if (customData == null) {
return item;
}
if (customData.remove(nbtTagName("rarity")) != null) {
data.remove(StructuredDataKey.RARITY);
removeCustomTag(data, customData);
}
return item; return item;
} }
@ -206,21 +214,6 @@ public final class BlockItemPacketRewriter1_21 extends StructuredItemRewriter<Cl
}); });
} }
public static void resetRarityValues(final Item item, final String tagName) {
final StructuredDataContainer dataContainer = item.dataContainer();
final CompoundTag customData = dataContainer.get(StructuredDataKey.CUSTOM_DATA);
if (customData == null) {
return;
}
if (customData.remove(tagName) != null) {
dataContainer.remove(StructuredDataKey.RARITY);
if (customData.isEmpty()) {
dataContainer.remove(StructuredDataKey.CUSTOM_DATA);
}
}
}
private int itemToJubeboxSong(final int id) { private int itemToJubeboxSong(final int id) {
String identifier = Protocol1_20_5To1_21.MAPPINGS.getFullItemMappings().identifier(id); String identifier = Protocol1_20_5To1_21.MAPPINGS.getFullItemMappings().identifier(id);
if (!identifier.contains("music_disc_")) { if (!identifier.contains("music_disc_")) {

View File

@ -484,9 +484,7 @@ public final class BlockItemPacketRewriter1_21_2 extends StructuredItemRewriter<
if (customData.remove(nbtTagName("remove_custom_name")) != null) { if (customData.remove(nbtTagName("remove_custom_name")) != null) {
dataContainer.remove(StructuredDataKey.CUSTOM_NAME); dataContainer.remove(StructuredDataKey.CUSTOM_NAME);
if (customData.isEmpty()) { removeCustomTag(dataContainer, customData);
dataContainer.remove(StructuredDataKey.CUSTOM_DATA);
}
} }
final IntArrayTag emptyEnchantments = customData.getIntArrayTag(nbtTagName("0_enchants")); final IntArrayTag emptyEnchantments = customData.getIntArrayTag(nbtTagName("0_enchants"));
@ -504,9 +502,7 @@ public final class BlockItemPacketRewriter1_21_2 extends StructuredItemRewriter<
if (customData.remove(nbtTagName("remove_glint")) != null) { if (customData.remove(nbtTagName("remove_glint")) != null) {
dataContainer.remove(StructuredDataKey.ENCHANTMENT_GLINT_OVERRIDE); dataContainer.remove(StructuredDataKey.ENCHANTMENT_GLINT_OVERRIDE);
} }
if (customData.isEmpty()) { removeCustomTag(dataContainer, customData);
dataContainer.remove(StructuredDataKey.CUSTOM_DATA);
}
} }
return item; return item;
} }

View File

@ -41,6 +41,8 @@ import org.checkerframework.checker.nullness.qual.Nullable;
public class StructuredItemRewriter<C extends ClientboundPacketType, S extends ServerboundPacketType, public class StructuredItemRewriter<C extends ClientboundPacketType, S extends ServerboundPacketType,
T extends Protocol<C, ?, ?, S>> extends ItemRewriter<C, S, T> { T extends Protocol<C, ?, ?, S>> extends ItemRewriter<C, S, T> {
public static final String MARKER_KEY = "VV|custom_data";
public StructuredItemRewriter( public StructuredItemRewriter(
T protocol, T protocol,
Type<Item> itemType, Type<Item[]> itemArrayType, Type<Item> mappedItemType, Type<Item[]> mappedItemArrayType, Type<Item> itemType, Type<Item[]> itemArrayType, Type<Item> mappedItemType, Type<Item[]> mappedItemArrayType,
@ -205,15 +207,18 @@ public class StructuredItemRewriter<C extends ClientboundPacketType, S extends S
// Remove custom name // Remove custom name
if (customData.remove(nbtTagName("added_custom_name")) != null) { if (customData.remove(nbtTagName("added_custom_name")) != null) {
data.remove(StructuredDataKey.CUSTOM_NAME); data.remove(StructuredDataKey.CUSTOM_NAME);
removeCustomTag(data, customData);
} else { } else {
final Tag customName = removeBackupTag(customData, "custom_name"); final Tag customName = removeBackupTag(customData, "custom_name");
if (customName != null) { if (customName != null) {
data.set(StructuredDataKey.CUSTOM_NAME, customName); data.set(StructuredDataKey.CUSTOM_NAME, customName);
removeCustomTag(data, customData);
} }
final Tag itemName = removeBackupTag(customData, "item_name"); final Tag itemName = removeBackupTag(customData, "item_name");
if (itemName != null) { if (itemName != null) {
data.set(StructuredDataKey.ITEM_NAME, itemName); data.set(StructuredDataKey.ITEM_NAME, itemName);
removeCustomTag(data, customData);
} }
} }
} }
@ -223,6 +228,7 @@ public class StructuredItemRewriter<C extends ClientboundPacketType, S extends S
CompoundTag customData = data.get(StructuredDataKey.CUSTOM_DATA); CompoundTag customData = data.get(StructuredDataKey.CUSTOM_DATA);
if (customData == null) { if (customData == null) {
customData = new CompoundTag(); customData = new CompoundTag();
customData.putBoolean(MARKER_KEY, true);
data.set(StructuredDataKey.CUSTOM_DATA, customData); data.set(StructuredDataKey.CUSTOM_DATA, customData);
} }
return customData; return customData;
@ -239,6 +245,13 @@ public class StructuredItemRewriter<C extends ClientboundPacketType, S extends S
return customData.remove(nbtTagName(tagName)); return customData.remove(nbtTagName(tagName));
} }
protected void removeCustomTag(final StructuredDataContainer data, final CompoundTag customData) {
// Only remove if we initially added it and only the marker is left
if (customData.contains(MARKER_KEY) && customData.size() == 1) {
data.remove(StructuredDataKey.CUSTOM_DATA);
}
}
@FunctionalInterface @FunctionalInterface
private interface ItemHandler { private interface ItemHandler {