Skip to content

Commit

Permalink
Fix CUSTOM_DATA handling to properly restore original data in 1.20.5-…
Browse files Browse the repository at this point in the history
…>1.20.3

Counterpart PR of ViaVersion/ViaVersion#4094; Adds the now required handling of custom data. Ideally we shouldn't backup any tags at all since it prevents mods from edit them, but that would require more conversion code in both protocols.
  • Loading branch information
FlorianMichael committed Aug 14, 2024
1 parent 609205f commit 102f0c6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.viaversion.viaversion.api.minecraft.Holder;
import com.viaversion.viaversion.api.minecraft.Particle;
import com.viaversion.viaversion.api.minecraft.SoundEvent;
import com.viaversion.viaversion.api.minecraft.data.StructuredData;
import com.viaversion.viaversion.api.minecraft.data.StructuredDataContainer;
import com.viaversion.viaversion.api.minecraft.data.StructuredDataKey;
import com.viaversion.viaversion.api.minecraft.item.Item;
Expand Down Expand Up @@ -363,7 +364,15 @@ private static int colorId(final String color) {
data.set(StructuredDataKey.FIREWORKS, new Fireworks(1, new FireworkExplosion[0]));
}

final StructuredData<CompoundTag> customData = data.getNonEmpty(StructuredDataKey.CUSTOM_DATA);
final Item oldItem = vvProtocol.getItemRewriter().toOldItem(connection, item, DATA_CONVERTER);

if (customData != null) {
// We later don't know which tags are custom data and which are not because the VV conversion
// keeps converted data, so we backup the original custom data and restore it later
oldItem.tag().put(nbtTagName(), customData.value().copy());
}

if (oldItem.tag() != null && oldItem.tag().isEmpty()) {
// Improve item equality checks by removing empty tags
oldItem.setTag(null);
Expand All @@ -386,9 +395,14 @@ public Item handleItemToServer(final UserConnection connection, @Nullable final
// Convert to structured item first
final Item structuredItem = vvProtocol.getItemRewriter().toStructuredItem(connection, item);

if (item.tag() != null && item.tag().get(nbtTagName()) instanceof final CompoundTag tag) {
// Set original custom data from backup
structuredItem.dataContainer().set(StructuredDataKey.CUSTOM_DATA, tag);
}

structuredItem.dataContainer().setIdLookup(protocol, false);
enchantmentRewriter.handleToServer(structuredItem);

return super.handleItemToServer(connection, structuredItem);
}
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
projectVersion=5.0.3
projectVersion=5.0.4-SNAPSHOT

# Smile emoji
mcVersions=1.21,1.20.6,1.20.5,1.20.4, 1.20.3, 1.20.2, 1.20.1, 1.20, 1.19.4, 1.19.3, 1.19.2, 1.19.1, 1.19, 1.18.2, 1.18.1, 1.18, 1.17.1, 1.17, 1.16.5, 1.16.4, 1.16.3, 1.16.2, 1.16.1, 1.16, 1.15.2, 1.15.1, 1.15, 1.14.4, 1.14.3, 1.14.2, 1.14.1, 1.14, 1.13.2, 1.13.1, 1.13, 1.12.2, 1.12.1, 1.12, 1.11.2, 1.11.1, 1.11, 1.10.2, 1.10.1, 1.10
Expand Down

0 comments on commit 102f0c6

Please sign in to comment.