package de.ellpeck.actuallyadditions.mod.crafting; import com.google.gson.JsonObject; import net.minecraft.core.NonNullList; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.inventory.CraftingContainer; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.crafting.Ingredient; import net.minecraft.world.item.crafting.RecipeSerializer; import net.minecraft.world.item.crafting.ShapedRecipe; import net.minecraftforge.registries.ForgeRegistryEntry; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import javax.annotation.Nullable; public class RecipeKeepDataShaped extends ShapedRecipe { public static String NAME = "copy_nbt"; public static final Logger LOGGER = LogManager.getLogger(); public RecipeKeepDataShaped(ResourceLocation idIn, String groupIn, int recipeWidthIn, int recipeHeightIn, NonNullList recipeItemsIn, ItemStack recipeOutputIn) { super(idIn, groupIn, recipeWidthIn, recipeHeightIn, recipeItemsIn, recipeOutputIn); } public RecipeKeepDataShaped(ShapedRecipe shapedRecipe) { super(shapedRecipe.getId(), shapedRecipe.getGroup(), shapedRecipe.getRecipeWidth(), shapedRecipe.getRecipeHeight(), shapedRecipe.getIngredients(), shapedRecipe.getResultItem()); } @Override public ItemStack assemble(CraftingContainer inv) { final ItemStack craftingResult = super.assemble(inv); TargetNBTIngredient donorIngredient = null; ItemStack datasource = ItemStack.EMPTY; NonNullList ingredients = getIngredients(); for (Ingredient ingredient : ingredients) { if (ingredient instanceof TargetNBTIngredient) { donorIngredient = (TargetNBTIngredient) ingredient; break; } } if (donorIngredient != null && !inv.isEmpty()) { for (int i = 0; i < inv.getContainerSize(); i++) { final ItemStack item = inv.getItem(i); if (!item.isEmpty() && donorIngredient.test(item)) { datasource = item; break; } } } if (!datasource.isEmpty() && datasource.hasTag()) craftingResult.setTag(datasource.getTag().copy()); return craftingResult; } @Override public RecipeSerializer getSerializer() { return ActuallyRecipes.KEEP_DATA_SHAPED_RECIPE.get(); } public static class Serializer extends ForgeRegistryEntry> implements RecipeSerializer { @Nullable @Override public RecipeKeepDataShaped fromNetwork(ResourceLocation recipeId, FriendlyByteBuf buffer) { return new RecipeKeepDataShaped(RecipeSerializer.SHAPED_RECIPE.fromNetwork(recipeId, buffer)); } @Override public RecipeKeepDataShaped fromJson(ResourceLocation recipeId, JsonObject json) { try { return new RecipeKeepDataShaped(RecipeSerializer.SHAPED_RECIPE.fromJson(recipeId, json)); } catch (Exception exception) { LOGGER.info("Error reading "+ NAME +" Recipe from packet: ", exception); throw exception; } } @Override public void toNetwork(FriendlyByteBuf buffer, RecipeKeepDataShaped recipe) { try { RecipeSerializer.SHAPED_RECIPE.toNetwork(buffer, recipe); } catch (Exception exception) { LOGGER.info("Error writing "+ NAME +" Recipe to packet: ", exception); throw exception; } } } }