2016-12-06 18:10:43 +01:00
|
|
|
package de.ellpeck.actuallyadditions.mod.crafting;
|
2016-08-04 11:57:35 +02:00
|
|
|
|
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;
|
2024-03-13 23:05:00 +01:00
|
|
|
import net.neoforged.neoforge.attachment.AttachmentInternals;
|
2021-10-12 23:44:02 +02:00
|
|
|
|
|
|
|
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));
|
2021-10-12 23:44:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-03-03 01:20:53 +01:00
|
|
|
public ItemStack assemble(CraftingContainer inv, RegistryAccess registryAccess) {
|
|
|
|
final ItemStack craftingResult = super.assemble(inv, registryAccess);
|
2021-10-12 23:44:02 +02:00
|
|
|
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());
|
|
|
|
|
2024-03-13 23:05:00 +01:00
|
|
|
if (!datasource.isEmpty())
|
|
|
|
AttachmentInternals.copyStackAttachments(datasource, craftingResult);
|
|
|
|
|
2021-10-12 23:44:02 +02:00
|
|
|
return craftingResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public RecipeSerializer<?> getSerializer() {
|
2021-10-12 23:44:02 +02:00
|
|
|
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, $ -> $);
|
|
|
|
|
2021-10-12 23:44:02 +02:00
|
|
|
@Override
|
2024-03-04 21:50:33 +01:00
|
|
|
public Codec<RecipeKeepDataShaped> codec() {
|
|
|
|
return CODEC;
|
2021-10-12 23:44:02 +02:00
|
|
|
}
|
|
|
|
|
2024-03-04 21:50:33 +01:00
|
|
|
@Nullable
|
2021-10-12 23:44:02 +02:00
|
|
|
@Override
|
2024-03-04 21:50:33 +01:00
|
|
|
public RecipeKeepDataShaped fromNetwork(FriendlyByteBuf buffer) {
|
|
|
|
return new RecipeKeepDataShaped(RecipeSerializer.SHAPED_RECIPE.fromNetwork(buffer));
|
2021-10-12 23:44:02 +02:00
|
|
|
}
|
2016-08-04 11:57:35 +02:00
|
|
|
|
2021-10-12 23:44:02 +02:00
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public void toNetwork(FriendlyByteBuf buffer, RecipeKeepDataShaped recipe) {
|
2021-10-12 23:44:02 +02:00
|
|
|
try {
|
2024-03-02 21:23:08 +01:00
|
|
|
RecipeSerializer.SHAPED_RECIPE.toNetwork(buffer, recipe);
|
2021-10-12 23:44:02 +02:00
|
|
|
}
|
|
|
|
catch (Exception exception) {
|
2024-03-04 21:50:33 +01:00
|
|
|
ActuallyAdditions.LOGGER.info("Error writing "+ NAME +" Recipe to packet: ", exception);
|
2021-10-12 23:44:02 +02:00
|
|
|
throw exception;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-04 11:57:35 +02:00
|
|
|
}
|