diff options
Diffstat (limited to 'src/main/java/lv/enes/mc/eris_alchemy/recipe/SimplifiedRecipe.java')
| -rw-r--r-- | src/main/java/lv/enes/mc/eris_alchemy/recipe/SimplifiedRecipe.java | 50 |
1 files changed, 36 insertions, 14 deletions
diff --git a/src/main/java/lv/enes/mc/eris_alchemy/recipe/SimplifiedRecipe.java b/src/main/java/lv/enes/mc/eris_alchemy/recipe/SimplifiedRecipe.java index 469ed52..6bb1274 100644 --- a/src/main/java/lv/enes/mc/eris_alchemy/recipe/SimplifiedRecipe.java +++ b/src/main/java/lv/enes/mc/eris_alchemy/recipe/SimplifiedRecipe.java | |||
| @@ -4,13 +4,17 @@ import com.google.gson.JsonDeserializationContext; | |||
| 4 | import com.google.gson.JsonDeserializer; | 4 | import com.google.gson.JsonDeserializer; |
| 5 | import com.google.gson.JsonElement; | 5 | import com.google.gson.JsonElement; |
| 6 | import com.google.gson.JsonParseException; | 6 | import com.google.gson.JsonParseException; |
| 7 | import earth.terrarium.chipped.common.recipe.ChippedRecipe; | ||
| 7 | import jakarta.annotation.Nonnull; | 8 | import jakarta.annotation.Nonnull; |
| 9 | import lv.enes.mc.eris_alchemy.utils.ForeignUtils; | ||
| 8 | import lv.enes.mc.eris_alchemy.utils.IngredientProvider; | 10 | import lv.enes.mc.eris_alchemy.utils.IngredientProvider; |
| 9 | import lv.enes.mc.eris_alchemy.utils.ItemUtils; | 11 | import lv.enes.mc.eris_alchemy.utils.ItemUtils; |
| 10 | import lv.enes.mc.eris_alchemy.utils.RecipeUtils; | 12 | import lv.enes.mc.eris_alchemy.utils.RecipeUtils; |
| 13 | import net.minecraft.core.Holder; | ||
| 11 | import net.minecraft.core.RegistryAccess; | 14 | import net.minecraft.core.RegistryAccess; |
| 12 | import net.minecraft.core.registries.BuiltInRegistries; | 15 | import net.minecraft.core.registries.BuiltInRegistries; |
| 13 | import net.minecraft.resources.ResourceLocation; | 16 | import net.minecraft.resources.ResourceLocation; |
| 17 | import net.minecraft.world.item.Item; | ||
| 14 | import net.minecraft.world.item.ItemStack; | 18 | import net.minecraft.world.item.ItemStack; |
| 15 | import net.minecraft.world.item.crafting.Ingredient; | 19 | import net.minecraft.world.item.crafting.Ingredient; |
| 16 | import net.minecraft.world.item.crafting.Recipe; | 20 | import net.minecraft.world.item.crafting.Recipe; |
| @@ -21,20 +25,45 @@ import java.util.Arrays; | |||
| 21 | import java.util.Collection; | 25 | import java.util.Collection; |
| 22 | import java.util.List; | 26 | import java.util.List; |
| 23 | import java.util.Objects; | 27 | import java.util.Objects; |
| 28 | import java.util.function.Predicate; | ||
| 24 | import java.util.function.Supplier; | 29 | import java.util.function.Supplier; |
| 25 | import java.util.stream.Stream; | 30 | import java.util.stream.Stream; |
| 26 | 31 | ||
| 27 | public record SimplifiedRecipe(ItemStack output, List<ItemStack> remainder, List<Supplier<Ingredient>> input) { | 32 | public record SimplifiedRecipe(ItemStack output, List<ItemStack> remainder, List<Supplier<Ingredient>> input) { |
| 28 | public SimplifiedRecipe(Recipe<?> recipe, RegistryAccess registryAccess) { | 33 | public static List<SimplifiedRecipe> of(Recipe<?> recipe, RegistryAccess registryAccess) { |
| 29 | this( | 34 | if (ForeignUtils.IS_CHIPPED_AVAILABLE && recipe instanceof ChippedRecipe chippedRecipe) { |
| 35 | var remainder = List.<ItemStack>of(); | ||
| 36 | return chippedRecipe.tags() | ||
| 37 | .stream() | ||
| 38 | .flatMap(tag -> { | ||
| 39 | var items = tag.stream().map(Holder::value).toList(); | ||
| 40 | Predicate<Item> isOutput = item -> ItemUtils.getId(item).getNamespace().equals("chipped"); | ||
| 41 | var inputs = items.stream() | ||
| 42 | .filter(item -> !isOutput.test(item)) | ||
| 43 | .map(Ingredient::of) | ||
| 44 | .map(x -> (Supplier<Ingredient>) () -> x) | ||
| 45 | .map(List::of) | ||
| 46 | .toList(); | ||
| 47 | var outputs = items.stream().filter(isOutput).map(Item::getDefaultInstance).toList(); | ||
| 48 | return outputs.stream() | ||
| 49 | .flatMap( | ||
| 50 | output -> | ||
| 51 | inputs.stream() | ||
| 52 | .map(input -> new SimplifiedRecipe(output, remainder, input)) | ||
| 53 | ); | ||
| 54 | }) | ||
| 55 | .toList(); | ||
| 56 | } | ||
| 57 | |||
| 58 | return List.of(new SimplifiedRecipe( | ||
| 30 | RecipeUtils.getOutput(recipe, registryAccess), | 59 | RecipeUtils.getOutput(recipe, registryAccess), |
| 31 | List.of(), // TODO: | 60 | List.of(), // TODO |
| 32 | RecipeUtils.getIngredients(recipe) | 61 | RecipeUtils.getIngredients(recipe) |
| 33 | .stream() | 62 | .stream() |
| 34 | .filter(ingredient -> !ingredient.isEmpty()) | 63 | .filter(ingredient -> !ingredient.isEmpty()) |
| 35 | .map(x -> (Supplier<Ingredient>) () -> x) | 64 | .map(x -> (Supplier<Ingredient>)() -> x) |
| 36 | .toList() | 65 | .toList() |
| 37 | ); | 66 | )); |
| 38 | } | 67 | } |
| 39 | 68 | ||
| 40 | public Stream<ResourceLocation> dependencies() { | 69 | public Stream<ResourceLocation> dependencies() { |
| @@ -45,15 +74,8 @@ public record SimplifiedRecipe(ItemStack output, List<ItemStack> remainder, List | |||
| 45 | } | 74 | } |
| 46 | 75 | ||
| 47 | public boolean hasDuplication() { | 76 | public boolean hasDuplication() { |
| 48 | var outputId = ItemUtils.getId(output); | 77 | return remainder.stream().anyMatch(stack -> ItemStack.isSameItem(stack, output)) |
| 49 | var outputsInRemainder = remainder.stream() | 78 | || input.stream().anyMatch(ingredient -> ingredient.get().test(output)); |
| 50 | .map(ItemUtils::getId) | ||
| 51 | .anyMatch(id -> Objects.equals(id, outputId)); | ||
| 52 | if (outputsInRemainder) { | ||
| 53 | return true; | ||
| 54 | } | ||
| 55 | |||
| 56 | return input.stream().anyMatch(ingredient -> ingredient.get().test(output)); | ||
| 57 | } | 79 | } |
| 58 | 80 | ||
| 59 | public boolean isAllowed(BannedRecipe ban) { | 81 | public boolean isAllowed(BannedRecipe ban) { |