|
| 1 | +/* |
| 2 | + * This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards |
| 3 | + * Copyright (C) 2016-2024 ViaVersion and contributors |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | +package com.viaversion.viabackwards.protocol.v1_20_5to1_20_3.rewriter; |
| 19 | + |
| 20 | +import com.viaversion.nbt.tag.CompoundTag; |
| 21 | +import com.viaversion.nbt.tag.StringTag; |
| 22 | +import com.viaversion.viabackwards.api.BackwardsProtocol; |
| 23 | +import com.viaversion.viabackwards.api.rewriters.text.JsonNBTComponentRewriter; |
| 24 | +import com.viaversion.viaversion.api.connection.UserConnection; |
| 25 | +import com.viaversion.viaversion.api.minecraft.data.StructuredData; |
| 26 | +import com.viaversion.viaversion.api.minecraft.data.StructuredDataContainer; |
| 27 | +import com.viaversion.viaversion.api.minecraft.item.Item; |
| 28 | +import com.viaversion.viaversion.api.minecraft.item.StructuredItem; |
| 29 | +import com.viaversion.viaversion.api.type.types.version.Types1_20_5; |
| 30 | +import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.packet.ClientboundPacket1_20_5; |
| 31 | +import com.viaversion.viaversion.util.SerializerVersion; |
| 32 | +import java.util.List; |
| 33 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 34 | + |
| 35 | +public final class ComponentRewriter1_20_5 extends JsonNBTComponentRewriter<ClientboundPacket1_20_5> { |
| 36 | + |
| 37 | + private final com.viaversion.viaversion.protocols.v1_20_3to1_20_5.rewriter.ComponentRewriter1_20_5<ClientboundPacket1_20_5> vvRewriter; |
| 38 | + |
| 39 | + public ComponentRewriter1_20_5(final BackwardsProtocol<ClientboundPacket1_20_5, ?, ?, ?> protocol) { |
| 40 | + super(protocol, ReadType.NBT); |
| 41 | + vvRewriter = new com.viaversion.viaversion.protocols.v1_20_3to1_20_5.rewriter.ComponentRewriter1_20_5<>(protocol, Types1_20_5.STRUCTURED_DATA); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + protected void handleShowItem(final UserConnection connection, final CompoundTag itemTag, @Nullable final CompoundTag componentsTag) { |
| 46 | + super.handleShowItem(connection, itemTag, componentsTag); |
| 47 | + if (componentsTag == null) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + final StringTag idTag = itemTag.getStringTag("id"); |
| 52 | + if (idTag == null) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + final List<StructuredData<?>> data = vvRewriter.toData(connection, componentsTag); |
| 57 | + if (data.isEmpty()) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + final int identifier = this.protocol.getMappingData().getFullItemMappings().id(idTag.getValue()); |
| 62 | + |
| 63 | + final StructuredItem structuredItem = new StructuredItem(identifier, 1, new StructuredDataContainer(data.toArray(StructuredData[]::new))); |
| 64 | + final Item dataItem = protocol.getItemRewriter().handleItemToClient(connection, structuredItem); |
| 65 | + if (dataItem.tag() == null) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + itemTag.remove("components"); |
| 70 | + itemTag.put("tag", new StringTag(outputSerializerVersion().toSNBT(dataItem.tag()))); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + protected SerializerVersion inputSerializerVersion() { |
| 75 | + return SerializerVersion.V1_20_5; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + protected SerializerVersion outputSerializerVersion() { |
| 80 | + return SerializerVersion.V1_20_3; |
| 81 | + } |
| 82 | +} |
0 commit comments