ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/crafting/RecipeKeepDataShaped.java

86 lines
3.2 KiB
Java
Raw Normal View History

2016-12-06 18:10:43 +01:00
package de.ellpeck.actuallyadditions.mod.crafting;
2024-03-04 21:50:33 +01:00
import com.mojang.serialization.Codec;
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
2024-03-02 21:23:08 +01:00
import net.minecraft.core.NonNullList;
2024-03-03 01:20:53 +01:00
import net.minecraft.core.RegistryAccess;
2024-03-02 21:23:08 +01:00
import net.minecraft.network.FriendlyByteBuf;
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.neoforged.neoforge.attachment.AttachmentInternals;
import javax.annotation.Nullable;
public class RecipeKeepDataShaped extends ShapedRecipe {
public static String NAME = "copy_nbt";
public RecipeKeepDataShaped(ShapedRecipe shapedRecipe) {
2024-03-04 21:50:33 +01:00
super(shapedRecipe.getGroup(), shapedRecipe.category(), shapedRecipe.pattern, shapedRecipe.getResultItem(null));
}
@Override
2024-03-03 01:20:53 +01:00
public ItemStack assemble(CraftingContainer inv, RegistryAccess registryAccess) {
final ItemStack craftingResult = super.assemble(inv, registryAccess);
TargetNBTIngredient donorIngredient = null;
ItemStack datasource = ItemStack.EMPTY;
NonNullList<Ingredient> 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());
if (!datasource.isEmpty())
AttachmentInternals.copyStackAttachments(datasource, craftingResult);
return craftingResult;
}
@Override
2024-03-02 21:23:08 +01:00
public RecipeSerializer<?> getSerializer() {
return ActuallyRecipes.KEEP_DATA_SHAPED_RECIPE.get();
}
2024-03-03 01:20:53 +01:00
public static class Serializer implements RecipeSerializer<RecipeKeepDataShaped> {
2024-03-04 21:50:33 +01:00
private static final Codec<RecipeKeepDataShaped> CODEC = ShapedRecipe.Serializer.CODEC.xmap(RecipeKeepDataShaped::new, $ -> $);
@Override
2024-03-04 21:50:33 +01:00
public Codec<RecipeKeepDataShaped> codec() {
return CODEC;
}
2024-03-04 21:50:33 +01:00
@Nullable
@Override
2024-03-04 21:50:33 +01:00
public RecipeKeepDataShaped fromNetwork(FriendlyByteBuf buffer) {
return new RecipeKeepDataShaped(RecipeSerializer.SHAPED_RECIPE.fromNetwork(buffer));
}
@Override
2024-03-02 21:23:08 +01:00
public void toNetwork(FriendlyByteBuf buffer, RecipeKeepDataShaped recipe) {
try {
2024-03-02 21:23:08 +01:00
RecipeSerializer.SHAPED_RECIPE.toNetwork(buffer, recipe);
}
catch (Exception exception) {
2024-03-04 21:50:33 +01:00
ActuallyAdditions.LOGGER.info("Error writing "+ NAME +" Recipe to packet: ", exception);
throw exception;
}
}
}
}