diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ArmorTrim.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ArmorTrim.java index 225e0bb67..55c9e20f4 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ArmorTrim.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ArmorTrim.java @@ -45,4 +45,16 @@ public record ArmorTrim(Holder material, Holder material = this.material; + if (material.isDirect()) { + material = Holder.of(material.value().rewrite(idRewriteFunction)); + } + + Holder pattern = this.pattern; + if (pattern.isDirect()) { + pattern = Holder.of(pattern.value().rewrite(idRewriteFunction)); + } + return new ArmorTrim(material, pattern, showInTooltip); + } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ArmorTrimMaterial.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ArmorTrimMaterial.java index d26fbf000..fad4ddc8a 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ArmorTrimMaterial.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ArmorTrimMaterial.java @@ -67,4 +67,7 @@ public record ArmorTrimMaterial(String assetName, int itemId, float itemModelInd } }; + public ArmorTrimMaterial rewrite(final Int2IntFunction idRewriteFunction) { + return new ArmorTrimMaterial(assetName, idRewriteFunction.applyAsInt(itemId), itemModelIndex, overrideArmorMaterials, description); + } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ArmorTrimPattern.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ArmorTrimPattern.java index 0a5e658a3..aaa61fa1c 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ArmorTrimPattern.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ArmorTrimPattern.java @@ -48,4 +48,7 @@ public record ArmorTrimPattern(String assetName, int itemId, Tag description, bo } }; + public ArmorTrimPattern rewrite(final Int2IntFunction idRewriteFunction) { + return new ArmorTrimPattern(assetName, idRewriteFunction.applyAsInt(itemId), description, decal); + } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/PotDecorations.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/PotDecorations.java index 62ec39983..3ee279c1a 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/PotDecorations.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/PotDecorations.java @@ -22,9 +22,11 @@ */ package com.viaversion.viaversion.api.minecraft.item.data; +import com.viaversion.viaversion.api.data.MappingData; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.Types; import io.netty.buffer.ByteBuf; +import it.unimi.dsi.fastutil.ints.Int2IntFunction; public final class PotDecorations { @@ -73,4 +75,12 @@ public final class PotDecorations { private int item(final int index) { return index < 0 || index >= itemIds.length ? -1 : itemIds[index]; } + + public PotDecorations rewrite(final Int2IntFunction idRewriteFunction) { + final int[] newItems = new int[itemIds.length]; + for (int i = 0; i < itemIds.length; i++) { + newItems[i] = idRewriteFunction.applyAsInt(itemIds[i]); + } + return new PotDecorations(newItems); + } } \ No newline at end of file diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_21to1_20_5/rewriter/EntityPacketRewriter1_21.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_21to1_20_5/rewriter/EntityPacketRewriter1_21.java index 6a2522ad0..d239de512 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_21to1_20_5/rewriter/EntityPacketRewriter1_21.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_21to1_20_5/rewriter/EntityPacketRewriter1_21.java @@ -50,7 +50,7 @@ public final class EntityPacketRewriter1_21 extends EntityRewriter { final String type = Key.stripMinecraftNamespace(wrapper.passthrough(Type.STRING)); final RegistryEntry[] entries = wrapper.passthrough(Type.REGISTRY_ENTRY_ARRAY); - if (Key.stripMinecraftNamespace(type).equals("damage_type")) { + if (type.equals("damage_type")) { // Add required damage type final CompoundTag campfireDamageType = new CompoundTag(); campfireDamageType.putString("scaling", "when_caused_by_living_non_player"); diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/StructuredItemRewriter.java b/common/src/main/java/com/viaversion/viaversion/rewriter/StructuredItemRewriter.java index 735817a3c..d2cf64024 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/StructuredItemRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/StructuredItemRewriter.java @@ -19,11 +19,18 @@ package com.viaversion.viaversion.rewriter; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.data.MappingData; +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; +import com.viaversion.viaversion.api.minecraft.item.data.ArmorTrim; +import com.viaversion.viaversion.api.minecraft.item.data.PotDecorations; import com.viaversion.viaversion.api.protocol.Protocol; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType; import com.viaversion.viaversion.api.type.Type; +import it.unimi.dsi.fastutil.ints.Int2IntFunction; +import java.util.Map; public class StructuredItemRewriter> extends ItemRewriter { @@ -51,6 +58,8 @@ public class StructuredItemRewriter trimData = container.getNonEmpty(StructuredDataKey.TRIM); + if (trimData != null && idRewriter != null) { + trimData.setValue(trimData.value().rewrite(idRewriter)); + } + + final StructuredData potDecorationsData = container.getNonEmpty(StructuredDataKey.POT_DECORATIONS); + if (potDecorationsData != null && idRewriter != null) { + potDecorationsData.setValue(potDecorationsData.value().rewrite(idRewriter)); + } + + // Look for item types + for (final Map.Entry, StructuredData> entry : container.data().entrySet()) { + final StructuredData data = entry.getValue(); + if (data.isEmpty()) { + continue; + } + + final StructuredDataKey key = entry.getKey(); + final Class outputClass = key.type().getOutputClass(); + if (outputClass == Item.class) { + //noinspection unchecked + final StructuredData itemData = (StructuredData) data; + itemData.setValue(itemHandler.rewrite(connection, itemData.value())); + } else if (outputClass == Item[].class) { + //noinspection unchecked + final StructuredData itemArrayData = (StructuredData) data; + final Item[] items = itemArrayData.value(); + for (int i = 0; i < items.length; i++) { + items[i] = itemHandler.rewrite(connection, items[i]); + } + } + } + } + + @FunctionalInterface + public interface ItemHandler { + + @Nullable + Item rewrite(UserConnection connection, @Nullable Item item); + } }