| 1 | package net.minecraft.client.gui.screens.recipebook;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.HashSet;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Set;
|
| 7 | import java.util.function.Predicate;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.world.entity.player.StackedItemContents;
|
| 11 | import net.minecraft.world.item.crafting.display.RecipeDisplay;
|
| 12 | import net.minecraft.world.item.crafting.display.RecipeDisplayEntry;
|
| 13 | import net.minecraft.world.item.crafting.display.RecipeDisplayId;
|
| 14 |
|
| 15 | @Environment(EnvType.CLIENT)
|
| 16 | public class RecipeCollection {
|
| 17 | public static final RecipeCollection EMPTY = new RecipeCollection(List.of());
|
| 18 | private final List<RecipeDisplayEntry> entries;
|
| 19 | private final Set<RecipeDisplayId> craftable = new HashSet<>();
|
| 20 | private final Set<RecipeDisplayId> selected = new HashSet<>();
|
| 21 |
|
| 22 | public RecipeCollection(final List<RecipeDisplayEntry> recipes) {
|
| 23 | this.entries = recipes;
|
| 24 | }
|
| 25 |
|
| 26 | public void selectRecipes(final StackedItemContents stackedContents, final Predicate<RecipeDisplay> selector) {
|
| 27 | for (RecipeDisplayEntry entry : this.entries) {
|
| 28 | boolean isSelected = selector.test(entry.display());
|
| 29 | if (isSelected) {
|
| 30 | this.selected.add(entry.id());
|
| 31 | } else {
|
| 32 | this.selected.remove(entry.id());
|
| 33 | }
|
| 34 |
|
| 35 | if (isSelected && entry.canCraft(stackedContents)) {
|
| 36 | this.craftable.add(entry.id());
|
| 37 | } else {
|
| 38 | this.craftable.remove(entry.id());
|
| 39 | }
|
| 40 | }
|
| 41 | }
|
| 42 |
|
| 43 | public boolean isCraftable(final RecipeDisplayId recipe) {
|
| 44 | return this.craftable.contains(recipe);
|
| 45 | }
|
| 46 |
|
| 47 | public boolean hasCraftable() {
|
| 48 | return !this.craftable.isEmpty();
|
| 49 | }
|
| 50 |
|
| 51 | public boolean hasAnySelected() {
|
| 52 | return !this.selected.isEmpty();
|
| 53 | }
|
| 54 |
|
| 55 | public List<RecipeDisplayEntry> getRecipes() {
|
| 56 | return this.entries;
|
| 57 | }
|
| 58 |
|
| 59 | public List<RecipeDisplayEntry> getSelectedRecipes(final RecipeCollection.CraftableStatus selector) {
|
| 60 | Predicate<RecipeDisplayId> predicate = switch (selector) {
|
| 61 | case ANY -> this.selected::contains;
|
| 62 | case CRAFTABLE -> this.craftable::contains;
|
| 63 | case NOT_CRAFTABLE -> recipe -> this.selected.contains(recipe) && !this.craftable.contains(recipe);
|
| 64 | };
|
| 65 | List<RecipeDisplayEntry> result = new ArrayList<>();
|
| 66 |
|
| 67 | for (RecipeDisplayEntry entries : this.entries) {
|
| 68 | if (predicate.test(entries.id())) {
|
| 69 | result.add(entries);
|
| 70 | }
|
| 71 | }
|
| 72 |
|
| 73 | return result;
|
| 74 | }
|
| 75 |
|
| 76 | @Environment(EnvType.CLIENT)
|
| 77 | public enum CraftableStatus {
|
| 78 | ANY,
|
| 79 | CRAFTABLE,
|
| 80 | NOT_CRAFTABLE;
|
| 81 | }
|
| 82 | }
|
| 83 |
|