|
| 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.api.rewriters; |
| 19 | + |
| 20 | +import com.viaversion.viabackwards.api.BackwardsProtocol; |
| 21 | +import com.viaversion.viabackwards.api.data.BackwardsMappings; |
| 22 | +import com.viaversion.viabackwards.api.data.MappedItem; |
| 23 | +import com.viaversion.viaversion.api.minecraft.data.StructuredData; |
| 24 | +import com.viaversion.viaversion.api.minecraft.data.StructuredDataContainer; |
| 25 | +import com.viaversion.viaversion.api.minecraft.data.StructuredDataKey; |
| 26 | +import com.viaversion.viaversion.api.minecraft.item.Item; |
| 27 | +import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; |
| 28 | +import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType; |
| 29 | +import com.viaversion.viaversion.api.type.Type; |
| 30 | +import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag; |
| 31 | +import com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag; |
| 32 | +import com.viaversion.viaversion.libs.opennbt.tag.builtin.NumberTag; |
| 33 | +import com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag; |
| 34 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 35 | + |
| 36 | +public class BackwardsStructuredItemRewriter<C extends ClientboundPacketType, S extends ServerboundPacketType, |
| 37 | + T extends BackwardsProtocol<C, ?, ?, S>> extends ItemRewriter<C, S, T> { |
| 38 | + |
| 39 | + public BackwardsStructuredItemRewriter(final T protocol, final Type<Item> itemType, final Type<Item[]> itemArrayType) { |
| 40 | + super(protocol, itemType, itemArrayType); |
| 41 | + } |
| 42 | + |
| 43 | + public BackwardsStructuredItemRewriter(final T protocol, final Type<Item> itemType, final Type<Item[]> itemArrayType, final Type<Item> mappedItemType, final Type<Item[]> mappedItemArrayType) { |
| 44 | + super(protocol, itemType, itemArrayType, mappedItemType, mappedItemArrayType); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public @Nullable Item handleItemToClient(@Nullable final Item item) { |
| 49 | + if (item == null) { |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + final StructuredDataContainer data = item.structuredData(); |
| 54 | + data.setIdLookup(protocol, true); |
| 55 | + |
| 56 | + // TODO Translatable rewriter on name and lore |
| 57 | + |
| 58 | + final BackwardsMappings mappingData = protocol.getMappingData(); |
| 59 | + final MappedItem mappedItem = mappingData != null ? mappingData.getMappedItem(item.identifier()) : null; |
| 60 | + if (mappedItem == null) { |
| 61 | + // Just rewrite the id |
| 62 | + if (mappingData != null && mappingData.getItemMappings() != null) { |
| 63 | + item.setIdentifier(mappingData.getNewItemId(item.identifier())); |
| 64 | + } |
| 65 | + return item; |
| 66 | + } |
| 67 | + |
| 68 | + // Save original id, set remapped id |
| 69 | + final CompoundTag tag = createCustomTag(item); |
| 70 | + tag.putInt(nbtTagName + "|id", item.identifier()); |
| 71 | + item.setIdentifier(mappedItem.getId()); |
| 72 | + |
| 73 | + // Add custom model data |
| 74 | + if (mappedItem.customModelData() != null && !data.contains(StructuredDataKey.CUSTOM_MODEL_DATA)) { |
| 75 | + data.add(StructuredDataKey.CUSTOM_MODEL_DATA, mappedItem.customModelData()); |
| 76 | + } |
| 77 | + |
| 78 | + // TODO custom name |
| 79 | + return item; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public @Nullable Item handleItemToServer(@Nullable final Item item) { |
| 84 | + if (item == null) { |
| 85 | + return null; |
| 86 | + } |
| 87 | + |
| 88 | + final BackwardsMappings mappingData = protocol.getMappingData(); |
| 89 | + if (mappingData != null && mappingData.getItemMappings() != null) { |
| 90 | + item.setIdentifier(mappingData.getOldItemId(item.identifier())); |
| 91 | + } |
| 92 | + |
| 93 | + final StructuredDataContainer data = item.structuredData(); |
| 94 | + data.setIdLookup(protocol, false); |
| 95 | + |
| 96 | + final CompoundTag tag = customTag(item); |
| 97 | + if (tag != null) { |
| 98 | + final Tag originalId = tag.remove(nbtTagName + "|id"); |
| 99 | + if (originalId instanceof IntTag) { |
| 100 | + item.setIdentifier(((NumberTag) originalId).asInt()); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + restoreDisplayTag(item); |
| 105 | + return item; |
| 106 | + } |
| 107 | + |
| 108 | + protected @Nullable CompoundTag customTag(final Item item) { |
| 109 | + final StructuredData<CompoundTag> customData = item.structuredData().getNonEmpty(StructuredDataKey.CUSTOM_DATA); |
| 110 | + return customData != null ? customData.value() : null; |
| 111 | + } |
| 112 | + |
| 113 | + protected CompoundTag createCustomTag(final Item item) { |
| 114 | + final StructuredDataContainer data = item.structuredData(); |
| 115 | + final StructuredData<CompoundTag> customData = data.getNonEmpty(StructuredDataKey.CUSTOM_DATA); |
| 116 | + if (customData != null) { |
| 117 | + return customData.value(); |
| 118 | + } |
| 119 | + |
| 120 | + final CompoundTag tag = new CompoundTag(); |
| 121 | + data.add(StructuredDataKey.CUSTOM_DATA, tag); |
| 122 | + return tag; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + protected void restoreDisplayTag(final Item item) { |
| 127 | + // TODO |
| 128 | + } |
| 129 | +} |
0 commit comments