From 4bcc213f1b69793d998db3d0b87951786e20bfd3 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Sun, 25 Aug 2024 12:03:56 +0200 Subject: [PATCH] Handle item tags in recipe ingredients --- .../v1_21_2to1_21/Protocol1_21_2To1_21.java | 12 +++- .../BlockItemPacketRewriter1_21_2.java | 15 ++++- .../v1_21_2to1_21/storage/ItemTagStorage.java | 56 +++++++++++++++++++ 3 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/storage/ItemTagStorage.java diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/Protocol1_21_2To1_21.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/Protocol1_21_2To1_21.java index 4ca0d38e..2b8ae84b 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/Protocol1_21_2To1_21.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/Protocol1_21_2To1_21.java @@ -24,6 +24,7 @@ import com.viaversion.viabackwards.api.rewriters.TranslatableRewriter; import com.viaversion.viabackwards.protocol.v1_21_2to1_21.rewriter.BlockItemPacketRewriter1_21_2; import com.viaversion.viabackwards.protocol.v1_21_2to1_21.rewriter.EntityPacketRewriter1_21_2; import com.viaversion.viabackwards.protocol.v1_21_2to1_21.storage.InventoryStateIdStorage; +import com.viaversion.viabackwards.protocol.v1_21_2to1_21.storage.ItemTagStorage; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_20_5; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; @@ -65,8 +66,8 @@ public final class Protocol1_21_2To1_21 extends BackwardsProtocol soundRewriter = new SoundRewriter<>(this); soundRewriter.registerSound1_19_3(ClientboundPackets1_21_2.SOUND); @@ -94,6 +95,12 @@ public final class Protocol1_21_2To1_21 extends BackwardsProtocol protocol.getMappingData().getNewItemId(id)); if (ingredient.hasTagKey()) { - // TODO - return new Item[]{new StructuredItem(1, 1)}; + final ItemTagStorage tagStorage = wrapper.user().get(ItemTagStorage.class); + final int[] tagEntries = tagStorage.itemTag(ingredient.tagKey()); + if (tagEntries == null || tagEntries.length == 0) { + // Most cannot be empty; add a dummy ingredient, though this would only come from bad data + return new Item[]{new StructuredItem(1, 1)}; + } + + final Item[] items = new Item[tagEntries.length]; + for (int i = 0; i < tagEntries.length; i++) { + items[i] = new StructuredItem(tagEntries[i], 1); + } + return items; } final int[] ids = ingredient.ids(); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/storage/ItemTagStorage.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/storage/ItemTagStorage.java new file mode 100644 index 00000000..fcc6de84 --- /dev/null +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/storage/ItemTagStorage.java @@ -0,0 +1,56 @@ +/* + * 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.protocol.v1_21_2to1_21.storage; + +import com.viaversion.viaversion.api.connection.StorableObject; +import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; +import com.viaversion.viaversion.api.type.Types; +import com.viaversion.viaversion.util.Key; +import java.util.HashMap; +import java.util.Map; +import org.checkerframework.checker.nullness.qual.Nullable; + +public final class ItemTagStorage implements StorableObject { + + private Map itemTags = new HashMap<>(); + + public int @Nullable [] itemTag(final String key) { + return itemTags.get(Key.stripMinecraftNamespace(key)); + } + + public void readItemTags(final PacketWrapper wrapper) { + final int length = wrapper.passthrough(Types.VAR_INT); + for (int i = 0; i < length; i++) { + final String registryKey = wrapper.passthrough(Types.STRING); + final int tagsSize = wrapper.passthrough(Types.VAR_INT); + + final boolean itemRegistry = Key.stripMinecraftNamespace(registryKey).equals("item"); + if (itemRegistry) { + this.itemTags = new HashMap<>(tagsSize); + } + + for (int j = 0; j < tagsSize; j++) { + final String key = wrapper.passthrough(Types.STRING); + final int[] ids = wrapper.passthrough(Types.VAR_INT_ARRAY_PRIMITIVE); + if (itemRegistry) { + this.itemTags.put(Key.stripMinecraftNamespace(key), ids); + } + } + } + } +}