Handle item tags in recipe ingredients

This commit is contained in:
Nassim Jahnke 2024-08-25 12:03:56 +02:00
parent ff94019b08
commit 4bcc213f1b
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
3 changed files with 79 additions and 4 deletions

View File

@ -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<ClientboundPac
protected void registerPackets() {
super.registerPackets();
tagRewriter.registerGeneric(ClientboundPackets1_21_2.UPDATE_TAGS);
tagRewriter.registerGeneric(ClientboundConfigurationPackets1_21.UPDATE_TAGS);
registerClientbound(ClientboundPackets1_21_2.UPDATE_TAGS, this::storeTags);
registerClientbound(ClientboundConfigurationPackets1_21.UPDATE_TAGS, this::storeTags);
final SoundRewriter<ClientboundPacket1_21_2> 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<ClientboundPac
cancelClientbound(ClientboundPackets1_21_2.MOVE_MINECART_ALONG_TRACK); // TODO
}
private void storeTags(final PacketWrapper wrapper) {
tagRewriter.getGenericHandler().handle(wrapper);
wrapper.resetReader();
wrapper.user().get(ItemTagStorage.class).readItemTags(wrapper);
}
private void clientInformation(final PacketWrapper wrapper) {
wrapper.passthrough(Types.STRING); // Locale
wrapper.passthrough(Types.BYTE); // View distance
@ -110,6 +117,7 @@ public final class Protocol1_21_2To1_21 extends BackwardsProtocol<ClientboundPac
public void init(final UserConnection user) {
addEntityTracker(user, new EntityTrackerBase(user, EntityTypes1_20_5.PLAYER));
user.put(new InventoryStateIdStorage());
user.put(new ItemTagStorage());
}
@Override

View File

@ -20,6 +20,7 @@ package com.viaversion.viabackwards.protocol.v1_21_2to1_21.rewriter;
import com.viaversion.viabackwards.api.rewriters.BackwardsStructuredItemRewriter;
import com.viaversion.viabackwards.protocol.v1_21_2to1_21.Protocol1_21_2To1_21;
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.data.MappingData;
import com.viaversion.viaversion.api.minecraft.HolderSet;
@ -223,8 +224,18 @@ public final class BlockItemPacketRewriter1_21_2 extends BackwardsStructuredItem
private Item[] ingredient(final PacketWrapper wrapper) {
final HolderSet ingredient = wrapper.read(Types.HOLDER_SET).rewrite(id -> 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();

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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<String, int[]> 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);
}
}
}
}
}