From 445d798a17b3e02c244194d9e2ec5567bac494ba Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Thu, 29 Feb 2024 20:21:18 +0100 Subject: [PATCH] Partially implement mapped item handling --- .../BackwardsStructuredItemRewriter.java | 129 ++++++++++++++++++ .../api/rewriters/ItemRewriter.java | 8 +- .../api/rewriters/StructuredItemRewriter.java | 79 ----------- .../packets/BlockItemPackets1_13.java | 7 +- .../BlockItemPacketRewriter1_20_5.java | 35 ++--- .../rewriter/EntityPacketRewriter1_20_5.java | 5 +- .../rewriter/BlockItemPacketRewriter1_99.java | 7 +- 7 files changed, 157 insertions(+), 113 deletions(-) create mode 100644 common/src/main/java/com/viaversion/viabackwards/api/rewriters/BackwardsStructuredItemRewriter.java delete mode 100644 common/src/main/java/com/viaversion/viabackwards/api/rewriters/StructuredItemRewriter.java diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/BackwardsStructuredItemRewriter.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/BackwardsStructuredItemRewriter.java new file mode 100644 index 00000000..710c80d0 --- /dev/null +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/BackwardsStructuredItemRewriter.java @@ -0,0 +1,129 @@ +/* + * This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.viaversion.viabackwards.api.rewriters; + +import com.viaversion.viabackwards.api.BackwardsProtocol; +import com.viaversion.viabackwards.api.data.BackwardsMappings; +import com.viaversion.viabackwards.api.data.MappedItem; +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.protocol.packet.ClientboundPacketType; +import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType; +import com.viaversion.viaversion.api.type.Type; +import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag; +import com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag; +import com.viaversion.viaversion.libs.opennbt.tag.builtin.NumberTag; +import com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag; +import org.checkerframework.checker.nullness.qual.Nullable; + +public class BackwardsStructuredItemRewriter> extends ItemRewriter { + + public BackwardsStructuredItemRewriter(final T protocol, final Type itemType, final Type itemArrayType) { + super(protocol, itemType, itemArrayType); + } + + public BackwardsStructuredItemRewriter(final T protocol, final Type itemType, final Type itemArrayType, final Type mappedItemType, final Type mappedItemArrayType) { + super(protocol, itemType, itemArrayType, mappedItemType, mappedItemArrayType); + } + + @Override + public @Nullable Item handleItemToClient(@Nullable final Item item) { + if (item == null) { + return null; + } + + final StructuredDataContainer data = item.structuredData(); + data.setIdLookup(protocol, true); + + // TODO Translatable rewriter on name and lore + + final BackwardsMappings mappingData = protocol.getMappingData(); + final MappedItem mappedItem = mappingData != null ? mappingData.getMappedItem(item.identifier()) : null; + if (mappedItem == null) { + // Just rewrite the id + if (mappingData != null && mappingData.getItemMappings() != null) { + item.setIdentifier(mappingData.getNewItemId(item.identifier())); + } + return item; + } + + // Save original id, set remapped id + final CompoundTag tag = createCustomTag(item); + tag.putInt(nbtTagName + "|id", item.identifier()); + item.setIdentifier(mappedItem.getId()); + + // Add custom model data + if (mappedItem.customModelData() != null && !data.contains(StructuredDataKey.CUSTOM_MODEL_DATA)) { + data.add(StructuredDataKey.CUSTOM_MODEL_DATA, mappedItem.customModelData()); + } + + // TODO custom name + return item; + } + + @Override + public @Nullable Item handleItemToServer(@Nullable final Item item) { + if (item == null) { + return null; + } + + final BackwardsMappings mappingData = protocol.getMappingData(); + if (mappingData != null && mappingData.getItemMappings() != null) { + item.setIdentifier(mappingData.getOldItemId(item.identifier())); + } + + final StructuredDataContainer data = item.structuredData(); + data.setIdLookup(protocol, false); + + final CompoundTag tag = customTag(item); + if (tag != null) { + final Tag originalId = tag.remove(nbtTagName + "|id"); + if (originalId instanceof IntTag) { + item.setIdentifier(((NumberTag) originalId).asInt()); + } + } + + restoreDisplayTag(item); + return item; + } + + protected @Nullable CompoundTag customTag(final Item item) { + final StructuredData customData = item.structuredData().getNonEmpty(StructuredDataKey.CUSTOM_DATA); + return customData != null ? customData.value() : null; + } + + protected CompoundTag createCustomTag(final Item item) { + final StructuredDataContainer data = item.structuredData(); + final StructuredData customData = data.getNonEmpty(StructuredDataKey.CUSTOM_DATA); + if (customData != null) { + return customData.value(); + } + + final CompoundTag tag = new CompoundTag(); + data.add(StructuredDataKey.CUSTOM_DATA, tag); + return tag; + } + + @Override + protected void restoreDisplayTag(final Item item) { + // TODO + } +} diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/ItemRewriter.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/ItemRewriter.java index a02898d1..b6e986d4 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/ItemRewriter.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/ItemRewriter.java @@ -155,7 +155,9 @@ public class ItemRewriter. - */ -package com.viaversion.viabackwards.api.rewriters; - -import com.viaversion.viabackwards.api.BackwardsProtocol; -import com.viaversion.viabackwards.api.data.MappedItem; -import com.viaversion.viaversion.api.minecraft.item.Item; -import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; -import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType; -import com.viaversion.viaversion.api.type.Type; -import org.checkerframework.checker.nullness.qual.Nullable; - -public class StructuredItemRewriter> extends ItemRewriter { - - public StructuredItemRewriter(T protocol, Type itemType, Type itemArrayType) { - super(protocol, itemType, itemArrayType); - } - - public StructuredItemRewriter(T protocol, Type itemType, Type itemArrayType, Type mappedItemType, Type mappedItemArrayType) { - super(protocol, itemType, itemArrayType, mappedItemType, mappedItemArrayType); - } - - @Override - public @Nullable Item handleItemToClient(@Nullable Item item) { - if (item == null) { - return null; - } - - // TODO Translatable rewriter on name and lore - - MappedItem data = protocol.getMappingData() != null ? protocol.getMappingData().getMappedItem(item.identifier()) : null; - if (data == null) { - // Just rewrite the id - if (protocol.getMappingData() != null && protocol.getMappingData().getItemMappings() != null) { - item.setIdentifier(protocol.getMappingData().getNewItemId(item.identifier())); - } - } - - // TODO Save original id, set remapped id - if (protocol.getMappingData() != null && protocol.getMappingData().getItemMappings() != null) { - item.setIdentifier(protocol.getMappingData().getNewItemId(item.identifier())); - } - // TODO custom model data and custom name - return item; - } - - @Override - public @Nullable Item handleItemToServer(@Nullable Item item) { - if (item == null) return null; - - if (protocol.getMappingData() != null && protocol.getMappingData().getItemMappings() != null) { - item.setIdentifier(protocol.getMappingData().getOldItemId(item.identifier())); - } - - // TODO restore original id and display - return item; - } - - @Override - protected void restoreDisplayTag(final Item item) { - // TODO - } -} diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/BlockItemPackets1_13.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/BlockItemPackets1_13.java index 0f822ac2..da24f5d9 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/BlockItemPackets1_13.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/BlockItemPackets1_13.java @@ -256,7 +256,12 @@ public class BlockItemPackets1_13 extends com.viaversion.viabackwards.api.rewrit map(Type.UNSIGNED_BYTE); map(Type.ITEM1_13_SHORT_ARRAY, Type.ITEM1_8_SHORT_ARRAY); - handler(itemArrayToClientHandler(Type.ITEM1_8_SHORT_ARRAY)); + handler(wrapper -> { + final Item[] items = wrapper.get(Type.ITEM1_8_SHORT_ARRAY, 0); + for (Item item : items) { + handleItemToClient(item); + } + }); } }); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/rewriter/BlockItemPacketRewriter1_20_5.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/rewriter/BlockItemPacketRewriter1_20_5.java index f7b40484..8dbc653b 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/rewriter/BlockItemPacketRewriter1_20_5.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/rewriter/BlockItemPacketRewriter1_20_5.java @@ -17,9 +17,9 @@ */ package com.viaversion.viabackwards.protocol.protocol1_20_3to1_20_5.rewriter; -import com.viaversion.viabackwards.api.rewriters.ItemRewriter; -import com.viaversion.viabackwards.api.rewriters.StructuredItemRewriter; +import com.viaversion.viabackwards.api.rewriters.BackwardsStructuredItemRewriter; import com.viaversion.viabackwards.protocol.protocol1_20_3to1_20_5.Protocol1_20_3To1_20_5; +import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.minecraft.Particle; import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.type.Type; @@ -29,13 +29,16 @@ import com.viaversion.viaversion.api.type.types.version.Types1_20_5; import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.packet.ServerboundPacket1_20_3; import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.packet.ServerboundPackets1_20_3; import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.rewriter.RecipeRewriter1_20_3; +import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.Protocol1_20_5To1_20_3; import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.packet.ClientboundPacket1_20_5; import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.packet.ClientboundPackets1_20_5; import com.viaversion.viaversion.rewriter.BlockRewriter; import com.viaversion.viaversion.util.Key; import org.checkerframework.checker.nullness.qual.Nullable; -public final class BlockItemPacketRewriter1_20_5 extends StructuredItemRewriter { +public final class BlockItemPacketRewriter1_20_5 extends BackwardsStructuredItemRewriter { + + private final Protocol1_20_5To1_20_3 vvProtocol = Via.getManager().getProtocolManager().getProtocol(Protocol1_20_5To1_20_3.class); public BlockItemPacketRewriter1_20_5(final Protocol1_20_3To1_20_5 protocol) { super(protocol, Types1_20_5.ITEM, Types1_20_5.ITEM_ARRAY, Type.ITEM1_20_2, Type.ITEM1_20_2_ARRAY); @@ -135,27 +138,7 @@ public final class BlockItemPacketRewriter1_20_5 extends StructuredItemRewriter< } }); - final RecipeRewriter1_20_3 recipeRewriter = new RecipeRewriter1_20_3(protocol) { - @Override - protected Type itemType() { - return Types1_20_5.ITEM; - } - - @Override - protected Type itemArrayType() { - return Types1_20_5.ITEM_ARRAY; - } - - @Override - protected Type mappedItemType() { - return Type.ITEM1_20_2; - } - - @Override - protected Type mappedItemArrayType() { - return Type.ITEM1_20_2_ARRAY; - } - }; + final RecipeRewriter1_20_3 recipeRewriter = new RecipeRewriter1_20_3<>(protocol); protocol.registerClientbound(ClientboundPackets1_20_5.DECLARE_RECIPES, wrapper -> { final int size = wrapper.passthrough(Type.VAR_INT); for (int i = 0; i < size; i++) { @@ -175,7 +158,7 @@ public final class BlockItemPacketRewriter1_20_5 extends StructuredItemRewriter< if (item == null) return null; super.handleItemToClient(item); - return com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.rewriter.BlockItemPacketRewriter1_20_5.toOldItem(item); + return vvProtocol.getItemRewriter().toOldItem(item); } @Override @@ -183,7 +166,7 @@ public final class BlockItemPacketRewriter1_20_5 extends StructuredItemRewriter< if (item == null) return null; // Convert to structured item first - final Item structuredItem = com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.rewriter.BlockItemPacketRewriter1_20_5.toStructuredItem(item); + final Item structuredItem = vvProtocol.getItemRewriter().toStructuredItem(item); return super.handleItemToServer(structuredItem); } } \ No newline at end of file diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/rewriter/EntityPacketRewriter1_20_5.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/rewriter/EntityPacketRewriter1_20_5.java index bfdf2df5..b35ea084 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/rewriter/EntityPacketRewriter1_20_5.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/rewriter/EntityPacketRewriter1_20_5.java @@ -26,6 +26,7 @@ import com.viaversion.viaversion.api.data.entity.DimensionData; import com.viaversion.viaversion.api.minecraft.RegistryEntry; import com.viaversion.viaversion.api.minecraft.entities.EntityType; import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_20_5; +import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.version.Types1_20_3; @@ -67,7 +68,9 @@ public final class EntityPacketRewriter1_20_5 extends EntityRewriter { +public final class BlockItemPacketRewriter1_99 extends BackwardsStructuredItemRewriter { public BlockItemPacketRewriter1_99(final Protocol1_98To1_99 protocol) { - super(protocol, Type.ITEM1_20_2, Type.ITEM1_20_2_ARRAY); + super(protocol, /*old types*/Types1_20_5.ITEM, Types1_20_5.ITEM_ARRAY); } @Override