Update VV usage

This commit is contained in:
Nassim Jahnke 2024-09-29 13:42:26 +02:00
parent 1d1e9bc040
commit f4253641e1
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
4 changed files with 38 additions and 45 deletions

View File

@ -20,14 +20,12 @@ package com.viaversion.viabackwards.api.rewriters;
import com.viaversion.nbt.tag.CompoundTag;
import com.viaversion.nbt.tag.IntTag;
import com.viaversion.nbt.tag.ListTag;
import com.viaversion.nbt.tag.NumberTag;
import com.viaversion.nbt.tag.Tag;
import com.viaversion.viabackwards.api.BackwardsProtocol;
import com.viaversion.viabackwards.api.data.BackwardsMappingData;
import com.viaversion.viabackwards.api.data.MappedItem;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.Particle;
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;
@ -86,12 +84,12 @@ public class BackwardsStructuredItemRewriter<C extends ClientboundPacketType, S
item.setIdentifier(mappedItem.id());
// Add custom model data
if (mappedItem.customModelData() != null && !dataContainer.contains(StructuredDataKey.CUSTOM_MODEL_DATA)) {
if (mappedItem.customModelData() != null && !dataContainer.has(StructuredDataKey.CUSTOM_MODEL_DATA)) {
dataContainer.set(StructuredDataKey.CUSTOM_MODEL_DATA, mappedItem.customModelData());
}
// Set custom name - only done if there is no original one
if (!dataContainer.contains(StructuredDataKey.CUSTOM_NAME)) {
if (!dataContainer.has(StructuredDataKey.CUSTOM_NAME)) {
dataContainer.set(StructuredDataKey.CUSTOM_NAME, mappedItem.tagName());
tag.putBoolean(nbtTagName("added_custom_name"), true);
}
@ -114,11 +112,10 @@ public class BackwardsStructuredItemRewriter<C extends ClientboundPacketType, S
item.setIdentifier(mappingData.getOldItemId(item.identifier()));
}
final CompoundTag tag = customTag(item);
if (tag != null) {
final Tag originalId = tag.remove(nbtTagName("id"));
if (originalId instanceof IntTag) {
item.setIdentifier(((NumberTag) originalId).asInt());
final CompoundTag customData = dataContainer.get(StructuredDataKey.CUSTOM_DATA);
if (customData != null) {
if (customData.remove(nbtTagName("id")) instanceof final IntTag originalTag) {
item.setIdentifier(originalTag.asInt());
}
}
@ -127,11 +124,6 @@ public class BackwardsStructuredItemRewriter<C extends ClientboundPacketType, S
return item;
}
protected @Nullable CompoundTag customTag(final Item item) {
final StructuredData<CompoundTag> customData = item.dataContainer().getNonEmpty(StructuredDataKey.CUSTOM_DATA);
return customData != null ? customData.value() : null;
}
protected void saveListTag(CompoundTag tag, ListTag<?> original, String name) {
// Multiple places might try to backup data
String backupName = nbtTagName(name);

View File

@ -25,7 +25,6 @@ import com.viaversion.nbt.tag.Tag;
import com.viaversion.viabackwards.api.data.BackwardsMappingData;
import com.viaversion.viabackwards.utils.ChatUtil;
import com.viaversion.viaversion.api.data.Mappings;
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;
@ -66,23 +65,19 @@ public class StructuredEnchantmentRewriter {
public void handleToServer(final Item item) {
final StructuredDataContainer data = item.dataContainer();
final StructuredData<CompoundTag> customData = data.getNonEmpty(StructuredDataKey.CUSTOM_DATA);
if (customData == null) {
return;
final CompoundTag customData = data.get(StructuredDataKey.CUSTOM_DATA);
if (customData != null) {
rewriteEnchantmentsToServer(data, customData, StructuredDataKey.ENCHANTMENTS);
rewriteEnchantmentsToServer(data, customData, StructuredDataKey.STORED_ENCHANTMENTS);
}
final CompoundTag tag = customData.value();
rewriteEnchantmentsToServer(data, tag, StructuredDataKey.ENCHANTMENTS);
rewriteEnchantmentsToServer(data, tag, StructuredDataKey.STORED_ENCHANTMENTS);
}
public void rewriteEnchantmentsToClient(final StructuredDataContainer data, final StructuredDataKey<Enchantments> key, final IdRewriteFunction rewriteFunction, final DescriptionSupplier descriptionSupplier, final boolean storedEnchant) {
final StructuredData<Enchantments> enchantmentsData = data.getNonEmpty(key);
if (enchantmentsData == null || enchantmentsData.value().size() == 0) {
final Enchantments enchantments = data.get(key);
if (enchantments == null || enchantments.size() == 0) {
return;
}
final Enchantments enchantments = enchantmentsData.value();
final List<Tag> loreToAdd = new ArrayList<>();
boolean removedEnchantments = false;
boolean updatedLore = false;
@ -104,7 +99,7 @@ public class StructuredEnchantmentRewriter {
if (!removedEnchantments) {
// Backup original before doing modifications
final CompoundTag customData = data.computeIfAbsent(StructuredDataKey.CUSTOM_DATA, $ -> new CompoundTag()).value();
final CompoundTag customData = customData(data);
itemRewriter.saveListTag(customData, asTag(enchantments), key.identifier());
removedEnchantments = true;
}
@ -127,12 +122,12 @@ public class StructuredEnchantmentRewriter {
}
if (removedEnchantments) {
final CompoundTag tag = data.computeIfAbsent(StructuredDataKey.CUSTOM_DATA, $ -> new CompoundTag()).value();
final CompoundTag tag = customData(data);
if (!storedEnchant && enchantments.size() == 0) {
// Add glint override if there are no enchantments left
final StructuredData<Boolean> glintOverride = data.getNonEmpty(StructuredDataKey.ENCHANTMENT_GLINT_OVERRIDE);
final Boolean glintOverride = data.get(StructuredDataKey.ENCHANTMENT_GLINT_OVERRIDE);
if (glintOverride != null) {
tag.putBoolean(itemRewriter.nbtTagName("glint"), glintOverride.value());
tag.putBoolean(itemRewriter.nbtTagName("glint"), glintOverride);
} else {
tag.putBoolean(itemRewriter.nbtTagName("noglint"), true);
}
@ -146,10 +141,10 @@ public class StructuredEnchantmentRewriter {
if (updatedLore) {
// Save original lore
final CompoundTag tag = data.computeIfAbsent(StructuredDataKey.CUSTOM_DATA, $ -> new CompoundTag()).value();
final StructuredData<Tag[]> loreData = data.getNonEmpty(StructuredDataKey.LORE);
if (loreData != null) {
final List<Tag> loreList = Arrays.asList(loreData.value());
final CompoundTag tag = customData(data);
final Tag[] lore = data.get(StructuredDataKey.LORE);
if (lore != null) {
final List<Tag> loreList = Arrays.asList(lore);
itemRewriter.saveGenericTagList(tag, loreList, "lore");
loreToAdd.addAll(loreList);
} else {
@ -159,6 +154,15 @@ public class StructuredEnchantmentRewriter {
}
}
private CompoundTag customData(final StructuredDataContainer data) {
CompoundTag tag = data.get(StructuredDataKey.CUSTOM_DATA);
if (tag == null) {
tag = new CompoundTag();
data.set(StructuredDataKey.CUSTOM_DATA, tag);
}
return tag;
}
private ListTag<CompoundTag> asTag(final Enchantments enchantments) {
final ListTag<CompoundTag> listTag = new ListTag<>(CompoundTag.class);
for (final Int2IntMap.Entry entry : enchantments.enchantments().int2IntEntrySet()) {

View File

@ -30,7 +30,6 @@ import com.viaversion.viaversion.api.connection.UserConnection;
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;
@ -363,25 +362,25 @@ public final class BlockItemPacketRewriter1_20_5 extends BackwardsStructuredItem
// Text components since we skip the usual rewrite method
updateComponent(connection, item, StructuredDataKey.ITEM_NAME, "item_name");
updateComponent(connection, item, StructuredDataKey.CUSTOM_NAME, "custom_name");
final StructuredData<Tag[]> loreData = data.getNonEmpty(StructuredDataKey.LORE);
if (loreData != null) {
for (final Tag tag : loreData.value()) {
final Tag[] lore = data.get(StructuredDataKey.LORE);
if (lore != null) {
for (final Tag tag : lore) {
protocol.getComponentRewriter().processTag(connection, tag);
}
}
// In 1.20.6, some items have default values which are not written into the components
if (item.identifier() == 1105 && !data.contains(StructuredDataKey.FIREWORKS)) {
if (item.identifier() == 1105 && !data.has(StructuredDataKey.FIREWORKS)) {
data.set(StructuredDataKey.FIREWORKS, new Fireworks(1, new FireworkExplosion[0]));
}
final StructuredData<CompoundTag> customData = data.getNonEmpty(StructuredDataKey.CUSTOM_DATA);
final CompoundTag customData = data.get(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());
oldItem.tag().put(nbtTagName(), customData.copy());
}
if (oldItem.tag() != null && oldItem.tag().isEmpty()) {

View File

@ -28,7 +28,6 @@ import com.viaversion.viabackwards.protocol.v1_21to1_20_5.storage.EnchantmentsPa
import com.viaversion.viabackwards.protocol.v1_21to1_20_5.storage.OpenScreenStorage;
import com.viaversion.viabackwards.protocol.v1_21to1_20_5.storage.PlayerRotationStorage;
import com.viaversion.viaversion.api.connection.UserConnection;
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;
@ -195,7 +194,7 @@ public final class BlockItemPacketRewriter1_21 extends BackwardsStructuredItemRe
downgradeItemData(item);
final StructuredDataContainer dataContainer = item.dataContainer();
if (dataContainer.contains(StructuredDataKey.RARITY)) {
if (dataContainer.has(StructuredDataKey.RARITY)) {
return item;
}
@ -233,12 +232,11 @@ public final class BlockItemPacketRewriter1_21 extends BackwardsStructuredItemRe
}
private void rewriteEnchantmentToServer(final EnchantmentsPaintingsStorage storage, final Item item, final StructuredDataKey<Enchantments> key) {
final StructuredData<Enchantments> enchantmentsData = item.dataContainer().getNonEmpty(key);
if (enchantmentsData == null) {
final Enchantments enchantments = item.dataContainer().get(key);
if (enchantments == null) {
return;
}
final Enchantments enchantments = enchantmentsData.value();
final List<PendingIdChange> updatedIds = new ArrayList<>();
for (final Int2IntMap.Entry entry : enchantments.enchantments().int2IntEntrySet()) {
final int id = entry.getIntKey();