Use recipe inputs from old recipe packet

This commit is contained in:
Nassim Jahnke 2024-10-14 14:35:39 +02:00
parent cff2303c62
commit 84bd1d0782
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
5 changed files with 47 additions and 83 deletions

View File

@ -18,6 +18,8 @@
package com.viaversion.viaversion.protocols.v1_21to1_21_2;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.data.MappingData;
import com.viaversion.viaversion.api.data.MappingDataBase;
import com.viaversion.viaversion.api.minecraft.data.StructuredDataKey;
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_21_2;
import com.viaversion.viaversion.api.protocol.AbstractProtocol;
@ -37,7 +39,6 @@ import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.packet.ServerboundPac
import com.viaversion.viaversion.protocols.v1_20_5to1_21.packet.ClientboundConfigurationPackets1_21;
import com.viaversion.viaversion.protocols.v1_20_5to1_21.packet.ClientboundPacket1_21;
import com.viaversion.viaversion.protocols.v1_20_5to1_21.packet.ClientboundPackets1_21;
import com.viaversion.viaversion.protocols.v1_21to1_21_2.data.MappingData1_21_2;
import com.viaversion.viaversion.protocols.v1_21to1_21_2.packet.ClientboundPacket1_21_2;
import com.viaversion.viaversion.protocols.v1_21to1_21_2.packet.ClientboundPackets1_21_2;
import com.viaversion.viaversion.protocols.v1_21to1_21_2.packet.ServerboundPacket1_21_2;
@ -54,7 +55,7 @@ import static com.viaversion.viaversion.util.ProtocolUtil.packetTypeMap;
public final class Protocol1_21To1_21_2 extends AbstractProtocol<ClientboundPacket1_21, ClientboundPacket1_21_2, ServerboundPacket1_20_5, ServerboundPacket1_21_2> {
public static final MappingData1_21_2 MAPPINGS = new MappingData1_21_2();
public static final MappingData MAPPINGS = new MappingDataBase("1.21", "1.21.2");
private final EntityPacketRewriter1_21_2 entityRewriter = new EntityPacketRewriter1_21_2(this);
private final BlockItemPacketRewriter1_21_2 itemRewriter = new BlockItemPacketRewriter1_21_2(this);
private final TagRewriter<ClientboundPacket1_21> tagRewriter = new TagRewriter<>(this);
@ -180,7 +181,7 @@ public final class Protocol1_21To1_21_2 extends AbstractProtocol<ClientboundPack
}
@Override
public MappingData1_21_2 getMappingData() {
public MappingData getMappingData() {
return MAPPINGS;
}

View File

@ -1,63 +0,0 @@
/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
* 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.viaversion.protocols.v1_21to1_21_2.data;
import com.viaversion.nbt.tag.CompoundTag;
import com.viaversion.viaversion.api.data.MappingDataBase;
import com.viaversion.viaversion.api.data.MappingDataLoader;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.type.Types;
import java.util.ArrayList;
import java.util.List;
public final class MappingData1_21_2 extends MappingDataBase {
private final List<RecipeInputs> recipeInputs = new ArrayList<>();
public MappingData1_21_2() {
super("1.21", "1.21.2");
}
@Override
protected void loadExtras(final CompoundTag data) {
final CompoundTag extraMappings = MappingDataLoader.INSTANCE.loadNBT("recipe-inputs-1.21.2.nbt");
addRecipeInputs(extraMappings, "smithing_addition");
addRecipeInputs(extraMappings, "smithing_template");
addRecipeInputs(extraMappings, "smithing_base");
addRecipeInputs(extraMappings, "furnace_input");
addRecipeInputs(extraMappings, "smoker_input");
addRecipeInputs(extraMappings, "blast_furnace_input");
addRecipeInputs(extraMappings, "campfire_input");
}
private void addRecipeInputs(final CompoundTag tag, final String key) {
final int[] ids = tag.getIntArrayTag(key).getValue();
recipeInputs.add(new RecipeInputs(key, ids));
}
public void writeInputs(final PacketWrapper wrapper) {
wrapper.write(Types.VAR_INT, recipeInputs.size());
for (final RecipeInputs inputs : recipeInputs) {
wrapper.write(Types.STRING, inputs.key);
wrapper.write(Types.VAR_INT_ARRAY_PRIMITIVE, inputs.ids.clone());
}
}
public record RecipeInputs(String key, int[] ids) {
}
}

View File

@ -240,10 +240,9 @@ public final class BlockItemPacketRewriter1_21_2 extends StructuredItemRewriter<
}
rewriter.finalizeRecipes();
// These are used for client predictions, such as what items can be used as fuel in a furnace
protocol.getMappingData().writeInputs(wrapper);
rewriter.writeStoneCutterRecipes(wrapper);
// These are used for client predictions, such smithing or furnace inputs.
// Other recipes will be written in RECIPE/RECIPE_BOOK_ADD
rewriter.writeUpdateRecipeInputs(wrapper);
});
protocol.registerClientbound(ClientboundPackets1_21.RECIPE, ClientboundPackets1_21_2.RECIPE_BOOK_ADD, wrapper -> {

View File

@ -27,8 +27,11 @@ import com.viaversion.viaversion.api.type.types.version.Types1_21_2;
import com.viaversion.viaversion.protocols.v1_20_2to1_20_3.rewriter.RecipeRewriter1_20_3;
import com.viaversion.viaversion.protocols.v1_20_5to1_21.packet.ClientboundPacket1_21;
import com.viaversion.viaversion.util.Key;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSet;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
@ -39,9 +42,11 @@ import org.checkerframework.checker.nullness.qual.Nullable;
// Use directly as a connection storage. Slightly weird, but easiest and closed off from other packages
final class RecipeRewriter1_21_2 extends RecipeRewriter1_20_3<ClientboundPacket1_21> implements StorableObject {
private static final int[] EMPTY_ARRAY = new int[0];
private final List<StoneCutterRecipe> stoneCutterRecipes = new ArrayList<>();
private final Object2IntMap<String> recipeGroups = new Object2IntOpenHashMap<>();
private final Map<String, Recipe> recipesByKey = new HashMap<>();
private final Map<String, IntSet> recipeInputs = new Object2ObjectArrayMap<>();
private final List<Recipe> recipes = new ArrayList<>();
private String currentRecipeIdentifier;
@ -52,6 +57,11 @@ final class RecipeRewriter1_21_2 extends RecipeRewriter1_20_3<ClientboundPacket1
RecipeRewriter1_21_2(final Protocol<ClientboundPacket1_21, ?, ?, ?> protocol) {
super(protocol);
recipeGroups.defaultReturnValue(-1);
recipeHandlers.put("smelting", wrapper -> handleSmelting("furnace_input", wrapper));
recipeHandlers.put("blasting", wrapper -> handleSmelting("blast_furnace_input", wrapper));
recipeHandlers.put("smoking", wrapper -> handleSmelting("smoker_input", wrapper));
recipeHandlers.put("campfire_cooking", wrapper -> handleSmelting("campfire_input", wrapper));
}
@Override
@ -77,17 +87,17 @@ final class RecipeRewriter1_21_2 extends RecipeRewriter1_20_3<ClientboundPacket1
@Override
public void handleSmithingTransform(final PacketWrapper wrapper) {
readIngredient(wrapper); // Template
readIngredient(wrapper); // Base
readIngredient(wrapper); // Addition
readRecipeInputs("smithing_template", wrapper);
readRecipeInputs("smithing_base", wrapper);
readRecipeInputs("smithing_addition", wrapper);
wrapper.read(itemType()); // Result
}
@Override
public void handleSmithingTrim(final PacketWrapper wrapper) {
readIngredient(wrapper);
readIngredient(wrapper);
readIngredient(wrapper);
readRecipeInputs("smithing_template", wrapper);
readRecipeInputs("smithing_base", wrapper);
readRecipeInputs("smithing_addition", wrapper);
}
@Override
@ -123,11 +133,10 @@ final class RecipeRewriter1_21_2 extends RecipeRewriter1_20_3<ClientboundPacket1
addRecipe(recipe);
}
@Override
public void handleSmelting(final PacketWrapper wrapper) {
public void handleSmelting(final String key, final PacketWrapper wrapper) {
final int group = readRecipeGroup(wrapper);
final int category = wrapper.read(Types.VAR_INT);
final Item[] ingredient = readIngredient(wrapper);
final Item[] ingredient = readRecipeInputs(key, wrapper);
final Item result = rewrite(wrapper.user(), wrapper.read(itemType()));
final float experience = wrapper.read(Types.FLOAT);
final int cookingTime = wrapper.read(Types.VAR_INT);
@ -177,7 +186,20 @@ final class RecipeRewriter1_21_2 extends RecipeRewriter1_20_3<ClientboundPacket1
return displayId >= 0 && displayId < recipes.size() ? recipes.get(displayId) : null;
}
public void writeStoneCutterRecipes(final PacketWrapper wrapper) {
public void finalizeRecipes() {
// Need to be sorted alphabetically
stoneCutterRecipes.sort(Comparator.comparing(recipe -> recipe.identifier));
}
public void writeUpdateRecipeInputs(final PacketWrapper wrapper) {
// Smithing and smelting inputs
wrapper.write(Types.VAR_INT, recipeInputs.size());
for (final Map.Entry<String, IntSet> entry : recipeInputs.entrySet()) {
wrapper.write(Types.STRING, entry.getKey());
wrapper.write(Types.VAR_INT_ARRAY_PRIMITIVE, entry.getValue().toArray(EMPTY_ARRAY));
}
// Stonecutter recipes
wrapper.write(Types.VAR_INT, stoneCutterRecipes.size());
for (final StoneCutterRecipe recipe : stoneCutterRecipes) {
final HolderSet ingredient = toHolderSet(recipe.ingredient());
@ -186,9 +208,14 @@ final class RecipeRewriter1_21_2 extends RecipeRewriter1_20_3<ClientboundPacket1
}
}
public void finalizeRecipes() {
// Need to be sorted alphabetically
stoneCutterRecipes.sort(Comparator.comparing(recipe -> recipe.identifier));
private Item[] readRecipeInputs(final String key, final PacketWrapper wrapper) {
// Collect inputs for new UPDATE_RECIPE
final Item[] ingredient = readIngredient(wrapper);
final IntSet ids = recipeInputs.computeIfAbsent(key, $ -> new IntOpenHashSet(ingredient.length));
for (final Item item : ingredient) {
ids.add(item.identifier());
}
return ingredient;
}
interface Recipe {