Rewrite lock nbt

This commit is contained in:
Nassim Jahnke 2024-09-29 13:16:12 +02:00
parent ce655d0679
commit 25b521dfcf
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
4 changed files with 29 additions and 7 deletions

View File

@ -17,13 +17,13 @@
*/
package com.viaversion.viaversion.protocols.v1_21to1_21_2.rewriter;
import com.viaversion.nbt.tag.CompoundTag;
import com.viaversion.nbt.tag.StringTag;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.data.MappingData;
import com.viaversion.viaversion.api.minecraft.Holder;
import com.viaversion.viaversion.api.minecraft.HolderSet;
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;
@ -49,6 +49,7 @@ import com.viaversion.viaversion.rewriter.BlockRewriter;
import com.viaversion.viaversion.rewriter.SoundRewriter;
import com.viaversion.viaversion.rewriter.StructuredItemRewriter;
import com.viaversion.viaversion.util.Key;
import com.viaversion.viaversion.util.TagUtil;
import com.viaversion.viaversion.util.Unit;
public final class BlockItemPacketRewriter1_21_2 extends StructuredItemRewriter<ClientboundPacket1_21, ServerboundPacket1_21_2, Protocol1_21To1_21_2> {
@ -286,10 +287,25 @@ public final class BlockItemPacketRewriter1_21_2 extends StructuredItemRewriter<
dataContainer.replaceKey(StructuredDataKey.BUNDLE_CONTENTS1_21, StructuredDataKey.BUNDLE_CONTENTS1_21_2);
dataContainer.replaceKey(StructuredDataKey.POTION_CONTENTS1_20_5, StructuredDataKey.POTION_CONTENTS1_21_2);
dataContainer.replace(StructuredDataKey.FIRE_RESISTANT, StructuredDataKey.DAMAGE_RESISTANT, fireResistant -> new DamageResistant("minecraft:is_fire"));
dataContainer.replace(StructuredDataKey.LOCK, lock -> {
final CompoundTag predicateTag = new CompoundTag();
final CompoundTag itemComponentsTag = new CompoundTag();
predicateTag.put("components", itemComponentsTag);
itemComponentsTag.put("custom_name", lock);
return predicateTag;
});
}
public static void downgradeItemData(final Item item) {
final StructuredDataContainer dataContainer = item.dataContainer();
dataContainer.replace(StructuredDataKey.LOCK, StructuredDataKey.LOCK, lock -> {
final CompoundTag predicateTag = (CompoundTag) lock;
final CompoundTag itemComponentsTag = predicateTag.getCompoundTag("components");
if (itemComponentsTag != null) {
return TagUtil.getNamespacedStringTag(itemComponentsTag, "custom_name");
}
return null;
});
dataContainer.replace(StructuredDataKey.INSTRUMENT1_21_2, StructuredDataKey.INSTRUMENT1_20_5, instrument -> {
if (instrument.hasId()) {
return Holder.of(instrument.id());
@ -298,11 +314,10 @@ public final class BlockItemPacketRewriter1_21_2 extends StructuredItemRewriter<
return Holder.of(new Instrument1_20_5(value.soundEvent(), (int) (value.useDuration() * 20), value.range()));
});
dataContainer.replace(StructuredDataKey.FOOD1_21_2, StructuredDataKey.FOOD1_21, food -> {
final StructuredData<Consumable1_21_2> consumableData = dataContainer.getNonEmpty(StructuredDataKey.CONSUMABLE1_21_2);
final StructuredData<Item> useRemainderData = dataContainer.getNonEmpty(StructuredDataKey.USE_REMAINDER);
final Item usingConvertsTo = useRemainderData != null ? useRemainderData.value() : null;
final float eatSeconds = consumableData != null ? consumableData.value().consumeSeconds() : 1.6F;
return new FoodProperties1_20_5(food.nutrition(), food.saturationModifier(), food.canAlwaysEat(), eatSeconds, usingConvertsTo, new FoodProperties1_20_5.FoodEffect[0]);
final Consumable1_21_2 consumableData = dataContainer.get(StructuredDataKey.CONSUMABLE1_21_2);
final Item useRemainderData = dataContainer.get(StructuredDataKey.USE_REMAINDER);
final float eatSeconds = consumableData != null ? consumableData.consumeSeconds() : 1.6F;
return new FoodProperties1_20_5(food.nutrition(), food.saturationModifier(), food.canAlwaysEat(), eatSeconds, useRemainderData, new FoodProperties1_20_5.FoodEffect[0]);
});
dataContainer.replaceKey(StructuredDataKey.CONTAINER1_21_2, StructuredDataKey.CONTAINER1_21);
dataContainer.replaceKey(StructuredDataKey.CHARGED_PROJECTILES1_21_2, StructuredDataKey.CHARGED_PROJECTILES1_21);

View File

@ -61,6 +61,7 @@ public final class ComponentRewriter1_21_2 extends ComponentRewriter<Clientbound
}
TagUtil.removeNamespaced(componentsTag, "fire_resistant");
TagUtil.removeNamespaced(componentsTag, "lock");
}
public static void convertAttributes(final CompoundTag componentsTag, final FullMappings mappings) {

View File

@ -123,7 +123,7 @@ public class StructuredItemRewriter<C extends ClientboundPacketType, S extends S
}
if (mappingData.getFullItemMappings() != null) {
final Function<String, String> itemIdRewriter = clientbound ? id -> mappedIdentifier(mappingData.getFullItemMappings(), id) : id -> unmappedIdentifier(mappingData.getFullItemMappings(), id);
container.updateIfPresent(StructuredDataKey.USE_COOLDOWN, value -> value.rewrite(itemIdRewriter));
container.replace(StructuredDataKey.USE_COOLDOWN, value -> value.rewrite(itemIdRewriter));
}
if (mappingData.getBlockMappings() != null) {
final Int2IntFunction blockIdRewriter = clientbound ? mappingData::getNewBlockId : mappingData::getOldBlockId;

View File

@ -19,6 +19,7 @@ package com.viaversion.viaversion.util;
import com.viaversion.nbt.tag.CompoundTag;
import com.viaversion.nbt.tag.ListTag;
import com.viaversion.nbt.tag.StringTag;
import com.viaversion.nbt.tag.Tag;
import java.util.Map;
import org.checkerframework.checker.nullness.qual.Nullable;
@ -69,6 +70,11 @@ public final class TagUtil {
return listTag != null ? listTag : tag.getListTag(Key.stripMinecraftNamespace(key), CompoundTag.class);
}
public static @Nullable StringTag getNamespacedStringTag(final CompoundTag tag, final String key) {
final StringTag stringTag = tag.getStringTag(Key.namespaced(key));
return stringTag != null ? stringTag : tag.getStringTag(Key.stripMinecraftNamespace(key));
}
public static Tag handleDeep(final Tag tag, final TagUpdater consumer) {
return handleDeep(null, tag, consumer);
}